SlideShare a Scribd company logo
CHAPTER
FORTYNINE
USING THE NATURAL LIBRARY
In this chapter we will learn how to use the Natural Library to quickly define a language that contains a group of
commands.
To start using the library, We need to call naturallib.ring
load "naturallib.ring"
After loading the library, We can use the NaturalLanguage class that contains the next methods :-
• SetLanguageName(cLanguageName)
• setCommandsPath(cFolder)
• SetPackageName(cPackageName)
• UseCommand(cCommandName)
• SetOperators(cOperators)
• RunFile(cFileName)
• RunString(cString)
49.1 Natural Library - Demo Program
We will write the natural code in a Text file, for example program.txt
File: program.txt
Welcome to the Ring programming language!
What you are reading now is not comments, I swear!
After many years of programming I decided to think different about
programming and solve the problems in a better way.
We are writing commands or code and the Ring language is reading
it to understand us! Sure, What you are seeing now is
just ***part of the code - Not the Complete Program***
You have to write little things before and after this
part to be able to run it!
It is the natural part of our code where we can write in English,
Arabic or any Natural Language Then we will tell the computer
through the Ring language what must happens! in a way that we can scale
for large frameworks and programs.
411
Ring Documentation, Release 1.8
Just imagine what will happens to the world of programming once
we create many powerful frameworks using the Ring language that
uses this way (Natural Programming).
For example When we say Hello to the Machine, It can reply! and when we
say count from 1 to 5 it will understand us, Also if
we said count from 5 to 1 it will
understand us too! You can see the Output window!
This Goal is not new, but the Ring language comes
with an innovative solution to this problem.
Output:
Hello, Sir!
The Numbers!
1
2
3
4
5
I will count Again!
5
4
3
2
1
To execute the natural code, We have start.ring
In start.ring we define the language and the commands.
File: start.ring
load "stdlib.ring"
load "naturallib.ring"
New NaturalLanguage {
SetLanguageName(:MyLanguage)
SetCommandsPath(CurrentDir()+"/../command")
SetPackageName("MyLanguage.Natural")
UseCommand(:Hello)
UseCommand(:Count)
RunFile("program.txt")
}
We defined a language called MyLanguage, We have folder for the language commands.
49.1. Natural Library - Demo Program 412
Ring Documentation, Release 1.8
Each command will define a class that belong to the MyLanguage.Natural package.
We will define two commands, Hello and Count.
So we must have two files for defining the commands in the CurrentDir()+”/../command” folder
File: hello.ring
DefineNaturalCommand.SyntaxIsKeyword([
:Package = "MyLanguage.Natural",
:Keyword = :hello,
:Function = func {
See "Hello, Sir!" + nl + nl
}
])
File: count.ring
DefineNaturalCommand.SyntaxIsKeywordNumberNumber([
:Package = "MyLanguage.Natural",
:Keyword = :count,
:Function = func {
if not isattribute(self,:count_times) {
AddAttribute(self,:count_times)
Count_Times = 0
}
if Expr(1) > Expr(2) {
nStep = -1
else
nStep = 1
}
if Count_Times = 0 {
see nl+"The Numbers!" + nl
Count_Times++
else
see nl + "I will count Again!" +nl
}
for x = Expr(1) to Expr(2) step nStep {
see nl+x+nl
}
CommandReturn(fabs(Expr(1)-Expr(2))+1)
}
])
49.2 Defining Commands
To define new command we can use the DefineNaturalCommand object
This object provides the next methods :-
• SyntaxIsKeyword(aPara)
• SyntaxIsKeywordNumber(aPara)
• SyntaxIsKeywordNumberNumber(aPara)
• SyntaxIsKeywordNumbers(aPara,nCount)
• SyntaxIsKeywordString(aPara)
• SyntaxIsKeywordStringString(aPara)
49.2. Defining Commands 413
Ring Documentation, Release 1.8
• SyntaxIsKeywordStrings(aPara,nCount)
• SyntaxIsKeywordExpression(aPara)
• SyntaxIsKeywordExpressionExpression(aPara)
• SyntaxIsKeywordExpressions(aPara,nCount)
• SyntaxIsCommand(aPara)
• SyntaxIsCommandNumber(aPara)
• SyntaxIsCommandNumberNumber(aPara)
• SyntaxIsCommandNumbers(aPara,nCount)
• SyntaxIsCommandString(aPara)
• SyntaxIsCommandStringString(aPara)
• SyntaxIsCommandStrings(aPara,nCount)
• SyntaxIsCommandExpression(aPara)
• SyntaxIsCommandExpressionExpression(aPara)
• SyntaxIsCommandExpressions(aPara,nCount)
File: mylanguage.ring
load "stdlib.ring"
load "naturallib.ring"
MyLanguage = New NaturalLanguage {
SetLanguageName(:MyLanguage)
setCommandsPath(CurrentDir()+"/../command")
SetPackageName("MyLanguage.Natural")
UseCommand(:Hello)
UseCommand(:Count)
UseCommand(:Print)
UseCommand(:IWantWindow)
UseCommand(:WindowTitleIs)
UseCommand(:IWantButton)
}
Example (1)
In the next example we will define the Print command.
We will use the SyntaxIsKeywordExpression() Method.
We pass list (as Hash) to the method. We determine the package name, the keyword and the function that will be
executed.
Inside this function we uses the Expr(nExprNumber) function to get the expression value that the user will write after
the keyword.
File: print.ring
DefineNaturalCommand.SyntaxIsKeywordExpression([
:Package = "MyLanguage.Natural",
:Keyword = :print,
:Function = func {
See Expr(1)
}
])
49.2. Defining Commands 414
Ring Documentation, Release 1.8
Usage:
load "mylanguage.ring"
MyLanguage.RunString('
print "Hello, World!"
')
Output:
Hello, World!
Example (2)
File: iwantwindow.ring
DefineNaturalCommand.SyntaxIsCommand([
:Package = "MyLanguage.Natural",
:Command = "i want window",
:Function = func {
See "Command: I want window" + nl
}
])
Usage:
load "mylanguage.ring"
MyLanguage.RunString('
i want window
')
Output:
Command: I want window
Example (3)
File: windowtitleis.ring
DefineNaturalCommand.SyntaxIsCommandString([
:Package = "MyLanguage.Natural",
:Command = "window title is",
:Function = func {
See "Command: Window title is " + Expr(1) + nl
}
])
Usage:
load "mylanguage.ring"
MyLanguage.RunString('
I want window and the window title is "Hello World"
')
Output:
Command: I want window
Command: Window title is Hello World
49.2. Defining Commands 415
Ring Documentation, Release 1.8
49.3 Natural Library - Operators
In the next example we uses the Count command without using operators
load "mylanguage.ring"
MyLanguage.RunString("
Hello
Count 1 5
Count 5 1
")
We can add more description
load "mylanguage.ring"
MyLanguage.RunString("
Hello, Please Count from 1 to 5 then count from 5 to 1
")
Also we can use operators like “(” and ”)” around the instruction
load "mylanguage.ring"
MyLanguage {
SetOperators("()")
RunString("
Here we will play and will try something
that looks like Lisp Syntax
(count (count 1 5) (count 20 15))
Just for fun!
")
}
49.4 Defining commands using classes
This section is related to the implementation details.
When we define new command, Each command is defined by the Natural Library as a class.
We have the choice to define commands using the simple interface provided by the DefineNaturalCommand object or
by defining new class as in the next examples.
If we used DefineNaturalCommand (More Simple), The class will be defined during the runtime.
File: hello.ring
Package MyLanguage.Natural
class Hello
func AddAttributes_Hello
AddAttribute(self,:hello)
func GetHello
See "Hello, Sir!" + nl + nl
File: count.ring
49.3. Natural Library - Operators 416
Ring Documentation, Release 1.8
Package MyLanguage.Natural
class Count
func Getcount
StartCommand()
CommandData()[:name] = :Count
CommandData()[:nExpr] = 0
CommandData()[:aExpr] = []
func BraceExprEval_Count nValue
if isCommand() and CommandData()[:name] = :Count {
if isNumber(nValue) {
CommandData()[:nExpr]++
CommandData()[:aExpr] + nValue
if CommandData()[:nExpr] = 2 {
Count_Execute()
}
}
}
func AddAttributes_Count
AddAttribute(self,:count)
func Count_Execute
if not isattribute(self,:count_times) {
AddAttribute(self,:count_times)
Count_Times = 0
}
if Expr(1) > Expr(2) {
nStep = -1
else
nStep = 1
}
if Count_Times = 0 {
see nl+"The Numbers!" + nl
Count_Times++
else
see nl + "I will count Again!" +nl
}
for x = Expr(1) to Expr(2) step nStep {
see nl+x+nl
}
CommandReturn(fabs(Expr(1)-Expr(2))+1)
49.4. Defining commands using classes 417
CHAPTER
FIFTY
WEB DEVELOPMENT (CGI LIBRARY)
In this chapter we will learn about developing Web applications using a CGI Library written in the Ring language.
50.1 Configure the Apache web server
We can use Ring with any web server that support CGI. In this section we will learn about using Ring with the Apache
HTTP Server.
You can download Apache from : http://httpd.apache.org/
Or you can get it included with other projects like
XAMPP : https://www.apachefriends.org/download.html
Install then open the file:
xamppapacheconfhttpd.conf
search for
<Directory />
Then after it add
Options FollowSymLinks +ExecCGI
So we have
<Directory />
Options FollowSymLinks +ExecCGI
Search for the next line and be sure that it’s not commented
LoadModule cgi_module modules/mod_cgi.so
Search for : AddHandler cgi-script
Then add ”.ring” to the supported cgi extensions
Example
AddHandler cgi-script .cgi .ring
Example
AddHandler cgi-script .cgi .pl .asp .ring
418
Ring Documentation, Release 1.8
Run/Start the server
Create your web applications in a directory supported by the web server.
Example:
Apache2.2htdocsmywebapplicationfolder
Example:
xampphtdocsmywebapplicationfolder
Inside the source code file (*.ring), Add this line
#!ring -cgi
Note: Change the previous line based on the path to ring.exe in your machine
50.2 Ring CGI Hello World Program
The next program is the Hello World program
#!ring -cgi
See "content-type : text/html" +nl+nl+
"Hello World!" + nl
50.3 Hello World Program using the Web Library
We can use the web library to write CGI Web applications quickly
Example (1) :
#!ring -cgi
Load "weblib.ring"
Import System.Web
New Page
{
Text("Hello World!")
}
Example (2) :
#!ring -cgi
Load "weblib.ring"
Import System.Web
WebPage()
{
Text("Hello World!")
}
50.2. Ring CGI Hello World Program 419
Ring Documentation, Release 1.8
Tip: the difference between ex. 1 and ex. 2 is using WebPage() function to return the page object instead of creating
the object using new statement.
50.4 Web Library Features
The next features are provided by the Web library to quickly create web applications.
• Generate HTML pages using functions
• Generate HTML pages using objects
• HTTP Get
• HTTP Post
• Files Upload
• URL Encode
• Templates
• CRUD MVC Sample
• Users Logic & Registration Sample
50.5 HTTP Get Example
The Page User Interface
#!ring -cgi
Load "weblib.ring"
Import System.Web
New Page
{
Title = "Test HTTP Get"
divstart([ :style = StyleSizeFull() ] )
boxstart()
text( "Test HTTP GET" )
newline()
boxend()
divstart([ :style = Styledivcenter("600px","550px") +
StyleGradient(21) ])
divstart([:style = stylefloatleft() + stylesize("100px","100%") +
stylecolor("black") + stylegradient(58)])
formstart("ex5.ring")
tablestart([ :style = stylesize("65%","90%") +
stylemarginleft("35%") +
stylemargintop("30%") ])
rowstart([])
cellstart([])
text ( "Name : " )
cellend()
cellstart([])
cTextboxStyle = StyleMarginLeft("5%") +
StyleWidth("250px") +
StyleColor("black") +
50.4. Web Library Features 420

More Related Content

PDF
The Ring programming language version 1.5.1 book - Part 38 of 180
PDF
The Ring programming language version 1.7 book - Part 43 of 196
PPTX
Lean & Mean Tokyo Cabinet Recipes (with Lua) - FutureRuby '09
ODP
using python module: doctest
PPT
An Introduction to Solr
PDF
Power of Puppet 4
ODP
Biopython
PDF
The Ring programming language version 1.9 book - Part 20 of 210
The Ring programming language version 1.5.1 book - Part 38 of 180
The Ring programming language version 1.7 book - Part 43 of 196
Lean & Mean Tokyo Cabinet Recipes (with Lua) - FutureRuby '09
using python module: doctest
An Introduction to Solr
Power of Puppet 4
Biopython
The Ring programming language version 1.9 book - Part 20 of 210

What's hot (20)

PPTX
An introduction to Raku
PDF
Symfony Performance
PDF
The Ring programming language version 1.8 book - Part 18 of 202
PPTX
Large scale nlp using python's nltk on azure
PDF
Natural Language Toolkit (NLTK), Basics
PPT
Automating a Vendor File Load Process with Perl and Shell Scripting
ODP
PHP applications/environments monitoring: APM & Pinba
PPTX
Developing High Performance Application with Aerospike & Go
PDF
Python高级编程(二)
PDF
Java Full Throttle
PDF
The Ring programming language version 1.5.4 book - Part 39 of 185
PDF
Go for Rubyists
PDF
IO Streams, Files and Directories
PDF
Apache Thrift
PDF
System Programming and Administration
PPT
Python - Getting to the Essence - Points.com - Dave Park
PDF
The Ring programming language version 1.5.3 book - Part 14 of 184
PDF
What we can learn from Rebol?
PDF
Perl 101
PDF
Python build your security tools.pdf
An introduction to Raku
Symfony Performance
The Ring programming language version 1.8 book - Part 18 of 202
Large scale nlp using python's nltk on azure
Natural Language Toolkit (NLTK), Basics
Automating a Vendor File Load Process with Perl and Shell Scripting
PHP applications/environments monitoring: APM & Pinba
Developing High Performance Application with Aerospike & Go
Python高级编程(二)
Java Full Throttle
The Ring programming language version 1.5.4 book - Part 39 of 185
Go for Rubyists
IO Streams, Files and Directories
Apache Thrift
System Programming and Administration
Python - Getting to the Essence - Points.com - Dave Park
The Ring programming language version 1.5.3 book - Part 14 of 184
What we can learn from Rebol?
Perl 101
Python build your security tools.pdf
Ad

Similar to The Ring programming language version 1.8 book - Part 45 of 202 (20)

PDF
The Ring programming language version 1.5.3 book - Part 39 of 184
PDF
The Ring programming language version 1.10 book - Part 49 of 212
PDF
The Ring programming language version 1.6 book - Part 42 of 189
PDF
The Ring programming language version 1.5.4 book - Part 11 of 185
PDF
The Ring programming language version 1.4 book - Part 11 of 30
PPTX
The GO Language : From Beginners to Gophers
PDF
The Ring programming language version 1.9 book - Part 48 of 210
PDF
The Ring programming language version 1.7 book - Part 92 of 196
PDF
The Ring programming language version 1.5.1 book - Part 10 of 180
PDF
The Ring programming language version 1.6 book - Part 13 of 189
PDF
The Ring programming language version 1.9 book - Part 17 of 210
PDF
The Ring programming language version 1.5.3 book - Part 11 of 184
PDF
The Ring programming language version 1.5.3 book - Part 189 of 194
PDF
The Ring programming language version 1.7 book - Part 14 of 196
PDF
The Ring programming language version 1.10 book - Part 18 of 212
PDF
The Ring programming language version 1.8 book - Part 15 of 202
PDF
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
PPTX
File handle in PROGRAMMable extensible interpreted .pptx
PDF
The Ring programming language version 1.5.1 book - Part 13 of 180
PDF
The Ring programming language version 1.5.2 book - Part 39 of 181
The Ring programming language version 1.5.3 book - Part 39 of 184
The Ring programming language version 1.10 book - Part 49 of 212
The Ring programming language version 1.6 book - Part 42 of 189
The Ring programming language version 1.5.4 book - Part 11 of 185
The Ring programming language version 1.4 book - Part 11 of 30
The GO Language : From Beginners to Gophers
The Ring programming language version 1.9 book - Part 48 of 210
The Ring programming language version 1.7 book - Part 92 of 196
The Ring programming language version 1.5.1 book - Part 10 of 180
The Ring programming language version 1.6 book - Part 13 of 189
The Ring programming language version 1.9 book - Part 17 of 210
The Ring programming language version 1.5.3 book - Part 11 of 184
The Ring programming language version 1.5.3 book - Part 189 of 194
The Ring programming language version 1.7 book - Part 14 of 196
The Ring programming language version 1.10 book - Part 18 of 212
The Ring programming language version 1.8 book - Part 15 of 202
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
File handle in PROGRAMMable extensible interpreted .pptx
The Ring programming language version 1.5.1 book - Part 13 of 180
The Ring programming language version 1.5.2 book - Part 39 of 181
Ad

More from Mahmoud Samir Fayed (20)

PDF
The Ring programming language version 1.10 book - Part 212 of 212
PDF
The Ring programming language version 1.10 book - Part 211 of 212
PDF
The Ring programming language version 1.10 book - Part 210 of 212
PDF
The Ring programming language version 1.10 book - Part 208 of 212
PDF
The Ring programming language version 1.10 book - Part 207 of 212
PDF
The Ring programming language version 1.10 book - Part 205 of 212
PDF
The Ring programming language version 1.10 book - Part 206 of 212
PDF
The Ring programming language version 1.10 book - Part 204 of 212
PDF
The Ring programming language version 1.10 book - Part 203 of 212
PDF
The Ring programming language version 1.10 book - Part 202 of 212
PDF
The Ring programming language version 1.10 book - Part 201 of 212
PDF
The Ring programming language version 1.10 book - Part 200 of 212
PDF
The Ring programming language version 1.10 book - Part 199 of 212
PDF
The Ring programming language version 1.10 book - Part 198 of 212
PDF
The Ring programming language version 1.10 book - Part 197 of 212
PDF
The Ring programming language version 1.10 book - Part 196 of 212
PDF
The Ring programming language version 1.10 book - Part 195 of 212
PDF
The Ring programming language version 1.10 book - Part 194 of 212
PDF
The Ring programming language version 1.10 book - Part 193 of 212
PDF
The Ring programming language version 1.10 book - Part 192 of 212
The Ring programming language version 1.10 book - Part 212 of 212
The Ring programming language version 1.10 book - Part 211 of 212
The Ring programming language version 1.10 book - Part 210 of 212
The Ring programming language version 1.10 book - Part 208 of 212
The Ring programming language version 1.10 book - Part 207 of 212
The Ring programming language version 1.10 book - Part 205 of 212
The Ring programming language version 1.10 book - Part 206 of 212
The Ring programming language version 1.10 book - Part 204 of 212
The Ring programming language version 1.10 book - Part 203 of 212
The Ring programming language version 1.10 book - Part 202 of 212
The Ring programming language version 1.10 book - Part 201 of 212
The Ring programming language version 1.10 book - Part 200 of 212
The Ring programming language version 1.10 book - Part 199 of 212
The Ring programming language version 1.10 book - Part 198 of 212
The Ring programming language version 1.10 book - Part 197 of 212
The Ring programming language version 1.10 book - Part 196 of 212
The Ring programming language version 1.10 book - Part 195 of 212
The Ring programming language version 1.10 book - Part 194 of 212
The Ring programming language version 1.10 book - Part 193 of 212
The Ring programming language version 1.10 book - Part 192 of 212

Recently uploaded (20)

PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
System and Network Administraation Chapter 3
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
top salesforce developer skills in 2025.pdf
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
L1 - Introduction to python Backend.pptx
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Introduction to Artificial Intelligence
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Odoo POS Development Services by CandidRoot Solutions
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
System and Network Administraation Chapter 3
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Design an Analysis of Algorithms I-SECS-1021-03
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Odoo Companies in India – Driving Business Transformation.pdf
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
How Creative Agencies Leverage Project Management Software.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
top salesforce developer skills in 2025.pdf
Navsoft: AI-Powered Business Solutions & Custom Software Development
L1 - Introduction to python Backend.pptx
How to Migrate SBCGlobal Email to Yahoo Easily
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Introduction to Artificial Intelligence
2025 Textile ERP Trends: SAP, Odoo & Oracle
Lecture 3: Operating Systems Introduction to Computer Hardware Systems

The Ring programming language version 1.8 book - Part 45 of 202

  • 1. CHAPTER FORTYNINE USING THE NATURAL LIBRARY In this chapter we will learn how to use the Natural Library to quickly define a language that contains a group of commands. To start using the library, We need to call naturallib.ring load "naturallib.ring" After loading the library, We can use the NaturalLanguage class that contains the next methods :- • SetLanguageName(cLanguageName) • setCommandsPath(cFolder) • SetPackageName(cPackageName) • UseCommand(cCommandName) • SetOperators(cOperators) • RunFile(cFileName) • RunString(cString) 49.1 Natural Library - Demo Program We will write the natural code in a Text file, for example program.txt File: program.txt Welcome to the Ring programming language! What you are reading now is not comments, I swear! After many years of programming I decided to think different about programming and solve the problems in a better way. We are writing commands or code and the Ring language is reading it to understand us! Sure, What you are seeing now is just ***part of the code - Not the Complete Program*** You have to write little things before and after this part to be able to run it! It is the natural part of our code where we can write in English, Arabic or any Natural Language Then we will tell the computer through the Ring language what must happens! in a way that we can scale for large frameworks and programs. 411
  • 2. Ring Documentation, Release 1.8 Just imagine what will happens to the world of programming once we create many powerful frameworks using the Ring language that uses this way (Natural Programming). For example When we say Hello to the Machine, It can reply! and when we say count from 1 to 5 it will understand us, Also if we said count from 5 to 1 it will understand us too! You can see the Output window! This Goal is not new, but the Ring language comes with an innovative solution to this problem. Output: Hello, Sir! The Numbers! 1 2 3 4 5 I will count Again! 5 4 3 2 1 To execute the natural code, We have start.ring In start.ring we define the language and the commands. File: start.ring load "stdlib.ring" load "naturallib.ring" New NaturalLanguage { SetLanguageName(:MyLanguage) SetCommandsPath(CurrentDir()+"/../command") SetPackageName("MyLanguage.Natural") UseCommand(:Hello) UseCommand(:Count) RunFile("program.txt") } We defined a language called MyLanguage, We have folder for the language commands. 49.1. Natural Library - Demo Program 412
  • 3. Ring Documentation, Release 1.8 Each command will define a class that belong to the MyLanguage.Natural package. We will define two commands, Hello and Count. So we must have two files for defining the commands in the CurrentDir()+”/../command” folder File: hello.ring DefineNaturalCommand.SyntaxIsKeyword([ :Package = "MyLanguage.Natural", :Keyword = :hello, :Function = func { See "Hello, Sir!" + nl + nl } ]) File: count.ring DefineNaturalCommand.SyntaxIsKeywordNumberNumber([ :Package = "MyLanguage.Natural", :Keyword = :count, :Function = func { if not isattribute(self,:count_times) { AddAttribute(self,:count_times) Count_Times = 0 } if Expr(1) > Expr(2) { nStep = -1 else nStep = 1 } if Count_Times = 0 { see nl+"The Numbers!" + nl Count_Times++ else see nl + "I will count Again!" +nl } for x = Expr(1) to Expr(2) step nStep { see nl+x+nl } CommandReturn(fabs(Expr(1)-Expr(2))+1) } ]) 49.2 Defining Commands To define new command we can use the DefineNaturalCommand object This object provides the next methods :- • SyntaxIsKeyword(aPara) • SyntaxIsKeywordNumber(aPara) • SyntaxIsKeywordNumberNumber(aPara) • SyntaxIsKeywordNumbers(aPara,nCount) • SyntaxIsKeywordString(aPara) • SyntaxIsKeywordStringString(aPara) 49.2. Defining Commands 413
  • 4. Ring Documentation, Release 1.8 • SyntaxIsKeywordStrings(aPara,nCount) • SyntaxIsKeywordExpression(aPara) • SyntaxIsKeywordExpressionExpression(aPara) • SyntaxIsKeywordExpressions(aPara,nCount) • SyntaxIsCommand(aPara) • SyntaxIsCommandNumber(aPara) • SyntaxIsCommandNumberNumber(aPara) • SyntaxIsCommandNumbers(aPara,nCount) • SyntaxIsCommandString(aPara) • SyntaxIsCommandStringString(aPara) • SyntaxIsCommandStrings(aPara,nCount) • SyntaxIsCommandExpression(aPara) • SyntaxIsCommandExpressionExpression(aPara) • SyntaxIsCommandExpressions(aPara,nCount) File: mylanguage.ring load "stdlib.ring" load "naturallib.ring" MyLanguage = New NaturalLanguage { SetLanguageName(:MyLanguage) setCommandsPath(CurrentDir()+"/../command") SetPackageName("MyLanguage.Natural") UseCommand(:Hello) UseCommand(:Count) UseCommand(:Print) UseCommand(:IWantWindow) UseCommand(:WindowTitleIs) UseCommand(:IWantButton) } Example (1) In the next example we will define the Print command. We will use the SyntaxIsKeywordExpression() Method. We pass list (as Hash) to the method. We determine the package name, the keyword and the function that will be executed. Inside this function we uses the Expr(nExprNumber) function to get the expression value that the user will write after the keyword. File: print.ring DefineNaturalCommand.SyntaxIsKeywordExpression([ :Package = "MyLanguage.Natural", :Keyword = :print, :Function = func { See Expr(1) } ]) 49.2. Defining Commands 414
  • 5. Ring Documentation, Release 1.8 Usage: load "mylanguage.ring" MyLanguage.RunString(' print "Hello, World!" ') Output: Hello, World! Example (2) File: iwantwindow.ring DefineNaturalCommand.SyntaxIsCommand([ :Package = "MyLanguage.Natural", :Command = "i want window", :Function = func { See "Command: I want window" + nl } ]) Usage: load "mylanguage.ring" MyLanguage.RunString(' i want window ') Output: Command: I want window Example (3) File: windowtitleis.ring DefineNaturalCommand.SyntaxIsCommandString([ :Package = "MyLanguage.Natural", :Command = "window title is", :Function = func { See "Command: Window title is " + Expr(1) + nl } ]) Usage: load "mylanguage.ring" MyLanguage.RunString(' I want window and the window title is "Hello World" ') Output: Command: I want window Command: Window title is Hello World 49.2. Defining Commands 415
  • 6. Ring Documentation, Release 1.8 49.3 Natural Library - Operators In the next example we uses the Count command without using operators load "mylanguage.ring" MyLanguage.RunString(" Hello Count 1 5 Count 5 1 ") We can add more description load "mylanguage.ring" MyLanguage.RunString(" Hello, Please Count from 1 to 5 then count from 5 to 1 ") Also we can use operators like “(” and ”)” around the instruction load "mylanguage.ring" MyLanguage { SetOperators("()") RunString(" Here we will play and will try something that looks like Lisp Syntax (count (count 1 5) (count 20 15)) Just for fun! ") } 49.4 Defining commands using classes This section is related to the implementation details. When we define new command, Each command is defined by the Natural Library as a class. We have the choice to define commands using the simple interface provided by the DefineNaturalCommand object or by defining new class as in the next examples. If we used DefineNaturalCommand (More Simple), The class will be defined during the runtime. File: hello.ring Package MyLanguage.Natural class Hello func AddAttributes_Hello AddAttribute(self,:hello) func GetHello See "Hello, Sir!" + nl + nl File: count.ring 49.3. Natural Library - Operators 416
  • 7. Ring Documentation, Release 1.8 Package MyLanguage.Natural class Count func Getcount StartCommand() CommandData()[:name] = :Count CommandData()[:nExpr] = 0 CommandData()[:aExpr] = [] func BraceExprEval_Count nValue if isCommand() and CommandData()[:name] = :Count { if isNumber(nValue) { CommandData()[:nExpr]++ CommandData()[:aExpr] + nValue if CommandData()[:nExpr] = 2 { Count_Execute() } } } func AddAttributes_Count AddAttribute(self,:count) func Count_Execute if not isattribute(self,:count_times) { AddAttribute(self,:count_times) Count_Times = 0 } if Expr(1) > Expr(2) { nStep = -1 else nStep = 1 } if Count_Times = 0 { see nl+"The Numbers!" + nl Count_Times++ else see nl + "I will count Again!" +nl } for x = Expr(1) to Expr(2) step nStep { see nl+x+nl } CommandReturn(fabs(Expr(1)-Expr(2))+1) 49.4. Defining commands using classes 417
  • 8. CHAPTER FIFTY WEB DEVELOPMENT (CGI LIBRARY) In this chapter we will learn about developing Web applications using a CGI Library written in the Ring language. 50.1 Configure the Apache web server We can use Ring with any web server that support CGI. In this section we will learn about using Ring with the Apache HTTP Server. You can download Apache from : http://httpd.apache.org/ Or you can get it included with other projects like XAMPP : https://www.apachefriends.org/download.html Install then open the file: xamppapacheconfhttpd.conf search for <Directory /> Then after it add Options FollowSymLinks +ExecCGI So we have <Directory /> Options FollowSymLinks +ExecCGI Search for the next line and be sure that it’s not commented LoadModule cgi_module modules/mod_cgi.so Search for : AddHandler cgi-script Then add ”.ring” to the supported cgi extensions Example AddHandler cgi-script .cgi .ring Example AddHandler cgi-script .cgi .pl .asp .ring 418
  • 9. Ring Documentation, Release 1.8 Run/Start the server Create your web applications in a directory supported by the web server. Example: Apache2.2htdocsmywebapplicationfolder Example: xampphtdocsmywebapplicationfolder Inside the source code file (*.ring), Add this line #!ring -cgi Note: Change the previous line based on the path to ring.exe in your machine 50.2 Ring CGI Hello World Program The next program is the Hello World program #!ring -cgi See "content-type : text/html" +nl+nl+ "Hello World!" + nl 50.3 Hello World Program using the Web Library We can use the web library to write CGI Web applications quickly Example (1) : #!ring -cgi Load "weblib.ring" Import System.Web New Page { Text("Hello World!") } Example (2) : #!ring -cgi Load "weblib.ring" Import System.Web WebPage() { Text("Hello World!") } 50.2. Ring CGI Hello World Program 419
  • 10. Ring Documentation, Release 1.8 Tip: the difference between ex. 1 and ex. 2 is using WebPage() function to return the page object instead of creating the object using new statement. 50.4 Web Library Features The next features are provided by the Web library to quickly create web applications. • Generate HTML pages using functions • Generate HTML pages using objects • HTTP Get • HTTP Post • Files Upload • URL Encode • Templates • CRUD MVC Sample • Users Logic & Registration Sample 50.5 HTTP Get Example The Page User Interface #!ring -cgi Load "weblib.ring" Import System.Web New Page { Title = "Test HTTP Get" divstart([ :style = StyleSizeFull() ] ) boxstart() text( "Test HTTP GET" ) newline() boxend() divstart([ :style = Styledivcenter("600px","550px") + StyleGradient(21) ]) divstart([:style = stylefloatleft() + stylesize("100px","100%") + stylecolor("black") + stylegradient(58)]) formstart("ex5.ring") tablestart([ :style = stylesize("65%","90%") + stylemarginleft("35%") + stylemargintop("30%") ]) rowstart([]) cellstart([]) text ( "Name : " ) cellend() cellstart([]) cTextboxStyle = StyleMarginLeft("5%") + StyleWidth("250px") + StyleColor("black") + 50.4. Web Library Features 420