summaryrefslogtreecommitdiff
path: root/test/trans.d/trans-go.lm
blob: 30a5600fd68e23484599c5fda95e0a2a7fe1c776 (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
int rw_go_factor( Factor: indep::factor )
{
	if match Factor [`first_token_char]
	{
		send Out "data\[ts\]"
	}
	else if match Factor [tk_ident `[ expr `]]
	{
		send Out
			"[$Factor.tk_ident]\[ [rw_go_expr(Factor.expr)] \]
	}
	else if match Factor [tk_ident `( expr `)]
	{
		send Out
			"[$Factor.tk_ident]( [rw_go_expr(Factor.expr)] )
	}
	elsif match Factor [`< type `> `( expr `)]
	{
		send Out
			"( [rw_go_type(Factor.type)] ) ( [rw_go_expr(Factor.expr)] )
	}
	elsif match Factor [`( expr `)]
	{
		send Out
			"( [rw_go_expr(Factor.expr)] )
	}
	elsif match Factor ['true']
	{
		send Out '1'
	}
	elsif match Factor ['false']
	{
		send Out '0'
	}
	elsif match Factor [`buffer]
	{
		send Out
			"string ( buffer\[:blen\] )"
	}
	else
	{
		send Out [$Factor]
	}
}

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

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

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

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

int rw_go_var_decl( VarDecl: indep::var_decl )
{
	send Out
		"var [$VarDecl.tk_ident] [rw_go_opt_array(VarDecl.opt_arr)] [rw_go_type( VarDecl.type )] ;
}

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

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

int rw_go_if_stmt( IfStmt: indep::if_stmt )
{
	send Out
		"if ( [rw_go_expr( IfStmt.expr )] ) {
		"	[rw_go_stmt_list( IfStmt._repeat_stmt )]
		"}"

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

	send Out "
}

int rw_go_print_stmt( Stmt: indep::print_stmt )
{
	if match Stmt [`print_int expr `;] {
		send Out
			"fmt.Print( [rw_go_expr(Stmt.expr)] );"
	}
	else if match Stmt [`print_buf E1: expr `, E2: expr `;]
	{
		send Out
			"fmt.Print( string ( [rw_go_expr(E1)]\[:[rw_go_expr(E2)]\] ));"
	}
	else if match Stmt [`print_str expr `;]
	{
		send Out
			"fmt.Print( [rw_go_expr( Stmt.expr )] );"
	}
	else if match Stmt [`print_token `;]
	{
		send Out
			"fmt.Print( data\[ts:te\] );" 
	}
}

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

void rw_go_buf_stmt( BufStmt: indep::buf_stmt )
{
	switch BufStmt
	case [`buf_clear `( `) `;] {
		send Out
			"	blen = 0;
	}
	case [`buf_append `( `) `;] {
		send Out
			"	buffer\[blen\] = fc;
			"	blen += 1;
			"	buffer\[blen\] = 0;
	}
}


int rw_go_stmt( Stmt: indep::stmt )
{
	if match Stmt [var_decl]
		rw_go_var_decl( Stmt.var_decl )
	else if match Stmt [expr_stmt]
		rw_go_expr_stmt( Stmt.expr_stmt )
	else if match Stmt [if_stmt]
		rw_go_if_stmt( Stmt.if_stmt )
	else if match Stmt [print_stmt]
		rw_go_print_stmt( Stmt.print_stmt )
	else if match Stmt [buf_stmt]
		rw_go_buf_stmt( Stmt.buf_stmt )
	else if	match Stmt [ragel_stmt]
		rw_go_ragel_stmt( Stmt.ragel_stmt )
}

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

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

void rw_go( Output: stream )
{
	send Output
		"/*
		" * @LANG: go

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

	send Output
		" * @GENERATED: true
		" */
		"
		"package main
		"import \"fmt\"
		"

	Init: indep::stmt* = RagelTree.Init
	for Stmt: indep::stmt in Init {
		if match Stmt [Decl: var_decl] {
			Out = new parser<out_code::lines>()
			rw_go_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]

		# Translate to specific language.
		rw_go_action_block( IndepActionBlock )

		# Reparse back to ragel action block.
		Action = parse ragel::action_block[$Out->tree]
	}

	send Output ['\n' @Section '\n']

	send Output
		"var cs int;
		"var blen int;
		"var buffer \[1024\] byte;
		"
		"%% write data;
		"
		"func prepare() {

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

	send Output
		"	%% write init;
		"}
		"
		"func exec(data string) {
		"	var p int = 0
		"	var pe int = len(data)

	if NeedsEof {
		send Output
			"	var eof int = pe
	}

	send Output
		"	%% write exec;
		"}
		"func finish() {
		"	if cs >= [MachineName.word]_first_final {
		"		fmt.Println(\"ACCEPT\")
		"	} else {
		"		fmt.Println(\"FAIL\")
		"	}
		"}

	send Output
		~var inp []string = []string {

	for InputString: indep::input_string in RagelTree {
		send Output [^InputString ",\n"]
	}

	send Output
		"};
		"

	send Output
		"func main() {
		"	for _, data := range inp {
		"		prepare()
		"		exec(data)
		"		finish()
		"	}
		"}
}