Import from my private repository. Snapshot after version 5.16, immediately
[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 alExprExtendToD AlExprExtend [repeat al_expr_extend]
99         deconstruct AlExprExtend
100                 Op [al_expr_op] Term [al_term] Rest [repeat al_expr_extend]
101         construct DRest [repeat d_expr_extend]
102                 _ [alExprExtendToD Rest]
103         replace [repeat d_expr_extend]
104         by
105                 Op Term DRest
106 end function
107
108 function alExprToD AlExpr [al_expr]
109         deconstruct AlExpr
110                 ALTerm [al_term] AlExprExtend [repeat al_expr_extend]
111         construct DExprExtend [repeat d_expr_extend]
112                 _ [alExprExtendToD AlExprExtend]
113         construct Result [opt d_expr]
114                 ALTerm DExprExtend
115         replace [opt d_expr]
116         by
117                 Result
118 end function
119
120 function alStmtToD2 AlStmt [action_lang_stmt]
121         deconstruct AlStmt
122                 AlExpr [al_expr] ';
123         construct OptDExpr [opt d_expr]
124                 _ [alExprToD AlExpr]
125         deconstruct OptDExpr
126                 DExpr [d_expr]
127         replace [repeat d_lang_stmt]
128         by
129                 DExpr ';
130 end function
131
132 function alOptElseD AlOptElse [opt al_else]
133         deconstruct AlOptElse
134                 'else 
135                         AlSubStmt [action_lang_stmt]
136         construct AlSubStmts [repeat action_lang_stmt]
137                 AlSubStmt
138         construct DSubStmts [repeat d_lang_stmt]
139                 _ [alToD AlSubStmts]
140         deconstruct DSubStmts
141                 DSubStmt [d_lang_stmt]
142         replace [opt d_else]
143         by
144                 'else 
145                         DSubStmt
146 end function
147
148 function alStmtToD3 AlStmt [action_lang_stmt]
149         deconstruct AlStmt
150                 'if '( AlExpr [al_expr] ')
151                         AlSubStmt [action_lang_stmt]
152                 AlOptElse [opt al_else]
153         construct OptDExpr [opt d_expr]
154                 _ [alExprToD AlExpr]
155         deconstruct OptDExpr
156                 DExpr [d_expr]
157         construct AlSubStmts [repeat action_lang_stmt]
158                 AlSubStmt
159         construct DSubStmts [repeat d_lang_stmt]
160                 _ [alToD AlSubStmts]
161         deconstruct DSubStmts
162                 DSubStmt [d_lang_stmt]
163         construct OptDElse [opt d_else]
164                 _ [alOptElseD AlOptElse]
165         replace [repeat d_lang_stmt]
166         by
167                 'if '( DExpr ')
168                         DSubStmt
169                 OptDElse
170 end function
171
172 function alStmtToD4a AlStmt [action_lang_stmt]
173         deconstruct AlStmt
174                 'printi Id [id] ';
175         replace [repeat d_lang_stmt]
176         by
177                 'writef '( '"%d" ', Id ');
178 end function
179
180 function alStmtToD4b AlStmt [action_lang_stmt]
181         deconstruct AlStmt
182                 'prints String [stringlit] ';
183         replace [repeat d_lang_stmt]
184         by
185                 'writef '( '"%s" ', String ');
186 end function
187
188 function alStmtToD5 AlStmt [action_lang_stmt]
189         deconstruct AlStmt
190                 '{ AlSubStmts [repeat action_lang_stmt] '}
191         construct DSubStmts [repeat d_lang_stmt]
192                 _ [alToD AlSubStmts]
193         replace [repeat d_lang_stmt]
194         by
195                 '{ DSubStmts '}
196 end function
197
198 function alStmtToD6 AlStmt [action_lang_stmt]
199         deconstruct AlStmt
200                 RagelStmt [al_ragel_stmt]
201         replace [repeat d_lang_stmt]
202         by
203                 RagelStmt
204 end function
205
206 function alToD AlStmts [repeat action_lang_stmt]
207         deconstruct AlStmts
208                 FirstStmt [action_lang_stmt] Rest [repeat action_lang_stmt]
209         construct DFirst [repeat d_lang_stmt]
210                 _ 
211                         [alStmtToD1 FirstStmt]
212                         [alStmtToD2 FirstStmt]
213                         [alStmtToD3 FirstStmt]
214                         [alStmtToD4a FirstStmt]
215                         [alStmtToD4b FirstStmt]
216                         [alStmtToD5 FirstStmt]
217                         [alStmtToD6 FirstStmt]
218         construct DRest [repeat d_lang_stmt]
219                 _ [alToD Rest]
220         replace [repeat d_lang_stmt]
221         by
222                 DFirst [. DRest]
223 end function
224
225 rule actionTransD
226         replace [al_host_block]
227                 '{ AlStmts [repeat action_lang_stmt] '}
228         construct DStmts [repeat d_lang_stmt]
229                 _ [alToD AlStmts]
230         by
231                 '{ DStmts '}
232 end rule
233
234 function langTransD
235         replace [program]
236                 Definitions [repeat action_lang_stmt]
237                 '%%
238                 Initializations [repeat action_lang_stmt]
239                 RagelDef [ragel_def]
240         construct DDefinitions [repeat d_lang_stmt]
241                 _ [alToD Definitions]
242         construct DInitializations [repeat d_lang_stmt]
243                 _ [alToD Initializations]
244         by
245                 DDefinitions
246                 '%%
247                 DInitializations
248                 RagelDef [actionTransD]
249 end function
250
251 function main
252         replace [program]
253                 P [program]
254         by
255                 P [langTransD]
256 end function