Sunteți pe pagina 1din 10

Ring Documentation, Release 1.

Load "stdlib.ring"

osecuirty = new secuirty


See "Test the secuirty Class Methods" + nl
oSecuirty {
see md5("hello") + nl +
sha1("hello") + nl + sha256("hello") + nl +
sha512("hello") + nl + sha384("hello") + nl +
sha256("hello") + nl
list = 0:15 cKey="" for x in list cKey += char(x) next
list = 1:8 cIV = "" for x in list cIV += char(x) next
cCipher = encrypt("hello",cKey,cIV)
see cCipher + nl + decrypt(cCipher,cKey,cIV) + nl
}

44.19 Internet Class

Methods:
• download(cURL)
• sendemail(cSMTPServer,cEmail,cPassword,cSender,cReceiver,cCC,cTitle,cContent)
example:
Load "stdlib.ring"

ointernet = new internet


See "Test the internet Class Methods" + nl
ointernet {
see download("www.ring-lang.sf.net")
}

44.19. Internet Class 363


CHAPTER

FORTYFIVE

DECLARATIVE PROGRAMMING USING NESTED STRUCTURES

In this chapter we are going to learn how to build declarative programming world using nested structures on the top of
object oriented.
We will learn about
• Creating Objects inside Lists
• Composition and Returning Objects and Lists by Reference
• Executing code after the end of object access
• Declarative Programming on the top of Object-Oriented

45.1 Creating Objects inside Lists

We can create objects inside lists during list definition. Also we can add objects to the list at any time using the Add()
function or the + operator.
Example:
alist = [new point, new point, new point] # create list contains three objects

alist + [1,2,3] # add another item to the list

see "Item 4 is a list contains 3 items" + nl


see alist[4]

add(alist , new point)


alist + new point

alist[5] { x = 100 y = 200 z = 300 }


alist[6] { x = 50 y = 150 z = 250 }

see "Object inside item 5" + nl


see alist[5]
see "Object inside item 6" + nl
see alist[6]

class point x y z

Output:
Item 4 is a list contains 3 items
1
2

364
Ring Documentation, Release 1.6

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

45.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 caller 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 it’s 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

45.2. Composition and Returning Objects and Lists by Reference 365


Ring Documentation, Release 1.6

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

45.2. Composition and Returning Objects and Lists by Reference 366


Ring Documentation, Release 1.6

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

45.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!

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

45.3. Executing code after the end of object access 367


Ring Documentation, Release 1.6

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!

45.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
{

45.5. More beautiful Code 368


Ring Documentation, Release 1.6

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

45.5. More beautiful Code 369


CHAPTER

FORTYSIX

NATURAL LANGUAGE PROGRAMMING

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

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

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

370
Ring Documentation, Release 1.6

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

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

46.3. Change the Ring Keyword ‘And’ 371


Ring Documentation, Release 1.6

# 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

46.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:

46.4. Change the Ring Operator ‘+’ 372

S-ar putea să vă placă și