SlideShare a Scribd company logo
Ring Documentation, Release 1.7
point()
{
x = 50
y = 150
z = 250
}
}
# Functions and Classes
Func screen return new screen
Class Screen
content = []
func point
content + new point
return content[len(content)]
func braceend
see "I have " + len(content) + " points!"
Class point
x=10 y=20 z=30
func braceend
see self
Output:
x: 100.000000
y: 200.000000
z: 300.000000
x: 50.000000
y: 150.000000
z: 250.000000
I have 2 points!
46.5 More beautiful Code
We can get better results and a more beautiful code when we can avoid writing () after the method name when
the methods doesn’t take parameters. This feature is not provided directly by the Ring language because there is a
difference between object methods and object attributes. We can get a similar effect on the syntax of the code when
we define a getter method for the object attribute. For example instead of defining the point() method. we will define
the point attribute then the getpoint() method that will be executed once you try to get the value of the point attribute.
since we write the variable name direcly without () we can write point instead of point() and the method getpoint()
will create the object and return the object reference for us.
Example:
new Container
{
Point
{
46.5. More beautiful Code 382
Ring Documentation, Release 1.7
x=10
y=20
z=30
}
}
Class Container
aObjs = []
point
func getpoint
aObjs + new Point
return aObjs[len(aObjs)]
Class Point x y z
func braceend
see "3D Point" + nl + x + nl + y + nl + z + nl
Output
3D Point
10
20
30
46.5. More beautiful Code 383
CHAPTER
FORTYSEVEN
NATURAL LANGUAGE PROGRAMMING
Using the Ring programming language, we can create Natural programming languages based on classes and objects.
47.1 History
In 2010, I developed a new programming language called Supernova (developed using PWCT). This language uses
a code that looks similar to Natural Language statements to create simple GUI applications. Now after five years, In
the Ring programming language, we can get similar results, but now we have the ability to create/use code similar to
Natural language statements in any domain that we like or need.
The Ring programming language comes with the Supernova spirit, but with more generalization and with mix of other
languages spirits.
47.2 Example
The next example presents how to create a class that define two instructions
The first instruction is : I want window
The second instruction is : Window title = <expr>
Also keywords that can be ignored like the ‘the’ keyword
New App
{
I want window
The window title = "hello world"
}
Class App
# Attributes for the instruction I want window
i want window
nIwantwindow = 0
# Attributes for the instruction Window title
# Here we don't define the window attribute again
title
nWindowTitle = 0
# Keywords to ignore, just give them any value
the=0
func geti
384
Ring Documentation, Release 1.7
if nIwantwindow = 0
nIwantwindow++
ok
func getwant
if nIwantwindow = 1
nIwantwindow++
ok
func getwindow
if nIwantwindow = 2
nIwantwindow= 0
see "Instruction : I want window" + nl
ok
if nWindowTitle = 0
nWindowTitle++
ok
func settitle cValue
if nWindowTitle = 1
nWindowTitle=0
see "Instruction : Window Title = " + cValue + nl
ok
Output:
Instruction : I want window
Instruction : Window Title = hello world
47.3 Change the Ring Keyword ‘And’
What if we want to connect between the two instructions using ‘and’
We have a problem because in Ring ‘and’ is a keyword
We can change that using the ChangeRingKeyword command.
Syntax:
ChangeRingKeyword <oldkeyword> <newkeyword>
Note: remember to restore the keyword again
Tip: The ChangeRingKeyword command is executed in the scanner stage by the compiler (before parsing).
Example:
ChangeRingKeyword and _and
New App
{
I want window and the window title = "hello world"
}
Class App
47.3. Change the Ring Keyword ‘And’ 385
Ring Documentation, Release 1.7
# Attributes for the instruction I want window
i want window
nIwantwindow = 0
# Attributes for the instruction Window title
# Here we don't define the window attribute again
title
nWindowTitle = 0
# Keywords to ignore, just give them any value
the=0 and=0
ChangeRingKeyword _and and
func geti
if nIwantwindow = 0
nIwantwindow++
ok
func getwant
if nIwantwindow = 1
nIwantwindow++
ok
func getwindow
if nIwantwindow = 2
nIwantwindow= 0
see "Instruction : I want window" + nl
ok
if nWindowTitle = 0
nWindowTitle++
ok
func settitle cValue
if nWindowTitle = 1
nWindowTitle=0
see "Instruction : Window Title = " + cValue + nl
ok
func getand
see "Using : and" + nl
Output:
Instruction : I want window
Using : and
Instruction : Window Title = hello world
47.4 Change the Ring Operator ‘+’
What if we want to define a new behavior for any operator like the “+” operator.
We can do this change using the ChangeRingOperator command to hide operator (change it’s name)
Then we can use the operator as identifier that we can handle it’s behaviour
Syntax:
47.4. Change the Ring Operator ‘+’ 386
Ring Documentation, Release 1.7
ChangeRingOperator <oldoperator> <newoperator>
Note: remember to restore the operator again
Tip: The ChangeRingOperator command is executed in the scanner stage by the compiler (before parsing).
Example:
ChangeRingOperator + _+
New App {
+
}
Class App
+
func get+
see "Plus operator"
ChangeRingOperator _+ +
Output:
Plus operator
47.5 Change the ‘=’ operator to ‘is’
Example:
ChangeRingKeyword and _and
ChangeRingOperator = is
New App
{
I want window and the window title is "hello world"
}
ChangeRingOperator is =
Class App
# Attributes for the instruction I want window
i want window
nIwantwindow = 0
# Attributes for the instruction Window title
# Here we don't define the window attribute again
title
nWindowTitle = 0
# Keywords to ignore, just give them any value
the=0 and=0
ChangeRingKeyword _and and
func geti
if nIwantwindow = 0
47.5. Change the ‘=’ operator to ‘is’ 387
Ring Documentation, Release 1.7
nIwantwindow++
ok
func getwant
if nIwantwindow = 1
nIwantwindow++
ok
func getwindow
if nIwantwindow = 2
nIwantwindow= 0
see "Instruction : I want window" + nl
ok
if nWindowTitle = 0
nWindowTitle++
ok
func settitle cValue
if nWindowTitle = 1
nWindowTitle=0
see "Instruction : Window Title = " + cValue + nl
ok
47.6 Using Eval() with our Natural Code
Example:
func Main
cProgram = ' I want window and the window title is "hello world" '
MyLanguage(cProgram)
Func MyLanguage cCode
# We add to the code the instructions that change keywords and operators
# Because Eval() uses a new Compiler Object (the original keywords and operatos).
cCode = '
ChangeRingKeyword and _and
ChangeRingOperator = is
' + cCode
New App
{
eval(cCode)
}
Class App
# Attributes for the instruction I want window
i want window
nIwantwindow = 0
# Attributes for the instruction Window title
# Here we don't define the window attribute again
47.6. Using Eval() with our Natural Code 388
Ring Documentation, Release 1.7
title
nWindowTitle = 0
# Keywords to ignore, just give them any value
the=0
ChangeRingKeyword and _and
and=0
ChangeRingKeyword _and and
func geti
if nIwantwindow = 0
nIwantwindow++
ok
func getwant
if nIwantwindow = 1
nIwantwindow++
ok
func getwindow
if nIwantwindow = 2
nIwantwindow= 0
see "Instruction : I want window" + nl
ok
if nWindowTitle = 0
nWindowTitle++
ok
func settitle cValue
if nWindowTitle = 1
nWindowTitle=0
see "Instruction : Window Title = " + cValue + nl
ok
47.7 BraceStart and BraceEnd Methods
We can write code that will be executed before/after using { }
Example:
o1 = new test {
see "Hello" + nl
}
o1 {}
class test
func bracestart
see "start" + nl
func braceend
see "end" + nl
Output:
47.7. BraceStart and BraceEnd Methods 389
Ring Documentation, Release 1.7
start
Hello
end
start
end
47.8 BraceExprEval Method
The next example demonstrates how to use the “BraceExprEval” method to get expressions in Natural code.
Example:
new natural {
create 5
}
class natural
create=0
lkeyword = false
func braceexpreval r
if lkeyword lkeyword=false return ok
see "expr eval" + nl
see "type: " + type(r) see nl
see "value : " see r see nl
func getcreate
lkeyword = true
see "create" + nl
Output:
create
expr eval
type: NUMBER
value : 5
47.9 Real Natural Code
The next example is a more advanced example
# Natural Code
new program {
Accept 2 numbers then print the sum
}
# Natural Code Implementation
class program
# Keywords
Accept=0 numbers=0 then=0 print=0 the=0 sum=0
# Execution
func braceexpreval x
value = x
func getnumbers
for x=1 to value
see "Enter Number ("+x+") :" give nNumber
47.8. BraceExprEval Method 390
Ring Documentation, Release 1.7
aNumbers + nNumber
next
func getsum
nSUm = 0
for x in aNumbers nSum+= x next
see "The Sum : " + nSum
private
value=0 aNumbers=[]
Output:
Enter Number (1) :3
Enter Number (2) :4
The Sum : 7
47.10 BraceError() Method
The next examples demonstrates how to use the “BraceError” method to handle errors when accessing the object using
braces {}.
Example:
func main
o1 = new point {
x=10 y=20 z=30
TEST
SEE test
}
class point x y z
func braceerror
see "Handle Error!" + nl
SEE "Message :" + cCatchError + nl
if ( left(cCatchError,11) = "Error (R24)" ) and not isattribute(self,"test")
see "add attribute" + nl
addattribute(self,"test")
test = 10
ok
see "done" + nl
return
Output:
Handle Error!
Message :Error (R24) : Using uninitialized variable : test
add attribute
done
10
Example:
new point {
x=10 y=20 z=30
test()
see "mmm..." + NL
}
47.10. BraceError() Method 391

More Related Content

PDF
The Ring programming language version 1.4.1 book - Part 11 of 31
PDF
The Ring programming language version 1.8 book - Part 44 of 202
PDF
The Ring programming language version 1.5.2 book - Part 38 of 181
PDF
The Ring programming language version 1.9 book - Part 47 of 210
PDF
The Ring programming language version 1.5.3 book - Part 38 of 184
PDF
The Ring programming language version 1.10 book - Part 48 of 212
PDF
The Ring programming language version 1.5 book - Part 7 of 31
PDF
The Ring programming language version 1.3 book - Part 29 of 88
The Ring programming language version 1.4.1 book - Part 11 of 31
The Ring programming language version 1.8 book - Part 44 of 202
The Ring programming language version 1.5.2 book - Part 38 of 181
The Ring programming language version 1.9 book - Part 47 of 210
The Ring programming language version 1.5.3 book - Part 38 of 184
The Ring programming language version 1.10 book - Part 48 of 212
The Ring programming language version 1.5 book - Part 7 of 31
The Ring programming language version 1.3 book - Part 29 of 88

What's hot (20)

PDF
The Ring programming language version 1.2 book - Part 27 of 84
PDF
The Ring programming language version 1.6 book - Part 40 of 189
PDF
The Ring programming language version 1.5.4 book - Part 38 of 185
PDF
Asynchronous JS in Odoo
PPTX
Oojs 1.1
PPTX
Working effectively with legacy code
PDF
JavaProgrammingManual
PDF
The Ring programming language version 1.5.1 book - Part 175 of 180
PPTX
Understanding Async/Await in Javascript
PDF
C++ practical
PPT
CodeMash - Building Rich Apps with Groovy SwingBuilder
PPTX
Max Koretskyi "Why are Angular and React so fast?"
PPTX
Oop objects_classes
PPT
AngularJS Testing Strategies
PDF
Let the type system be your friend
PDF
How to Create a l10n Payroll Structure
PDF
The Ring programming language version 1.5.2 book - Part 37 of 181
DOCX
Java codes
PDF
Promise: async programming hero
PDF
Tdd.eng.ver
The Ring programming language version 1.2 book - Part 27 of 84
The Ring programming language version 1.6 book - Part 40 of 189
The Ring programming language version 1.5.4 book - Part 38 of 185
Asynchronous JS in Odoo
Oojs 1.1
Working effectively with legacy code
JavaProgrammingManual
The Ring programming language version 1.5.1 book - Part 175 of 180
Understanding Async/Await in Javascript
C++ practical
CodeMash - Building Rich Apps with Groovy SwingBuilder
Max Koretskyi "Why are Angular and React so fast?"
Oop objects_classes
AngularJS Testing Strategies
Let the type system be your friend
How to Create a l10n Payroll Structure
The Ring programming language version 1.5.2 book - Part 37 of 181
Java codes
Promise: async programming hero
Tdd.eng.ver
Ad

