You are on page 1of 10

Ring Documentation, Release 1.

3
Object inside item 5
x: 100.000000
y: 200.000000
z: 300.000000
Object inside item 6
x: 50.000000
y: 150.000000
z: 250.000000

39.2 Composition and Returning Objects and Lists by Reference

When we use composition and have object as one of the class attributes, when we return that object it will be returned
by reference.
if the called used the assignment operator, another copy of the object will be created.
The caller can avoid using the assignment operator and use the returned reference directly to access the object.
The same is done also if the attribute is a list (not object).

Note: Objects and Lists are treated using the same rules. When you pass them to function they are passed by
reference,

when you return them from functions they are returned by value except if its an object attribute where a return by
reference will be done.
Example:
o1 = new Container
myobj = o1.addobj() # the assignment will create another copy
myobj.x = 100
myobj.y = 200
myobj.z = 300
see o1.aobjs[1] # print the object inside the container
see myobj # print the copy

Class Container
aObjs = []
func addobj
aobjs + new point
return aobjs[len(aobjs)] # return object by reference

Class point
x = 10
y = 20
z = 30

Output:
x: 10.000000
y: 20.000000
z: 30.000000
x: 100.000000
y: 200.000000
z: 300.000000

39.2. Composition and Returning Objects and Lists by Reference 240


Ring Documentation, Release 1.2

Example(2):
func main
o1 = new screen {
content[point()] {
x = 100
y = 200
z = 300
}
content[point()] {
x = 50
y = 150
z = 250
}
}
see o1.content[1]
see o1.content[2]

Class Screen
content = []
func point
content + new point
return len(content)

Class point
x = 10
y = 20
z = 30

Output:
x: 100.000000
y: 200.000000
z: 300.000000
x: 50.000000
y: 150.000000
z: 250.000000

Example(3):
func main
o1 = New Screen {
point() { # access the object using reference
x = 100
y = 200
z = 300
}
point() { # access the object using reference
x = 50
y = 150
z = 250
}
}
see o1.content[1]
see o1.content[2]

Class Screen
content = []
func point
content + new point

39.2. Composition and Returning Objects and Lists by Reference 241


Ring Documentation, Release 1.2

return content[len(content)] # return the object by reference

Class point x=10 y=20 z=30

Output:
x: 100.000000
y: 200.000000
z: 300.000000
x: 50.000000
y: 150.000000
z: 250.000000

39.3 Executing code after the end of object access

We can access an object using { } to use object attributes and methods.


if the object contains a method called BraceEnd(), it will be executed before the end of the object access.
Example:
New Point { See "How are you?" + nl }

Class Point x y z
func braceend
see "I'm fine, Thank you!" + nl

Output:
How are you?
I'm fine, Thank you!

39.4 Declarative Programming on the top of Object-Oriented

The next features enable us to build and use declartive programming environment using nested structures on the top
of object oriented
using {} to access the object attributes and methods
BraceEnd() Method
returning objects by reference
Setter/Getter Methods (optional)
Example:
# Declartive Programming (Nested Structures)

Screen()
{

point()
{
x = 100
y = 200
z = 300

39.3. Executing code after the end of object access 242


Ring Documentation, Release 1.2

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!

39.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 doesnt 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
{

39.5. More beautiful Code 243


Ring Documentation, Release 1.2

Point
{
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

39.5. More beautiful Code 244


CHAPTER

FORTY

NATURAL LANGUAGE PROGRAMMING

Using the Ring programming language, we can create Natural programming languages based on classes and objects.

40.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 sprite, but with more generalization and with mix of other
languages sprites.

40.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

245
Ring Documentation, Release 1.2

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

40.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

40.3. Change the Ring Keyword And 246


Ring Documentation, Release 1.2

# 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

40.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 its name)
Then we can use the operator as identifier that we can handle its behaviour
Syntax:

40.4. Change the Ring Operator + 247


Ring Documentation, Release 1.2

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

40.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

40.5. Change the = operator to is 248


Ring Documentation, Release 1.2

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

40.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

40.6. Using Eval() with our Natural Code 249

You might also like