2 single: Natural Language Programming; Introduction
4 ============================
5 Natural Language Programming
6 ============================
8 Using the Ring programming language, we can create Natural programming languages based
9 on classes and objects.
12 pair: Natural Language Programming; History
17 In 2010, I developed a new programming language called Supernova (developed using PWCT).
18 This language uses a code that looks similar to Natural Language statements to create simple GUI applications.
19 Now after five years, In the Ring programming language, we can get similar results, but now we have the ability
20 to create/use code similar to Natural language statements in any domain that we like or need.
22 The Ring programming language comes with the Supernova spirit, but with more generalization and with mix of other languages
26 pair: Natural Language Programming; Example
31 The next example presents how to create a class that define two instructions
33 The first instruction is : I want window
35 The second instruction is : Window title = <expr>
37 Also keywords that can be ignored like the 'the' keyword
44 The window title = "hello world"
49 # Attributes for the instruction I want window
52 # Attributes for the instruction Window title
53 # Here we don't define the window attribute again
56 # Keywords to ignore, just give them any value
72 see "Instruction : I want window" + nl
81 see "Instruction : Window Title = " + cValue + nl
89 Instruction : I want window
90 Instruction : Window Title = hello world
94 pair: Natural Language Programming; Change the Ring Keyword 'And'
96 Change the Ring Keyword 'And'
97 =============================
99 What if we want to connect between the two instructions using 'and'
101 We have a problem because in Ring 'and' is a keyword
103 We can change that using the ChangeRingKeyword command.
109 ChangeRingKeyword <oldkeyword> <newkeyword>
111 .. note:: remember to restore the keyword again
113 .. tip:: The ChangeRingKeyword command is executed in the scanner stage by the compiler (before parsing).
119 ChangeRingKeyword and _and
123 I want window and the window title = "hello world"
128 # Attributes for the instruction I want window
131 # Attributes for the instruction Window title
132 # Here we don't define the window attribute again
135 # Keywords to ignore, just give them any value
138 ChangeRingKeyword _and and
153 see "Instruction : I want window" + nl
162 see "Instruction : Window Title = " + cValue + nl
166 see "Using : and" + nl
174 Instruction : I want window
176 Instruction : Window Title = hello world
180 pair: Natural Language Programming; Change the Ring Operator '+'
182 Change the Ring Operator '+'
183 ============================
185 What if we want to define a new behavior for any operator like the "+" operator.
187 We can do this change using the ChangeRingOperator command to hide operator (change it's name)
189 Then we can use the operator as identifier that we can handle it's behaviour
195 ChangeRingOperator <oldoperator> <newoperator>
197 .. note:: remember to restore the operator again
199 .. tip:: The ChangeRingOperator command is executed in the scanner stage by the compiler (before parsing).
205 ChangeRingOperator + _+
216 ChangeRingOperator _+ +
225 pair: Natural Language Programming; Change the '=' operator to 'is'
227 Change the '=' operator to 'is'
228 ===============================
234 ChangeRingKeyword and _and
235 ChangeRingOperator = is
239 I want window and the window title is "hello world"
242 ChangeRingOperator is =
246 # Attributes for the instruction I want window
249 # Attributes for the instruction Window title
250 # Here we don't define the window attribute again
253 # Keywords to ignore, just give them any value
256 ChangeRingKeyword _and and
271 see "Instruction : I want window" + nl
280 see "Instruction : Window Title = " + cValue + nl
285 pair: Natural Language Programming; Using Eval() with our Natural Code
287 Using Eval() with our Natural Code
288 ==================================
296 cProgram = ' I want window and the window title is "hello world" '
300 Func MyLanguage cCode
302 # We add to the code the instructions that change keywords and operators
303 # Because Eval() uses a new Compiler Object (the original keywords and operatos).
306 ChangeRingKeyword and _and
307 ChangeRingOperator = is
318 # Attributes for the instruction I want window
321 # Attributes for the instruction Window title
322 # Here we don't define the window attribute again
325 # Keywords to ignore, just give them any value
328 ChangeRingKeyword and _and
330 ChangeRingKeyword _and and
345 see "Instruction : I want window" + nl
354 see "Instruction : Window Title = " + cValue + nl
359 pair: Natural Language Programming; BraceStart and BraceEnd Methods
361 BraceStart and BraceEnd Methods
362 ===============================
364 We can write code that will be executed before/after using { }
396 pair: Natural Language Programming; BraceExprEval Method
401 The next example demonstrates how to use the "BraceExprEval" method to get expressions in
416 if lkeyword lkeyword=false return ok
418 see "type: " + type(r) see nl
419 see "value : " see r see nl
434 pair: Natural Language Programming; Real Natual Code
439 The next example is a more advanced example
445 Accept 2 numbers then print the sum
448 # Natural Code Implementation
451 Accept=0 numbers=0 then=0 print=0 the=0 sum=0
458 see "Enter Number ("+x+") :" give nNumber
463 for x in aNumbers nSum+= x next
464 see "The Sum : " + nSum
477 pair: Natural Language Programming; BraceError() Method
482 The next examples demonstrates how to use the "BraceError" method to handle errors when accessing
483 the object using braces {}.
498 see "Handle Error!" + nl
499 SEE "Message :" + cCatchError + nl
500 if ( left(cCatchError,11) = "Error (R24)" ) and not isattribute(self,"test")
501 see "add attribute" + nl
502 addattribute(self,"test")
513 Message :Error (R24) : Using uninitialized variable : test
530 see "Handle Error!" + nl
531 see "Message :" + cCatchError + nl
540 Message :Error (R3) : Calling Function without definition !: test
548 pair: Natural Language Programming; Clean Natural Code
553 Instead of typing the literal as "literal" we can accept the words directly.
557 The next example accept hello world instead of "hello world"
559 But this example uses braceend() to check the end of the instruction
561 This means that this class process only one natural statement that end with literal.
565 ChangeRingKeyword and _and
569 I want window and the window title is hello world
574 # Attributes for the instruction I want window
577 # Attributes for the instruction Window title
578 # Here we don't define the window attribute again
581 # Keywords to ignore, just give them any value
586 ChangeRingKeyword _and and
601 see "Instruction : I want window" + nl
619 see "Instruction : Window Title = " + literal + nl
624 c= substr(cCatchError,":")
626 c= substr(cCatchError,":")
627 cCatchError=substr(cCatchError,c+1)
629 literal += substr(cCatchError,1)