tagged release 0.7.1
[parrot.git] / examples / past / four_plus_one.pir
blob9e827e912c0f4ef768d6a20cb5e83ffc12236dec
1 # $Id$
3 # Set up a PAST data structure that represents a sub and run it.
5 # code for (assuming package scoped variables)
6 # { say( 4 + 1 ); }
8 # a dump of the PAST looks like this:
10 =for example
12 "stmts" => PMC 'PAST::Stmts'  {
13     <source> => "4 + 1\n"
14     <pos> => 0
15     [0] => PMC 'PAST::Op'  {
16         <name> => "say"
17         <pasttype> => "call"
18         [0] => PMC 'PAST::Op'  {
19             <pasttype> => "bind"
20             [0] => PMC 'PAST::Var'  {
21                 <name> => "last"
22                 <scope> => "package"
23                 <lvalue> => 1
24             }
25             [1] => PMC 'PAST::Op'  {
26                 <name> => "infix:+"
27                 <pasttype> => undef
28                 <pirop> => "n_add"
29                 <lvalue> => undef
30                 <source> => "+"
31                 <pos> => 2
32                 [0] => PMC 'PAST::Val'  {
33                     <value> => "4"
34                     <returns> => "Integer"
35                     <source> => "4"
36                     <pos> => 0
37                 }
38                 [1] => PMC 'PAST::Val'  {
39                     <value> => "1"
40                     <returns> => "Integer"
41                     <source> => "1"
42                     <pos> => 4
43                 }
44             }
45         }
46     }
49 =cut
51 .include "library/dumper.pir"
53 .namespace []
55 .sub '__onload' :init
56     load_bytecode 'PGE.pbc'
57     load_bytecode 'PGE/Text.pbc'
58     load_bytecode 'PGE/Util.pbc'
59     load_bytecode 'PGE/Dumper.pbc'
60     load_bytecode 'PCT.pbc'
61 .end
63 .sub main :main
65     .local pmc val_4
66     val_4 = new 'PAST::Val'
67     val_4.init( 'value' => '4', 'returns' => 'Integer' )
69     .local pmc val_1
70     val_1 = new 'PAST::Val'
71     val_1.init( 'value' => '1', 'returns' => 'Integer' )
73     .local pmc op_add
74     op_add = new 'PAST::Op'
75     op_add.init( val_4, val_1, 'name' => 'infix:+', 'pirop' => 'n_add' )
77     .local pmc var_last
78     var_last = new 'PAST::Var'
79     var_last.init( 'name' => 'last', 'scope' => 'package', 'lvalue' => 1 )
81     .local pmc op_bind
82     op_bind = new 'PAST::Op'
83     op_bind.init( var_last, op_add, 'pasttype' => 'bind' )
85     .local pmc op_say
86     op_say = new 'PAST::Op'
87     op_say.init( op_bind, 'name' => 'say', 'pasttype' => 'call' )
89     .local pmc stmts
90     stmts = new 'PAST::Stmts'
91     stmts.'init'( op_say, 'name'=>'stmts' )
93     # compile to PIR and display
94     .local pmc astcompiler
95     astcompiler = new [ 'PCT::HLLCompiler' ]
96     astcompiler.'removestage'('parse')
97     astcompiler.'removestage'('past')
99     astcompiler.'eval'(stmts)
101    # _dumper( stmts, 'stmts' )
102    # _dumper( astcompiler, 'astcompiler' )
104    # .local pmc ost
105    # ost = astcompiler.'post'(stmts)
106    # _dumper( ost, 'ost' )
108    # .local pmc pir
109    # pir = astcompiler.'pir'(ost)
110    # _dumper( pir, 'pir' )
111    # say pir
113 .end
116 .sub 'say'
117     .param pmc args :slurpy
118     if null args goto end
119     .local pmc iter
120     iter = new 'Iterator', args
121   loop:
122     unless iter goto end
123     $P0 = shift iter
124     print $P0
125     goto loop
126   end:
127     print "\n"
128     .return ()
129 .end
131 # Local Variables:
132 #   mode: pir
133 #   fill-column: 100
134 # End:
135 # vim: expandtab shiftwidth=4 ft=pir: