681baa2eada8d79bd99796453498390a53f76982
[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 alTermToC
120         replace [al_term]
121                 'first_token_char
122         by
123                 'tokstart '[0]
124 end function
125
126 function alExprExtendToC AlExprExtend [repeat al_expr_extend]
127         deconstruct AlExprExtend
128                 Op [al_expr_op] Term [al_term] Rest [repeat al_expr_extend]
129         construct RestC [repeat c_expr_extend]
130                 _ [alExprExtendToC Rest]
131         replace [repeat c_expr_extend]
132         by
133                 Op Term [alTermToC] RestC
134 end function
135
136 function alExprToC AlExpr [al_expr]
137         deconstruct AlExpr
138                 ALTerm [al_term] AlExprExtend [repeat al_expr_extend]
139         construct CExprExtend [repeat c_expr_extend]
140                 _ [alExprExtendToC AlExprExtend]
141         construct Result [opt c_expr]
142                 ALTerm [alTermToC] CExprExtend
143         replace [opt c_expr]
144         by
145                 Result [boolVals1] [boolVals2]
146 end function
147
148 function alStmtToC2 AlStmt [action_lang_stmt]
149         deconstruct AlStmt
150                 AlExpr [al_expr] ';
151         construct OptCExpr [opt c_expr]
152                 _ [alExprToC AlExpr]
153         deconstruct OptCExpr
154                 CExpr [c_expr]
155         replace [repeat c_lang_stmt]
156         by
157                 CExpr ';
158 end function
159
160 function alOptElseC AlOptElse [opt al_else]
161         deconstruct AlOptElse
162                 'else 
163                         AlSubStmt [action_lang_stmt]
164         construct AlSubStmts [repeat action_lang_stmt]
165                 AlSubStmt
166         construct CSubStmts [repeat c_lang_stmt]
167                 _ [alToC AlSubStmts]
168         deconstruct CSubStmts
169                 CSubStmt [c_lang_stmt]
170         replace [opt c_else]
171         by
172                 'else 
173                         CSubStmt
174 end function
175
176 function alStmtToC3 AlStmt [action_lang_stmt]
177         deconstruct AlStmt
178                 'if '( AlExpr [al_expr] ')
179                         AlSubStmt [action_lang_stmt]
180                 AlOptElse [opt al_else]
181         construct OptCExpr [opt c_expr]
182                 _ [alExprToC AlExpr]
183         deconstruct OptCExpr
184                 CExpr [c_expr]
185         construct AlSubStmts [repeat action_lang_stmt]
186                 AlSubStmt
187         construct CSubStmts [repeat c_lang_stmt]
188                 _ [alToC AlSubStmts]
189         deconstruct CSubStmts
190                 CSubStmt [c_lang_stmt]
191         construct OptCElse [opt c_else]
192                 _ [alOptElseC AlOptElse]
193         replace [repeat c_lang_stmt]
194         by
195                 'if '( CExpr ')
196                         CSubStmt
197                 OptCElse
198 end function
199
200 function alStmtToC4a AlStmt [action_lang_stmt]
201         deconstruct AlStmt
202                 'printi Id [id] ';
203         replace [repeat c_lang_stmt]
204         by
205                 'printf '( '"%i" ', Id ');
206 end function
207
208 function alStmtToC4b AlStmt [action_lang_stmt]
209         deconstruct AlStmt
210                 'prints String [stringlit] ';
211         replace [repeat c_lang_stmt]
212         by
213                 'fputs '( String , 'stdout ');
214 end function
215
216 function alStmtToC4c AlStmt [action_lang_stmt]
217         deconstruct AlStmt
218                 'printb Id [id] ';
219         replace [repeat c_lang_stmt]
220         by
221                 'fwrite '( Id ', '1 ', 'pos ', 'stdout ');
222 end function
223
224 function alStmtToC4d AlStmt [action_lang_stmt]
225         deconstruct AlStmt
226                 'print_token ';
227         replace [repeat c_lang_stmt]
228         by
229                 'fwrite '( 'tokstart ', '1 ', 'tokend '- 'tokstart ', 'stdout ');
230 end function
231
232 function alStmtToC5 AlStmt [action_lang_stmt]
233         deconstruct AlStmt
234                 '{ AlSubStmts [repeat action_lang_stmt] '}
235         construct CSubStmts [repeat c_lang_stmt]
236                 _ [alToC AlSubStmts]
237         replace [repeat c_lang_stmt]
238         by
239                 '{ CSubStmts '}
240 end function
241
242 function alStmtToC6 AlStmt [action_lang_stmt]
243         deconstruct AlStmt
244                 RagelStmt [al_ragel_stmt]
245         replace [repeat c_lang_stmt]
246         by
247                 RagelStmt
248 end function
249
250 function alToC AlStmts [repeat action_lang_stmt]
251         deconstruct AlStmts
252                 FirstStmt [action_lang_stmt] Rest [repeat action_lang_stmt]
253         construct FirstC [repeat c_lang_stmt]
254                 _ 
255                         [alStmtToC1 FirstStmt]
256                         [alStmtToC2 FirstStmt]
257                         [alStmtToC3 FirstStmt]
258                         [alStmtToC4a FirstStmt]
259                         [alStmtToC4b FirstStmt]
260                         [alStmtToC4c FirstStmt]
261                         [alStmtToC4d FirstStmt]
262                         [alStmtToC5 FirstStmt]
263                         [alStmtToC6 FirstStmt]
264         construct RestC [repeat c_lang_stmt]
265                 _ [alToC Rest]
266         replace [repeat c_lang_stmt]
267         by
268                 FirstC [. RestC]
269 end function
270
271 rule actionTransC
272         replace [al_host_block]
273                 '{ AlStmts [repeat action_lang_stmt] '}
274         construct CStmts [repeat c_lang_stmt]
275                 _ [alToC AlStmts]
276         by
277                 '{ CStmts '}
278 end rule
279
280 function langTransC
281         replace [program]
282                 Definitions [repeat action_lang_stmt]
283                 '%%
284                 Initializations [repeat action_lang_stmt]
285                 RagelDef [ragel_def]
286         construct CDefinitions [repeat c_lang_stmt]
287                 _ [alToC Definitions]
288         construct CInitializations [repeat c_lang_stmt]
289                 _ [alToC Initializations]
290         by
291                 CDefinitions
292                 '%%
293                 CInitializations
294                 RagelDef [actionTransC]
295 end function
296
297 function main
298         replace [program]
299                 P [program]
300         by
301                 P [langTransC]
302 end function