summaryrefslogtreecommitdiff
path: root/test/ragel.d/trans-ocaml.lm
blob: 6cec5ad45d5f92abc95d6f11876d71a1b98621f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
int rw_ocaml_factor( Factor: indep::factor )
{
	switch Factor
	case [`first_token_char] {
		send Out
			"( Char.code data.\[ts.contents\] )"
	}
	case [tk_ident `[ expr `]] {
		send Out
			"[$Factor.tk_ident]\[ [rw_ocaml_expr(Factor.expr)] \]
	}
	case [ `fentry `( E: expr `)] {
		send Out
			"fentry( [E] )"
	}
	case [tk_ident `( expr `)] {
		send Out
			"[$Factor.tk_ident]( [rw_ocaml_expr(Factor.expr)] )
	}
	case [`< type `> `( expr `)] {
		send Out
			"( [rw_ocaml_expr(Factor.expr)] )
	}
	case [`( expr `)] {
		send Out
			"( [rw_ocaml_expr(Factor.expr)] )
	}
	case ['true'] {
		send Out 'true'
	}
	case ['false'] {
		send Out 'false'
	}
	case "'0'" {
		send Out
			"( Char.code '0' )"
	}
	case "'a'" {
		send Out
			"( Char.code 'a' )"
	}
	case [`fc] {
		send Out
			"fc"
	}
	case [tk_ident] {
		send Out
			"[Factor.tk_ident].contents"
	}
	case [`buffer]
	{
		send Out
			"String.sub buffer 0 blen.contents"
	}
	case [`blen]
	{
		send Out
			"blen.contents"
	}
	default {
		send Out
			[Factor]
	}
}

void rw_ocaml_type( Type: indep::type )
{
	if match Type [`int]
	{
		send Out "int"
	}
	elsif match Type [`bool]
	{
		send Out "bool"
	}
	elsif match Type [`char]
	{
		send Out "char"
	}
	elsif match Type [`ptr]
	{
		send Out "char *"
	}
	elsif match Type [`byte]
	{
		send Out "unsigned char"
	}
}

void rw_ocaml_abs_expr( Expr: indep::abs_expr )
{
	if ( Expr.Op ) {
		send Out
			"[rw_ocaml_abs_expr(Expr.E1)] [$Expr.Op] [rw_ocaml_abs_expr( Expr.E2 )]"
	}
	else {
		rw_ocaml_factor( Expr.factor )
	}
}

void rw_ocaml_expr( Expr: indep::expr )
{
	AbsExpr: indep::abs_expr = indep::abs_comparative( Expr.comparative )
	rw_ocaml_abs_expr( AbsExpr )
}

void rw_ocaml_opt_array( OptArr: indep::opt_arr )
{
	if OptArr.expr {
		send Out "\[[rw_ocaml_expr( OptArr.expr )]\]"
	}
}

int rw_ocaml_var_decl( VarDecl: indep::var_decl )
{
	OptArr: indep::opt_arr = VarDecl.opt_arr
	if OptArr.expr {
		send Out
			"let [$VarDecl.tk_ident] = Array.make 32 0
	}
	else if match VarDecl.type [`bool] {
		send Out 
			"let [VarDecl.tk_ident] = ref false
	}
	else {
		send Out
			"let [VarDecl.tk_ident] = ref 0
	}
}

void rw_ocaml_opt_sub( OptSub: indep::opt_sub )
{
	if ( OptSub.expr )
		send Out "\[[rw_ocaml_expr(OptSub.expr)]\]"
}

int rw_ocaml_expr_stmt( ExprStmt: indep::expr_stmt )
{
	if match ExprStmt [tk_ident opt_sub `= expr `;]
	{
		if match ExprStmt.opt_sub [] {
			send Out
				"[$ExprStmt.tk_ident rw_ocaml_opt_sub(ExprStmt.opt_sub)] := [rw_ocaml_expr(ExprStmt.expr)];
		}
		else {
			send Out
				"Array.set [$ExprStmt.tk_ident] [rw_ocaml_expr(ExprStmt.opt_sub.expr)] [rw_ocaml_expr(ExprStmt.expr)];

		}
	}
	else if match ExprStmt [expr `;]
	{
		send Out
			"[rw_ocaml_expr(ExprStmt.expr)];
	}
}

int rw_ocaml_if_stmt( IfStmt: indep::if_stmt )
{
	send Out
		"if [rw_ocaml_expr( IfStmt.expr )] then
		"begin
		"	[rw_ocaml_stmt_list( IfStmt._repeat_stmt )]
		"end 

	if ( IfStmt.opt_else._repeat_stmt ) {
		send Out
			"else
			"begin
			"	[rw_ocaml_stmt_list( IfStmt.opt_else._repeat_stmt )]
			"end
	}
	send Out
		";
}

