summaryrefslogtreecommitdiff
path: root/test/trans.d/trans.lm
blob: b9fa5870c8f10f1c8d3d00605be118249fe00b2b (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
include 'ragel.lm'
include 'ragel-c.lm'
include 'ragel-ocaml.lm'
include 'ragel-ruby.lm'
include 'ragel-crack.lm'

namespace indep
	# Opening the test case header.
	lex
		ignore / ( [ \t] | NL )+ /

		token c_comm_open
			/ '/*' /
	end

	# Contents of test case heaer.
	lex
		token comm_name /'@' [A-Za-z0-9_]+ ':' /
		token comm_term /'*/'/
		token comm_any / any /
	end

	token comm_val /[^\n]* '\n' /

	lex 
		literal `%%{ `}%%

		literal `int `bool `char `ptr `byte `if `else
				`print_int `print_buf `print_str `print_token
				`first_token_char `buffer `blen `buf_append `buf_clear

		literal `fpc `fc `fcurs `ftargs `fentry
				`fhold `fexec `fgoto `fnext `fcall
				`fret `fbreak `fncall `fnret `fnbreak

		literal `; `< `> `( `) `[ `] `=
				`* `! `{ `} `+ `- `== `!=
				`>= `<= `,

		ignore / ( [ \t] | NL )+ /

		token tk_ident /ident/
		token tk_number /digit+/
		token tk_hex_number /'0x' [0-9a-fA-F]+/

		rl INPUT /'INPUT'/
		rl OUTPUT /'OUTPUT'/

		token sect_INPUT
			/ '#'+ ' '* INPUT ' '* '#'+ '\n' /

		token sect_OUTPUT
			/ '#'+ ' '* OUTPUT ' '* '#'+ '\n' / -ni

		token string
			/ s_literal | d_literal /

		ignore / cpp_comment /
		ignore / c_comment /
	end

	lex
		token output_line /[^\n]* '\n'/
	end

	def comm_def
		[comm_name comm_val]

	def comm_item
		[comm_def]
	|	[comm_any]

	def test_header
		[c_comm_open comm_item* comm_term]

	def input_string
		[string]

	def input_list
		[input_list input_string]
	| 	[input_string]

	def input
		[input_list]

	def output
		[output_line*]

	def factor
		[`first_token_char]
	|	[`buffer]
	|	[`blen]
	|	[tk_ident]
	|	[tk_ident `[ expr `]]
	|	[tk_ident `( expr `)]
	|	[tk_number]
	|	[`- tk_number]
	|	[tk_hex_number]
	|	[string]
	|	[`< type `> `( expr `)]
	|	[`( expr `)]
	|	[ragel_factor]

	def ragel_factor
		[`fentry `( expr `)]
	|	[`fc]
	|	[`fcurs]
	|	[`ftargs]

	def mult_op
		[`*]

	def add_op
		[`+]
	|	[`-]

	def cmp_op
		[`<]
	|	[`>]
	|	[`!=]
	|	[`==]
	|	[`<=]
	|	[`>=]

	def abs_op
		[mult_op]
	|	[add_op]
	|	[cmp_op]

	abs_expr abs_multiplicative( Expr: indep::multiplicative )
	{
		if ( !Expr.Op ) {
			return cons abs_expr [Expr.factor]
		}
		else {
			return cons abs_expr [
				abs_multiplicative( Expr._multiplicative )
				@Expr.Op
				Expr.factor
			]
		}
	}

	abs_expr abs_additive( Expr: indep::additive )
	{
		if ( !Expr.Op ) {
			return cons abs_expr [
				abs_multiplicative( Expr.multiplicative )
			]
		}
		else {
			return cons abs_expr [
				abs_additive( Expr._additive )
				@Expr.Op
				abs_multiplicative( Expr.multiplicative )
			]
		}
	}

	abs_expr abs_comparative( Expr: indep::comparative )
	{
		if ( !Expr.Op ) {
			return cons abs_expr [
				abs_additive( Expr.additive )
			]
		}
		else {
			return cons abs_expr [
				abs_comparative( Expr._comparative )
				@Expr.Op
				abs_additive( Expr.additive )
			]
		}
	}

	def multiplicative
		[multiplicative Op: mult_op factor]
	|	[factor]

	def additive
		[additive Op: add_op multiplicative]
	|	[multiplicative]

	def comparative
		[comparative Op: cmp_op additive]
	|	[additive]

	def abs_expr
		[E1: abs_expr Op: abs_op E2: abs_expr]
	|	[factor]

	def expr
		[comparative]

	def type
		[`int]
	|	[`bool]
	|	[`char]
	|	[`ptr]
	|	[`byte]

	def opt_arr
		[`[ expr `]]
	|	[]

	def var_decl
		[type tk_ident opt_arr `;]

	def opt_sub
		[ `[ expr `] ]
	|	[]

	def expr_stmt
		[tk_ident opt_sub `= expr `;]
	|	[expr `;]

	def if_stmt
		[`if `( expr `) `{ stmt* `} opt_else]

	def opt_else
		[`else `{ stmt* `}]
	|	[]

	def print_stmt
		[`print_int expr `;]
	|	[`print_buf expr `, expr `;]
	|	[`print_str expr `;]
	|	[`print_token `;]

	def ragel_stmt
		[`fgoto tk_ident `;]
	|	[`fcall tk_ident `;]
	|	[`fncall tk_ident `;]
	|	[`fnext tk_ident `;]
	|	[`fgoto `* expr `;]
	|	[`fcall `* expr `;]
	|	[`fncall `* expr `;]
	|	[`fnext `* expr `;]
	|	[`fexec expr `;]
	|	[`fhold `;]
	|	[`fbreak `;]
	|	[`fnbreak `;]
	|	[`fret `;]
	|	[`fnret `;]

	def buf_stmt
		[`buf_append `( `) `;]
	|	[`buf_clear `( `) `;]

	def stmt
		[var_decl]
	|	[expr_stmt]
	|	[if_stmt]
	|	[print_stmt]
	|	[buf_stmt]
	|	[ragel_stmt]

	def action_block
		[`{ stmt* `}]
	|	[`{ expr `}]

	def section
		[`%%{ ragel::ragel_start `}%%]

	def start
		[
			test_header
			Init: stmt*
			section
			sect_INPUT
			input
			sect_OUTPUT
			output
		]
end

namespace out_code
	lex
		token line
			/[^\n]* '\n'/
	end

	def lines
		[line*]

	alias line_parser
		parser<lines>
end

global Out: parser<out_code::lines>

include 'trans-c.lm'
include 'trans-asm.lm'
include 'trans-d.lm'
include 'trans-csharp.lm'
include 'trans-go.lm'
include 'trans-java.lm'
include 'trans-ruby.lm'
include 'trans-ocaml.lm'
include 'trans-rust.lm'
include 'trans-julia.lm'
include 'trans-crack.lm'

str argvPop()
{
	AE: list_el<str> = argv->pop_el()
	return AE->value
}

Lang: str = argvPop()
OutputFile: str = argvPop()
InputFile: str = argvPop()
global ClassName: str = argvPop()

Input: stream = open( InputFile, "r" )
Output: stream = open( OutputFile, "w" )

global RagelTree: indep::start = parse indep::start[ Input ]

if ( !RagelTree ) {
	print( error, '\n' )
	exit(1)
}

# Find the machine name.
global MachineName: ragel::machine_name = ragel::machine_name in RagelTree

global NeedsEof: bool = false
global ProhibitGenflags: str
for CommDef: indep::comm_def in RagelTree {
	switch CommDef
	case "@NEEDS_EOF: yes\n"
	{
		NeedsEof = true
	}
	case "@PROHIBIT_GENFLAGS:[CV: comm_val]"
	{
		ProhibitGenflags = $CV
	}
}

if ( Lang == 'c' )
	rw_c( Output )
elsif ( Lang == 'asm' )
	rw_asm( Output )
elsif ( Lang == 'd' )
	rw_d( Output )
elsif ( Lang == 'cs' )
	rw_csharp( Output )
elsif ( Lang == 'go' )
	rw_go( Output )
elsif ( Lang == 'java' )
	rw_java( Output )
elsif ( Lang == 'ruby' )
	rw_ruby( Output )
elsif ( Lang == 'ocaml' )
	rw_ocaml( Output )
elsif ( Lang == 'rust' )
	trans_rust::rust( Output )
elsif ( Lang == 'julia' )
	trans_julia::julia( Output )
elsif ( Lang == 'crack' )
	trans_crack::crack( Output )
else {
	print( 'trans: unrecognized language: ', Lang, '\n' )
}