Declaration of stacks now works.
[external/ragel.git] / test / langtrans_ruby.txl
1 include "testcase.txl"
2
3 keys
4         'boolean 'new
5 end keys
6
7
8 define ruby_statements
9                 [repeat ruby_lang_stmt]
10 end define
11
12 define ruby_lang_stmt
13                 [al_ragel_stmt]
14         |       [ruby_expr_stmt]
15         |       [ruby_if_stmt]
16         |       [EX] 'do [IN] [NL] [ruby_statements] [EX] 'end [IN] [NL]
17 end define
18
19 define ruby_type_decl
20                 [al_type_decl]
21         |       'boolean
22 end define
23
24 define ruby_expr_stmt
25                 [ruby_expr] '; [NL]
26 end define
27
28 define ruby_expr
29                 [ruby_term] [repeat ruby_expr_extend]
30 end define
31
32 define ruby_expr_extend
33                 [al_expr_op] [ruby_term]
34 end define
35
36 define ruby_term
37                 [al_term]
38         |       [stringlit] [union]
39         |       [id] [repeat ruby_dot_id]
40         |       [SPOFF] [id] [repeat ruby_dot_id] '( [SPON] [ruby_args] ')
41         |       [union]
42 end define
43
44 define ruby_dot_id
45                 '. [id]
46 end define
47
48 define ruby_args
49                 [list ruby_expr] 
50 end define
51
52 define ruby_sign
53                 '- | '+
54 end define
55
56 define ruby_if_stmt
57                 'if [ruby_expr] [NL] [IN]
58                         [ruby_statements] [EX]
59                 [opt ruby_else]
60                 'end [NL]
61 end define
62
63 define ruby_else
64                 'else [NL] [IN]
65                         [ruby_statements] [EX]
66 end define
67
68 define ruby_lang
69                 [ruby_statements]
70                 '%% [NL]
71                 [ruby_statements]
72                 [ragel_def]
73 end define
74
75 define program
76                 [lang_indep]
77         |       [ruby_lang]
78 end define
79
80 redefine al_host_block
81                 '{ [NL] [IN] [al_statements] [EX] '} [NL]
82         |       '{ [NL] [IN] [ruby_statements] [EX] '} [NL]
83 end define
84
85 function initDecl1 VarDecl [al_variable_decl]
86         deconstruct VarDecl
87                 'bool Id [id] ';
88         replace [repeat ruby_lang_stmt]
89         by
90                 Id '= 'false ';
91 end function
92
93 function initDecl2 VarDecl [al_variable_decl]
94         deconstruct VarDecl
95                 'char Id [id] ';
96         replace [repeat ruby_lang_stmt]
97         by
98                 Id '= ''c' ';
99 end function
100
101 function initDecl3 VarDecl [al_variable_decl]
102         deconstruct VarDecl
103                 'int Id [id] ';
104         replace [repeat ruby_lang_stmt]
105         by
106                 Id '= '0 ';
107 end function
108
109 function initDecl4 VarDecl [al_variable_decl]
110         deconstruct VarDecl
111                 'ptr Id [id] ';
112         replace [repeat ruby_lang_stmt]
113         by
114                 Id '= '-1 ';
115 end function
116
117 function initDecl5 VarDecl [al_variable_decl]
118         deconstruct VarDecl
119                 'int Id [id] Union [union] ';
120         replace [repeat ruby_lang_stmt]
121         by
122                 Id '= '[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] ';
123 end function
124
125
126 function alStmtToRuby1 AlStmt [action_lang_stmt]
127         deconstruct AlStmt
128                 VarDecl [al_variable_decl]
129         deconstruct VarDecl
130                 Type [al_type_decl] Id [id] OptUnion [opt union] ';
131         replace [repeat ruby_lang_stmt]
132         by
133                 _ [initDecl1 VarDecl] [initDecl2 VarDecl] 
134                         [initDecl3 VarDecl] [initDecl4 VarDecl]
135                         [initDecl5 VarDecl]
136 end function
137
138 function alExprExtendToRuby AlExprExtend [repeat al_expr_extend]
139         deconstruct AlExprExtend
140                 Op [al_expr_op] Term [al_term] Rest [repeat al_expr_extend]
141         construct RubyRest [repeat ruby_expr_extend]
142                 _ [alExprExtendToRuby Rest]
143         replace [repeat ruby_expr_extend]
144         by
145                 Op Term RubyRest
146 end function
147
148 % Note: this doesn't go into the ( al_expr ) form of al_term.
149 function alExprToRuby AlExpr [al_expr]
150         deconstruct AlExpr
151                 ALTerm [al_term] AlExprExtend [repeat al_expr_extend]
152         construct RubyExprExtend [repeat ruby_expr_extend]
153                 _ [alExprExtendToRuby AlExprExtend]
154         construct Result [opt ruby_expr]
155                 ALTerm RubyExprExtend
156         replace [opt ruby_expr]
157         by
158                 Result 
159 end function
160
161 function alStmtToRuby2 AlStmt [action_lang_stmt]
162         deconstruct AlStmt
163                 AlExpr [al_expr] ';
164         construct OptRubyExpr [opt ruby_expr]
165                 _ [alExprToRuby AlExpr]
166         deconstruct OptRubyExpr
167                 RubyExpr [ruby_expr]
168         replace [repeat ruby_lang_stmt]
169         by
170                 RubyExpr ';
171 end function
172
173 function liftBlock
174         replace [repeat ruby_lang_stmt]
175                 'do Block [repeat ruby_lang_stmt] 'end
176         by
177                 Block
178 end function
179
180 function alOptElseRuby AlOptElse [opt al_else]
181         deconstruct AlOptElse
182                 'else 
183                         AlSubStmt [action_lang_stmt]
184         construct AlSubStmts [repeat action_lang_stmt]
185                 AlSubStmt
186         construct RubySubStmts [repeat ruby_lang_stmt]
187                 _ [alToRuby AlSubStmts]
188         deconstruct RubySubStmts
189                 RubySubStmt [ruby_lang_stmt]
190         replace [opt ruby_else]
191         by
192                 'else 
193                         RubySubStmts [liftBlock]
194 end function
195
196 function alStmtToRuby3 AlStmt [action_lang_stmt]
197         deconstruct AlStmt
198                 'if '( AlExpr [al_expr] ')
199                         AlSubStmt [action_lang_stmt]
200                 AlOptElse [opt al_else]
201         construct OptRubyExpr [opt ruby_expr]
202                 _ [alExprToRuby AlExpr]
203         deconstruct OptRubyExpr
204                 RubyExpr [ruby_expr]
205         construct AlSubStmts [repeat action_lang_stmt]
206                 AlSubStmt
207         construct RubySubStmts [repeat ruby_lang_stmt]
208                 _ [alToRuby AlSubStmts]
209         construct OptRubyElse [opt ruby_else]
210                 _ [alOptElseRuby AlOptElse]
211         replace [repeat ruby_lang_stmt]
212         by
213                 'if RubyExpr
214                         RubySubStmts [liftBlock]
215                 OptRubyElse
216                 'end
217 end function
218
219 function alStmtToRuby4a AlStmt [action_lang_stmt]
220         deconstruct AlStmt
221                 'printi Id [id] ';
222         replace [repeat ruby_lang_stmt]
223         by
224                 'print '( Id ') ';
225 end function
226
227 function alStmtToRuby4b AlStmt [action_lang_stmt]
228         deconstruct AlStmt
229                 'prints String [stringlit] ';
230         replace [repeat ruby_lang_stmt]
231         by
232                 'print '( String ') ';
233 end function
234
235 function alStmtToRuby5 AlStmt [action_lang_stmt]
236         deconstruct AlStmt
237                 '{ AlSubStmts [repeat action_lang_stmt] '}
238         construct RubySubStmts [repeat ruby_lang_stmt]
239                 _ [alToRuby AlSubStmts]
240         replace [repeat ruby_lang_stmt]
241         by
242                 'do RubySubStmts 'end
243 end function
244
245 function alStmtToRuby6 AlStmt [action_lang_stmt]
246         deconstruct AlStmt
247                 RagelStmt [al_ragel_stmt]
248         replace [repeat ruby_lang_stmt]
249         by
250                 RagelStmt
251 end function
252
253 rule fixCharLit
254         replace $ [al_term]
255                 CharLit [charlit]
256         construct BaseId [id]
257                 'id
258         construct Id [id]
259                 BaseId [unquote CharLit]
260         construct EmptyString [stringlit]
261                 '""
262         construct Repl [stringlit]
263                 EmptyString [quote Id]
264         by
265                 Repl '[0]
266 end rule
267
268
269 function alToRuby AlStmts [repeat action_lang_stmt]
270         deconstruct AlStmts
271                 FirstStmt [action_lang_stmt] Rest [repeat action_lang_stmt]
272         construct RubyFirst [repeat ruby_lang_stmt]
273                 _ 
274                         [alStmtToRuby1 FirstStmt]
275                         [alStmtToRuby2 FirstStmt]
276                         [alStmtToRuby3 FirstStmt]
277                         [alStmtToRuby4a FirstStmt]
278                         [alStmtToRuby4b FirstStmt]
279                         [alStmtToRuby5 FirstStmt]
280                         [alStmtToRuby6 FirstStmt]
281                         [fixCharLit]
282         construct RubyRest [repeat ruby_lang_stmt]
283                 _ [alToRuby Rest]
284         replace [repeat ruby_lang_stmt]
285         by
286                 RubyFirst [. RubyRest]
287 end function
288
289 rule actionTransRuby
290         replace [al_host_block]
291                 '{ AlStmts [repeat action_lang_stmt] '}
292         construct RubyStmts [repeat ruby_lang_stmt]
293                 _ [alToRuby AlStmts]
294         by
295                 '{ RubyStmts '}
296 end rule
297
298 rule lowercaseMachine
299         replace $ [machine_stmt]
300                 'machine Id [id] ';
301         by
302                 'machine Id [tolower] ';
303 end rule
304
305 function langTransRuby
306         replace [program]
307                 Definitions [repeat action_lang_stmt]
308                 '%%
309                 Initializations [repeat action_lang_stmt]
310                 RagelDef [ragel_def]
311         construct RubyDefinitions [repeat ruby_lang_stmt]
312                 _ [alToRuby Definitions]
313         construct RubyInitializations [repeat ruby_lang_stmt]
314                 _ [alToRuby Initializations]
315         construct NewRagelDef [ragel_def]
316                 RagelDef [actionTransRuby] [lowercaseMachine]
317         import ArrayInits [ruby_statements]
318                 ArrayInitStmts [repeat ruby_lang_stmt]
319         by
320                 RubyDefinitions
321                 '%%
322                 ArrayInitStmts [. RubyInitializations]
323                 NewRagelDef
324 end function
325
326 function main
327         replace [program]
328                 P [program]
329         export ArrayInits [ruby_statements]
330                 _
331         by
332                 P [langTransRuby] 
333 end function