int rw_ocaml_print_stmt( Stmt: indep::print_stmt )
{
	if match Stmt [`print_int expr `;] {
		send Out
			"print_int( [rw_ocaml_expr(Stmt.expr)] );
	}
	else if match Stmt [`print_buf `;]
	{
		send Out
			"print_string( String.sub buffer 0 blen.contents );
	}
	else if match Stmt [`print_str expr `;]
	{
		send Out
			"print_string( [rw_ocaml_expr( Stmt.expr )] );
	}
	else if match Stmt [`print_token `;]
	{
		send Out
			"for i = ts.contents to te.contents - 1 do print_char data.\[i\] done; "
	}
}

void rw_ocaml_buf_stmt( BufStmt: indep::buf_stmt )
{
	switch BufStmt
	case [`buf_clear `( `) `;] {
		send Out
			"begin
			"	blen := 0;
			"end
	}
	case [`buf_append `( `) `;] {
		send Out
			"	begin
			"	Bytes.set buffer blen.contents data.\[p.contents\];
			"	blen := blen.contents + 1;
			"	end
	}
}

int rw_ocaml_ragel_stmt( Stmt: indep::ragel_stmt )
{
	switch Stmt
	case [`fnext `* E: expr `;] {
		send Out
			"fnext *[rw_ocaml_expr(E)];
	}
	case [`fncall `* E: expr `;] {
		send Out
			"fncall *[rw_ocaml_expr(E)];
	}
	default {
		send Out
			[Stmt]
	}
}

int rw_ocaml_stmt( Stmt: indep::stmt )
{
	if match Stmt [var_decl]
		rw_ocaml_var_decl( Stmt.var_decl )
	else if match Stmt [expr_stmt]
		rw_ocaml_expr_stmt( Stmt.expr_stmt )
	else if match Stmt [if_stmt]
		rw_ocaml_if_stmt( Stmt.if_stmt )
	else if match Stmt [print_stmt]
		rw_ocaml_print_stmt( Stmt.print_stmt )
	else if match Stmt [buf_stmt]
		rw_ocaml_buf_stmt( Stmt.buf_stmt )
	else if	match Stmt [ragel_stmt]
		rw_ocaml_ragel_stmt( Stmt.ragel_stmt )
}

void rw_ocaml_stmt_list( StmtList: indep::stmt* )
{
	for Stmt: indep::stmt in repeat( StmtList )
		rw_ocaml_stmt( Stmt )
}

int rw_ocaml_action_block( ActionBlock: indep::action_block )
{
	Out = new parser<out_code::lines>()
	if match ActionBlock [`{ stmt* `}] {
		send Out
			"{[rw_ocaml_stmt_list( ActionBlock._repeat_stmt )]}
	}
	else if match ActionBlock [`{ expr `}] {
		send Out
			"{[rw_ocaml_expr( ActionBlock.expr )]}
	}
	send Out [] eos
}

void rw_ocaml( Output: stream )
{
	send Output
		"(*
		" * @LANG: ocaml
		" * @GENERATED: true

	if ProhibitGenflags {
		send Output
			" * @PROHIBIT_GENFLAGS:[ProhibitGenflags]
	}

	send Output
		" *)
		"

	Init: indep::stmt* = RagelTree.Init
	for Stmt: indep::stmt in Init {
		if match Stmt [Decl: var_decl] {
			Out = new parser<out_code::lines>()
			rw_ocaml_var_decl( Decl )
			send Out [] eos
			send Output [Out->tree]
		}
	}

	Section: indep::section = RagelTree.section
	for Action: ragel::action_block in Section {
		# Reparse as lang-independent code.
		parse IndepActionBlock: indep::action_block[$Action]
		if ( !IndepActionBlock ) {
			print( "error parsing indep action block: ", error, '\n', Action )
			exit(1)
		}

		rw_ocaml_action_block( IndepActionBlock )

		# Reparse back to ragel action block.
		Action = parse ragel::action_block[$Out->tree]
		if ( !Action ) {
			print( "error parsing ragel action block: ", error, '\n', $Out->tree )
			exit(1)
		}
	}

	send Output
		"
		"[Section]
		"
		"
		"%% write data;
		"
		"let exec data = 
		"  let buffer = Bytes.create(1024) in 
		"  let blen :int ref = ref 0 in
		"  let cs = ref 0 in
		"  let p = ref 0 in
		"  let pe = ref (String.length data) in
		"  let nfa_len = ref 0 in
		"  let nfa_count = ref 0 in
		"  let nfa_bp_state = Array.make 20 0 in
		"  let nfa_bp_p = Array.make 20 0 in

	if NeedsEof {
		send Output
			"	let eof = pe in
	}


	for Stmt: indep::stmt in Init {
		if match Stmt [ExprStmt: expr_stmt] {
			Out = new parser<out_code::lines>()
			rw_ocaml_expr_stmt( ExprStmt )
			send Out [] eos
			send Output [Out->tree]
		}
	}

	send Output
		"	%% write init;
		"	%% write exec;
		"	if !cs >= [MachineName.mn_word]_first_final then
		"		print_string \"ACCEPT\\n\"
		"	else
		"		print_string \"FAIL\\n\"
		";;
		"

	send Output
		"let () =

	for InputString: indep::input_string in RagelTree {
		send Output 
			"	exec [^InputString];
	}

	send Output
		"  ()
		";;
		"
}