First cut of a test-suite transformation for Ruby. Some of the tests work.
[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         |       'new [ruby_type_decl] [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 clearUnion Type [ruby_type_decl] Id [id] 
86         replace [opt union]
87                 Union [union]
88         import ArrayInits [ruby_statements]
89                 Stmts [repeat ruby_lang_stmt]
90         export ArrayInits 
91                 Id '= 'new Type Union '; Stmts
92         by
93                 '[]
94 end function
95
96 function initDecl1 Id [id] DeclType [al_type_decl]
97         deconstruct DeclType
98                 'bool
99         replace [repeat ruby_lang_stmt]
100         by
101                 Id '= 'false ';
102 end function
103
104 function initDecl2 Id [id] DeclType [al_type_decl]
105         deconstruct DeclType
106                 'char
107         replace [repeat ruby_lang_stmt]
108         by
109                 Id '= ''c' ';
110 end function
111
112 function initDecl3 Id [id] DeclType [al_type_decl]
113         deconstruct DeclType
114                 'int
115         replace [repeat ruby_lang_stmt]
116         by
117                 Id '= '0 ';
118 end function
119
120 function initDecl4 Id [id] DeclType [al_type_decl]
121         deconstruct DeclType
122                 'ptr
123         replace [repeat ruby_lang_stmt]
124         by
125                 Id '= '-1 ';
126 end function
127
128 function alStmtToRuby1 AlStmt [action_lang_stmt]
129         deconstruct AlStmt
130                 VarDecl [al_variable_decl]
131         deconstruct VarDecl
132                 Type [al_type_decl] Id [id] OptUnion [opt union] ';
133         construct RubyType [ruby_type_decl]
134                 Type
135         replace [repeat ruby_lang_stmt]
136         by
137                 _ [initDecl1 Id Type] [initDecl2 Id Type] 
138                         [initDecl3 Id Type] [initDecl4 Id Type] 
139 end function
140
141 function alExprExtendToRuby AlExprExtend [repeat al_expr_extend]
142         deconstruct AlExprExtend
143                 Op [al_expr_op] Term [al_term] Rest [repeat al_expr_extend]
144         construct RubyRest [repeat ruby_expr_extend]
145                 _ [alExprExtendToRuby Rest]
146         replace [repeat ruby_expr_extend]
147         by
148                 Op Term RubyRest
149 end function
150
151 % Note: this doesn't go into the ( al_expr ) form of al_term.
152 function alExprToRuby AlExpr [al_expr]
153         deconstruct AlExpr
154                 ALTerm [al_term] AlExprExtend [repeat al_expr_extend]
155         construct RubyExprExtend [repeat ruby_expr_extend]
156                 _ [alExprExtendToRuby AlExprExtend]
157         construct Result [opt ruby_expr]
158                 ALTerm RubyExprExtend
159         replace [opt ruby_expr]
160         by
161                 Result 
162 end function
163
164 function alStmtToRuby2 AlStmt [action_lang_stmt]
165         deconstruct AlStmt
166                 AlExpr [al_expr] ';
167         construct OptRubyExpr [opt ruby_expr]
168                 _ [alExprToRuby AlExpr]
169         deconstruct OptRubyExpr
170                 RubyExpr [ruby_expr]
171         replace [repeat ruby_lang_stmt]
172         by
173                 RubyExpr ';
174 end function
175
176 function liftBlock
177         replace [repeat ruby_lang_stmt]
178                 'do Block [repeat ruby_lang_stmt] 'end
179         by
180                 Block
181 end function
182
183 function alOptElseRuby AlOptElse [opt al_else]
184         deconstruct AlOptElse
185                 'else 
186                         AlSubStmt [action_lang_stmt]
187         construct AlSubStmts [repeat action_lang_stmt]
188                 AlSubStmt
189         construct RubySubStmts [repeat ruby_lang_stmt]
190                 _ [alToRuby AlSubStmts]
191         deconstruct RubySubStmts
192                 RubySubStmt [ruby_lang_stmt]
193         replace [opt ruby_else]
194         by
195                 'else 
196                         RubySubStmts [liftBlock]
197 end function
198
199 function alStmtToRuby3 AlStmt [action_lang_stmt]
200         deconstruct AlStmt
201                 'if '( AlExpr [al_expr] ')
202                         AlSubStmt [action_lang_stmt]
203                 AlOptElse [opt al_else]
204         construct OptRubyExpr [opt ruby_expr]
205                 _ [alExprToRuby AlExpr]
206         deconstruct OptRubyExpr
207                 RubyExpr [ruby_expr]
208         construct AlSubStmts [repeat action_lang_stmt]
209                 AlSubStmt
210         construct RubySubStmts [repeat ruby_lang_stmt]
211                 _ [alToRuby AlSubStmts]
212         construct OptRubyElse [opt ruby_else]
213                 _ [alOptElseRuby AlOptElse]
214         replace [repeat ruby_lang_stmt]
215         by
216                 'if RubyExpr
217                         RubySubStmts [liftBlock]
218                 OptRubyElse
219                 'end
220 end function
221
222 function alStmtToRuby4a AlStmt [action_lang_stmt]
223         deconstruct AlStmt
224                 'printi Id [id] ';
225         replace [repeat ruby_lang_stmt]
226         by
227                 'print '( Id ') ';
228 end function
229
230 function alStmtToRuby4b AlStmt [action_lang_stmt]
231         deconstruct AlStmt
232                 'prints String [stringlit] ';
233         replace [repeat ruby_lang_stmt]
234         by
235                 'print '( String ') ';
236 end function
237
238 function alStmtToRuby5 AlStmt [action_lang_stmt]
239         deconstruct AlStmt
240                 '{ AlSubStmts [repeat action_lang_stmt] '}
241         construct RubySubStmts [repeat ruby_lang_stmt]
242                 _ [alToRuby AlSubStmts]
243         replace [repeat ruby_lang_stmt]
244         by
245                 'do RubySubStmts 'end
246 end function
247
248 function alStmtToRuby6 AlStmt [action_lang_stmt]
249         deconstruct AlStmt
250                 RagelStmt [al_ragel_stmt]
251         replace [repeat ruby_lang_stmt]
252         by
253                 RagelStmt
254 end function
255
256 rule fixCharLit
257         replace $ [al_term]
258                 CharLit [charlit]
259         construct BaseId [id]
260                 'id
261         construct Id [id]
262                 BaseId [unquote CharLit]
263         construct EmptyString [stringlit]
264                 '""
265         construct Repl [stringlit]
266                 EmptyString [quote Id]
267         by
268                 Repl '[0]
269 end rule
270
271
272 function alToRuby AlStmts [repeat action_lang_stmt]
273         deconstruct AlStmts
274                 FirstStmt [action_lang_stmt] Rest [repeat action_lang_stmt]
275         construct RubyFirst [repeat ruby_lang_stmt]
276                 _ 
277                         [alStmtToRuby1 FirstStmt]
278                         [alStmtToRuby2 FirstStmt]
279                         [alStmtToRuby3 FirstStmt]
280                         [alStmtToRuby4a FirstStmt]
281                         [alStmtToRuby4b FirstStmt]
282                         [alStmtToRuby5 FirstStmt]
283                         [alStmtToRuby6 FirstStmt]
284                         [fixCharLit]
285         construct RubyRest [repeat ruby_lang_stmt]
286                 _ [alToRuby Rest]
287         replace [repeat ruby_lang_stmt]
288         by
289                 RubyFirst [. RubyRest]
290 end function
291
292 rule actionTransRuby
293         replace [al_host_block]
294                 '{ AlStmts [repeat action_lang_stmt] '}
295         construct RubyStmts [repeat ruby_lang_stmt]
296                 _ [alToRuby AlStmts]
297         by
298                 '{ RubyStmts '}
299 end rule
300
301 rule lowercaseMachine
302         replace $ [machine_stmt]
303                 'machine Id [id] ';
304         by
305                 'machine Id [tolower] ';
306 end rule
307
308 function langTransRuby
309         replace [program]
310                 Definitions [repeat action_lang_stmt]
311                 '%%
312                 Initializations [repeat action_lang_stmt]
313                 RagelDef [ragel_def]
314         construct RubyDefinitions [repeat ruby_lang_stmt]
315                 _ [alToRuby Definitions]
316         construct RubyInitializations [repeat ruby_lang_stmt]
317                 _ [alToRuby Initializations]
318         construct NewRagelDef [ragel_def]
319                 RagelDef [actionTransRuby] [lowercaseMachine]
320         import ArrayInits [ruby_statements]
321                 ArrayInitStmts [repeat ruby_lang_stmt]
322         by
323                 RubyDefinitions
324                 '%%
325                 ArrayInitStmts [. RubyInitializations]
326                 NewRagelDef
327 end function
328
329 function main
330         replace [program]
331                 P [program]
332         export ArrayInits [ruby_statements]
333                 _
334         by
335                 P [langTransRuby] 
336 end function