-
Notifications
You must be signed in to change notification settings - Fork 4
/
fj_parser.py
859 lines (693 loc) · 26.6 KB
/
fj_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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
from os import path
from pathlib import Path
from typing import Set, List, Tuple, Dict, Union
import sly
# noinspection PyProtectedMember
from sly.lex import Token
# noinspection PyProtectedMember
from sly.yacc import YaccProduction as ParsedRule
from flipjump.utils.exceptions import FlipJumpExprException, FlipJumpParsingException
from flipjump.assembler.inner_classes.expr import Expr, get_minimized_expr
from flipjump.assembler.inner_classes.ops import (
get_used_labels,
get_declared_labels,
CodePosition,
MacroName,
Op,
Macro,
INITIAL_MACRO_NAME,
MacroCall,
RepCall,
FlipJump,
WordFlip,
Label,
Segment,
Reserve,
Pad,
)
curr_file: Path
curr_file_short_name: str
curr_text: str
curr_namespace: List[str]
all_errors: str
error_occurred: bool
def get_position(lineno: int) -> CodePosition:
return CodePosition(str(curr_file), curr_file_short_name, lineno)
def syntax_error(lineno: int, msg: str = '') -> None:
global error_occurred, all_errors
error_occurred = True
curr_position = get_position(lineno)
if msg:
error_string = f"Syntax Error in {curr_position}:\n {msg}"
else:
error_string = f"Syntax Error in {curr_position}"
all_errors += f"{error_string}\n"
print(error_string)
def syntax_warning(line: int, is_error: bool, msg: str = '') -> None:
error_string = f"Syntax Warning in file {curr_file}"
if line is not None:
error_string += f" line {line}"
if msg:
error_string += f":\n {msg}"
if is_error:
global error_occurred, all_errors
error_occurred = True
all_errors += f"{error_string}\n"
print(error_string)
# Regex for the parser
id_re = r'[a-zA-Z_][a-zA-Z_0-9]*'
dot_id_re = fr'(({id_re})|\.*)?(\.({id_re}))+'
bin_num = r'0[bB][01]+'
hex_num = r'0[xX][0-9a-fA-F]+'
dec_num = r'[0-9]+'
char_escape_dict = {
'0': 0x0,
'a': 0x7,
'b': 0x8,
'e': 0x1B,
'f': 0xC,
'n': 0xA,
'r': 0xD,
't': 0x9,
'v': 0xB,
'\\': 0x5C,
"'": 0x27,
'"': 0x22,
'?': 0x3F,
}
escape_chars = ''.join(k for k in char_escape_dict)
char = fr'[ -~]|\\[{escape_chars}]|\\[xX][0-9a-fA-F]{{2}}'
number_re = fr"({bin_num})|({hex_num})|('({char})')|({dec_num})"
string_re = fr'"({char})*"'
def get_char_value_and_length(s: str) -> Tuple[int, int]:
if s[0] != '\\':
return ord(s[0]), 1
if s[1] in char_escape_dict:
return char_escape_dict[s[1]], 2
return int(s[2:4], 16), 4
# noinspection PyUnboundLocalVariable,PyRedeclaration,PyPep8Naming,PyMethodMayBeStatic
class FJLexer(sly.Lexer):
# noinspection PyUnresolvedReferences
tokens = {NS, DEF, REP, WFLIP, PAD, SEGMENT, RESERVE, ID, DOT_ID, NUMBER, STRING, LE, GE, EQ, NEQ, SHL, SHR, NL, SC}
literals = {
'=',
'+',
'-',
'*',
'/',
'%',
'(',
')',
'$',
'^',
'|',
'&',
'?',
':',
'<',
'>',
'"',
'#',
'{',
'}',
"@",
",",
}
ignore_ending_comment = r'//.*'
ignore_line_continuation = r'\\[ \t]*\n'
# Tokens
DOT_ID = dot_id_re
ID = id_re
NUMBER = number_re
STRING = string_re
# noinspection PyUnresolvedReferences
ID[r'def'] = DEF
# noinspection PyUnresolvedReferences
ID[r'rep'] = REP
# noinspection PyUnresolvedReferences
ID[r'ns'] = NS
# noinspection PyUnresolvedReferences
ID[r'wflip'] = WFLIP
# noinspection PyUnresolvedReferences
ID[r'pad'] = PAD
# noinspection PyUnresolvedReferences
ID[r'segment'] = SEGMENT
# noinspection PyUnresolvedReferences
ID[r'reserve'] = RESERVE
LE = "<="
GE = ">="
EQ = "=="
NEQ = "!="
SHL = r'<<'
SHR = r'>>'
# Punctuations
NL = r'[\r\n]'
SC = r';'
ignore = ' \t'
# noinspection PyTypeChecker
def NUMBER(self, t: Token) -> Token:
n = t.value
if len(n) >= 2:
# noinspection PyUnresolvedReferences
if n[0] == "'":
t.value = get_char_value_and_length(n[1:-1])[0]
elif n[1] in 'xX':
t.value = int(n, 16)
elif n[1] in 'bB':
t.value = int(n, 2)
else:
t.value = int(n)
else:
t.value = int(t.value)
return t
def STRING(self, t: Token) -> Token:
chars = []
s = t.value[1:-1]
i = 0
while i < len(s):
val, length = get_char_value_and_length(s[i:])
chars.append(val)
i += length
t.value = sum(val << (i * 8) for i, val in enumerate(chars))
return t
def NL(self, t: Token) -> Token:
self.lineno += 1
return t
def ignore_line_continuation(self, t: Token) -> Token:
self.lineno += 1
return t
def error(self, t: Token) -> None:
global error_occurred, all_errors
error_occurred = True
error_string = f"Lexing Error in {get_position(self.lineno)}: {t.value[0]}"
all_errors += f"{error_string}\n"
print(error_string)
self.index += 1
def next_address() -> Expr:
return Expr('$')
def _get_main_macro_code_position(first_file: Tuple[str, Path]) -> CodePosition:
(short_file_name, fj_file_path) = first_file
return CodePosition(str(fj_file_path.absolute()), short_file_name, 1)
# noinspection PyUnusedLocal,PyUnresolvedReferences,PyPep8Naming
class FJParser(sly.Parser):
tokens = FJLexer.tokens
# TODO #249 - add Unary Minus (-), Unary Not (~).
# Maybe add logical or (||) and logical and (&&). Maybe handle power (**).
precedence = (
('right', '?', ':'),
('left', '|'),
('left', '^'),
('nonassoc', '<', '>', LE, GE),
('left', EQ, NEQ),
('left', '&'),
('left', SHL, SHR),
('left', '+', '-'),
('left', '*', '/', '%'),
('right', '#'),
)
# debugfile = 'src/parser.out'
def __init__(self, memory_width: int, warning_as_errors: bool, first_file: Tuple[str, Path]):
self.consts: Dict[str, Expr] = {'w': Expr(memory_width)}
self.warning_as_errors: bool = warning_as_errors
self.macros: Dict[MacroName, Macro] = {
INITIAL_MACRO_NAME: Macro([], [], [], '', _get_main_macro_code_position(first_file))
}
def validate_free_macro_name(self, name: MacroName, lineno: int) -> None:
if name in self.macros:
syntax_error(
lineno, f'macro {name} is declared twice! ' f'also declared in {self.macros[name].code_position}.'
)
def validate_params(self, ids: List[str], macro_name: MacroName, lineno: int) -> None:
for param_id in ids:
if param_id in self.consts:
syntax_error(
lineno,
f'parameter {param_id} in macro {macro_name}) '
f'is also defined as a constant variable (with value {self.consts[param_id]})',
)
seen_ids = set()
for _id in ids:
if _id in seen_ids:
syntax_error(lineno, f'parameter {_id} in macro {macro_name}) is declared twice!')
else:
seen_ids.add(_id)
def validate_label_usage(
self,
labels_used: Set[str],
labels_declared: Set[str],
regular_labels: Set[str],
extern_labels: Set[str],
global_labels: Set[str],
lineno: int,
macro_name: MacroName,
) -> None:
self.validate_labels_groups(extern_labels, global_labels, regular_labels, lineno, macro_name)
self.validate_no_unused_labels(regular_labels, global_labels, labels_declared, labels_used, lineno, macro_name)
self.validate_no_unknown_label_uses(
regular_labels, global_labels, labels_declared, labels_used, lineno, macro_name
)
self.validate_no_bad_label_declarations(regular_labels, extern_labels, labels_declared, lineno, macro_name)
self.validate_all_extern_labels_are_declared(extern_labels, labels_declared, lineno, macro_name)
@staticmethod
def validate_labels_groups(
extern_labels: Set[str], global_labels: Set[str], regular_labels: Set[str], lineno: int, macro_name: MacroName
) -> None:
if global_labels & extern_labels:
syntax_error(
lineno,
f"In macro {macro_name}: "
f"extern labels can't be global labels: " + ', '.join(global_labels & extern_labels),
)
if global_labels & regular_labels:
syntax_error(
lineno,
f"In macro {macro_name}: "
f"extern labels can't be regular labels: " + ', '.join(global_labels & regular_labels),
)
if extern_labels & regular_labels:
syntax_error(
lineno,
f"In macro {macro_name}: "
f"global labels can't be regular labels: " + ', '.join(extern_labels & regular_labels),
)
def validate_no_unused_labels(
self,
regular_labels: Set[str],
global_labels: Set[str],
labels_declared: Set[str],
labels_used: Set[str],
lineno: int,
macro_name: MacroName,
) -> None:
unused_labels = regular_labels.union(global_labels) - labels_used.union(
self.to_base_name(label) for label in labels_declared
)
if unused_labels:
syntax_warning(
lineno,
self.warning_as_errors,
f"In macro {macro_name}: " f"unused labels: {', '.join(unused_labels)}.",
)
def validate_all_extern_labels_are_declared(
self, extern_labels: Set[str], labels_declared: Set[str], lineno: int, macro_name: MacroName
) -> None:
unused_labels = extern_labels - {self.to_base_name(label) for label in labels_declared}
if unused_labels:
syntax_warning(
lineno,
self.warning_as_errors,
f"In macro {macro_name}: " f"undeclared extern label: {', '.join(unused_labels)}.",
)
def validate_no_bad_label_declarations(
self,
regular_labels: Set[str],
extern_labels: Set[str],
labels_declared: Set[str],
lineno: int,
macro_name: MacroName,
) -> None:
bad_declarations = labels_declared - set(
self.ns_full_name(label) for label in extern_labels.union(regular_labels)
)
if bad_declarations:
syntax_warning(
lineno,
self.warning_as_errors,
f"In macro {macro_name}: " f"Declared a not extern/parameter label: {', '.join(bad_declarations)}.",
)
def validate_no_unknown_label_uses(
self,
regular_labels: Set[str],
global_labels: Set[str],
labels_declared: Set[str],
labels_used: Set[str],
lineno: int,
macro_name: MacroName,
) -> None:
bad_uses = labels_used - global_labels - regular_labels - set(labels_declared) - {'$'}
if bad_uses:
syntax_warning(
lineno,
self.warning_as_errors,
f"In macro {macro_name}: "
f"Used a not global/parameter/declared-extern label: {', '.join(bad_uses)}.",
)
@staticmethod
def validate_no_segment_or_reserve(ops: List[Op], macro_name: MacroName) -> None:
for op in ops:
if isinstance(op, Segment):
syntax_error(op.code_position.line, f"segment can't be declared inside a macro ({macro_name}).")
if isinstance(op, Reserve):
syntax_error(op.code_position.line, f"reserve can't be declared inside a macro ({macro_name}).")
def validate_macro_declaration(
self,
name: MacroName,
ops: List[Op],
lineno: int,
params: List[str],
local_params: List[str],
global_params: Set[str],
extern_params: Set[str],
) -> None:
self.validate_free_macro_name(name, lineno)
regular_params = params + local_params
self.validate_params(regular_params, name, lineno)
self.validate_label_usage(
get_used_labels(ops),
get_declared_labels(ops),
set(regular_params),
set(extern_params),
set(global_params),
lineno,
name,
)
self.validate_no_segment_or_reserve(ops, name)
@staticmethod
def ns_name() -> str:
return '.'.join(curr_namespace)
@staticmethod
def ns_full_name(base_name: str) -> str:
return '.'.join(curr_namespace + [base_name])
@staticmethod
def base_name_to_ns_full_name(base_name: str, lineno: int) -> str:
without_dots = base_name.lstrip('.')
if len(without_dots) == len(base_name):
return base_name
num_of_dots = len(base_name) - len(without_dots)
if num_of_dots - 1 > len(curr_namespace):
syntax_error(
lineno,
f'Used more leading dots than current namespace depth ' f'({num_of_dots}-1 > {len(curr_namespace)})',
)
return '.'.join(curr_namespace[: len(curr_namespace) - (num_of_dots - 1)] + [without_dots])
@staticmethod
def to_base_name(name: str) -> str:
return name.split('.')[-1]
def error(self, token: Token) -> None:
global error_occurred, all_errors
error_occurred = True
if token is None:
error_string = (
f'Syntax Error in {get_position(self.line_position(None))}. '
f'Maybe missing }} or {{ before this line?'
)
else:
error_string = f'Syntax Error in {get_position(token.lineno)}, token=("{token.type}", {token.value})'
all_errors += f"{error_string}\n"
print(error_string)
# Parsing Rules:
@_('definable_line_statements')
def program(self, p: ParsedRule) -> None:
ops = p.definable_line_statements
self.macros[INITIAL_MACRO_NAME].ops += ops
# noinspection PyUnresolvedReferences
@_('definable_line_statements NL definable_line_statement')
def definable_line_statements(self, p: ParsedRule) -> List[Op]:
if p.definable_line_statement:
p.definable_line_statements.extend(p.definable_line_statement)
return p.definable_line_statements
@_('definable_line_statement')
def definable_line_statements(self, p: ParsedRule) -> List[Op]:
if p.definable_line_statement:
return p.definable_line_statement
return []
@_('')
def empty(self, p: ParsedRule) -> None:
return None
@_('line_statement')
def definable_line_statement(self, p: ParsedRule) -> List[Op]:
return p.line_statement
@_('macro_def')
def definable_line_statement(self, p: ParsedRule) -> List[Op]:
return []
@_('NS ID')
def namespace(self, p: ParsedRule) -> None:
curr_namespace.append(p.ID)
@_('namespace "{" NL definable_line_statements NL "}"')
def definable_line_statement(self, p: ParsedRule) -> List[Op]:
curr_namespace.pop()
return p.definable_line_statements
@_('DEF ID macro_params "{" NL line_statements NL "}"')
def macro_def(self, p: ParsedRule) -> None:
params, local_params, global_params, extern_params = p.macro_params
name = MacroName(self.ns_full_name(p.ID), len(params))
ops = p.line_statements
self.validate_macro_declaration(name, ops, p.lineno, params, local_params, global_params, extern_params)
self.macros[name] = Macro(params, local_params, ops, self.ns_name(), get_position(p.lineno))
return None
@_('empty')
def maybe_ids(self, p: ParsedRule) -> List[str]:
return []
@_('IDs')
def maybe_ids(self, p: ParsedRule) -> List[str]:
return p.IDs
@_('empty')
def maybe_local_ids(self, p: ParsedRule) -> List[str]:
return []
@_('"@" IDs')
def maybe_local_ids(self, p: ParsedRule) -> List[str]:
return p.IDs
@_('empty')
def maybe_global_ids(self, p: ParsedRule) -> List[str]:
return []
@_('"<" ids')
def maybe_global_ids(self, p: ParsedRule) -> List[str]:
return p.ids
@_('empty')
def maybe_extern_ids(self, p: ParsedRule) -> List[str]:
return []
@_('">" IDs')
def maybe_extern_ids(self, p: ParsedRule) -> List[str]:
return p.IDs
@_('maybe_ids maybe_local_ids maybe_global_ids maybe_extern_ids')
def macro_params(self, p: ParsedRule) -> Tuple[List[str], List[str], List[str], List[str]]:
return p.maybe_ids, p.maybe_local_ids, p.maybe_global_ids, p.maybe_extern_ids
@_('IDs "," ID')
def IDs(self, p: ParsedRule) -> List[str]:
return p.IDs + [p.ID]
@_('ID')
def IDs(self, p: ParsedRule) -> List[str]:
return [p.ID]
@_('line_statements NL line_statement')
def line_statements(self, p: ParsedRule) -> List[Op]:
p.line_statements.extend(p.line_statement)
return p.line_statements
@_('line_statement')
def line_statements(self, p: ParsedRule) -> List[Op]:
return p.line_statement
# @_('empty')
# def line_statements(self, p: ParsedRule) -> List[Op]:
# return []
@_('empty')
def line_statement(self, p: ParsedRule) -> List[Op]:
return []
@_('statement')
def line_statement(self, p: ParsedRule) -> List[Op]:
if p.statement:
return [p.statement]
return []
@_('label statement')
def line_statement(self, p: ParsedRule) -> List[Op]:
if p.statement:
return [p.label, p.statement]
return [p.label]
@_('label')
def line_statement(self, p: ParsedRule) -> List[Op]:
return [p.label]
@_('ID ":"')
def label(self, p: ParsedRule) -> Label:
return Label(self.ns_full_name(p.ID), get_position(p.lineno))
@_('expr SC')
def statement(self, p: ParsedRule) -> FlipJump:
return FlipJump(p.expr, next_address(), get_position(p.lineno))
@_('expr SC expr')
def statement(self, p: ParsedRule) -> FlipJump:
return FlipJump(p.expr0, p.expr1, get_position(p.lineno))
@_('SC expr')
def statement(self, p: ParsedRule) -> FlipJump:
return FlipJump(Expr(0), p.expr, get_position(p.lineno))
@_('SC')
def statement(self, p: ParsedRule) -> FlipJump:
return FlipJump(Expr(0), next_address(), get_position(p.lineno))
@_('ID')
def id(self, p: ParsedRule) -> Tuple[str, int]:
return p.ID, p.lineno
@_('DOT_ID')
def id(self, p: ParsedRule) -> Tuple[str, int]:
return self.base_name_to_ns_full_name(p.DOT_ID, p.lineno), p.lineno
@_('ids "," id')
def ids(self, p: ParsedRule) -> List[str]:
return p.ids + [p.id[0]]
@_('id')
def ids(self, p: ParsedRule) -> List[str]:
return [p.id[0]]
@_('id')
def statement(self, p: ParsedRule) -> MacroCall:
macro_name, lineno = p.id
return MacroCall(macro_name, [], get_position(lineno))
@_('id expressions')
def statement(self, p: ParsedRule) -> MacroCall:
macro_name, lineno = p.id
return MacroCall(macro_name, p.expressions, get_position(lineno))
@_('WFLIP expr "," expr')
def statement(self, p: ParsedRule) -> WordFlip:
return WordFlip(p.expr0, p.expr1, next_address(), get_position(p.lineno))
@_('WFLIP expr "," expr "," expr')
def statement(self, p: ParsedRule) -> WordFlip:
return WordFlip(p.expr0, p.expr1, p.expr2, get_position(p.lineno))
@_('PAD expr')
def statement(self, p: ParsedRule) -> Pad:
return Pad(p.expr, get_position(p.lineno))
@_('ID "=" expr')
def statement(self, p: ParsedRule) -> None:
name = self.ns_full_name(p.ID)
if name in self.consts:
syntax_error(p.lineno, f'Can\'t redeclare the variable "{name}".')
evaluated = p.expr.eval_new(self.consts)
try:
self.consts[name] = Expr(int(evaluated))
except FlipJumpExprException:
syntax_error(p.lineno, f'Can\'t evaluate expression: {str(evaluated)}.')
@_('REP "(" expr "," ID ")" id')
def statement(self, p: ParsedRule) -> RepCall:
macro_name, lineno = p.id
code_position = get_position(lineno)
return RepCall(p.expr, p.ID, macro_name, [], code_position)
@_('REP "(" expr "," ID ")" id expressions')
def statement(self, p: ParsedRule) -> RepCall:
macro_name, lineno = p.id
code_position = get_position(lineno)
return RepCall(p.expr, p.ID, macro_name, p.expressions, code_position)
@_('SEGMENT expr')
def statement(self, p: ParsedRule) -> Segment:
return Segment(p.expr, get_position(p.lineno))
@_('RESERVE expr')
def statement(self, p: ParsedRule) -> Reserve:
return Reserve(p.expr, get_position(p.lineno))
@_('expressions "," expr')
def expressions(self, p: ParsedRule) -> List[Expr]:
return p.expressions + [p.expr]
@_('expr')
def expressions(self, p: ParsedRule) -> List[Expr]:
return [p.expr]
@_('expr_')
def expr(self, p: ParsedRule) -> Expr:
return p.expr_[0]
@_('expr_ "+" expr_')
def expr_(self, p: ParsedRule) -> Tuple[Expr, int]:
return get_minimized_expr('+', (p.expr_0[0], p.expr_1[0])), p.lineno
@_('expr_ "-" expr_')
def expr_(self, p: ParsedRule) -> Tuple[Expr, int]:
return get_minimized_expr('-', (p.expr_0[0], p.expr_1[0])), p.lineno
@_('expr_ "*" expr_')
def expr_(self, p: ParsedRule) -> Tuple[Expr, int]:
return get_minimized_expr('*', (p.expr_0[0], p.expr_1[0])), p.lineno
@_('"#" expr_')
def expr_(self, p: ParsedRule) -> Tuple[Expr, int]:
return get_minimized_expr('#', (p.expr_[0],)), p.lineno
@_('expr_ "/" expr_')
def expr_(self, p: ParsedRule) -> Tuple[Expr, int]:
return get_minimized_expr('/', (p.expr_0[0], p.expr_1[0])), p.lineno
@_('expr_ "%" expr_')
def expr_(self, p: ParsedRule) -> Tuple[Expr, int]:
return get_minimized_expr('%', (p.expr_0[0], p.expr_1[0])), p.lineno
@_('expr_ SHL expr_')
def expr_(self, p: ParsedRule) -> Tuple[Expr, int]:
return get_minimized_expr('<<', (p.expr_0[0], p.expr_1[0])), p.lineno
@_('expr_ SHR expr_')
def expr_(self, p: ParsedRule) -> Tuple[Expr, int]:
return get_minimized_expr('>>', (p.expr_0[0], p.expr_1[0])), p.lineno
@_('expr_ "^" expr_')
def expr_(self, p: ParsedRule) -> Tuple[Expr, int]:
return get_minimized_expr('^', (p.expr_0[0], p.expr_1[0])), p.lineno
@_('expr_ "|" expr_')
def expr_(self, p: ParsedRule) -> Tuple[Expr, int]:
return get_minimized_expr('|', (p.expr_0[0], p.expr_1[0])), p.lineno
@_('expr_ "&" expr_')
def expr_(self, p: ParsedRule) -> Tuple[Expr, int]:
return get_minimized_expr('&', (p.expr_0[0], p.expr_1[0])), p.lineno
@_('expr_ "?" expr_ ":" expr_')
def expr_(self, p: ParsedRule) -> Tuple[Expr, int]:
return get_minimized_expr('?:', (p.expr_0[0], p.expr_1[0], p.expr_2[0])), p.lineno
@_('expr_ "<" expr_')
def expr_(self, p: ParsedRule) -> Tuple[Expr, int]:
return get_minimized_expr('<', (p.expr_0[0], p.expr_1[0])), p.lineno
@_('expr_ ">" expr_')
def expr_(self, p: ParsedRule) -> Tuple[Expr, int]:
return get_minimized_expr('>', (p.expr_0[0], p.expr_1[0])), p.lineno
@_('expr_ LE expr_')
def expr_(self, p: ParsedRule) -> Tuple[Expr, int]:
return get_minimized_expr('<=', (p.expr_0[0], p.expr_1[0])), p.lineno
@_('expr_ GE expr_')
def expr_(self, p: ParsedRule) -> Tuple[Expr, int]:
return get_minimized_expr('>=', (p.expr_0[0], p.expr_1[0])), p.lineno
@_('expr_ EQ expr_')
def expr_(self, p: ParsedRule) -> Tuple[Expr, int]:
return get_minimized_expr('==', (p.expr_0[0], p.expr_1[0])), p.lineno
@_('expr_ NEQ expr_')
def expr_(self, p: ParsedRule) -> Tuple[Expr, int]:
return get_minimized_expr('!=', (p.expr_0[0], p.expr_1[0])), p.lineno
@_('"(" expr_ ")"')
def expr_(self, p: ParsedRule) -> Tuple[Expr, int]:
return p.expr_
@_('NUMBER')
def expr_(self, p: ParsedRule) -> Tuple[Expr, int]:
return Expr(p.NUMBER), p.lineno
@_('STRING')
def expr_(self, p: ParsedRule) -> Tuple[Expr, int]:
return Expr(p.STRING), p.lineno
@_('"$"')
def expr_(self, p: ParsedRule) -> Tuple[Expr, int]:
return next_address(), p.lineno
@_('id')
def expr_(self, p: ParsedRule) -> Tuple[Expr, int]:
id_str, lineno = p.id
if id_str in self.consts:
return self.consts[id_str], lineno
return Expr(id_str), lineno
def exit_if_errors() -> None:
if error_occurred:
raise FlipJumpParsingException(
f'Errors found in file {curr_file}. ' f'Assembly stopped.\n\nThe Errors:\n{all_errors}'
)
def validate_current_file(files_seen: Set[Union[str, Path]]) -> None:
if not path.isfile(curr_file):
raise FlipJumpParsingException(f"No such file {curr_file}.")
if curr_file_short_name in files_seen:
raise FlipJumpParsingException(f"Short file name is repeated: '{curr_file_short_name}'.")
abs_path = curr_file.absolute()
if abs_path in files_seen:
raise FlipJumpParsingException(f".fj file path is repeated: '{abs_path}'.")
files_seen.add(curr_file_short_name)
files_seen.add(abs_path)
def lex_parse_curr_file(lexer: FJLexer, parser: FJParser) -> None:
global curr_text, curr_namespace
curr_text = curr_file.open('r').read()
curr_namespace = []
lex_res = lexer.tokenize(curr_text)
exit_if_errors()
parser.parse(lex_res)
exit_if_errors()
def parse_macro_tree(
input_files: List[Tuple[str, Path]], memory_width: int, warning_as_errors: bool
) -> Dict[MacroName, Macro]:
"""
parse the .fj files and create a macro-dictionary.
The files will be parsed as if they were concatenated.
@param input_files:[in]: a list of (short_file_name, fj_file_path). The files will to be parsed in that given order.
@param memory_width:[in]: the memory-width
@param warning_as_errors:[in]: treat warnings as errors (stop execution on warnings)
@return: the macro-dictionary.
"""
global curr_file, curr_file_short_name, error_occurred, all_errors
error_occurred = False
all_errors = ''
files_seen: Set[Union[str, Path]] = set()
if not input_files:
raise FlipJumpParsingException("The FlipJump parser got an empty files list.")
lexer = FJLexer()
parser = FJParser(memory_width, warning_as_errors, input_files[0])
for curr_file_short_name, curr_file in input_files:
validate_current_file(files_seen)
lex_parse_curr_file(lexer, parser)
return parser.macros