Similar to The Ring programming language version 1.7 book - Part 42 of 196 (20)

PDF
The Ring programming language version 1.5.1 book - Part 37 of 180
PDF
The Ring programming language version 1.6 book - Part 41 of 189
PDF
The Ring programming language version 1.4 book - Part 11 of 30
PDF
The Ring programming language version 1.5.4 book - Part 39 of 185
PDF
The Ring programming language version 1.3 book - Part 30 of 88
PDF
The Ring programming language version 1.3 book - Part 5 of 88
PDF
The Ring programming language version 1.10 book - Part 49 of 212
PDF
The Ring programming language version 1.9 book - Part 48 of 210
PDF
The Ring programming language version 1.8 book - Part 7 of 202
PDF
The Ring programming language version 1.5.2 book - Part 6 of 181
PDF
The Ring programming language version 1.5.3 book - Part 39 of 184
PDF
The Ring programming language version 1.9 book - Part 7 of 210
PDF
The Ring programming language version 1.8 book - Part 80 of 202
PDF
The Ring programming language version 1.9 book - Part 99 of 210
PPTX
UNIT-5 object oriented programming lecture
PDF
The Ring programming language version 1.2 book - Part 5 of 84
PDF
The Ring programming language version 1.2 book - Part 28 of 84
PDF
The Ring programming language version 1.5.4 book - Part 73 of 185
PDF
Rechecking SharpDevelop: Any New Bugs?
PDF
The Ring programming language version 1.5.1 book - Part 36 of 180
The Ring programming language version 1.5.1 book - Part 37 of 180
The Ring programming language version 1.6 book - Part 41 of 189
The Ring programming language version 1.4 book - Part 11 of 30
The Ring programming language version 1.5.4 book - Part 39 of 185
The Ring programming language version 1.3 book - Part 30 of 88
The Ring programming language version 1.3 book - Part 5 of 88
The Ring programming language version 1.10 book - Part 49 of 212
The Ring programming language version 1.9 book - Part 48 of 210
The Ring programming language version 1.8 book - Part 7 of 202
The Ring programming language version 1.5.2 book - Part 6 of 181
The Ring programming language version 1.5.3 book - Part 39 of 184
The Ring programming language version 1.9 book - Part 7 of 210
The Ring programming language version 1.8 book - Part 80 of 202
The Ring programming language version 1.9 book - Part 99 of 210
UNIT-5 object oriented programming lecture
The Ring programming language version 1.2 book - Part 5 of 84
The Ring programming language version 1.2 book - Part 28 of 84
The Ring programming language version 1.5.4 book - Part 73 of 185
Rechecking SharpDevelop: Any New Bugs?
The Ring programming language version 1.5.1 book - Part 36 of 180
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
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
A Presentation on Artificial Intelligence
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Empathic Computing: Creating Shared Understanding
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Spectroscopy.pptx food analysis technology
PPTX
OMC Textile Division Presentation 2021.pptx
PPTX
1. Introduction to Computer Programming.pptx
PDF
Getting Started with Data Integration: FME Form 101
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Network Security Unit 5.pdf for BCA BBA.
NewMind AI Weekly Chronicles - August'25-Week II
Mobile App Security Testing_ A Comprehensive Guide.pdf
Programs and apps: productivity, graphics, security and other tools
Reach Out and Touch Someone: Haptics and Empathic Computing
Unlocking AI with Model Context Protocol (MCP)
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
A Presentation on Artificial Intelligence
Assigned Numbers - 2025 - Bluetooth® Document
Empathic Computing: Creating Shared Understanding
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Spectroscopy.pptx food analysis technology
OMC Textile Division Presentation 2021.pptx
1. Introduction to Computer Programming.pptx
Getting Started with Data Integration: FME Form 101
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

