[t][TT #1610] Add tests for Parrot_compile_string
[parrot.git] / examples / past / 01-sub.pir
blobc267552043843583534dff20e36a794757272fcf
1 # Copyright (C) 2006-2008, Parrot Foundation.
2 # $Id$
4 =for doc
6 The PAST that is set up in this example
7 roughly represents following Perl 6 code:
9     sub foo
10     {
11         my $a = 4;
12         my $b = $a + 1;
13         say($b);
14     }
16 =cut
18 .namespace []
20 .sub '__onload' :init
21     load_bytecode "dumper.pbc"
22     load_bytecode 'PGE.pbc'
23     load_bytecode 'PGE/Text.pbc'
24     load_bytecode 'PGE/Util.pbc'
25     load_bytecode 'PGE/Dumper.pbc'
26     load_bytecode 'PCT.pbc'
27 .end
29 .sub 'main' :main
30     .param pmc args
32     .local pmc block
33     block = new ['PAST';'Block']
34     block.'init'( 'blocktype' => 'declaration', 'name' => 'foo' )
35     block.'symbol'( '$a', 'scope' => 'lexical' )
36     block.'symbol'( '$b', 'scope' => 'lexical' )
38     .local pmc stmts
39     stmts = new ['PAST';'Stmts']
40     stmts.'init'()
41     stmts.'attr'( 'source', 'my $a = 4; my $b = $a + 1; say( $b );', 1 )
42     block.'push'(stmts)
44     # $a = 4
45     $P0 = new ['PAST';'Val']
46     $P0.'init'( 'value' => '4', 'returns' => 'Integer' )
47     $P0.'attr'( 'source', '4', 1 )
48     $P1 = new ['PAST';'Var']
49     $P1.'init'( 'name' => '$a', 'viviself' => 'Undef', 'isdecl' => 1 )
50     $P1.'attr'( 'source', '$a', 1 )
51     $P2 = new ['PAST';'Op']
52     $P2.'init'( $P1, $P0, 'pasttype' => 'copy', 'name' => 'infix:=', 'lvalue' => 1 )
53     $P2.'attr'( 'source', '=', 1 )
54     stmts.'push'($P2)
56     # $b = $a + 1
57     $P0 = new ['PAST';'Var']
58     $P0.'init'( 'name' => '$a', 'viviself' => 'Undef' )
59     $P1 = new ['PAST';'Val']
60     $P1.'init'( 'value' => '1', 'returns' => 'Integer')
61     $P2 = new ['PAST';'Op']
62     $P2.'init'( $P0, $P1, 'name' => 'infix:+', 'pirop' => 'add')
63     $P3 = new ['PAST';'Var']
64     $P3.'init'( 'name' => '$b', 'viviself' => 'Undef', 'isdecl' => 1 )
65     $P4 = new ['PAST';'Op']
66     $P4.'init'( $P3, $P2, 'name' => 'infix:=', 'pasttype' => 'copy')
67     $P4.'attr'( 'source', '=', 1 )
68     stmts.'push'($P4)
70     # say($b)
71     $P0 = new ['PAST';'Var']
72     $P0.'init'( 'name' => '$b' )
73     $P1 = new ['PAST';'Op']
74     $P1.'init'( $P0, 'name' => 'say', 'pasttype' => 'call' )
75     stmts.'push'($P1)
77     # set up compiler, preliminary stages are removed because we
78     # already have a PAST data structure
79     .local pmc astcompiler
80     astcompiler = new [ 'PCT';'HLLCompiler' ]
81     astcompiler.'removestage'('parse')
82     astcompiler.'removestage'('past')
84 =for development
86     # _dumper( block, 'block' )
88     # compile to PIR and display
89     $S99 = astcompiler.'compile'(block, 'target' => 'pir')
90     print $S99
92 =cut
94     #compile to bytecode and execute
95     $P99 = astcompiler.'compile'(block)
96     $P99()
97 .end
100 .sub 'say'
101     .param pmc args :slurpy
102     if null args goto end
103     .local pmc it
104     it = iter args
105   loop:
106     unless it goto end
107     $P0 = shift it
108     print $P0
109     goto loop
110   end:
111     print "\n"
112     .return ()
113 .end
115 # Local Variables:
116 #   mode: pir
117 #   fill-column: 100
118 # End:
119 # vim: expandtab shiftwidth=4 ft=pir: