Initialize Tizen 2.3
[external/ragel.git] / test / langtrans_d.txl
1 include "testcase.txl"
2
3 define d_statements
4                 [repeat d_lang_stmt]
5 end define
6
7 define d_lang_stmt
8                 [al_ragel_stmt]
9         |       [d_variable_decl]
10         |       [d_expr_stmt]
11         |       [d_if_stmt]
12         |       [EX] '{ [IN] [NL] [d_statements] [EX] '} [IN] [NL]
13 end define
14
15 define d_variable_decl
16                 [d_type_decl] [id] [opt union] '; [NL]
17 end define
18
19 define d_type_decl
20                 [al_type_decl]
21         |       'char '*
22 end define
23
24 define d_expr_stmt
25                 [d_expr] '; [NL]
26 end define
27
28 define d_expr
29                 [d_term] [repeat d_expr_extend]
30 end define
31
32 define d_expr_extend
33                 [al_expr_op] [d_term]
34 end define
35
36 define d_term
37                 [al_term]
38         |       [id] '( [d_args] ')
39 end define
40
41 define d_args
42                 [list d_expr] 
43 end define
44
45 define d_sign
46                 '- | '+
47 end define
48
49 define d_if_stmt
50                 'if '( [d_expr] ') [NL] [IN]
51                         [d_lang_stmt] [EX]
52                 [opt d_else]
53 end define
54
55 define d_else
56                 'else [NL] [IN]
57                         [d_lang_stmt] [EX]
58 end define
59
60 define d_lang
61                 [d_statements]
62                 '%% [NL]
63                 [d_statements]
64                 [ragel_def]
65 end define
66
67 define program
68                 [lang_indep]
69         |       [d_lang]
70 end define
71
72 redefine al_host_block
73                 '{ [NL] [IN] [al_statements] [EX] '} [NL]
74         |       '{ [NL] [IN] [d_statements] [EX] '} [NL]
75 end define
76
77 rule ptrTypes
78         replace [d_type_decl]
79                 'ptr
80         by
81                 'char '*
82 end rule
83
84 function alStmtToD1 AlStmt [action_lang_stmt]
85         deconstruct AlStmt
86                 VarDecl [al_variable_decl]
87         deconstruct VarDecl
88                 Type [al_type_decl] Id [id] OptUnion [opt union] ';
89         construct DType [d_type_decl]
90                 Type
91         construct Result [d_variable_decl]
92                 DType [ptrTypes] Id OptUnion ';
93         replace [repeat d_lang_stmt]
94         by
95                 Result
96 end function
97
98 function alTermToD
99         replace [al_term]
100                 'first_token_char
101         by
102                 'ts '[0]
103 end function
104
105 function alExprExtendToD AlExprExtend [repeat al_expr_extend]
106         deconstruct AlExprExtend
107                 Op [al_expr_op] Term [al_term] Rest [repeat al_expr_extend]
108         construct DRest [repeat d_expr_extend]
109                 _ [alExprExtendToD Rest]
110         replace [repeat d_expr_extend]
111         by
112                 Op Term [alTermToD] DRest
113 end function
114
115 function alExprToD AlExpr [al_expr]
116         deconstruct AlExpr
117                 ALTerm [al_term] AlExprExtend [repeat al_expr_extend]
118         construct DExprExtend [repeat d_expr_extend]
119                 _ [alExprExtendToD AlExprExtend]
120         construct Result [opt d_expr]
121                 ALTerm [alTermToD] DExprExtend
122         replace [opt d_expr]
123         by
124                 Result
125 end function
126
127 function alStmtToD2 AlStmt [action_lang_stmt]
128         deconstruct AlStmt
129                 AlExpr [al_expr] ';
130         construct OptDExpr [opt d_expr]
131                 _ [alExprToD AlExpr]
132         deconstruct OptDExpr
133                 DExpr [d_expr]
134         replace [repeat d_lang_stmt]
135         by
136                 DExpr ';
137 end function
138
139 function alOptElseD AlOptElse [opt al_else]
140         deconstruct AlOptElse
141                 'else 
142                         AlSubStmt [action_lang_stmt]
143         construct AlSubStmts [repeat action_lang_stmt]
144                 AlSubStmt
145         construct DSubStmts [repeat d_lang_stmt]
146                 _ [alToD AlSubStmts]
147         deconstruct DSubStmts
148                 DSubStmt [d_lang_stmt]
149         replace [opt d_else]
150         by
151                 'else 
152                         DSubStmt
153 end function
154
155 function alStmtToD3 AlStmt [action_lang_stmt]
156         deconstruct AlStmt
157                 'if '( AlExpr [al_expr] ')
158                         AlSubStmt [action_lang_stmt]
159                 AlOptElse [opt al_else]
160         construct OptDExpr [opt d_expr]
161                 _ [alExprToD AlExpr]
162         deconstruct OptDExpr
163                 DExpr [d_expr]
164         construct AlSubStmts [repeat action_lang_stmt]
165                 AlSubStmt
166         construct DSubStmts [repeat d_lang_stmt]
167                 _ [alToD AlSubStmts]
168         deconstruct DSubStmts
169                 DSubStmt [d_lang_stmt]
170         construct OptDElse [opt d_else]
171                 _ [alOptElseD AlOptElse]
172         replace [repeat d_lang_stmt]
173         by
174                 'if '( DExpr ')
175                         DSubStmt
176                 OptDElse
177 end function
178
179 function alStmtToD4a AlStmt [action_lang_stmt]
180         deconstruct AlStmt
181                 'printi Id [id] ';
182         replace [repeat d_lang_stmt]
183         by
184                 'writef '( '"%d" ', Id ') ';
185 end function
186
187 function alStmtToD4b AlStmt [action_lang_stmt]
188         deconstruct AlStmt
189                 'prints String [stringlit] ';
190         replace [repeat d_lang_stmt]
191         by
192                 'writef '( '"%s" ', String ') ';
193 end function
194
195 function alStmtToD4c AlStmt [action_lang_stmt]
196         deconstruct AlStmt
197                 'printb Id [id] ';
198         replace [repeat d_lang_stmt]
199         by
200                 '_s '= Id '[0..pos] ';
201                 'writef '( '"%s" ', '_s ') ';
202 end function
203
204 function alStmtToD4d AlStmt [action_lang_stmt]
205         deconstruct AlStmt
206                 'print_token ';
207         replace [repeat d_lang_stmt]
208         by
209                 '_s '= ts '[0..(te-ts)] ';
210                 'writef '( '"%s" ', '_s ') ';
211 end function
212
213 function alStmtToD5 AlStmt [action_lang_stmt]
214         deconstruct AlStmt
215                 '{ AlSubStmts [repeat action_lang_stmt] '}
216         construct DSubStmts [repeat d_lang_stmt]
217                 _ [alToD AlSubStmts]
218         replace [repeat d_lang_stmt]
219         by
220                 '{ DSubStmts '}
221 end function
222
223 function alStmtToD6 AlStmt [action_lang_stmt]
224         deconstruct AlStmt
225                 RagelStmt [al_ragel_stmt]
226         replace [repeat d_lang_stmt]
227         by
228                 RagelStmt
229 end function
230
231 function alToD AlStmts [repeat action_lang_stmt]
232         deconstruct AlStmts
233                 FirstStmt [action_lang_stmt] Rest [repeat action_lang_stmt]
234         construct DFirst [repeat d_lang_stmt]
235                 _ 
236                         [alStmtToD1 FirstStmt]
237                         [alStmtToD2 FirstStmt]
238                         [alStmtToD3 FirstStmt]
239                         [alStmtToD4a FirstStmt]
240                         [alStmtToD4b FirstStmt]
241                         [alStmtToD4c FirstStmt]
242                         [alStmtToD4d FirstStmt]
243                         [alStmtToD5 FirstStmt]
244                         [alStmtToD6 FirstStmt]
245         construct DRest [repeat d_lang_stmt]
246                 _ [alToD Rest]
247         replace [repeat d_lang_stmt]
248         by
249                 DFirst [. DRest]
250 end function
251
252 rule actionTransD
253         replace [al_host_block]
254                 '{ AlStmts [repeat action_lang_stmt] '}
255         construct DStmts [repeat d_lang_stmt]
256                 _ [alToD AlStmts]
257         by
258                 '{ DStmts '}
259 end rule
260
261 function langTransD
262         replace [program]
263                 Definitions [repeat action_lang_stmt]
264                 '%%
265                 Initializations [repeat action_lang_stmt]
266                 RagelDef [ragel_def]
267         construct DDefinitions [repeat d_lang_stmt]
268                 _ [alToD Definitions]
269         construct DInitializations [repeat d_lang_stmt]
270                 _ [alToD Initializations]
271         by
272                 DDefinitions
273                 '%%
274                 DInitializations
275                 RagelDef [actionTransD]
276 end function
277
278 function main
279         replace [program]
280                 P [program]
281         by
282                 P [langTransD]
283 end function