summaryrefslogtreecommitdiff
path: root/test/ragel.d/trans-julia.lm
blob: cf153c640e2e2c5b4ef6da2577a81617da63a259 (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
namespace trans_julia

int factor( Factor: indep::factor )
{
	if match Factor [`first_token_char]
	{
		send Out "data\[ts+1\]"
	}
	else if match Factor [tk_ident `[ expr `]]
	{
		send Out
			"[$Factor.tk_ident]\[ [expr(Factor.expr)] \]
	}
	else if match Factor [tk_ident `( expr `)]
	{
		send Out
			"[$Factor.tk_ident]( [expr(Factor.expr)] )
	}
	elsif match Factor [`< type `> `( expr `)]
	{
		send Out
			"convert( [type(Factor.type)], ( [expr(Factor.expr)] ) ) 
	}
	elsif match Factor [`( expr `)]
	{
		send Out
			"( [expr(Factor.expr)] )
	}
	elsif match Factor ['true']
	{
		send Out 'true'
	}
	elsif match Factor ['false']
	{
		send Out 'false'
	}
	elsif match Factor [`buffer]
	{
		send Out
			"buffer"
	}
	elsif match Factor [`blen]
	{
		send Out
			"length(buffer)"
	}
	else 
	{
		send Out [$Factor]
	}
}

void type( Type: indep::type )
{
	switch Type
	case [`int]
	{
		send Out "Int"
	}
	case [`bool]
	{
		send Out "Int8"
	}
	case [`char]
	{
		send Out "Uint8"
	}
	case [`ptr]
	{
		send Out "i32
	}
	case [`byte]
	{
		send Out "unsigned char"
	}
}

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

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

void array_init( Size: int )
{
	while ( Size > 1 ) {
		send Out
			"0, "
		Size = Size - 1
	}

	if ( Size > 0 ) {
		send Out
			"0"
	}
}

int var_decl( VarDecl: indep::var_decl )
{
	if VarDecl.opt_arr.expr {
		send Out
			"[VarDecl.tk_ident] = [type( VarDecl.type )]"
				"\[ [array_init( atoi($VarDecl.opt_arr.expr) )] \];
	}
	else {
		send Out
			"[VarDecl.tk_ident] = 0;
	}
}

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

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

int if_stmt( IfStmt: indep::if_stmt )
{
	send Out
		"if ( [expr( IfStmt.expr )] )
		"	[stmt_list( IfStmt._repeat_stmt )]

	if ( IfStmt.opt_else._repeat_stmt ) {
		send Out
			"else
			"	[stmt_list( IfStmt.opt_else._repeat_stmt )]
	}

	send Out
		"end
}

int print_stmt( Stmt: indep::print_stmt )
{
	switch Stmt
	case [`print_int expr `;] {
		send Out
			"print( [expr(Stmt.expr)] );
	}
	case [`print_buf `;]
	{
		send Out
			"print( buffer );
	}
	case [`print_str expr `;]
	{
		send Out
			"print( [expr( Stmt.expr )] );
	}
	case [`print_token `;]
	{
		send Out
			"print( String( data\[(ts+1) : (te)\] ) );
	}
	case [`print_off `;]
	{
		send Out
			"print( p );" 
	}
}

void buf_stmt( BufStmt: indep::buf_stmt )
{
	switch BufStmt
	case [`buf_clear `( `) `;] {
		send Out
			"	buffer = \"\";
	}
	case [`buf_append `( `) `;] {
		send Out
			"	buffer = buffer * String(Char\[fc\]);
	}
}


int ragel_stmt( Stmt: indep::ragel_stmt )
{
	send Out
		[$Stmt]
}

int stmt( Stmt: indep::stmt )
{
	switch Stmt 
	case [var_decl]
		var_decl( Stmt.var_decl )
	case [expr_stmt]
		expr_stmt( Stmt.expr_stmt )
	case [if_stmt]
		if_stmt( Stmt.if_stmt )
	case [print_stmt]
		print_stmt( Stmt.print_stmt )
	case [buf_stmt]
		buf_stmt( Stmt.buf_stmt )
	case [ragel_stmt]
		ragel_stmt( Stmt.ragel_stmt )
}

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

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

void julia( Output: stream )
{
	# Translate action blocks.
	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, '\n', Action )
			exit(1)
		}

		action_block( IndepActionBlock )

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

	send Output
		"//
		"// @LANG: julia
		"// @GENERATED: true

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

	send Output
		"//
		"


	send Output
		"
		"[Section]
		"
		"%% write data;
		"
		"function m( data_string::AbstractString )
		"	data = Vector{UInt8}( data_string )
		"	p = 0
		"	pe = length(data)
		"	eof = length(data)
		"	cs = 0
		"	buffer = \"\"
		"	nfa_len = 0
		"	nfa_count = 0
		"	nfa_bp_state = Array{Int, 1}(undef, 20)
		"	nfa_bp_p = Array{Int, 1}(undef, 20)

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

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

	send Output
		"
		"	%% write init;
		"	%% write exec;
		"
		"	if ( cs >= [MachineName.mn_word]_first_final )
		"		println( \"ACCEPT\" );
		"	else
		"		println( \"FAIL\" );
		"	end
		"end



	send Output
		~

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

}

end