Added clang4, a new language independent test case derived from clang1. Also
[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 alStmtToD4c AlStmt [action_lang_stmt]
189         deconstruct AlStmt
190                 'printb Id [id] ';
191         replace [repeat d_lang_stmt]
192         by
193                 '_s '= Id '[0..pos] ';
194                 'writef '( '"%s" ', '_s ') ';
195 end function
196
197 function alStmtToD5 AlStmt [action_lang_stmt]
198         deconstruct AlStmt
199                 '{ AlSubStmts [repeat action_lang_stmt] '}
200         construct DSubStmts [repeat d_lang_stmt]
201                 _ [alToD AlSubStmts]
202         replace [repeat d_lang_stmt]
203         by
204                 '{ DSubStmts '}
205 end function
206
207 function alStmtToD6 AlStmt [action_lang_stmt]
208         deconstruct AlStmt
209                 RagelStmt [al_ragel_stmt]
210         replace [repeat d_lang_stmt]
211         by
212                 RagelStmt
213 end function
214
215 function alToD AlStmts [repeat action_lang_stmt]
216         deconstruct AlStmts
217                 FirstStmt [action_lang_stmt] Rest [repeat action_lang_stmt]
218         construct DFirst [repeat d_lang_stmt]
219                 _ 
220                         [alStmtToD1 FirstStmt]
221                         [alStmtToD2 FirstStmt]
222                         [alStmtToD3 FirstStmt]
223                         [alStmtToD4a FirstStmt]
224                         [alStmtToD4b FirstStmt]
225                         [alStmtToD4c FirstStmt]
226                         [alStmtToD5 FirstStmt]
227                         [alStmtToD6 FirstStmt]
228         construct DRest [repeat d_lang_stmt]
229                 _ [alToD Rest]
230         replace [repeat d_lang_stmt]
231         by
232                 DFirst [. DRest]
233 end function
234
235 rule actionTransD
236         replace [al_host_block]
237                 '{ AlStmts [repeat action_lang_stmt] '}
238         construct DStmts [repeat d_lang_stmt]
239                 _ [alToD AlStmts]
240         by
241                 '{ DStmts '}
242 end rule
243
244 function langTransD
245         replace [program]
246                 Definitions [repeat action_lang_stmt]
247                 '%%
248                 Initializations [repeat action_lang_stmt]
249                 RagelDef [ragel_def]
250         construct DDefinitions [repeat d_lang_stmt]
251                 _ [alToD Definitions]
252         construct DInitializations [repeat d_lang_stmt]
253                 _ [alToD Initializations]
254         by
255                 DDefinitions
256                 '%%
257                 DInitializations
258                 RagelDef [actionTransD]
259 end function
260
261 function main
262         replace [program]
263                 P [program]
264         by
265                 P [langTransD]
266 end function