tagged release 0.7.1
[parrot.git] / t / compilers / imcc / syn / const.t
blobb221a8f6229e57de6d76c18e71640d7b19cf652b
1 #!perl
2 # Copyright (C) 2001-2008, The Perl Foundation.
3 # $Id$
5 use strict;
6 use warnings;
7 use lib qw( . lib ../lib ../../lib );
8 use vars qw($TODO);
10 use Test::More;
11 use Parrot::Config;
12 use Parrot::Test tests => 34;
14 pir_output_is( <<'CODE', <<'OUT', "globalconst 1" );
16 .sub 'main' :main
17     .globalconst int N = 5
18     _main()
19 .end
21 .sub '_sub1'
22     print N
23     print "\n"
24 .end
26 .sub '_main'
27     _sub1()
28 .end
29 CODE
31 OUT
33 pir_output_is( <<'CODE', <<'OUT', "globalconst 2" );
34 .sub 'test' :main
35     .globalconst int N = 5
36     _main()
37 .end
39 .sub '_sub1'
40     .local int x
41     x = 10 + N
42     print x
43     print "\n"
44 .end
46 .sub '_main'
47      _sub1()
48 .end
49 CODE
51 OUT
53 pir_output_is( <<'CODE', <<'OUT', "globalconst 3" );
55 .sub 'call_sub1'
56     'sub1'()
57 .end
59 .sub 'test' :main
60     .globalconst int N = 5
61     'call_sub1'()
62 .end
64 .sub 'sub1'
65     print N
66     print "\n"
67 .end
69 CODE
71 OUT
73 pir_output_is( <<'CODE', <<'OUT', "array/hash consts" );
74 .sub 'main' :main
75    .local pmc ar
76    .local pmc ha
77    .local string key1
78    .const string key2 = "key2"
79    .local int idx1
80    .const int idx2 = 2
81    ar = new 'Array'
82    ar = 3
83    ha = new 'Hash'
84    key1 = "key1"
85    idx1 = 1
86    ha[key1] = idx1
87    ha[key2] = idx2
88    $I0 = ha[key1]
89    $I1 = ha[key2]
90    ar[idx1] = $I0
91    ar[idx2] = $I1
92    $I2 = ar[idx1]
93    $I3 = ar[idx2]
94    print $I2
95    print $I3
96    print "\n"
97 .end
98 CODE
102 pir_output_is( <<'CODE', <<'OUT', "escaped" );
103 .sub 'main' :main
104    $S0 = "\""
105    print $S0
106    print "\\"
107    $S0 = "\"\\\"\n"
108    print $S0
109 .end
110 CODE
111 "\"\"
114 # fix editor highlighting "
116 pir_output_is( <<'CODE', <<'OUT', "PMC const 1 - Sub" );
117 .sub 'main' :main
118     .const .Sub $P0 = "foo"
119     print "ok 1\n"
120     $P0()
121     print "ok 3\n"
122 .end
123 .sub foo
124     print "ok 2\n"
125 .end
126 CODE
127 ok 1
128 ok 2
129 ok 3
132 pir_output_is( <<'CODE', <<'OUT', "PMC const 2 - Sub ident" );
133 .sub 'main' :main
134     .const .Sub func = "foo"
135     print "ok 1\n"
136     func()
137     print "ok 3\n"
138 .end
139 .sub foo
140     print "ok 2\n"
141 .end
142 CODE
143 ok 1
144 ok 2
145 ok 3
148 pasm_output_is( <<'CODE', <<'OUT', "const I/N mismatch" );
149     set I0, 2.0
150     print I0
151     print "\n"
152     set N0, 2
153     print N0
154     print "\nok\n"
155     end
156 CODE
158 2.000000
162 pir_output_is( <<'CODE', <<'OUT', "const I/N mismatch 2" );
163 .sub 'main' :main
164     .const int i = 2.0
165     print i
166     print "\n"
167     .const num n = 2
168     print n
169     print "\nok\n"
170     .const string s = ascii:"ok 2\n"
171     print s
172 .end
173 CODE
175 2.000000
177 ok 2
180 pir_output_is( <<'CODE', <<'OUT', 'PIR heredocs: accepts double quoted terminator' );
181 .sub 'main' :main
182     $S0 = <<"quotage"
183 I want an elephant
184 Oh, I want an elephat!
185 Oh, woo, elephants, yeah :-O
186 quotage
188     print $S0
189 .end
190 CODE
191 I want an elephant
192 Oh, I want an elephat!
193 Oh, woo, elephants, yeah :-O
196 pir_output_is( <<'CODE', <<'OUT', 'PIR heredocs: accepts inline with concat' );
197 .sub 'main' :main
198     $S0 = ""
199     $I0 = 0
200 LOOP:
201     $S0 = concat <<"end"
202 ending
204     inc $I0
205     if $I0 < 5 goto LOOP
206     print $S0
207 .end
208 CODE
209 ending
210 ending
211 ending
212 ending
213 ending
216 pir_output_is( <<'CODE', <<'OUT', "PIR heredoc: accepts terminator with any word chars" );
217 .sub 'main' :main
218     $S0 = <<"AnY_w0Rd_ch4rS"
219 so much depends
220 upon
222 a red wheel
223 barrow
225 glazed with rain
226 water
228 beside the white
229 chickens
230 AnY_w0Rd_ch4rS
232     print $S0
233 .end
234 CODE
235 so much depends
236 upon
238 a red wheel
239 barrow
241 glazed with rain
242 water
244 beside the white
245 chickens
248 pir_output_is( <<'CODE', <<'OUT', 'PIR heredoc: single quoted terminator' );
249 .sub 'main' :main
250     $S0 = <<'Jabberwocky'
251 `Twas brillig, and the slithy toves
252   Did gyre and gimble in the wabe;
253 All mimsy were the borogoves,
254   And the mome raths outgrabe.
255 Jabberwocky
256     print $S0
257 .end
258 CODE
259 `Twas brillig, and the slithy toves
260   Did gyre and gimble in the wabe;
261 All mimsy were the borogoves,
262   And the mome raths outgrabe.
265 pir_output_is( <<'CODE', <<'OUT', 'PIR heredoc: single quoted - backslash' );
266 .sub 'main' :main
267     $S0 = <<'SQ'
268 abc\tdef
270     print $S0
271 .end
272 CODE
273 abc\tdef
276 pir_error_output_like( <<'CODE', <<'OUT', 'PIR heredoc: rejects unquoted terminator' );
277 .sub 'main' :main
278     $S0 = <<Jabberwocky
279 "Beware the Jabberwock, my son!
280   The jaws that bite, the claws that catch!
281 Beware the Jubjub bird, and shun
282   The frumious Bandersnatch!"
283 Jabberwocky
284     print $S0
285 .end
286 CODE
287 /^error:imcc:syntax error, unexpected SHIFT_LEFT.*/
290 pir_error_output_like( <<'CODE', <<'OUT', "PIR heredoc: rejects inline heredoc" );
291 .sub 'main' :main
292     $S0 .= <<Jabberwocky
293 He took his vorpal sword in hand:
294   Long time the manxome foe he sought --
295 So rested he by the Tumtum tree,
296   And stood awhile in thought.
297 Jabberwocky
299     print $S0
300 .end
301 CODE
302 /^error:imcc:syntax error, unexpected SHIFT_LEFT.*/
305 pir_error_output_like( <<'CODE', <<'OUT', "PIR heredoc: rejects null terminator" );
306 .sub 'main' :main
307     $S0 = <<
308 And, as in uffish thought he stood,
309   The Jabberwock, with eyes of flame,
310 Came whiffling through the tulgey wood,
311   And burbled as it came!
313     print $S0
314 .end
315 CODE
316 /^error:imcc:syntax error, unexpected SHIFT_LEFT.*/
319 pir_error_output_like( <<'CODE', <<'OUT', "PIR heredoc: rejects terminator with spaces" );
320 .sub 'main' :main
321     $S0 = << "terminator with spaces"
322 One, two! One, two! And through and through
323   The vorpal blade went snicker-snack!
324 He left it dead, and with its head
325   He went galumphing back.
326 terminator with spaces
328     print $S0
329 .end
330 CODE
331 /^error:imcc:syntax error, unexpected SHIFT_LEFT.*/
334 pir_output_is( <<'CODE', <<'OUT', "PIR heredoc: accepts terminator with non-word chars" );
335 .sub 'main' :main
336     $S0 = <<"#non$word-chars."
337 'And, has thou slain the Jabberwock?
338   Come to my arms, my beamish boy!
339 O frabjous day! Callooh! Callay!'
340   He chortled in his joy.
341 #non$word-chars.
343     print $S0
344 .end
345 CODE
346 'And, has thou slain the Jabberwock?
347   Come to my arms, my beamish boy!
348 O frabjous day! Callooh! Callay!'
349   He chortled in his joy.
352 pir_output_is( <<'CODE', <<'OUT', "PIR heredoc: accepts terminator with unprintable chars" );
353 .sub 'main' :main
354     $S0 = <<"\0\1\2\3"
355 `Twas brillig, and the slithy toves
356   Did gyre and gimble in the wabe;
357 All mimsy were the borogoves,
358   And the mome raths outgrabe.
359 \0\1\2\3
360     print $S0
361 .end
362 CODE
363 `Twas brillig, and the slithy toves
364   Did gyre and gimble in the wabe;
365 All mimsy were the borogoves,
366   And the mome raths outgrabe.
369 pir_error_output_like( <<'CODE', <<'OUT', "PIR heredoc: rejects interpolated terminator" );
370 .sub 'main' :main
371     $S1 = 'e_e_cummings'
372     $S0 = <<$S1
384 iness
385 e_e_cummings
386     print $S0
387 .end
388 CODE
389 /^error:imcc:syntax error, unexpected SHIFT_LEFT.*/
392 pir_output_is( <<'CODE', <<'OUT', "PIR heredoc: rejects variable interpolation" );
393 .sub 'main' :main
394     $S0 = 'parrot'
395     print <<"*<:-O"
396 Happy Birthday to you,
397 Happy Birthday to you.
398 Happy Birthday dear $S0,
399 Happy Birthday to you!
400 *<:-O
401 .end
402 CODE
403 Happy Birthday to you,
404 Happy Birthday to you.
405 Happy Birthday dear $S0,
406 Happy Birthday to you!
409 pir_output_is( <<'CODE', <<'OUT', "PIR heredoc: allow empty lines" );
410 .sub 'main' :main
411     $S0 = 'parrot'
412     print <<"END_HERE"
414 The line above is empty.
415 END_HERE
416 .end
417 CODE
419 The line above is empty.
422 pir_error_output_like( <<'CODE', <<'OUT', "PIR heredoc: line numbers" );
423 .sub main :main
424     .local string s
425     .local pmc nil
426     bounds 1   # force line nums
427     s = <<"EOT"
428 line 1
429 line 2
430 line 3
431 line 4
433     print nil  # force err
434 .end
435 CODE
436 /^Null PMC.*:11\)$/s
439 pir_output_is( <<'CODE', <<'OUT', "PIR heredoc: double quoted strings" );
440 .sub main :main
441   $S0 = <<"HEREDOC"
442 print "hello"
443 HEREDOC
444   print $S0
445   end
446 .end
447 CODE
448 print "hello"
451 pir_output_is( <<'CODE', <<'OUT', "PIR heredoc: double quotes - two in a row" );
452 .sub main :main
453     print <<"QUOTES"
455 QUOTES
456 .end
457 CODE
461 pir_output_is( <<'CODE', <<'OUT', "PIR heredoc: double quotes - with anything between" );
462 .sub main :main
463     print <<"QUOTES"
464 "anything"
465 QUOTES
466 .end
467 CODE
468 "anything"
471 pir_output_is(
472     <<'CODE', <<'OUT', "PIR heredoc: double quotes - two in a row prefaced by anything" );
473 .sub main :main
474     print <<"QUOTES"
475 anything""
476 QUOTES
477 .end
478 CODE
479 anything""
482 pir_output_is( <<'CODE', <<'OUT', "PIR heredoc: double quotes - escaped with anything between" );
483 .sub main :main
484     print <<"QUOTES"
485 \"anything\"
486 QUOTES
487 .end
488 CODE
489 "anything"
492 pir_output_is(
493     <<'CODE', <<'OUT', "PIR heredoc: escaped characters, escaped quotes, starting quotes" );
494 .sub test :main
495         .local string test
497         test = <<"TEST"
498 { \{ \\{
499 w \w \\w
500 " \" \\"
501 { \{ \\{
502 w \w \\w
503 " \" \\"
504 { \{ \\{
505 w \w \\w
506 TEST
507         print test
508 .end
509 CODE
510 { { \{
511 w w \w
512 " " \"
513 { { \{
514 w w \w
515 " " \"
516 { { \{
517 w w \w
520 pir_output_is( <<'CODE', <<'OUT', "heredoc not eol 1" );
521 .sub main :main
522     .local string code
523     code = ''
524     emit(code, <<"HERE", 10)
525 line 1
526 line %d
527 line 2
528 HERE
529 .end
530 .sub emit
531     .param string code
532     .param string more
533     .param pmc args  :slurpy
534     $S0 = sprintf more, args
535     code .= $S0
536     print code
537 .end
538 CODE
539 line 1
540 line 10
541 line 2
544 pir_error_output_like( <<'CODE', <<'OUT', "heredoc not eol 2 - nested" );
545 .sub main :main
546     cat(<<"H1", <<"H2")
547 line 1
548 line 2
550 line 3
551 line 4
553 .end
554 .sub cat
555     .param string p1
556     .param string p2
557     p1 .= p2
558     print p1
559 .end
560 CODE
561 /nested heredoc not supported/
564 pir_output_is( <<'CODE', <<'OUT', ".const in mixed opcodes" );
565 .sub main :main
566     .const int I = 5
567     .local num f
568     f = 2.0
569     f *= I
570     print f
571     print "\n"
572 .end
573 CODE
574 10.000000
577 pir_output_is( <<'CODE', <<'OUT', "RT # 34991" );
578 .const int c = 12
579 .sub test
580     .local num a
581     a = 96
582     # Uncomment this line, and the c symbol is 'forgotten'
583     a += c
584     print a
585     print "\n"
586     print c
587     print "\n"
588     end
589 .end
590 CODE
591 108.000000
595 # Local Variables:
596 #   mode: cperl
597 #   cperl-indent-level: 4
598 #   fill-column: 100
599 # End:
600 # vim: expandtab shiftwidth=4: