tagged release 0.6.4
[parrot.git] / languages / lua / src / lua51.pir
blob201c37ce97cb596d5f78b3bc3b311232fa74a426
1 # Copyright (C) 2006-2008, The Perl Foundation.
2 # $Id$
4 =head1 NAME
6 src/lua51.pir -- The compiler for Lua 5.1
8 =head1 DESCRIPTION
10 This compiler extends C<PCT::HLLCompiler>
11 (see F<compilers/pct/src/PCT/HLLCompiler.pir>)
13 This compiler defines the following stages:
15 =over 4
17 =item * parse F<languages/lua/src/lua51.pg>
19 =item * PAST  F<languages/lua/src/PASTGrammar.tg>
21 =item * POST  F<languages/lua/src/POSTGrammar.tg>
23 =back
25 Used by F<languages/lua/lua.pir>.
27 =cut
29 .namespace [ 'Lua' ]
31 .sub '__onload' :anon :load :init
32     load_bytecode 'PCT.pbc'
33     load_bytecode 'PGE/Text.pbc'
35     new $P0, 'P6metaclass'
36     $P0.'new_class'('Lua::Compiler', 'parent'=>'PCT::HLLCompiler', 'attr'=>'$ostgrammar')
38     new $P0, 'Lua::Compiler'
39     $P0.'language'('Lua')
40     $P0.'parsegrammar'('Lua::Grammar')
41     $P0.'astgrammar'('Lua::PAST::Grammar')
42     $P0.'ostgrammar'('Lua::POST::Grammar')
44     $P0.'commandline_prompt'('> ')
45 .end
48 .namespace [ 'Lua::Compiler' ]
50 =head2 Overloaded methods
52 =over 4
54 =item C<ostgrammar([string grammar])>
56 Accessor for the 'ostgrammar' attribute.
58 =cut
60 .sub 'ostgrammar' :method
61     .param string value        :optional
62     .param int has_value       :opt_flag
63     .return self.'attr'('$ostgrammar', value, has_value)
64 .end
67 =item C<post(source [, adverbs :slurpy :named])>
69 Transform C<source> using the compiler's C<ostgrammar>
70 according to any options given by C<adverbs>, and return the
71 resulting ost.
73 =back
75 =cut
77 .sub 'post' :method
78     .param pmc source
79     .param pmc adverbs         :slurpy :named
80     .local string ostgrammar_name
81     .local pmc ostgrammar, ostbuilder
82     ostgrammar_name = self.'ostgrammar'()
83     unless ostgrammar_name goto default_ostgrammar
84     ostgrammar = new ostgrammar_name
85     ostbuilder = ostgrammar.'apply'(source)
86     .return ostbuilder.'get'('post')
88   default_ostgrammar:
89     $P0 = compreg 'PAST'
90     .return $P0.'compile'(source, adverbs :flat :named)
91 .end
94 .namespace [ 'Lua::PAST::Grammar' ]
96 =head2 Functions
98 =over 4
100 =item C<internal_error>
102 used in F<languages/lua/src/PASTGrammar.tg>
104 =cut
106 .sub internal_error
107     .param string msg
108     $S0 = "ERROR_INTERNAL (PAST): " . msg
109     $S0 .= "\n"
110     printerr $S0
111     exit 1
112 .end
115 .namespace [ 'Lua::POST::Grammar' ]
117 =item C<internal_error>
119 used in F<languages/lua/src/POSTGrammar.tg>
121 =cut
123 .sub internal_error
124     .param string msg
125     $S0 = "ERROR_INTERNAL (POST): " . msg
126     $S0 .= "\n"
127     printerr $S0
128     exit 1
129 .end
132 .namespace [ 'POST::Node' ]
134 .sub 'has_call_in_last_op' :method
135     $I0 = isa self, 'POST::Ops'
136     unless $I0 goto L1
137     $P0 = self.'list'()
138     $I0 = elements $P0
139     unless $I0 goto L1
140     $P1 = $P0[-1]
141     $I0 = isa $P1, 'POST::Op'
142     unless $I0 goto L1
143     $S0 = $P1.'pirop'()
144     unless $S0 == 'call' goto L1
145     .return (1, $P1)
146   L1:
147     .return (0)
148 .end
151 .namespace [ 'Lua::POST::Sub' ]
153 .sub '__onload' :anon :load :init
154     new $P0, 'P6metaclass'
155     $P0.'new_class'('Lua::POST::Sub', 'parent'=>'POST::Sub')
156 .end
158 .sub 'ops_const' :method
159     .param pmc value           :optional
160     .param int has_value       :opt_flag
161     .return self.'attr'('ops_const', value, has_value)
162 .end
164 .sub 'ops_subr' :method
165     .param pmc value           :optional
166     .param int has_value       :opt_flag
167     .return self.'attr'('ops_subr', value, has_value)
168 .end
170 .sub 'storage_const' :method
171     .param pmc value           :optional
172     .param int has_value       :opt_flag
173     .return self.'attr'('storage_const', value, has_value)
174 .end
176 .sub 'storage_lex' :method
177     .param pmc value           :optional
178     .param int has_value       :opt_flag
179     .return self.'attr'('storage_lex', value, has_value)
180 .end
183 .namespace [ 'Lua::POST::Chunk' ]
185 .sub '__onload' :anon :load :init
186     new $P0, 'P6metaclass'
187     $P0.'new_class'('Lua::POST::Chunk', 'parent'=>'Lua::POST::Sub')
188 .end
190 .sub 'prologue' :method
191     .param pmc value           :optional
192     .param int has_value       :opt_flag
193     .return self.'attr'('prologue', value, has_value)
194 .end
197 .namespace [ 'POST::Compiler' ]
199 .sub 'pir' :method :multi(_, ['Lua::POST::Chunk'])
200     .param pmc node
201     .local pmc code
202     code = self.'pir_children'(node)
203     $S0 = node.'prologue'()
204     if $S0 == '' goto L1
205     new code, 'CodeString'
206     code.'emit'($S0)
207     $P0 = self.'code'()
208     code .= $P0
209     self.'code'(code)
210   L1:
211     .return (code)
212 .end
215 .namespace [ 'Lua::Symbtab' ]
217 .sub '__onload' :anon :load :init
218     $P0 = subclass 'ResizablePMCArray', 'Lua::Symbtab'
219     new $P0, 'Integer'
220     set $P0, 0
221     set_global '$nb', $P0
222 .end
224 .sub 'insert' :method
225     .param string name
226     $P0 = self[0]
227     $I0 = exists $P0[name]
228     if $I0 goto L1
229     $S0 = name . '_'
230     $P1 = get_global '$nb'
231     $S1 = $P1
232     $S0 .= $S1
233     $P0[name] = $S0
234   L1:
235     $S0 = $P0[name]
236     .return ($S0)
237 .end
239 .sub 'lookup' :method
240     .param string name
241     .local pmc iter
242     new iter, 'Iterator', self
243   L1:
244     unless iter goto L2
245     $P0 = shift iter
246     $I0 = exists $P0[name]
247     unless $I0 goto L1
248     $S0 = $P0[name]
249     .return (1, $S0)
250   L2:
251     .return (0)
252 .end
254 .sub 'push_scope' :method
255     new $P0, 'Hash'
256     unshift self, $P0
257     $P1 = get_global '$nb'
258     inc $P1
259 .end
261 .sub 'pop_scope' :method
262     $P0 = shift self
263 .end
265 .include 'languages/lua/src/grammar51.pir'
266 .include 'languages/lua/src/lua51_gen.pir'
267 .include 'languages/lua/src/PASTGrammar_gen.pir'
268 .include 'languages/lua/src/POSTGrammar_gen.pir'
270 =back
272 =head1 AUTHORS
274 Klaas-Jan Stol <parrotcode@gmail.com>
276 Francois Perrad
278 =cut
281 # Local Variables:
282 #   mode: pir
283 #   fill-column: 100
284 # End:
285 # vim: expandtab shiftwidth=4 ft=pir: