Initialize Tizen 2.3
[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 redefine cond_action_stmt
78                 'action [id] '{ [al_expr] '} [NL]
79         |       'action [id] '{ [c_expr] '} [NL]
80 end redefine
81
82
83 rule boolTypes
84         replace [al_type_decl]
85                 'bool
86         by
87                 'int
88 end rule
89
90 rule ptrTypes
91         replace [c_type_decl]
92                 'ptr
93         by
94                 'char '*
95 end rule
96
97 rule boolVals1
98         replace [al_term]
99                 'true
100         by
101                 '1
102 end rule
103
104 rule boolVals2
105         replace [al_term]
106                 'false
107         by
108                 '0
109 end rule
110
111 function alStmtToC1 AlStmt [action_lang_stmt]
112         deconstruct AlStmt
113                 VarDecl [al_variable_decl]
114         deconstruct VarDecl
115                 Type [al_type_decl] Id [id] OptUnion [opt union]';
116         construct CType [c_type_decl]
117                 Type
118         construct Result [c_variable_decl]
119                 CType [boolTypes] [ptrTypes] Id OptUnion ';
120         replace [repeat c_lang_stmt]
121         by
122                 Result
123 end function
124
125 function alTermToC
126         replace [al_term]
127                 'first_token_char
128         by
129                 'ts '[0]
130 end function
131
132 function alExprExtendToC AlExprExtend [repeat al_expr_extend]
133         deconstruct AlExprExtend
134                 Op [al_expr_op] Term [al_term] Rest [repeat al_expr_extend]
135         construct RestC [repeat c_expr_extend]
136                 _ [alExprExtendToC Rest]
137         replace [repeat c_expr_extend]
138         by
139                 Op Term [alTermToC] RestC
140 end function
141
142 function alExprToC AlExpr [al_expr]
143         deconstruct AlExpr
144                 ALTerm [al_term] AlExprExtend [repeat al_expr_extend]
145         construct CExprExtend [repeat c_expr_extend]
146                 _ [alExprExtendToC AlExprExtend]
147         construct Result [opt c_expr]
148                 ALTerm [alTermToC] CExprExtend
149         replace [opt c_expr]
150         by
151                 Result [boolVals1] [boolVals2]
152 end function
153
154 function alStmtToC2 AlStmt [action_lang_stmt]
155         deconstruct AlStmt
156                 AlExpr [al_expr] ';
157         construct OptCExpr [opt c_expr]
158                 _ [alExprToC AlExpr]
159         deconstruct OptCExpr
160                 CExpr [c_expr]
161         replace [repeat c_lang_stmt]
162         by
163                 CExpr ';
164 end function
165
166 function alOptElseC AlOptElse [opt al_else]
167         deconstruct AlOptElse
168                 'else 
169                         AlSubStmt [action_lang_stmt]
170         construct AlSubStmts [repeat action_lang_stmt]
171                 AlSubStmt
172         construct CSubStmts [repeat c_lang_stmt]
173                 _ [alToC AlSubStmts]
174         deconstruct CSubStmts
175                 CSubStmt [c_lang_stmt]
176         replace [opt c_else]
177         by
178                 'else 
179                         CSubStmt
180 end function
181
182 function alStmtToC3 AlStmt [action_lang_stmt]
183         deconstruct AlStmt
184                 'if '( AlExpr [al_expr] ')
185                         AlSubStmt [action_lang_stmt]
186                 AlOptElse [opt al_else]
187         construct OptCExpr [opt c_expr]
188                 _ [alExprToC AlExpr]
189         deconstruct OptCExpr
190                 CExpr [c_expr]
191         construct AlSubStmts [repeat action_lang_stmt]
192                 AlSubStmt
193         construct CSubStmts [repeat c_lang_stmt]
194                 _ [alToC AlSubStmts]
195         deconstruct CSubStmts
196                 CSubStmt [c_lang_stmt]
197         construct OptCElse [opt c_else]
198                 _ [alOptElseC AlOptElse]
199         replace [repeat c_lang_stmt]
200         by
201                 'if '( CExpr ')
202                         CSubStmt
203                 OptCElse
204 end function
205
206 function alStmtToC4a AlStmt [action_lang_stmt]
207         deconstruct AlStmt
208                 'printi Id [id] ';
209         replace [repeat c_lang_stmt]
210         by
211                 'printf '( '"%i" ', Id ');
212 end function
213
214 function alStmtToC4b AlStmt [action_lang_stmt]
215         deconstruct AlStmt
216                 'prints String [stringlit] ';
217         replace [repeat c_lang_stmt]
218         by
219                 'fputs '( String , 'stdout ');
220 end function
221
222 function alStmtToC4c AlStmt [action_lang_stmt]
223         deconstruct AlStmt
224                 'printb Id [id] ';
225         replace [repeat c_lang_stmt]
226         by
227                 'fwrite '( Id ', '1 ', 'pos ', 'stdout ');
228 end function
229
230 function alStmtToC4d AlStmt [action_lang_stmt]
231         deconstruct AlStmt
232                 'print_token ';
233         replace [repeat c_lang_stmt]
234         by
235                 'fwrite '( 'ts ', '1 ', 'te '- 'ts ', 'stdout ');
236 end function
237
238 function alStmtToC5 AlStmt [action_lang_stmt]
239         deconstruct AlStmt
240                 '{ AlSubStmts [repeat action_lang_stmt] '}
241         construct CSubStmts [repeat c_lang_stmt]
242                 _ [alToC AlSubStmts]
243         replace [repeat c_lang_stmt]
244         by
245                 '{ CSubStmts '}
246 end function
247
248 function alStmtToC6 AlStmt [action_lang_stmt]
249         deconstruct AlStmt
250                 RagelStmt [al_ragel_stmt]
251         replace [repeat c_lang_stmt]
252         by
253                 RagelStmt
254 end function
255
256 function alToC AlStmts [repeat action_lang_stmt]
257         deconstruct AlStmts
258                 FirstStmt [action_lang_stmt] Rest [repeat action_lang_stmt]
259         construct FirstC [repeat c_lang_stmt]
260                 _ 
261                         [alStmtToC1 FirstStmt]
262                         [alStmtToC2 FirstStmt]
263                         [alStmtToC3 FirstStmt]
264                         [alStmtToC4a FirstStmt]
265                         [alStmtToC4b FirstStmt]
266                         [alStmtToC4c FirstStmt]
267                         [alStmtToC4d FirstStmt]
268                         [alStmtToC5 FirstStmt]
269                         [alStmtToC6 FirstStmt]
270         construct RestC [repeat c_lang_stmt]
271                 _ [alToC Rest]
272         replace [repeat c_lang_stmt]
273         by
274                 FirstC [. RestC]
275 end function
276
277 rule actionTransC
278         replace [al_host_block]
279                 '{ AlStmts [repeat action_lang_stmt] '}
280         construct CStmts [repeat c_lang_stmt]
281                 _ [alToC AlStmts]
282         by
283                 '{ CStmts '}
284 end rule
285
286 rule condTransC
287         replace [cond_action_stmt]
288                 'action Id [id] '{ AlExpr [al_expr] '}
289         construct OptCExpr [opt c_expr]
290                 _ [alExprToC AlExpr]
291         deconstruct OptCExpr
292                 CExpr [c_expr]
293         by
294                 'action Id '{ CExpr '}
295 end rule
296
297 function langTransC
298         replace [program]
299                 Definitions [repeat action_lang_stmt]
300                 '%%
301                 Initializations [repeat action_lang_stmt]
302                 RagelDef [ragel_def]
303         construct CDefinitions [repeat c_lang_stmt]
304                 _ [alToC Definitions]
305         construct CInitializations [repeat c_lang_stmt]
306                 _ [alToC Initializations]
307         by
308                 CDefinitions
309                 '%%
310                 CInitializations
311                 RagelDef [actionTransC] [condTransC]
312 end function
313
314 function main
315         replace [program]
316                 P [program]
317         by
318                 P [langTransC]
319 end function