Debian Codegraph @

trip/trip.rexx

raw
1 /*
2 * The Regina Rexx Interpreter
3 * Copyright (C) 1992 Anders Christensen <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 /*--------------------------------------------------------------------*/
21 /* The purpose of this program is to try some strange constructions */
22 /* in REXX, and to fool the interpreter to make a mistake */
23 /* */
24 /* Created ... ... : April 1989 */
25 /* Last modified . : 14th March 2003 */
26 /* */
27 /* Current version : 2.00 */
28 /* */
29 /* Written by Anders Christensen, Norwegian Institute of Technology */
30 /* E-mail addr: Bitnet : [email protected] */
31 /* Internet : [email protected] */
32 /* Uninett : [email protected] */
33 /*--------------------------------------------------------------------*/
34
35 written = 0
36
37 /*--------------------------------------------------------------------*/
38 /* First check comments */
39 /*--------------------------------------------------------------------*/
40 call notify 'comments'
41
42 /*----- Comments can take any character ------------------------------*/
43 /* !"#$%&' *(*+,-./ 01234567 89:;<=>?
44 * @ABCDEFG HIJKLMNO PQRSTUVW XYZ[\]^_
45 * `abcdefg hijklomo pqrstuvw xyz{|}~
46 *
47 * bel  bs  cr
48 esc 
49 * tab ff
50 */
51
52
53 /*----- Let's see if we can nest comments ----------------------------*/
54 test = /* /* */ ' */ ' ' /* ' /* */ */
55 if test == ' */ /* ' then
56 call complain "Comments can't be nested."
57 else if test \== ' ' then
58 call complain "Strange results from nesting-of-comments test"
59
60
61 /*----- Check if comments act as token separators --------------------*/
62 abc = 'First '
63 def = 'Second'
64 test = abc/* */def
65 if test \== 'First Second' then
66 call complain 'Comments are not a token separator.'
67
68
69 /*----- Check if comments get confused with whitespace ---------------*/
70 test1 /* */ = /* */ 'foo' /* */ 'bar'
71 test2/* */=/* */'foo'/* */'bar'
72
73 if (test1 \== 'foo bar') * (test2 \== 'foobar') then
74 call complain 'Comments get mixed with whitespace'
75
76
77 /*----- Comments can follow continuation char ------------------------*/
78 test = 'foo', /*comment*/
79 'bar'
80 if (test\=='foo bar') then
81 call complain 'Comment after continutation mark confuses interpreter'
82
83 /*----- Comments can come in front of left paran in functions --------*/
84 if words/* */('foo bar') words/* *//* */('foo bar') \== '2 2' then
85 call complain 'Comments not allows in front of left paran in function calls'
86
87 if 'WORDS'/* */('foo bar') 'WORDS'/* *//* */('foo bar') \== '2 2' then
88 call complain 'Comments not allows in front of left paran in function calls'
89
90
91
92 /*--------------------------------------------------------------------*/
93 /* Check the operation of strings */
94 /*--------------------------------------------------------------------*/
95 call notify 'strings'
96
97 /*----- Single and double quotes are eqivalent -----------------------*/
98 if " foo bar " \== ' foo bar ' then
99 call complain 'Single and double quotes are different'
100
101
102 /*----- Quotes might be quoted by doubled ----------------------------*/
103 if " "" ' """" '' ""'""' " \== ' " '' "" '''' "''"'' ' then
104 call complain 'Problems with quoting quotes in strings'
105
106
107 /*----- Let's verify that all characters are allowed in strings ------*/
108 /*
109 string = xrange()
110 interpret 'strin2 = "' || translate(xrange(,'"'),' ','0a00'x)||xrange('"', 'fe'x) || '"'
111 /*
112 * Unfortunately, there are several things we need to eliminate here.
113 * The most evident thing is newlines which are not allowed in literal
114 * strings. Regina is based on lex, which don't like 'ff'x or '00'x in
115 * the input string. Thus, in 'real life', we would set the limits to
116 * zero and ff, and not translate away newline.
117 */
118 do i=1 to 254
119 if substr(string,i+1,1) \== d2c(i) then
120 call complain 'Character' i 'is invalid in result strings',
121 'char='c2x(d2c(i)) 'string='c2x(substr(string,i+1,1))
122 if substr(strin2,i+1,1) \== d2c(i) & i\=10 then
123 call complain 'Character' i 'is invalid in source code',
124 'char='c2x(d2c(i)) 'string='c2x(substr(strin2,i+1,1) )
125 end
126
127 */
128
129
130
131 /*--------------------------------------------------------------------*/
132 /* Check that hex-strings behave properly */
133 /*--------------------------------------------------------------------*/
134 call notify 'hexstrings'
135
136 /*----- Check for simple use -----------------------------------------*/
137 if '52 65 78 78'x \== 'Rexx' then
138 call complain 'Big problems with hex-strings, simple use failed'
139
140
141 /*----- Both upper and lower case 'x' can be used --------------------*/
142 if '1234567890'X \== '1234567890'x then
143 call complain 'Upper and lower case "x" not equivalent'
144
145
146 /*----- Both upper and lower case hex digits may be used -------------*/
147 if 'deadbeef'X \== 'DEADBEEF'X then
148 call complain 'Upper and lower hex-digits not equivalent'
149
150
151 /*----- Spaces at byte boundaries, else error ------------------------*/
152 if 'de adbe ef'X \== 'dead beef'X then
153 call complain 'Does not handle spaces at byte boundaries'
154
155
156 /*----- First byte might be single hex-digit -------------------------*/
157 if 'abc'X \== '0abc'X | 'a bc'X \== '0a bc'X then
158 call complain 'Does not handle an missing initial zero'
159
160
161 /*----- Initial or terminating space not allowed ---------------------*/
162 signal on syntax name syntax_hexstring_001
163 interpret ,
164 "if ' 12'x \== '12'x then",
165 "call complain 'Leading space makes difference'"
166 call complain 'Initial space should not really be allowed'
167
168 syntax_hexstring_001:
169 signal on syntax name syntax_hexstring_002
170 interpret,
171 "if '12 'x \== '12'x then ",
172 "call complain 'Trailing space makes difference'"
173 call complain 'Trailing space should not really be allowed'
174
175 syntax_hexstring_002:
176
177
178 /*----- Spaces must be at byte boundaries ----------------------------*/
179 signal on syntax name syntax_hexstring_003
180 interpret,
181 "if '12 3 456 78'X \== '12 03 0456 78'X then",
182 "call complain 'Byte groups not padded out to byte boundaries'"
183 call complain 'Non-even numbers of hex-digits allowed, not standard'
184 syntax_hexstring_003:
185
186
187 /*----- The "X" must not be part of another token --------------------*/
188 drop X
189 drop XYZZY
190 if 'deedbeef'X||'YZZY' == 'deadbeef'XYZZY then
191 call complain 'Read as hexstring when it should not'
192
193
194
195
196 /*--------------------------------------------------------------------*/
197 /* Check that bin-strings behave properly */
198 /*--------------------------------------------------------------------*/
199 call notify 'binstrings'
200
201 /*----- Check for simple use -----------------------------------------*/
202 if '0101 0010 0110 0101 0111 1000 0111 1000'b \== 'Rexx' then
203 call complain 'Big problems with bin-strings, simple use failed'
204
205
206 /*----- Both upper and lower case 'x' can be used --------------------*/
207 if '1010010111110000'B \== '1010010111110000'b then
208 call complain 'Upper and lower case "b" not equivalent'
209
210
211 /*----- Spaces at nibble boundaries, else error ----------------------*/
212 if '0101 10101111 0000'B \== '01011010 11110000'B then
213 call complain 'Does not handle spaces at nibble boundaries'
214
215
216 /*----- First nibble might be less than four digits ------------------*/
217 if '0 0011'b \== '0000 0011'b | '111 0101'b \== '0111 0101'b then
218 call complain 'Does not handle an missing initial zero'
219
220
221 /*----- If needed, pad with extra nibble first -----------------------*/
222 if '11 0000 1111 0101 1010'B \== '0000 0011 0000 1111 0101 1010'b then
223 call complain 'Does not append extra zero nibble when needed'
224
225
226 /*----- Initial or terminating space not allowed ---------------------*/
227 signal on syntax name syntax_binstring_001
228 interpret ,
229 "if ' 11110000'b \== '11110000'b then",
230 "call complain 'Leading space makes difference'"
231 call complain 'Initial space should not really be allowed'
232
233 syntax_binstring_001:
234 signal on syntax name syntax_binstring_002
235 interpret,
236 "if '11110000 'b \== '11110000'b then ",
237 "call complain 'Trailing space makes difference'"
238 call complain 'Trailing space should not really be allowed'
239
240 syntax_binstring_002:
241
242
243 /*----- Spaces must be at nibble boundaries --------------------------*/
244 signal on syntax name syntax_binstring_003
245 interpret,
246 "if '1111 000 11 0'b \== '11110000 00110000'b then",
247 "call complain 'Nibble groups not padded out to nibble boundaries'"
248 call complain 'Non-even numbers of bin-digits in group allowed, not standard'
249 syntax_binstring_003:
250
251
252 /*----- The "B" must not be part of another token --------------------*/
253 drop B
254 drop BAR
255 if '111'B||'AR' == '111'BAR then
256 call complain 'Read as binstring when it should not'
257
258
259
260
261 /*--------------------------------------------------------------------*/
262 /* Is dataformats ok? */
263 /*--------------------------------------------------------------------*/
264 call notify 'dataformats'
265
266
267
268
269
270
271 /*--------------------------------------------------------------------*/
272 /* Then check if line continuations are OK */
273 /*--------------------------------------------------------------------*/
274 call notify 'continuations'
275
276 /*----- A line break should be interpreted as a space 'operator' -----*/
277 test = 'abc',
278 'def'
279 if test \== 'abc def' then
280 call complain 'Line continuations is not translated to space'
281
282
283 /*----- Does a comment after the comma confuse it? -------------------*/
284 test = 'abc', /* */
285 nop
286 if test \== 'abc NOP' then
287 call complain 'Comments confuse line continuations'
288
289 /*----- Let's see if it takes some heavy line continuation -----------*/
290 if,
291 6,
292 ==,
293 7,
294 then,
295 call complain "Don't take heavy line continuation"
296
297
298
299 /*--------------------------------------------------------------------*/
300 /* Let's test expressions */
301 /*--------------------------------------------------------------------*/
302 call notify 'expr'
303
304 /*-------- First, test all comparison operators ----------------------*/
305 str = \('abc'='abc') \('abc'=='abc') ('abc'\='abc') ('abc'\=='abc'),
306 ('abc'='xyz') ('abc'=='xyz') \('abc'\='xyz') \('abc'\=='xyz'),
307 \('abc'=' abc ') ('abc'==' abc ') ('abc'\=' abc ') \('abc'\==' abc '),
308 ('abc'\='abc') ('abc'\=='abc') \('abc'\='xyz') \('abc'\=='xyz'),
309 ('abc'\=' abc ') \('abc'\==' abc ')
310
311 if (str\='0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0') then
312 call complain 'Comparison operators do not work properly'
313
314
315 /*---- Then test all the logic operators -----------------------------*/
316 str = (1|1) (1|0) (0|1) (0|0)
317 if str\== '1 1 1 0' then
318 call complain 'Logical operator | does not seem to work'
319
320 str = (1&1) (1&0) (0&1) (0&0)
321 if str\=='1 0 0 0' then
322 call complain 'Logical operator & does not seem to work'
323
324 str = (1&&1) (1&&0) (0&&1) (0&&0)
325 if str\=='0 1 1 0' then
326 call complain 'Logical operator && does not seem to work'
327
328
329 /*----- Test logical NOT ---------------------------------------------*/
330 parse upper version ver
331 if pos('REGINA',ver)>0 then do
332 ipret = 'junk = (((1 \= 0) \= 0) ~= 0)'
333 signal on syntax name logical_nots
334 interpret ipret
335 if junk\=1 then
336 call complain "Didn't handle logical nots correctly"
337 end
338
339 signal after_log_not
340
341 logical_nots:
342 call complain "Didn't parse all logical nots correctly"
343
344 after_log_not:
345
346
347 /*----- Expressions are evaluated from left to right -----------------*/
348 theargs = '1 2 3'
349 answer = echoargs() echoargs() echoargs()
350 if (answer\=='1 2 3') then
351 call complain 'Expression not evaluated in correct order'
352 signal afterargs
353
354 echoargs:
355 parse var theargs thearg theargs
356 return thearg
357
358 afterargs:
359
360
361 /*----- Is associativity correct -------------------------------------*/
362 signal on syntax name syntax_oper1
363 if (\2**0) \== 1 then
364 call complain 'Something is really wrong with prefix \ and **'
365 call complain 'Incorrect associativelty between prefix \ and **'
366 syntax_oper1:
367
368 signal on syntax name syntax_oper2
369 if (\2**0) \== 7 then
370 call complain 'Something is really wrong with prefix \ and **'
371 call complain 'Incorrect associativelty between prefix \ and **'
372 syntax_oper2:
373
374 if (-2**2) \== 4 then
375 call complain 'Incorrect assiciativety between prefix - and **'
376
377 /* prefix + does not change the value of anything .... don't test */
378 /* if (+(-2)**2) \== 4 then */
379 /* call complain 'Incorrect assiciativety between prefix + and **' */
380
381
382 if (3//2**2) \== 3 then
383 call complain 'Incorrect assiciativety between // and **'
384
385 if (8%2**2) \== 2 then
386 call complain 'Incorrect assiciativety between % and **'
387
388 if (3*2**2) \== 12 then
389 call complain 'Incorrect assiciativety between * and **'
390
391 if (8/2**2) \== 2 then
392 call complain 'Incorrect assiciativety between / and **'
393
394
395 if (1+2*3) \== 7 then
396 call complain 'Incorrect assiciativety between + and *'
397
398 if (7-2*3) \== 1 then
399 call complain 'Incorrect assiciativety between - and *'
400
401 if (1+6/2) \== 4 then
402 call complain 'Incorrect assiciativety between + and /'
403
404 if (7-6/2) \== 4 then
405 call complain 'Incorrect assiciativety between - and /'
406
407 if (2+7//3) \== 3 then
408 call complain 'Incorrect assiciativety between + and //'
409
410 if (7-7//3) \== 6 then
411 call complain 'Incorrect assiciativety between - and //'
412
413 if (1+7%3) \== 3 then
414 call complain 'Incorrect assiciativety between + and %'
415
416 if (7-7%2) \== 4 then
417 call complain 'Incorrect assiciativety between - and %'
418
419
420
421 /*----- Are expressions evaluated completely -------------------------*/
422 count = 1
423 junk = ( 1 | subproc() )
424 if count \== 2 then
425 call complain 'OR expressions not completely evaluated'
426
427 count = 2
428 junk = ( 0 & subproc() )
429 if count \== 3 then
430 call complain 'AND expressions not completely evaluated'
431
432 signal afterexpr
433
434 subproc:
435 count = count + 1
436 return 1
437
438 afterexpr:
439
440
441 /*----- String compare if not both args are numeric ------------------*/
442 first = '12'
443 second = '2.3'
444
445 if (second > first) then
446 call complain 'simple numeric comparisation did not work'
447
448 first = '12E1.2'
449 if (second < first) then
450 call complain 'numeric comparisation when not both args are numbers'
451
452
453 /*----- Comparisation is case sensitive ------------------------------*/
454 first = 'FooBar'
455 second = 'FOObar'
456 if (first = second) then
457 call complain 'the = operator is case insensitive'
458
459 if (first == second) then
460 call complain 'the == operator is case insensitive'
461
462
463 /*----- Strings can not be assigned a value --------------------------*/
464
465 signal on syntax name strings_1
466 drop name
467 rc = -1000
468 interpret '123 = "hallo"'
469 rcsave = rc
470 call complain 'Did not trap setting of string constant'
471 if (rcsave \== '-1000') then
472 call complain 'suspect that command was performed instead of assignment'
473
474 strings_1:
475 signal on syntax name strings_2
476 drop name
477 rc = -1000
478 interpret '123 == "hallo"'
479 rcsave = rc
480 call complain 'Did not trap assignment with =='
481 if (rcsave \== '-1000') then
482 call complain 'suspect that command was performed instead of =='
483
484 strings_2:
485 parse source os .
486 If os = 'UNIX' | os = 'BEOS' | os = 'QNX' | os = 'SKYOS' Then
487 Do
488 'rm -f ./1'
489 'ln -s /bin/true ./1'
490 End
491
492 rc = -1000
493 123 <= "hallo"
494 if (rc \== '0') then
495 call complain 'Command was not executed, is "." in PATH ?'
496
497 If os = 'UNIX' | os = 'BEOS' | os = 'QNX' | os = 'SKYOS' Then
498 Do
499 'rm -f ./1'
500 End
501
502 /*--------------------------------------------------------------------*/
503 /* Then we test the random generator */
504 /*--------------------------------------------------------------------*/
505 call notify 'random'
506
507 /*----- Test if we can really set the seed ---------------------------*/
508 temp1 = ''
509 temp2 = ''
510 junk = random(0,999,666)
511 do 10
512 temp1 = temp1 random() ; end
513
514 junk = random(0,999,666)
515 do 10
516 temp2 = temp2 random() ; end
517
518 if (random(10,10)\==random(10,10)) then
519 call complain "Random does not respect upper and lower limits"
520
521 if \(temp1==temp2) then
522 call complain 'Can not set seed in random generator'
523
524 /*--------------------------------------------------------------------*/
525 /* Test associative arrays */
526 /*--------------------------------------------------------------------*/
527 call notify 'arrays'
528
529 /*----- Test if they work correctly ----------------------------------*/
530
531 One = 1
532 Two = 2
533 Three = 3
534 Alfa = 'A'
535 Beta = 'B'
536
537 Fish.1 = 'Hi'
538 Fish.Beta = 'Hello'
539 Fish. = 'Bye'
540 Fish.1.2 = '...'
541
542 if (Fish.Alfa \= Fish.Three) then
543 call complain "default mechanism do not work"
544
545 TwoThree = '1.2'
546 if (Fish.TwoThree \= Fish.One.Two) then
547 call complain "assosiative arrays work incorrectly"
548
549 /*-----------------
550 * Lexical analysis
551 *-----------------*/
552 call notify 'lexical'
553 i = 1
554 do while=3
555 i = i + 1
556 if i>5 then
557 leave
558 end
559
560 if (i\=6) then
561 call complain 'didn''t handle WHILE as ctrl variable correctly'
562
563 signal on syntax name lexsynt
564 interpret 'do (while=3); nop; end'
565 call complain "Incorrect handle of WHILE in exprf in DO"
566 lexsynt:
567
568
569
570
571
572 /*--------------------------------------------------------------------*/
573 /* Test labels */
574 /*--------------------------------------------------------------------*/
575 call notify 'labels'
576
577
578 /*----- Always use the first occurrence of a label -------------------*/
579 test = 'before'
580 signal thelabel
581 test = 'after'
582
583 thelabel:
584 if test\=='before' then
585 call complain 'Something is really wrong with labels'
586
587 signal jumpover
588 thelabel:
589 call complain 'didnt find first label, but second'
590 jumpover:
591
592
593 /*----- One statement can have more than one label ------------------*/
594 foobar = 1
595 signal first
596
597 first: second: third: foobar = foobar + 1
598 if (foobar=2) then signal second
599 if (foobar=3) then signal third
600
601 if (foobar\=4) then
602 call complain 'Multiple labels for one statement does not work'
603
604
605 /*----- Reserved words can be used as labels -------------------------*/
606 address: value: arg: call: on: off: name: do: end: to: by: for: forever:
607 while: until: drop: exit: if: then: else: interpret: iterate: leave: nop:
608 numeric: digits: form: scientific: engineering: fuzz: options: parse:
609 upper: linein: pull: source: with: var: version: procedure: expose: push:
610 queue: return: say: select: when: otherwise: signal: trace: nop
611
612
613
614 /*--------------------------------------------------------------------*/
615 /* Numbers */
616 /*--------------------------------------------------------------------*/
617 call notify 'numbers'
618
619 /*----- possible to assign values to numbers ? -----------------------*/
620 signal on syntax name ass_numbers_1
621 istring = '.34 = "hallo"'
622 interpret istring
623 call complain 'Assignment of string to number not trapped'
624
625 ass_numbers_1:
626 if .34 \== '.34' then
627 call complain 'The representation of number changed'
628
629 if .34 + 0 \== '0.34' then
630 call complain 'The value of number changed'
631
632
633 /*----- integer and fraction part zero simultaneously ? --------------*/
634
635 badnumbers = '. 1.23.4 .E1 1.2E2E1 1.2E+-2 12C4'
636 start_do_loop:
637 if badnumbers='' then
638 signal after_the_loop
639
640 parse var badnumbers badnumber badnumbers
641 signal on syntax name numbers_2
642 istring = 'sum = 1. +' badnumber '+ .1'
643 interpret istring
644 call complain 'The parsing did not choke on the number "'badnumber'".'
645
646 numbers_2:
647 signal on syntax name numbers_3
648 istring = 'sum = 1. + "'badnumber'" + .1'
649 interpret istring
650 call complain 'The pluss operator performed pluss on number "'badnumber'".'
651
652 numbers_3:
653 signal start_do_loop
654
655 after_the_loop:
656
657
658 /*----- Does it interpret multiple embedded signs as unary? ----------*/
659 signal on syntax name after_mult_emb
660 istring = 'bar = 1 + foo + 3'
661 foo = '+-2.0'
662 interpret istring
663 call complain 'Does not separate between embedded signs and unary operators'
664
665 after_mult_emb:
666
667
668 /*--------------------------------------------------------------------*/
669 /* Functions */
670 /*--------------------------------------------------------------------*/
671 call notify 'functions'
672
673
674 /*--------------------------------------------------------------------*/
675 call testing
676 if result \== '17' then
677 call complain 'RESULT not correctly set after call'
678 signal aftertesting
679
680 testing:
681 variable = 2 + 3 * 5
682 return variable
683
684 aftertesting:
685
686
687 /*--------------------------------------------------------------------*/
688 call nextlabel
689 signal afternextlabel
690
691 nextlabel:
692
693 if sourceline(sigl) \== 'call nextlabel' then
694 call complain 'SIGL not correctly set on signal statement'
695 return
696
697 afternextlabel:
698
699
700 /*--------------------------------------------------------------------*/
701 /* Test the codes ability to recurse */
702 /*--------------------------------------------------------------------*/
703 call notify 'recursing'
704
705 /*----- First a simple test ------------------------------------------*/
706 signal afterrecurse
707
708 recurse: procedure
709 parse arg parameter
710 if parameter>0 then do
711 /* say parameter */
712 return recurse(parameter-1) parameter
713 end
714 else
715 return 0
716
717 afterrecurse:
718 if recurse(3)\=='0 1 2 3' then
719 call complain "didn't recurse properly"
720
721 parse value recurse(100) with . '99' result .
722 if result \== '100' then
723 call complain "can't do 100 recursions"
724
725
726
727
728 say ''
729
730 exit 0
731
732
733
734 complain:
735 say ' ...'
736 say 'Tripped in line' sigl':' 'ARG'(1)'.'
737 length = charout(,' (')
738 return
739
740
741 tell:
742 str = arg(1)
743 if length+length(str)>70 then do
744 say ''
745 length = charout(,' ' || str || ', ') ; end
746 else
747 length = length + charout(,str || ', ')
748 return
749
750
751 notify:
752 parse arg word .
753 written = written + length(word) + 2
754 if written>75 then do
755 written = length(word)
756 say ' '
757 end
758 call charout , word || ', '
759 return
760
761