-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
766 lines (653 loc) · 24.3 KB
/
parser.py
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
import re
from pprint import pprint
from typing import List
from sly import Lexer, Parser
import ir
"""
"""
"""
Rust MIR simplified grammar for borrow-checking and CFG generation
<mir> ::= <function>*
<function> ::= FN <name> TO "(" <param> ")" "{" <block>+ "}"
<param> ::= (<name> ("," <name>)*)+
<block> ::= <name> ":" "{" <statement>* "}"
?<statements> ::= <statement> <statements> | <statement>
<statement> ::= <assignment> | <borrow> | <mut_borrow> | <return> | <call>
<assignment> ::= <expr> ";"
<borrow> ::= "&" <expr> ";"
<mut_borrow> ::= "&mut" <expr> ";"
<return> ::= RETURN ";"
<call> ::= <name> "(" <expr> ")" TO <expr> ;"
<expr> ::= <name> | <literal>
<name> ::= [a-zA-Z_][a-zA-Z0-9_]*
<literal> ::= [0-9]+
mir -> mir_function
mir_function -> FN name function_args ARROW "(" function_type ")" "{" function_body bblist "}"
function_args -> fuck
"""
# noinspection PyUnresolvedReferences,PyUnboundLocalVariable
class MirLexer(Lexer):
# Tokens
literals = {'+', '-', '*', '/', '(', ')', '=', ':', ';', ',', '.', '[', ']', '{', '}', '_', '<', '>'}
tokens = {
LOCATION,
FN,
NAME,
MUT,
LET,
LETMUT,
REF,
REFMUT,
BB,
RETURN,
DEREF,
TYPENAMES,
METHODNAMES,
CONST,
MODE,
NUMBER,
UNREACHABLE,
GOTO,
ARROW,
COLONTWICE,
STRING,
OTHERWISE,
PRIMITIVES,
AS,
SCOPE,
DEBUG,
THICKARROW,
ASSERT,
TRUE,
FALSE,
}
ignore = ' \t'
# ignore // comments
ignore_comment = r'//.*'
SCOPE = r'scope'
DEBUG = r'debug'
THICKARROW = r'=>'
# Tokens
LOCATION = r'\_\d+'
FN = r'fn'
LET = r'let'
LETMUT = r'letmut'
BB = r'bb\d+'
ARROW = r'->'
GOTO = r'goto'
COLONTWICE = r'::'
MUT = r'mut'
REFMUT = r'&mut'
REF = r'&'
DEREF = r'\*'
# s = ["Hashmap", "u32", "String", "string", "str", "Some", "std", "Index"]
# m = ["insert", "get", "index", "from", "discriminant", "switchInt"]
# stucts -> '|'.join(s) + '|'.join(['&'+n for n in s]) + '|'.join(['&mut'+n for n in s])
# methods -> '|'.join(m)
TYPENAMES = r'|'.join(
[
r'i32',
r'u32',
r'i64',
r'u64',
r'f32',
r'f64',
r'isize',
r'bool',
r'char',
r'string',
r'String',
r'str',
r'Some',
r'HashMap',
r'Index',
r'From',
r'discriminant',
r'CheckedAdd',
r'CheckedSub',
r'CheckedMul',
r'CheckedDiv',
r'std',
r'Thing',
r'Eq',
r'BitAnd',
r'Rem',
]
)
METHODNAMES = r'|'.join(["index", "insert", "from", "get", "new", "maybe_next"])
PRIMITIVES = r'|'.join(["switchInt", "drop"])
ASSERT = r'assert'
CONST = r'const'
MODE = r'|'.join(["move", "!move"])
RETURN = r'return'
UNREACHABLE = r'unreachable'
OTHERWISE = r'otherwise'
STRING = r'\".*?\"'
AS = r'as'
TRUE = r'true'
FALSE = r'false'
# Ignored pattern
ignore_newline = r'\n+'
# Extra action for newlines
def ignore_newline(self, t):
self.lineno += t.value.count('\n')
# This decorator allows us to add a logic before returning the matched token.
@_(r"(0|[1-9][0-9]*)")
def NUMBER(self, t):
t.value = int(t.value)
return t
def error(self, t):
print("Illegal character '%s'" % t.value[0])
self.index += 1
# noinspection PyUnresolvedReferences
class MirParser(Parser):
tokens = MirLexer.tokens
debugfile = 'parser.out'
start = 'bblist'
def __init__(self):
self.curr_bb_id: int = -1
self.cfg: ir.CFG = ir.CFG()
self.temp_stmts: List[ir.Statement] = []
self.stmt: ir.Statement = ir.Statement()
@staticmethod
def get_loc_or_bb_int(string):
# get int from location string
if string is list:
return string
return int(re.sub(r'\D', '', string))
def add_curr_stmt_and_reset(self):
self.temp_stmts.append(self.stmt)
self.stmt = ir.Statement()
"""
@_('FN NAME "(" "," ")" "{" BB "}"')
def function(self, p):
print('function', p.NAME, p.params, p.blocks)
# MIR type definitions
@_('LET LOCATION ":" TYPENAMES ";"')
def location_type_immut(self, p):
self.types[p.LOCATION] = p.TYPENAMES
return p.LOCATION
@_('LETMUT LOCATION ":" TYPENAMES ";"')
def location_type_mut(self, p):
self.types[p.LOCATION] = p.TYPENAMES
return p.LOCATION
# let mut loc: type ;
@_('LETMUT LOCATION ":" TYPENAMES ";"',
'LET LOCATION ":" TYPENAMES ";"',
'SCOPE NUMBER "{" DEBUG VARIABLE THICKARROW LOCATION ";" "}"')
def type_ignore(self, p):
print(p)
pass
# ignore everything and just process bblist
@_('FN type_ignore bblist')
def fn(self, p):
return p.bblist
"""
# stmtlist -> stmtlist stmt | stmt
# bblist -> bblist block | block
@_('block')
def bblist(self, p):
return p.block
@_('bblist block')
def bblist(self, p):
return p.bblist # FIXME, is correct concat and order of CFG?
# block
@_('BB ":" "{" stmtlist "}"')
def block(self, p):
try:
self.curr_bb_id = self.get_loc_or_bb_int(p.BB)
# create BasicBlock and add to CFG
block = ir.BasicBlock(name=self.curr_bb_id)
# add temp statements to BasicBlock
block.add_statements(self.temp_stmts)
self.cfg.add_bb(block)
except ValueError:
print('ERROR: Invalid BB id', p.BB)
exit(1)
# add goto blocks, if any self.temp_stmts has a bb_goto, add it to the current bb
# for last statement in temp_stmts
if len(self.temp_stmts) > 0:
last_stmt = self.temp_stmts[-1]
if (
isinstance(last_stmt, (ir.FunctionStatement, ir.PrimitiveFunctionStatement))
and last_stmt.bb_goto is not None
):
self.cfg.add_edge(self.curr_bb_id, last_stmt.bb_goto)
# reset temp_stmts
print(f'block{self.curr_bb_id} end')
print(f"flushing {len(self.temp_stmts)} temp_stmts")
self.temp_stmts.clear()
return self.cfg
# stmtlist -> stmtlist statement | statement
@_('stmtlist statement')
def stmtlist(self, p):
print('stmtlist, statment', p.statement)
return p.stmtlist
@_('statement')
def stmtlist(self, p):
print('stmtlist', p.statement)
return [p.statement]
# statement -> LOCATION = stmttype ; | GOTO ARROW BB ; | UNREACHABLE ; | RETURN | primitives | assert ;
@_('LOCATION "=" stmttype ";"')
def statement(self, p):
curr_stmt_id = self.get_loc_or_bb_int(p.LOCATION)
last_stmt = self.stmt
# if last stmt is an assignment, then we need to assign the curr_stmt_id
match last_stmt.stmt_type:
# do location assignment
case ir.StatementType.ASSIGN | ir.StatementType.FUNCTION_CALL | ir.StatementType.PRIMITIVE:
# check location is not in set of seen locations of temp_stmts for current bb
seen = [n.lhs_location for n in self.temp_stmts]
if curr_stmt_id in seen:
print(f'ERROR: curr_stmt_id {curr_stmt_id} already used')
exit(1)
else:
self.stmt.lhs_location = curr_stmt_id
print(f"set lhs_location of last_stmt to {curr_stmt_id}")
case ir.StatementType.UNREACHABLE | ir.StatementType.RETURN:
pass
case ir.StatementType.GOTO:
pass
# add curr to temp and reinitialise self.curr_stmt
self.add_curr_stmt_and_reset()
print('statement', p.LOCATION, p.stmttype)
return p.stmttype
@_('GOTO ARROW BB ";"')
def statement(self, p):
print('goto', p.BB)
self.stmt = ir.PrimitiveFunctionStatement()
self.stmt.bb_goto = self.get_loc_or_bb_int(p.BB)
self.add_curr_stmt_and_reset()
@_('UNREACHABLE ";"')
def statement(self, p):
print('unreachable', p.UNREACHABLE)
self.stmt.stmt_type = ir.StatementType.UNREACHABLE
self.add_curr_stmt_and_reset()
@_('RETURN ";"')
def statement(self, p):
print('return', p.RETURN)
self.stmt = ir.PrimitiveFunctionStatement()
self.stmt.primitive_type = "return"
self.add_curr_stmt_and_reset()
# stmttype -> LOCATION | constant | borrow | goto | unreachable | return | function_call | move
@_('LOCATION')
def stmttype(self, p):
print('stmttype location', p.LOCATION)
# create statement
self.stmt.stmt_type = ir.StatementType.ASSIGN
self.stmt.rhs_location = self.get_loc_or_bb_int(p.LOCATION)
@_('MODE LOCATION')
def stmttype(self, p):
print('stmttype location', p.LOCATION)
# create statement
self.stmt.stmt_type = ir.StatementType.ASSIGN
self.stmt.rhs_location = self.get_loc_or_bb_int(p.LOCATION)
@_('constant')
def stmttype(self, p):
print('stmttype', p.constant)
self.stmt.stmt_type = ir.StatementType.ASSIGN
self.stmt.rhs_value = p.constant[0]
self.stmt.value_type = p.constant[0]
self.stmt.rhs_value = ir.ValueType.CONST
return p.constant
# constant -> CONST NUMBER _ TYPE
@_('CONST NUMBER "_" TYPENAMES')
def constant(self, p):
print('constant item', p.CONST, p.NUMBER, p.TYPENAMES)
return p.NUMBER, p.TYPENAMES
@_('CONST FALSE', 'CONST TRUE')
def constant(self, p):
return p[1]
@_('borrow')
def stmttype(self, p):
print('stmttype', p.borrow)
self.stmt.stmt_type = ir.StatementType.ASSIGN
return p.borrow
# borrow -> REF source | REFMUT source
@_('REF source')
def borrow(self, p):
print('borrow', p.source)
self.stmt.value_type = ir.ValueType.BORROW
self.stmt.mutable = False
return p.source
@_('REFMUT source')
def borrow(self, p):
print('borrow', p.source)
self.stmt.value_type = ir.ValueType.BORROW
self.stmt.mutable = True
return p.source
# source -> ( source ) | LOCATION | DEREF LOCATION
@_('"(" source ")"')
def source(self, p):
print('source parens', p.source)
return p.source
@_('LOCATION')
def source(self, p):
print('source location', p.LOCATION)
self.stmt.rhs_location = self.get_loc_or_bb_int(p.LOCATION)
return p.LOCATION
@_('DEREF LOCATION')
def source(self, p):
print('source deref location', p.LOCATION)
self.stmt.rhs_location = self.get_loc_or_bb_int(p.LOCATION)
return p.LOCATION
# result type unwrap hack
@_('"(" "(" LOCATION AS TYPENAMES ")" "." NUMBER ":" REF TYPENAMES COLONTWICE TYPENAMES COLONTWICE TYPENAMES ")"')
def stmttype(self, p):
print('stmttype unwrap', p.LOCATION, p.NUMBER, p.TYPENAMES1)
self.stmt.stmt_type = ir.StatementType.ASSIGN
# self.stmt.mode = p.mode
self.stmt.rhs_location = self.get_loc_or_bb_int(p.LOCATION)
self.stmt.value_type = ir.ValueType.UNWRAP
self.stmt.unwrap_index = p.NUMBER
self.stmt.unwrap_type = p.TYPENAMES1
return p.TYPENAMES1
# result type unwrap hack
@_('mode "(" "(" LOCATION AS TYPENAMES ")" "." NUMBER ":" REFMUT TYPENAMES ")"')
def stmttype(self, p):
print('stmttype unwrap', p.LOCATION, p.NUMBER, p.TYPENAMES1)
self.stmt.stmt_type = ir.StatementType.ASSIGN
self.stmt.mode = p.mode
self.stmt.mutable = True
self.stmt.rhs_location = self.get_loc_or_bb_int(p.LOCATION)
self.stmt.value_type = ir.ValueType.UNWRAP
self.stmt.unwrap_index = p.NUMBER
self.stmt.unwrap_type = p.TYPENAMES1
return p.TYPENAMES1
# primitives
@_('PRIMITIVES "(" valueargs ")" goto_cond_block ";"')
def statement(self, p):
print('stmt primitive goto_cond', p.PRIMITIVES, p.valueargs, p.goto_cond_block if p.goto_cond_block else '')
self.stmt = ir.PrimitiveFunctionStatement()
self.stmt.primitive_type = p.PRIMITIVES
# DEBUG: assert p.valueargs is of type list
assert isinstance(p.valueargs, list)
self.stmt.primitive_args.append(p.valueargs)
self.stmt.bb_goto = p.goto_cond_block if p.goto_cond_block else None
self.add_curr_stmt_and_reset()
@_('PRIMITIVES "(" valueargs ")" goto_block ";"')
def statement(self, p):
print('stmt primitive goto_block', p.PRIMITIVES, p.valueargs, p.goto_block if p.goto_block else '')
self.stmt = ir.PrimitiveFunctionStatement()
# DEBUG: assert p.valueargs is of type list
assert isinstance(p.valueargs, list)
self.stmt.primitive_type = p.PRIMITIVES
self.stmt.primitive_args = p.valueargs
self.stmt.bb_goto = p.goto_block if p.goto_block else None
self.add_curr_stmt_and_reset()
# assert
@_('ASSERT "(" valueargs ")" goto_block ";"')
def statement(self, p):
print('stmt assert', p.valueargs, p.goto_block)
self.stmt = ir.PrimitiveFunctionStatement()
self.stmt.primitive_type = p.ASSERT
self.stmt.primitive_args = p.valueargs
self.stmt.bb_goto = p.goto_block
self.add_curr_stmt_and_reset()
@_('function_call')
def stmttype(self, p):
return p.function_call
# move -> MOVE "(" valueargs ")"
@_('mode "(" valueargs ")"')
def stmttype(self, p):
print('stmttype move', p.mode)
self.stmt = ir.PrimitiveFunctionStatement()
self.stmt.primitive_type = p.mode
self.stmt.primitive_args = p.valueargs
# todo: function
""" |-| method call on Struct
Struct::<A,B,C>::new(x, y) -> bb1;
^ struct ^ type-args ^value args
"""
# issue is recognising all:
# Type::<Type, Type>::method(args)
# Type::<Type, Type>::method::<Type, Type>(args) fixme
# these due to:
# <HashMap<u32, String> as Index<&u32>>::index(move _2, move _3) -> bb69
# <String as From<&str>>::from(const "init") -> bb69
# todo; update grammar to reflect impl, and get rid of 7 shift/reduce conflicts
# function_call -> generic COLONTWICE method_call ( valueargs ) goto_block
# method_call -> METHODNAME | METHODNAME turbofish
# turbofish -> COLONTWICE "<" typeargs ">"
# generic -> generic COLONTWICE "<" typeargs ">" cast
# | TYPENAMES "<" typeargs ">" cast
# | TYPENAMES
# | generic cast
# | "<" generic ">"
# cast -> AS TYPENAMES < typeargs > | empty
# typeargs -> typearg "," typeargs | typearg
# typearg -> TYPENAMES | REF TYPENAMES | REFMUT TYPENAMES
# HACK: primitive MIR-builtins can assign, but are functions
@_('TYPENAMES "(" valueargs ")"')
def function_call(self, p):
print('function_call', p.TYPENAMES, p.valueargs)
self.stmt: ir.PrimitiveFunctionStatement = ir.PrimitiveFunctionStatement()
self.stmt.stmt_type = ir.StatementType.ASSIGN
self.stmt.primitive_type = p.TYPENAMES
self.stmt.primitive_args = p.valueargs
return ir.StatementType.PRIMITIVE
@_('generic COLONTWICE method_call "(" valueargs ")" goto_block')
def function_call(self, p):
print('function_call', p.generic, p.method_call, p.valueargs, p.goto_block if p.goto_block else '')
self.stmt: ir.FunctionStatement = ir.FunctionStatement()
self.stmt.stmt_type = ir.StatementType.ASSIGN
self.stmt.function_type = p.generic
self.stmt.function_method = p.method_call
self.stmt.function_args = p.valueargs
self.stmt.bb_goto = p.goto_block if p.goto_block else None
return ir.StatementType.FUNCTION_CALL
@_('generic COLONTWICE method_call "(" valueargs ")"')
def function_call(self, p):
print('function_call', p.generic, p.method_call, p.valueargs)
self.stmt: ir.FunctionStatement = ir.FunctionStatement()
self.stmt.stmt_type = ir.StatementType.ASSIGN
self.stmt.function_type = p.generic
self.stmt.function_method = p.method_call
self.stmt.function_args = p.valueargs
return ir.StatementType.FUNCTION_CALL
@_('generic COLONTWICE "<" typeargs ">" cast')
def generic(self, p):
print('generic', p.generic, p.typeargs)
return p.generic, p.typeargs, p.cast if p.cast else None
@_('TYPENAMES "<" typeargs ">" cast')
def generic(self, p):
print('generic', p.TYPENAMES, p.typeargs)
return p.TYPENAMES, p.typeargs, p.cast if p.cast else None
@_('TYPENAMES')
def generic(self, p):
print('generic', p.TYPENAMES)
return p.TYPENAMES, None, None
@_('generic cast')
def generic(self, p):
print('generic', p.generic)
return p.generic, None, p.cast if p.cast else None
@_('"<" generic ">"')
def generic(self, p):
print('generic', p.generic)
return p.generic, None, None
@_('AS TYPENAMES "<" typeargs ">"')
def cast(self, p):
print('cast', p.TYPENAMES, p.typeargs)
return p.TYPENAMES, p.typeargs
@_('')
def empty(self, p):
pass
@_('empty')
def cast(self, p):
pass
# method_call -> METHODNAME | METHODNAME turbofish
@_('METHODNAMES')
def method_call(self, p):
print('method_call', p.METHODNAMES)
return p.METHODNAMES
@_('METHODNAMES turbofish')
def method_call(self, p):
print('method_call', p.METHODNAMES, p.turbofish)
return p.METHODNAMES, p.turbofish
# turbofish -> COLONTWICE "<" typeargs ">"
@_('COLONTWICE "<" typeargs ">"')
def turbofish(self, p):
print('turbofish', p.typeargs)
return p.typeargs
# typeargs -> typearg "," typeargs | typearg
# typearg -> TYPENAMES | REF TYPENAMES | REFMUT TYPENAMES
@_('typearg "," typeargs')
def typeargs(self, p):
print('typeargs', p.typearg, p.typeargs)
return [p.typearg] + p.typeargs
@_('typearg')
def typeargs(self, p):
print('typeargs', p.typearg)
return [p.typearg]
@_('TYPENAMES')
def typearg(self, p):
print('typearg', p.TYPENAMES)
return p.TYPENAMES
@_('REF TYPENAMES')
def typearg(self, p):
print('typearg', p.TYPENAMES)
return p.REF + p.TYPENAMES
@_('REFMUT TYPENAMES')
def typearg(self, p):
print('typearg', p.TYPENAMES)
return p.REFMUT + p.TYPENAMES
# valueargs -> valueargs "," valuearg | valuearg
@_('valueargs "," valuearg')
def valueargs(self, p):
print('valueargs', p.valueargs, p.valuearg)
# if either p.valueargs or p.valuearg is a list, then we need to flatten
if isinstance(p.valueargs, list) and isinstance(p.valuearg, list):
return p.valueargs + p.valuearg
elif isinstance(p.valueargs, list):
return p.valueargs + [p.valuearg]
elif isinstance(p.valuearg, list):
return [p.valueargs] + p.valuearg
else:
return [p.valueargs, p.valuearg]
@_('valuearg')
def valueargs(self, p):
print('valuearg', p.valuearg)
return [p.valuearg]
# valuearg -> LOCATION | MOVE LOCATION | valuearg_constant (reuse from stmt) | CONST STRING | STRING
# | mode "(" LOCATION "." NUMBER ":" TYPENAMES ")"
@_('LOCATION')
def valuearg(self, p):
print('valuearg loc', p.LOCATION)
return ir.FunctionArg(location=self.get_loc_or_bb_int(p.LOCATION))
@_('mode LOCATION')
def valuearg(self, p):
print('valuearg move loc', p.LOCATION, p.mode)
return ir.FunctionArg(mode=p.mode, location=self.get_loc_or_bb_int(p.LOCATION))
@_('constant')
def valuearg(self, p):
print('valuearg constant', p.constant)
# p.constant[0] = number, p.constant[1] = type
return ir.FunctionArg(constant=(p.constant[0], p.constant[1]))
@_('CONST STRING', 'STRING')
def valuearg(self, p):
print('valuearg', p.STRING)
# from method(const "init"), must be String due to typing of function call
return ir.FunctionArg(constant=(p.STRING, "String"))
# assert valuearg dot accessing with type weirdness
# | mode "(" LOCATION "." NUMBER ":" TYPENAMES ")"
@_('mode "(" LOCATION "." NUMBER ":" TYPENAMES ")"')
def valuearg(self, p):
print('valuearg', p.LOCATION, p.NUMBER, p.TYPENAMES)
return ir.FunctionArg(mode=ir.Mode.NOT_MOVE, location=self.get_loc_or_bb_int(p.LOCATION), type=p.TYPENAMES)
@_('LOCATION "." NUMBER : TYPENAMES')
def valuearg(self, p):
print('valuearg', p.LOCATION, p.NUMBER, p.TYPENAMES)
return ir.FunctionArg(location=self.get_loc_or_bb_int(p.LOCATION), type=p.TYPENAMES)
@_('MODE')
def mode(self, p):
print('mode', p.MODE)
function_mode = ir.Mode.NONE
match p.MODE:
case 'move':
function_mode = ir.Mode.MOVE
case 'ref':
function_mode = ir.MODE.NOT_MOVE
return function_mode
# goto_block -> ARROW BB
@_('ARROW BB')
def goto_block(self, p):
print('goto_block', p.BB)
return self.get_loc_or_bb_int(p.BB)
# goto_cond_block -> ARROW "[" goto_params "]"
@_('ARROW "[" goto_params "]"')
def goto_cond_block(self, p):
print('goto_cond_block', p.goto_params)
return p.goto_params
# goto_params -> goto_params "," goto_param | goto_param
@_('goto_params "," goto_param')
def goto_params(self, p):
print('goto_params', p.goto_params, p.goto_param)
# neatly:tm: collect bb gotos in list
if p.goto_params is None:
return [self.get_loc_or_bb_int(p.goto_param)]
elif type(p.goto_params) is list:
return p.goto_params + [self.get_loc_or_bb_int(p.goto_param)]
else:
return [self.get_loc_or_bb_int(p.goto_params)] + [self.get_loc_or_bb_int(p.goto_param)]
@_('goto_param')
def goto_params(self, p):
print('goto_param', p.goto_param)
return p.goto_param
# goto_param -> NUMBER "_" TYPENAMES ":" BB | OTHERWISE ":" BB
@_('NUMBER "_" TYPENAMES ":" BB')
def goto_param(self, p):
print('goto_param', p.NUMBER, p.TYPENAMES, p.BB)
return p.BB
@_('OTHERWISE ":" BB')
def goto_param(self, p):
print('goto_param', p.BB)
return p.BB
def parse(program):
lexer = MirLexer()
parser = MirParser()
return parser.parse(lexer.tokenize(program))
if __name__ == '__main__':
bold = '\033[1m'
unbold = '\033[0m'
header = "=" * 10
in_file = 'mir-source/expect-fail/mut-immut-borrow.mir'
text = open(in_file, 'r').read()
print(f"{header}\nlexing: ")
mir_lexer = MirLexer()
for tok in mir_lexer.tokenize(text=text):
print(
f"type= {bold}{tok.type:<11s}{unbold} "
f"value= {bold}{tok.value:<11}{unbold} "
f"lineno={tok.lineno:<10} "
f"index={tok.index:<10} "
f"end={tok.end:<10}"
)
print(f"{header}\nparsing: ")
res: ir.CFG = parse(text)
print(f"{header}\nparsing result: ")
print(f"{header}\ncfg pprint: ")
res.finalise_cfg()
res.pprint() if res else None
res.compute_reaching_definitions()
# read and print in_file to std out
print(f"{header}\nread and print in_file to std out: ")
print(open(in_file, 'r').read())
for bb in res.bbs:
print(f"{header}\nbb {bb.name} reaching definitions: ")
for i, stmt in enumerate(bb.stmts):
# print def in and def out
print(f"\tstmt:{i}")
print(f"\t\tdef in:")
pprint(stmt.def_in, width=20, indent=12)
print(f"\t\tdef out:")
pprint(stmt.def_out, width=20, indent=12)
res.compute_liveness()
for bb in res.bbs:
print(f"{header}\nbb {bb.name} liveness: ")
for i, stmt in enumerate(bb.stmts):
# print live in/live out
print(f"\tstmt:{i} live in: {stmt.live_in} \t live out: {stmt.live_out}")
print(f"{header}\nBorrow-checking: ")
bck_res = res.borrow_check(res.compute_borrows())
print(f"borrow-check thinks program is valid? {bck_res}")
exit(0 if bck_res else 1)