OSDN Git Service

【更新内容】
[ring-lang-081/ring.git] / docs / en / source / natural.txt
1 .. index:: 
2         single: Natural Language Programming; Introduction
3
4 ============================
5 Natural Language Programming
6 ============================
7
8 Using the Ring programming language, we can create Natural programming languages based
9 on classes and objects.
10
11 .. index:: 
12         pair: Natural Language Programming; History
13
14 History
15 =======
16
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.
21
22 The Ring programming language comes with the Supernova spirit, but with more generalization and with mix of other languages
23 spirits.
24
25 .. index:: 
26         pair: Natural Language Programming; Example
27
28 Example
29 =======
30
31 The next example presents how to create a class that define two instructions 
32
33 The first instruction is : I want window
34
35 The second instruction is : Window title = <expr>
36
37 Also keywords that can be ignored like the 'the' keyword
38
39 .. code-block:: ring
40
41         New App 
42         {
43                 I want window   
44                 The window title = "hello world"
45         }
46
47         Class App
48
49                 # Attributes for the instruction I want window
50                         i want window 
51                         nIwantwindow = 0
52                 # Attributes for the instruction Window title
53                 # Here we don't define the window attribute again
54                         title 
55                         nWindowTitle = 0
56                 # Keywords to ignore, just give them any value
57                         the=0   
58
59                 func geti
60                         if nIwantwindow = 0
61                                 nIwantwindow++
62                         ok
63
64                 func getwant
65                         if nIwantwindow = 1
66                                 nIwantwindow++
67                         ok
68
69                 func getwindow
70                         if nIwantwindow = 2
71                                 nIwantwindow= 0
72                                 see "Instruction : I want window" + nl
73                         ok
74                         if nWindowTitle = 0
75                                 nWindowTitle++
76                         ok
77         
78                 func settitle cValue
79                         if nWindowTitle = 1
80                                 nWindowTitle=0
81                                 see "Instruction : Window Title = " + cValue + nl
82                         ok
83                 
84
85 Output:
86
87 .. code-block:: ring
88
89         Instruction : I want window
90         Instruction : Window Title = hello world
91
92
93 .. index:: 
94         pair: Natural Language Programming; Change the Ring Keyword 'And'
95
96 Change the Ring Keyword 'And'
97 =============================
98
99 What if we want to connect between the two instructions using 'and'
100
101 We have a problem because in Ring 'and' is a keyword
102
103 We can change that using the ChangeRingKeyword command.
104
105 Syntax:
106
107 .. code-block:: ring
108         
109         ChangeRingKeyword  <oldkeyword>  <newkeyword>
110
111 .. note:: remember to restore the keyword again 
112
113 .. tip:: The ChangeRingKeyword command is executed in the scanner stage by the compiler (before parsing).
114         
115 Example:
116
117 .. code-block:: ring
118
119         ChangeRingKeyword       and  _and
120
121         New App
122         {
123                         I want window and the window title = "hello world"
124         }
125
126         Class App
127
128                         # Attributes for the instruction I want window
129                                         i want window
130                                         nIwantwindow = 0
131                         # Attributes for the instruction Window title
132                         # Here we don't define the window attribute again
133                                         title
134                                         nWindowTitle = 0
135                         # Keywords to ignore, just give them any value
136                                         the=0  and=0
137
138         ChangeRingKeyword       _and  and
139
140                         func geti
141                                 if nIwantwindow = 0
142                                         nIwantwindow++
143                                 ok
144
145                         func getwant
146                                 if nIwantwindow = 1
147                                         nIwantwindow++
148                                 ok
149
150                         func getwindow
151                                 if nIwantwindow = 2
152                                         nIwantwindow= 0
153                                         see "Instruction : I want window" + nl
154                                 ok
155                                 if nWindowTitle = 0
156                                         nWindowTitle++
157                                 ok
158
159                         func settitle cValue
160                                 if nWindowTitle = 1
161                                         nWindowTitle=0
162                                         see "Instruction : Window Title = " + cValue + nl
163                                 ok
164
165                         func getand
166                                 see "Using : and" + nl
167
168
169
170 Output:
171
172 .. code-block:: ring
173
174         Instruction : I want window
175         Using : and
176         Instruction : Window Title = hello world
177                         
178
179 .. index:: 
180         pair: Natural Language Programming; Change the Ring Operator '+'
181
182 Change the Ring Operator '+'
183 ============================
184
185 What if we want to define a new behavior for any operator like the "+" operator.
186
187 We can do this change using the ChangeRingOperator command to hide operator (change it's name)
188
189 Then we can use the operator as identifier that we can handle it's behaviour
190
191 Syntax:
192
193 .. code-block:: ring
194         
195         ChangeRingOperator  <oldoperator>  <newoperator>
196
197 .. note:: remember to restore the operator again 
198
199 .. tip:: The ChangeRingOperator command is executed in the scanner stage by the compiler (before parsing).
200         
201 Example:
202
203 .. code-block:: ring
204
205         ChangeRingOperator + _+
206
207         New App {
208                 +
209         }
210
211         Class App
212                 + 
213                 func get+
214                         see "Plus operator" 
215
216         ChangeRingOperator _+ +
217
218 Output:
219
220 .. code-block:: ring
221
222         Plus operator
223
224 .. index:: 
225         pair: Natural Language Programming; Change the '=' operator to 'is'
226
227 Change the '=' operator to 'is'
228 ===============================
229         
230 Example:
231
232 .. code-block:: ring
233
234         ChangeRingKeyword       and  _and
235         ChangeRingOperator      =    is
236
237         New App
238         {
239                         I want window and the window title is "hello world"
240         }
241
242         ChangeRingOperator      is    =
243
244         Class App
245
246                         # Attributes for the instruction I want window
247                                         i want window
248                                         nIwantwindow = 0
249                         # Attributes for the instruction Window title
250                         # Here we don't define the window attribute again
251                                         title
252                                         nWindowTitle = 0
253                         # Keywords to ignore, just give them any value
254                                         the=0  and=0
255
256         ChangeRingKeyword       _and  and
257
258                         func geti
259                                 if nIwantwindow = 0
260                                         nIwantwindow++
261                                 ok
262
263                         func getwant
264                                 if nIwantwindow = 1
265                                         nIwantwindow++
266                                 ok
267
268                         func getwindow
269                                 if nIwantwindow = 2
270                                         nIwantwindow= 0
271                                         see "Instruction : I want window" + nl
272                                 ok
273                                 if nWindowTitle = 0
274                                         nWindowTitle++
275                                 ok
276
277                         func settitle cValue
278                                 if nWindowTitle = 1
279                                         nWindowTitle=0
280                                         see "Instruction : Window Title = " + cValue + nl
281                                 ok
282
283
284 .. index:: 
285         pair: Natural Language Programming; Using Eval() with our Natural Code
286
287 Using Eval() with our Natural Code
288 ==================================
289
290 Example:
291
292 .. code-block:: ring
293
294         func Main
295
296           cProgram = ' I want window and the window title is "hello world" '
297
298           MyLanguage(cProgram)
299
300         Func MyLanguage cCode
301
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).
304           
305           cCode = '
306                 ChangeRingKeyword  and  _and
307                 ChangeRingOperator  =    is
308           ' + cCode
309
310           New App
311           {
312                   eval(cCode)
313           }
314
315
316           Class App
317
318                   # Attributes for the instruction I want window
319                           i want window
320                           nIwantwindow = 0
321                   # Attributes for the instruction Window title
322                   # Here we don't define the window attribute again
323                           title
324                           nWindowTitle = 0
325                   # Keywords to ignore, just give them any value
326                           the=0  
327
328                   ChangeRingKeyword  and  _and
329                           and=0
330                   ChangeRingKeyword  _and  and
331
332                   func geti
333                         if nIwantwindow = 0
334                           nIwantwindow++
335                         ok
336
337                   func getwant
338                         if nIwantwindow = 1
339                           nIwantwindow++
340                         ok
341
342                   func getwindow
343                         if nIwantwindow = 2
344                           nIwantwindow= 0
345                           see "Instruction : I want window" + nl
346                         ok
347                         if nWindowTitle = 0
348                           nWindowTitle++
349                         ok
350
351                   func settitle cValue
352                         if nWindowTitle = 1
353                           nWindowTitle=0
354                           see "Instruction : Window Title = " + cValue + nl
355                         ok
356
357                 
358 .. index:: 
359         pair: Natural Language Programming; BraceStart and BraceEnd Methods
360
361 BraceStart and BraceEnd Methods
362 ===============================
363
364 We can write code that will be executed before/after using { }
365
366 Example:
367
368 .. code-block:: ring
369
370         o1 = new test {
371                 see "Hello" + nl
372         }
373
374         o1 {}
375
376         class test
377
378                 func bracestart
379                         see "start" + nl
380
381                 func braceend
382                         see "end" + nl
383
384 Output:
385
386 .. code-block:: ring
387
388         start
389         Hello
390         end
391         start
392         end
393
394
395 .. index:: 
396         pair: Natural Language Programming; BraceExprEval Method
397
398 BraceExprEval Method
399 ====================
400
401 The next example demonstrates how to use the "BraceExprEval" method to get expressions in
402 Natural code.
403
404 Example:
405
406 .. code-block:: ring
407
408         new natural {
409                 create 5
410         }
411
412         class natural
413                 create=0
414                 lkeyword = false
415                 func braceexpreval r            
416                         if lkeyword lkeyword=false return ok
417                         see "expr eval" + nl
418                         see "type: " + type(r) see nl
419                         see "value : " see r see nl
420                 func getcreate
421                         lkeyword = true
422                         see "create" + nl
423
424 Output:
425
426 .. code-block:: ring
427
428         create
429         expr eval
430         type: NUMBER
431         value : 5
432
433 .. index:: 
434         pair: Natural Language Programming; Real Natual Code
435
436 Real Natural Code
437 =================
438
439 The next example is a more advanced example 
440
441 .. code-block:: ring
442
443         # Natural Code
444         new program {
445                 Accept 2 numbers then print the sum
446         }
447
448         # Natural Code Implementation
449         class program
450                 # Keywords
451                         Accept=0 numbers=0 then=0 print=0 the=0 sum=0
452
453                 # Execution
454                 func braceexpreval x
455                         value = x
456                 func getnumbers
457                         for x=1 to value
458                                 see "Enter Number ("+x+") :" give nNumber
459                                 aNumbers + nNumber
460                         next
461                 func getsum
462                         nSUm = 0
463                         for x in aNumbers nSum+= x next
464                         see "The Sum : " + nSum
465                 private
466                         value=0 aNumbers=[]
467                 
468 Output: 
469
470 .. code-block:: ring
471
472         Enter Number (1) :3
473         Enter Number (2) :4
474         The Sum : 7
475
476 .. index:: 
477         pair: Natural Language Programming; BraceError() Method
478
479 BraceError() Method
480 ===================
481
482 The next examples demonstrates how to use the "BraceError" method to handle errors when accessing
483 the object using braces {}.
484
485 Example:
486
487 .. code-block:: ring
488
489         func main
490                 o1 = new point {
491                         x=10 y=20 z=30
492                         TEST
493                         SEE test
494                 }
495
496         class point x y z
497                 func braceerror
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")
503                                 test = 10
504                         ok
505                         see "done" + nl
506                         return 
507                         
508 Output:
509
510 .. code-block:: ring
511
512         Handle Error!
513         Message :Error (R24) : Using uninitialized variable : test
514         add attribute
515         done
516         10
517         
518 Example:
519
520 .. code-block:: ring
521
522         new point {
523                 x=10 y=20 z=30
524                 test()
525                 see "mmm..." + NL
526         }
527
528         class point x y z
529                 func braceerror
530                         see "Handle Error!" + nl
531                         see "Message :" + cCatchError + nl
532                         see self
533                         see "Done" + NL
534                         
535 Output:
536
537 .. code-block:: ring
538
539         Handle Error!
540         Message :Error (R3) : Calling Function without definition !: test
541         x: 10.000000
542         y: 20.000000
543         z: 30.000000
544         Done
545         mmm...
546         
547 .. index:: 
548         pair: Natural Language Programming; Clean Natural Code
549
550 Clean Natural Code
551 ==================
552
553 Instead of typing the literal as "literal" we can accept the words directly.
554
555 Example:
556
557 The next example accept hello world instead of "hello world"
558
559 But this example uses braceend() to check the end of the instruction
560
561 This means that this class process only one natural statement that end with literal.
562
563 .. code-block:: ring
564
565         ChangeRingKeyword       and  _and
566
567         New App
568         {
569                         I want window and the window title is hello world
570         }
571
572         Class App
573
574                         # Attributes for the instruction I want window
575                                         i want window
576                                         nIwantwindow = 0
577                         # Attributes for the instruction Window title
578                         # Here we don't define the window attribute again
579                                         title is 
580                                         nWindowTitle = 0
581                         # Keywords to ignore, just give them any value
582                                         the=0  and=0
583                         # Data
584                                         literal = ""
585
586         ChangeRingKeyword       _and  and
587
588                         func geti
589                                 if nIwantwindow = 0
590                                         nIwantwindow++
591                                 ok
592
593                         func getwant
594                                 if nIwantwindow = 1
595                                         nIwantwindow++
596                                 ok
597
598                         func getwindow
599                                 if nIwantwindow = 2
600                                         nIwantwindow= 0
601                                         see "Instruction : I want window" + nl
602                                 ok
603                                 if nWindowTitle = 0
604                                         nWindowTitle++
605                                 ok
606
607                         func gettitle
608                                 if nWindowTitle = 1
609                                         nWindowTitle=2
610                                 ok
611                         
612                         func getis
613                                 if nWindowTitle = 2
614                                         nWindowTitle=3
615                                 ok                      
616
617                         func braceend
618                                 if nWindowTitle = 3
619                                         see "Instruction : Window Title = " + literal + nl
620                                         nWindowTitle = 0
621                                 ok
622
623                         func braceerror
624                                 c= substr(cCatchError,":")
625                                 while c > 0 
626                                         c= substr(cCatchError,":") 
627                                         cCatchError=substr(cCatchError,c+1)  
628                                 end
629                                 literal += substr(cCatchError,1)