First cut of a test-suite transformation for Ruby. Some of the tests work.
[external/ragel.git] / test / langtrans_c.txl
1 include "testcase.txl"
2
3 define c_statements
4                 [repeat c_lang_stmt]
5 end define
6
7 define c_lang_stmt
8                 [al_ragel_stmt]
9         |       [c_variable_decl]
10         |       [c_expr_stmt]
11         |       [c_if_stmt]
12         |       [EX] '{ [IN] [NL] [c_statements] [EX] '} [IN] [NL]
13 end define
14
15 define c_variable_decl
16                 [c_type_decl] [id] [opt union] '; [NL]
17 end define
18
19 define c_type_decl
20                 [al_type_decl]
21         |       'char '*
22 end define
23
24 define c_expr_stmt
25                 [c_expr] '; [NL]
26 end define
27
28 define c_expr
29                 [c_term] [repeat c_expr_extend]
30 end define
31
32 define c_expr_extend
33                 [al_expr_op] [c_term]
34 end define
35
36 define c_term
37                 [al_term]
38         |       [id] '( [c_args] ')
39 end define
40
41 define c_args
42                 [list c_expr] 
43 end define
44
45 define c_sign
46                 '- | '+
47 end define
48
49 define c_if_stmt
50                 'if '( [c_expr] ') [NL] [IN]
51                         [c_lang_stmt] [EX]
52                 [opt c_else]
53 end define
54
55 define c_else
56                 'else [NL] [IN]
57                         [c_lang_stmt] [EX]
58 end define
59
60 define c_lang
61                 [c_statements]
62                 '%% [NL]
63                 [c_statements]
64                 [ragel_def]
65 end define
66
67 define program
68                 [lang_indep]
69         |       [c_lang]
70 end define
71
72 redefine al_host_block
73                 '{ [NL] [IN] [al_statements] [EX] '} [NL]
74         |       '{ [NL] [IN] [c_statements] [EX] '} [NL]
75 end define
76
77 rule boolTypes
78         replace [al_type_decl]
79                 'bool
80         by
81                 'int
82 end rule
83
84 rule ptrTypes
85         replace [c_type_decl]
86                 'ptr
87         by
88                 'char '*
89 end rule
90
91 rule boolVals1
92         replace [al_term]
93                 'true
94         by
95                 '1
96 end rule
97
98 rule boolVals2
99         replace [al_term]
100                 'false
101         by
102                 '0
103 end rule
104
105 function alStmtToC1 AlStmt [action_lang_stmt]
106         deconstruct AlStmt
107                 VarDecl [al_variable_decl]
108         deconstruct VarDecl
109                 Type [al_type_decl] Id [id] OptUnion [opt union]';
110         construct CType [c_type_decl]
111                 Type
112         construct Result [c_variable_decl]
113                 CType [boolTypes] [ptrTypes] Id OptUnion ';
114         replace [repeat c_lang_stmt]
115         by
116                 Result
117 end function
118
119 function alExprExtendToC AlExprExtend [repeat al_expr_extend]
120         deconstruct AlExprExtend
121                 Op [al_expr_op] Term [al_term] Rest [repeat al_expr_extend]
122         construct RestC [repeat c_expr_extend]
123                 _ [alExprExtendToC Rest]
124         replace [repeat c_expr_extend]
125         by
126                 Op Term RestC
127 end function
128
129 function alExprToC AlExpr [al_expr]
130         deconstruct AlExpr
131                 ALTerm [al_term] AlExprExtend [repeat al_expr_extend]
132         construct CExprExtend [repeat c_expr_extend]
133                 _ [alExprExtendToC AlExprExtend]
134         construct Result [opt c_expr]
135                 ALTerm CExprExtend
136         replace [opt c_expr]
137         by
138                 Result [boolVals1] [boolVals2]
139 end function
140
141 function alStmtToC2 AlStmt [action_lang_stmt]
142         deconstruct AlStmt
143                 AlExpr [al_expr] ';
144         construct OptCExpr [opt c_expr]
145                 _ [alExprToC AlExpr]
146         deconstruct OptCExpr
147                 CExpr [c_expr]
148         replace [repeat c_lang_stmt]
149         by
150                 CExpr ';
151 end function
152
153 function alOptElseC AlOptElse [opt al_else]
154         deconstruct AlOptElse
155                 'else 
156                         AlSubStmt [action_lang_stmt]
157         construct AlSubStmts [repeat action_lang_stmt]
158                 AlSubStmt
159         construct CSubStmts [repeat c_lang_stmt]
160                 _ [alToC AlSubStmts]
161         deconstruct CSubStmts
162                 CSubStmt [c_lang_stmt]
163         replace [opt c_else]
164         by
165                 'else 
166                         CSubStmt
167 end function
168
169 function alStmtToC3 AlStmt [action_lang_stmt]
170         deconstruct AlStmt
171                 'if '( AlExpr [al_expr] ')
172                         AlSubStmt [action_lang_stmt]
173                 AlOptElse [opt al_else]
174         construct OptCExpr [opt c_expr]
175                 _ [alExprToC AlExpr]
176         deconstruct OptCExpr
177                 CExpr [c_expr]
178         construct AlSubStmts [repeat action_lang_stmt]
179                 AlSubStmt
180         construct CSubStmts [repeat c_lang_stmt]
181                 _ [alToC AlSubStmts]
182         deconstruct CSubStmts
183                 CSubStmt [c_lang_stmt]
184         construct OptCElse [opt c_else]
185                 _ [alOptElseC AlOptElse]
186         replace [repeat c_lang_stmt]
187         by
188                 'if '( CExpr ')
189                         CSubStmt
190                 OptCElse
191 end function
192
193 function alStmtToC4a AlStmt [action_lang_stmt]
194         deconstruct AlStmt
195                 'printi Id [id] ';
196         replace [repeat c_lang_stmt]
197         by
198                 'printf '( '"%i" ', Id ');
199 end function
200
201 function alStmtToC4b AlStmt [action_lang_stmt]
202         deconstruct AlStmt
203                 'prints String [stringlit] ';
204         replace [repeat c_lang_stmt]
205         by
206                 'fputs '( String , 'stdout ');
207 end function
208
209 function alStmtToC5 AlStmt [action_lang_stmt]
210         deconstruct AlStmt
211                 '{ AlSubStmts [repeat action_lang_stmt] '}
212         construct CSubStmts [repeat c_lang_stmt]
213                 _ [alToC AlSubStmts]
214         replace [repeat c_lang_stmt]
215         by
216                 '{ CSubStmts '}
217 end function
218
219 function alStmtToC6 AlStmt [action_lang_stmt]
220         deconstruct AlStmt
221                 RagelStmt [al_ragel_stmt]
222         replace [repeat c_lang_stmt]
223         by
224                 RagelStmt
225 end function
226
227 function alToC AlStmts [repeat action_lang_stmt]
228         deconstruct AlStmts
229                 FirstStmt [action_lang_stmt] Rest [repeat action_lang_stmt]
230         construct FirstC [repeat c_lang_stmt]
231                 _ 
232                         [alStmtToC1 FirstStmt]
233                         [alStmtToC2 FirstStmt]
234                         [alStmtToC3 FirstStmt]
235                         [alStmtToC4a FirstStmt]
236                         [alStmtToC4b FirstStmt]
237                         [alStmtToC5 FirstStmt]
238                         [alStmtToC6 FirstStmt]
239         construct RestC [repeat c_lang_stmt]
240                 _ [alToC Rest]
241         replace [repeat c_lang_stmt]
242         by
243                 FirstC [. RestC]
244 end function
245
246 rule actionTransC
247         replace [al_host_block]
248                 '{ AlStmts [repeat action_lang_stmt] '}
249         construct CStmts [repeat c_lang_stmt]
250                 _ [alToC AlStmts]
251         by
252                 '{ CStmts '}
253 end rule
254
255 function langTransC
256         replace [program]
257                 Definitions [repeat action_lang_stmt]
258                 '%%
259                 Initializations [repeat action_lang_stmt]
260                 RagelDef [ragel_def]
261         construct CDefinitions [repeat c_lang_stmt]
262                 _ [alToC Definitions]
263         construct CInitializations [repeat c_lang_stmt]
264                 _ [alToC Initializations]
265         by
266                 CDefinitions
267                 '%%
268                 CInitializations
269                 RagelDef [actionTransC]
270 end function
271
272 function main
273         replace [program]
274                 P [program]
275         by
276                 P [langTransC]
277 end function