The Ring programming language version 1.7 book - Part 42 of 196

  • 1. Ring Documentation, Release 1.7 point() { x = 50 y = 150 z = 250 } } # Functions and Classes Func screen return new screen Class Screen content = [] func point content + new point return content[len(content)] func braceend see "I have " + len(content) + " points!" Class point x=10 y=20 z=30 func braceend see self Output: x: 100.000000 y: 200.000000 z: 300.000000 x: 50.000000 y: 150.000000 z: 250.000000 I have 2 points! 46.5 More beautiful Code We can get better results and a more beautiful code when we can avoid writing () after the method name when the methods doesn’t take parameters. This feature is not provided directly by the Ring language because there is a difference between object methods and object attributes. We can get a similar effect on the syntax of the code when we define a getter method for the object attribute. For example instead of defining the point() method. we will define the point attribute then the getpoint() method that will be executed once you try to get the value of the point attribute. since we write the variable name direcly without () we can write point instead of point() and the method getpoint() will create the object and return the object reference for us. Example: new Container { Point { 46.5. More beautiful Code 382
  • 2. Ring Documentation, Release 1.7 x=10 y=20 z=30 } } Class Container aObjs = [] point func getpoint aObjs + new Point return aObjs[len(aObjs)] Class Point x y z func braceend see "3D Point" + nl + x + nl + y + nl + z + nl Output 3D Point 10 20 30 46.5. More beautiful Code 383
  • 3. CHAPTER FORTYSEVEN NATURAL LANGUAGE PROGRAMMING Using the Ring programming language, we can create Natural programming languages based on classes and objects. 47.1 History In 2010, I developed a new programming language called Supernova (developed using PWCT). This language uses a code that looks similar to Natural Language statements to create simple GUI applications. Now after five years, In the Ring programming language, we can get similar results, but now we have the ability to create/use code similar to Natural language statements in any domain that we like or need. The Ring programming language comes with the Supernova spirit, but with more generalization and with mix of other languages spirits. 47.2 Example The next example presents how to create a class that define two instructions The first instruction is : I want window The second instruction is : Window title = <expr> Also keywords that can be ignored like the ‘the’ keyword New App { I want window The window title = "hello world" } Class App # Attributes for the instruction I want window i want window nIwantwindow = 0 # Attributes for the instruction Window title # Here we don't define the window attribute again title nWindowTitle = 0 # Keywords to ignore, just give them any value the=0 func geti 384
  • 4. Ring Documentation, Release 1.7 if nIwantwindow = 0 nIwantwindow++ ok func getwant if nIwantwindow = 1 nIwantwindow++ ok func getwindow if nIwantwindow = 2 nIwantwindow= 0 see "Instruction : I want window" + nl ok if nWindowTitle = 0 nWindowTitle++ ok func settitle cValue if nWindowTitle = 1 nWindowTitle=0 see "Instruction : Window Title = " + cValue + nl ok Output: Instruction : I want window Instruction : Window Title = hello world 47.3 Change the Ring Keyword ‘And’ What if we want to connect between the two instructions using ‘and’ We have a problem because in Ring ‘and’ is a keyword We can change that using the ChangeRingKeyword command. Syntax: ChangeRingKeyword <oldkeyword> <newkeyword> Note: remember to restore the keyword again Tip: The ChangeRingKeyword command is executed in the scanner stage by the compiler (before parsing). Example: ChangeRingKeyword and _and New App { I want window and the window title = "hello world" } Class App 47.3. Change the Ring Keyword ‘And’ 385
  • 5. Ring Documentation, Release 1.7 # Attributes for the instruction I want window i want window nIwantwindow = 0 # Attributes for the instruction Window title # Here we don't define the window attribute again title nWindowTitle = 0 # Keywords to ignore, just give them any value the=0 and=0 ChangeRingKeyword _and and func geti if nIwantwindow = 0 nIwantwindow++ ok func getwant if nIwantwindow = 1 nIwantwindow++ ok func getwindow if nIwantwindow = 2 nIwantwindow= 0 see "Instruction : I want window" + nl ok if nWindowTitle = 0 nWindowTitle++ ok func settitle cValue if nWindowTitle = 1 nWindowTitle=0 see "Instruction : Window Title = " + cValue + nl ok func getand see "Using : and" + nl Output: Instruction : I want window Using : and Instruction : Window Title = hello world 47.4 Change the Ring Operator ‘+’ What if we want to define a new behavior for any operator like the “+” operator. We can do this change using the ChangeRingOperator command to hide operator (change it’s name) Then we can use the operator as identifier that we can handle it’s behaviour Syntax: 47.4. Change the Ring Operator ‘+’ 386
  • 6. Ring Documentation, Release 1.7 ChangeRingOperator <oldoperator> <newoperator> Note: remember to restore the operator again Tip: The ChangeRingOperator command is executed in the scanner stage by the compiler (before parsing). Example: ChangeRingOperator + _+ New App { + } Class App + func get+ see "Plus operator" ChangeRingOperator _+ + Output: Plus operator 47.5 Change the ‘=’ operator to ‘is’ Example: ChangeRingKeyword and _and ChangeRingOperator = is New App { I want window and the window title is "hello world" } ChangeRingOperator is = Class App # Attributes for the instruction I want window i want window nIwantwindow = 0 # Attributes for the instruction Window title # Here we don't define the window attribute again title nWindowTitle = 0 # Keywords to ignore, just give them any value the=0 and=0 ChangeRingKeyword _and and func geti if nIwantwindow = 0 47.5. Change the ‘=’ operator to ‘is’ 387
  • 7. Ring Documentation, Release 1.7 nIwantwindow++ ok func getwant if nIwantwindow = 1 nIwantwindow++ ok func getwindow if nIwantwindow = 2 nIwantwindow= 0 see "Instruction : I want window" + nl ok if nWindowTitle = 0 nWindowTitle++ ok func settitle cValue if nWindowTitle = 1 nWindowTitle=0 see "Instruction : Window Title = " + cValue + nl ok 47.6 Using Eval() with our Natural Code Example: func Main cProgram = ' I want window and the window title is "hello world" ' MyLanguage(cProgram) Func MyLanguage cCode # We add to the code the instructions that change keywords and operators # Because Eval() uses a new Compiler Object (the original keywords and operatos). cCode = ' ChangeRingKeyword and _and ChangeRingOperator = is ' + cCode New App { eval(cCode) } Class App # Attributes for the instruction I want window i want window nIwantwindow = 0 # Attributes for the instruction Window title # Here we don't define the window attribute again 47.6. Using Eval() with our Natural Code 388
  • 8. Ring Documentation, Release 1.7 title nWindowTitle = 0 # Keywords to ignore, just give them any value the=0 ChangeRingKeyword and _and and=0 ChangeRingKeyword _and and func geti if nIwantwindow = 0 nIwantwindow++ ok func getwant if nIwantwindow = 1 nIwantwindow++ ok func getwindow if nIwantwindow = 2 nIwantwindow= 0 see "Instruction : I want window" + nl ok if nWindowTitle = 0 nWindowTitle++ ok func settitle cValue if nWindowTitle = 1 nWindowTitle=0 see "Instruction : Window Title = " + cValue + nl ok 47.7 BraceStart and BraceEnd Methods We can write code that will be executed before/after using { } Example: o1 = new test { see "Hello" + nl } o1 {} class test func bracestart see "start" + nl func braceend see "end" + nl Output: 47.7. BraceStart and BraceEnd Methods 389
  • 9. Ring Documentation, Release 1.7 start Hello end start end 47.8 BraceExprEval Method The next example demonstrates how to use the “BraceExprEval” method to get expressions in Natural code. Example: new natural { create 5 } class natural create=0 lkeyword = false func braceexpreval r if lkeyword lkeyword=false return ok see "expr eval" + nl see "type: " + type(r) see nl see "value : " see r see nl func getcreate lkeyword = true see "create" + nl Output: create expr eval type: NUMBER value : 5 47.9 Real Natural Code The next example is a more advanced example # Natural Code new program { Accept 2 numbers then print the sum } # Natural Code Implementation class program # Keywords Accept=0 numbers=0 then=0 print=0 the=0 sum=0 # Execution func braceexpreval x value = x func getnumbers for x=1 to value see "Enter Number ("+x+") :" give nNumber 47.8. BraceExprEval Method 390
  • 10. Ring Documentation, Release 1.7 aNumbers + nNumber next func getsum nSUm = 0 for x in aNumbers nSum+= x next see "The Sum : " + nSum private value=0 aNumbers=[] Output: Enter Number (1) :3 Enter Number (2) :4 The Sum : 7 47.10 BraceError() Method The next examples demonstrates how to use the “BraceError” method to handle errors when accessing the object using braces {}. Example: func main o1 = new point { x=10 y=20 z=30 TEST SEE test } class point x y z func braceerror see "Handle Error!" + nl SEE "Message :" + cCatchError + nl if ( left(cCatchError,11) = "Error (R24)" ) and not isattribute(self,"test") see "add attribute" + nl addattribute(self,"test") test = 10 ok see "done" + nl return Output: Handle Error! Message :Error (R24) : Using uninitialized variable : test add attribute done 10 Example: new point { x=10 y=20 z=30 test() see "mmm..." + NL } 47.10. BraceError() Method 391