-
Notifications
You must be signed in to change notification settings - Fork 1
/
rcl_parser.rb
582 lines (447 loc) · 7.83 KB
/
rcl_parser.rb
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
require "json"
require "pp"
require_relative "common"
$tokens = nil
$pos = nil
$strings = []
def read_tokens(src)
tokens = []
src.each_line do |line|
token = Token.from_line(line)
tokens << token unless token.nil?
end
tokens
end
# --------------------------------
class ParseError < StandardError; end
def rest_head
$tokens[$pos ... $pos + 8]
.map { |t| format("%s<%s>", t.kind, t.value) }
end
def peek(offset = 0)
$tokens[$pos + offset]
end
def dump_state(msg = nil)
pp_e [
msg,
$pos,
rest_head
]
end
def assert_value(pos, exp)
t = peek()
if t.value != exp
msg = format(
"Assertion failed: expected(%s) actual(%s)",
exp.inspect,
t.inspect
)
raise ParseError, msg
end
end
def consume(str)
assert_value($pos, str)
$pos += 1
end
def end?
$tokens.size <= $pos
end
# --------------------------------
def _parse_arg
t = peek()
unless t.kind == :ident
raise ParseError, t
end
$pos += 1
t.value
end
def parse_args
args = []
if peek().value == ")"
return args
end
args << _parse_arg()
while peek().value == ","
consume ","
args << _parse_arg()
end
args
end
# --------------------------------
def parse_exprs
exprs = []
if peek().value == ")"
return exprs
end
exprs << parse_expr()
while peek().value == ","
consume ","
exprs << parse_expr()
end
exprs
end
# --------------------------------
def _parse_var_declare
t = peek()
$pos += 1
var_name = t.value
consume ";"
[:var, var_name]
end
def _parse_var_init
t = peek()
$pos += 1
var_name = t.value
consume "="
expr = parse_expr()
consume ";"
[:var, var_name, expr]
end
def parse_var_array
consume "["
t = peek()
$pos += 1
size = t.value.to_i
consume "]"
var_name = peek().value
$pos += 1
consume ";"
[:var_array, var_name, size]
end
def parse_var
consume "var"
if peek().value == "["
parse_var_array()
else
t = peek(1)
case t.value
when ";" then _parse_var_declare()
when "=" then _parse_var_init()
else
raise ParseError, t
end
end
end
def parse_expr_addr
consume "&"
var_name = peek().value
$pos += 1
[:addr, var_name]
end
def parse_deref
consume "*"
consume "("
expr = parse_expr()
consume ")"
[:deref, expr]
end
def binary_op?(t)
%(+ * == != <).include?(t.value)
end
def _parse_expr_factor_int
t = peek()
$pos += 1
t.value.to_i
end
def _parse_expr_factor_ident
if peek(1).value == "("
fn_name, *args = parse_funcall()
[:funcall, fn_name, *args]
else
t = peek()
$pos += 1
t.value
end
end
def _parse_expr_factor_sym
case peek().value
when "("
consume "("
expr = parse_expr()
consume ")"
expr
when "&"
parse_expr_addr()
when "*"
parse_deref()
else
raise "unexpected token (#{peek()})"
end
end
def _parse_expr_factor_kw
t = peek()
case t.value
when "true", "false"
$pos += 1
t.value
else
raise "unexpected token value (#{t})"
end
end
def _parse_expr_factor_str
t = peek()
$pos += 1
offset = nil
if $strings.any? { |s, _| s == t.value }
_, offset = $strings.find { |s, _| s == t.value }
else
offset = $strings.map { |s, _| s.bytesize + 1 }.sum
$strings << [t.value, offset]
end
# g_main_ + GO_STRINGS() + offset
[
:+,
[
:+,
"g_main_",
[:funcall, "GO_STRINGS"]
],
offset
]
end
def _parse_expr_factor
t = peek()
case t.kind
when :int then _parse_expr_factor_int()
when :ident then _parse_expr_factor_ident()
when :sym then _parse_expr_factor_sym()
when :kw then _parse_expr_factor_kw()
when :str then _parse_expr_factor_str()
else
raise ParseError, t
end
end
def parse_expr
expr = _parse_expr_factor()
while binary_op?(peek())
op = peek().value
$pos += 1
factor = _parse_expr_factor()
expr = [op.to_sym, expr, factor]
end
expr
end
def parse_set
if peek().value == "*"
var_name = parse_deref()
else
t = peek()
$pos += 1
var_name = t.value
end
consume "="
expr = parse_expr()
consume ";"
[:set, var_name, expr]
end
def parse_funcall
t = peek()
$pos += 1
func_name = t.value
consume "("
args = parse_exprs()
consume ")"
[func_name, *args]
end
def parse_call
funcall = parse_funcall()
consume ";"
[:call, *funcall]
end
def parse_return
consume "return"
t = peek()
if t.value == ";"
consume ";"
[:return]
else
expr = parse_expr()
consume ";"
[:return, expr]
end
end
def _parse_when_clause
t = peek()
case t.value
when "when"
consume "when"
consume "("
expr = parse_expr()
consume ")"
when "else"
consume "else"
expr = 1 # true
else
raise not_yet_impl("t", t)
end
stmts = parse_stmts()
[expr, *stmts]
end
def parse_case
consume "case"
when_clauses = []
while peek().value != "end"
when_clauses << _parse_when_clause()
end
consume "end"
[:case, *when_clauses]
end
def parse_while
consume "while"
consume "("
expr = parse_expr()
consume ")"
stmts = parse_stmts()
consume "end"
[:while, expr, stmts]
end
def parse_break
consume "break"
consume ";"
[:break]
end
def parse_vm_comment
consume "_cmt"
consume "("
t = peek()
$pos += 1
comment = t.value
consume ")"
consume ";"
[:_cmt, comment]
end
def parse_vm_debug
consume "_debug"
consume "("
consume ")"
consume ";"
[:call, "_debug"]
end
def parse_vm_panic
consume "_panic"
consume "("
consume ")"
consume ";"
[:call, "_panic"]
end
def parse_stmt
t = peek()
case t.value
when "return" then parse_return()
when "while" then parse_while()
when "break" then parse_break()
when "case" then parse_case()
when "_cmt" then parse_vm_comment()
when "_debug" then parse_vm_debug()
when "_panic" then parse_vm_panic()
else
if t.kind == :ident && peek(1).is(:sym, "(")
parse_call()
else
parse_set()
end
end
end
def parse_stmts
stmts = []
until (
peek().value == "end" ||
peek().value == "when" ||
peek().value == "else"
)
stmts << parse_stmt()
end
stmts
end
def parse_func
consume "def"
t = peek()
$pos += 1
func_name = t.value
consume "("
args = parse_args()
consume ")"
stmts = []
while peek().value != "end"
stmts <<
if peek().value == "var"
parse_var()
else
parse_stmt()
end
end
consume "end"
[:func, func_name, args, stmts]
end
def parse_global()
consume "global"
name = peek().value
$pos += 1
consume ";"
[:gvar, name]
end
def parse_top_stmt
t = peek()
case t.value
when "def" then parse_func()
when "global" then parse_global()
else
raise ParseError, "Unexpected token (#{t.inspect})"
end
end
def parse_top_stmts
stmts = []
until end?()
stmts << parse_top_stmt()
end
stmts
end
def parse
top_stmts = parse_top_stmts()
[:top_stmts, *top_stmts]
end
# --------------------------------
# *(offset + bi) = {byte.to_i};
def make_set_byte_stmt(bi, byte)
[
:set,
[:deref, [:+, "offset_", bi]],
byte.to_i
]
end
# def init_strings(g_)
# var offset_ = g_main_ + GO_STRINGS();
# *(offset_ + 0) = 97;
# *(offset_ + 1) = 98;
# *(offset_ + 2) = 0;
# # ...
# end
def make_init_strings_fn
stmts = [
[:var, "offset_", [:+, "g_main_", [:funcall, "GO_STRINGS"]]]
]
bi = 0
$strings.each { |str, _|
str.each_byte { |byte|
stmts << make_set_byte_stmt(bi, byte)
bi += 1
}
stmts << make_set_byte_stmt(bi, 0)
bi += 1
}
[:func, "init_strings", [], stmts]
end
# --------------------------------
in_file = ARGV[0]
$tokens = read_tokens(File.read(in_file))
$pos = 0
begin
tree = parse()
rescue ParseError => e
dump_state()
raise e
end
unless $strings.empty?
tree << make_init_strings_fn()
end
puts JSON.pretty_generate(tree)