Professional Documents
Culture Documents
The Ring Programming Language Version 1.5.1 Book - Part 38 of 180
The Ring Programming Language Version 1.5.1 Book - Part 38 of 180
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!
Output:
Hello, Sir!
The Numbers!
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.
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)
}
])
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"
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)
}
])
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
In the next example we uses the Count command without using operators
load "mylanguage.ring"
MyLanguage.RunString("
Hello
Count 1 5
Count 5 1
")
MyLanguage.RunString("
Hello, Please Count from 1 to 5 then count from 5 to 1
")
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!
")
}
class Hello
func AddAttributes_Hello
AddAttribute(self,:hello)
func GetHello
See "Hello, Sir!" + nl + nl
File: count.ring
Package MyLanguage.Natural
class Count
func Getcount
StartCommand()
CommandData()[:name] = :Count
CommandData()[:nExpr] = 0
CommandData()[:aExpr] = []
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)
FORTYSEVEN
In this chapter we will learn about developing Web applications using a CGI Library written in the Ring language.
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:
xampp\apache\conf\httpd.conf
search for
<Directory />
So we have
<Directory />
Options FollowSymLinks +ExecCGI
Search for the next line and be sure that its not commented
LoadModule cgi_module modules/mod_cgi.so
Example
AddHandler cgi-script .cgi .pl .asp .ring
351
Ring Documentation, Release 1.5.1
Example:
xampp\htdocs\mywebapplicationfolder
Note: Change the previous line based on the path to ring.exe in your machine
We can use the web library to write CGI Web applications quickly
Example (1) :
#!c:\ring\bin\ring.exe -cgi
Load "weblib.ring"
Import System.Web
New Page
{
Text("Hello World!")
}
Example (2) :
#!c:\ring\bin\ring.exe -cgi
Load "weblib.ring"
Import System.Web
WebPage()
{
Text("Hello World!")
}
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.
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
StyleBackColor("white")
textbox([ :name = "Name", :style = cTextboxStyle ] )
cellend()
rowend()
rowstart([])
cellstart([])
text ( "Address : " )
cellend()
cellstart([])
textbox([ :name = "Address", :style = cTextboxStyle] )
cellend()
rowend()
rowstart([])
cellstart([])
text ( "Phone : " )
cellend()
cellstart([])
textbox([ :name = "Phone", :style = cTextboxStyle ])
cellend()
rowend()
rowstart([])
cellstart([])
text ( "Age : " )
cellend()
cellstart([])
textbox([ :name = "Age", :style = cTextboxStyle ])
cellend()
rowend()
rowstart([])
cellstart([])
text ( "City: " )
cellend()
cellstart([])
listbox([ :name = "City", :items = ["Cairo","Riyadh","Jeddah"],
:style = stylemarginleft("5%") + stylewidth("400px") ] )
cellend()
rowend()
rowstart([])
cellstart([])
text ( "Country : " )
cellend()
cellstart([])
combobox([ :name = "Country",
:items = ["Egypt","Saudi Arabia","USA"],
:style = stylemarginleft("5%") +
stylewidth("400px")+
stylecolor("black")+
stylebackcolor("white")+
stylefontsize("14px") ])
cellend()
rowend()
rowstart([])
cellstart([])
text ( "Note : " )
cellend()
cellstart([])
editbox([ :name = "Notes",
:style = stylemarginleft("5%") +