-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.py
1266 lines (1037 loc) · 37.8 KB
/
parse.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
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from imp_lexer import RESERVED
import imp_lexer
IF = 'если'
ELSE = 'иначе'
WHILE = 'пока'
ARRAY = 'массив'
AND = 'и'
OR = 'или'
NOT = 'не'
EQUALS = '=='
GOE = '>='
LOE = '<='
GREATER = '>'
LESS = '<'
NOT_EQUALS = '!='
MUL = '*'
DIV = '/'
TRUE_DIV = '//'
FLOAT_POINT = '.'
ADD = '+'
MINUS = '-'
BLOCK_KEYWORDS = [IF, ELSE, WHILE]
OPERATORS = {'+': 'add', '*': 'mul', '-': 'sub', '/': 'div', '==': 'eql', 'и': 'and', 'или': 'or', }
BLOCK_END = '}'
BLOCK_BEGIN = '{'
SEPARATOR = ';'
ASSIGN = '='
PRINT = 'вывод'
INPUT = 'ввод'
class AssignStatement:
def __init__(self, tokens):
self.name = tokens[0][0]
self.condition = ConditionStatement(tokens[2:])
self.GA = None
def build_tree(self):
return 'AssignStatement(' + self.name + ' = ' + self.condition.build_tree() + ')'
def get_vars(self):
return [self.name] + self.condition.get_vars()
def get_numbers(self):
return self.condition.get_numbers()
def send_GA(self, GA):
self.condition.send_GA(GA)
self.GA = GA
def assemble(self):
b, n, ra = self.condition.assemble_final('EAX')
n = n + '\n' * min(1, len(n)) + f'mov DWORD PTR {self.GA.variable_address(self.name)}, {ra}'
return b, n
class PrintStatement:
def __init__(self, tokens):
self.condition = ConditionStatement(tokens[1:])
self.GA = None
def build_tree(self):
return 'PrintStatement(' + self.condition.build_tree() + ')'
def get_vars(self):
return self.condition.get_vars()
def get_numbers(self):
return self.condition.get_numbers()
def send_GA(self, GA):
self.condition.send_GA(GA)
self.GA = GA
def assemble(self):
begin, now, res_address = self.condition.assemble()
if str(res_address)[0] != '"':
now = now + '\n' * min(len(now), 1) + f'outint {res_address}\nnewline'
else:
now = now + '\n' * min(len(now), 1) + f'outstr {res_address}\nnewline'
return begin, now
class InputStatement:
def __init__(self, tokens):
if len(tokens) != 2:
raise RuntimeWarning(tokens, 'слишком много')
self.name = tokens[1][0]
def build_tree(self):
return 'InputStatement(' + self.name + ')'
def get_vars(self):
return [self.name]
def get_numbers(self):
return []
def send_GA(self, GA):
self.GA = GA
def assemble(self):
begin = ''
now = f'inint ECX ; Компилятор: сохраняем значение в регистр. Кто не понял, тот бот. Это MASM Дитя\n' + \
f'mov DWORD PTR {self.GA.variable_address(self.name)}, ECX ; Выгружаем значение из регистра куда надо'
return begin, now
class PassStatement:
def __init__(self, tokens):
pass
def build_tree(self):
return ''
def get_vars(self):
return []
def get_numbers(self):
return []
def send_GA(self, GA):
self.GA = GA
def assemble(self):
return '', ''
class ASMCodeInclude:
def __init__(self, tokens):
self.tokens = tokens
self.filename = tokens[0][0][25:]
self.asm_code = open(self.filename).read()
def build_tree(self):
return ''
def get_vars(self):
return []
def get_numbers(self):
return []
def send_GA(self, GA):
self.GA = GA
def assemble(self):
now = f'and al, al; Это включение в код, filename - {self.filename}\n{self.asm_code}'
return '', now
class ASMDataInclude:
def __init__(self, tokens):
self.tokens = tokens
self.filename = tokens[0][0][25:]
self.asm_data = open(self.filename).read()
def build_tree(self):
return ''
def get_vars(self):
return []
def get_numbers(self):
return []
def send_GA(self, GA):
self.GA = GA
def assemble(self):
return self.asm_data, ''
class ASMIncludeAppend:
def __init__(self, tokens):
self.tokens = tokens
self.filename = tokens[0][0][18:]
def build_tree(self):
return ''
def get_vars(self):
return []
def get_numbers(self):
return []
def send_GA(self, GA):
self.GA = GA
def assemble(self):
self.GA.append_include(self.filename)
return '', ''
def LowLevelStatement(tokens):
if len(tokens) == 0:
return PassStatement(tokens)
if 'ASM_CODE' == tokens[0][1]:
return ASMCodeInclude(tokens)
if 'ASM_DATA' == tokens[0][1]:
return ASMDataInclude(tokens)
if 'ASM_INCLUDE' == tokens[0][1]:
raise RuntimeWarning(tokens, 'ASSEMBLER_APPEND_INCLUDE')
if 'INCLUDE' == tokens[0][1]:
return IncludeCode(tokens)
if (PRINT, RESERVED) == tokens[0]:
return PrintStatement(tokens)
if (INPUT, RESERVED) == tokens[0]:
return InputStatement(tokens)
if (ASSIGN, RESERVED) == tokens[1]:
return AssignStatement(tokens)
# if (ARRAY, RESERVED) == tokens[0]:
# return ArrayStatement(tokens)
raise RuntimeWarning(tokens, 'что то не то')
class NumberStatement:
def __init__(self, tokens):
self.value = int(tokens[0][0])
def build_tree(self):
return str(self.value)
def get_vars(self):
return []
def get_numbers(self):
return [self.value]
def send_GA(self, GA):
self.GA = GA
def assemble(self):
begin = ''
now = ''
res_address = self.value
return begin, now, res_address
def assemble_final(self, res_address):
begin = ''
now = ''
res_address = self.value
return begin, now, res_address
class StringStatement:
def __init__(self, tokens):
self.value = tokens[0][0][1:-1]
def build_tree(self):
return "'" + self.value + "'"
def get_vars(self):
return []
def send_GA(self, GA):
self.GA = GA
def assemble(self):
begin = ''
now = ''
res_address = '"' + self.value + '"'
return begin, now, res_address
def assemble_final(self, res_address):
begin = ''
now = ''
res_address = '"' + self.value + '"'
return begin, now, res_address
def index(list1, el1):
try:
return list1.index(el1)
except ValueError:
return -1
def union(list1):
res = []
for el in list1:
if type(el) == type(list()):
res += el
else:
res += [el]
return res
class OperatorStatement:
def __init__(self, tokens):
self.op = tokens[0]
def build_tree(self):
return 'OP(' + self.op + ')'
def get_vars(self):
return []
def get_numbers(self):
return []
def send_GA(self, GA):
self.GA = GA
class VarStatement:
def __init__(self, tokens):
self.name = tokens[0][0]
def build_tree(self):
return 'Var:' + self.name
def get_vars(self):
return [self.name]
def get_numbers(self):
return []
def send_GA(self, GA):
self.GA = GA
def assemble(self):
return '', '', 'DWORD PTR ' + self.GA.variable_address(self.name)
def assemble_final(self, ra):
return '', f'mov EDX, DWORD PTR {self.GA.variable_address(self.name)} ;Извлекаем в регистр для меньшего ' \
f'количества багов\n' + \
f'mov {ra}, EDX ;Дада, запихиваем зачем-то снова', ra
def PreStatement(tokens):
# print('DEBUG:PreSTATEMENT:TOKENS:', tokens)
if tokens[0][0].isdigit():
return NumberStatement(tokens)
if tokens[0][1] == 'STRING':
return StringStatement(tokens)
else:
return VarStatement(tokens)
class ArithmeticStatement:
def __init__(self, tokens):
self.tokens = tokens
self.statements = []
i = 0
nesting_depth = 0
statement_begin = 0
while i < len(self.tokens):
if self.tokens[i] == ('(', RESERVED):
nesting_depth += 1
if self.tokens[i] == (')', RESERVED):
nesting_depth -= 1
if self.tokens[i] == ('[', RESERVED):
nesting_depth += 1
if self.tokens[i] == (']', RESERVED):
nesting_depth -= 1
if self.tokens[i] == ('(', RESERVED) and nesting_depth == 1:
statement_begin = i
# if (i - 1) < len(self.tokens) and nesting_depth == 0 and self.tokens[i][1] == 'ID' and self.tokens[
# i + 1] == ('[', RESERVED):
# statement_begin = i
# if self.tokens[i] == (']', RESERVED) and nesting_depth == 0:
# self.statements.append(tokens[statement_begin:i + 1])
elif self.tokens[i] == (')', RESERVED) and nesting_depth == 0:
self.statements.append(tokens[statement_begin:i + 1])
elif nesting_depth == 0:
self.statements.append(tokens[i])
i += 1
# print('DEBUG:ARITHMETIC:STATEMENTS', self.statements)
index_add = index(self.statements, (ADD, RESERVED))
index_min = index(self.statements, (MINUS, RESERVED))
index_div = index(self.statements, (DIV, RESERVED))
index_mul = index(self.statements, (MUL, RESERVED))
# print('DEBUG:ARITHMETIC:POSITION:', index_add, index_min, index_div, index_mul)
self.statement1 = None
self.statement2 = None
self.operator = None
if index_add != -1:
# print('DEBUG:ADD:')
self.statement1 = union(self.statements[:index_add])
self.statement2 = union(self.statements[index_add + 1:])
self.operator = self.statements[index_add]
elif index_min != -1:
# print('DEBUG:MIN:')
self.statement1 = union(self.statements[:index_min])
self.statement2 = union(self.statements[index_min + 1:])
self.operator = self.statements[index_min]
elif index_div != -1:
# print('DEBUG:DIV:')
self.statement1 = union(self.statements[:index_div])
self.statement2 = union(self.statements[index_div + 1:])
self.operator = self.statements[index_div]
elif index_mul != -1:
# print('DEBUG:MUL:')
self.statement1 = union(self.statements[:index_mul])
self.statement2 = union(self.statements[index_mul + 1:])
self.operator = self.statements[index_mul]
# print('DEBUG:ARITHMETIC:S1:S2:OP:', self.statement1, self.statement2, self.operator)
else:
raise RuntimeError('чето нет оператора', self.tokens)
self.statement1 = ConditionStatement(self.statement1)
self.statement2 = ConditionStatement(self.statement2)
self.operator = OperatorStatement(self.operator)
def build_tree(self):
return 'ArithmeticStatement(' + self.statement1.build_tree() + '~' + self.operator.build_tree() + \
'~' + self.statement2.build_tree() + ')'
def get_vars(self):
return self.statement1.get_vars() + self.statement2.get_vars()
def get_numbers(self):
return self.statement1.get_numbers() + self.statement2.get_numbers()
def send_GA(self, GA):
self.statement1.send_GA(GA)
self.statement2.send_GA(GA)
self.GA = GA
def assemble(self):
b1, n1, r1 = self.statement1.assemble()
b2, n2, r2 = self.statement2.assemble()
res_address = self.GA.arith_address
begin = b1 + '\n' + b2 + f'\n{res_address} dq ?'
now1 = n1 + '\n' * min(len(n1), 1) + n2 + '\n' * min(len(n1), 1)
now2 = ''
if OPERATORS[self.operator.op] == 'add':
now2 = f"""mov EAX, {r1}; переносим значение первого выражения в регистр
mov EBX, {r2} ; переносим значение второго выражения в регистр
add EAX, EBX ; складываем
mov DWORD PTR {res_address}, EAX ; переносим из регистра в ячейку памяти - результат"""
if OPERATORS[self.operator.op] == 'mul':
now2 = f"""mov EAX, {r1}; переносим значение первого выражения в регистр
mov EBX, {r2} ; переносим значение второго выражения в регистр
imul EBX ; перемножаем - результат в EAX
mov DWORD PTR {res_address}, EAX ; переносим из регистра в ячейку памяти - результат"""
if OPERATORS[self.operator.op] == 'add':
now2 = f"""mov EAX, {r1}; переносим значение первого выражения в регистр
mov EBX, {r2} ; переносим значение второго выражения в регистр
sub EAX, EBX ; вычитаем
mov DWORD PTR {res_address}, EAX ; переносим из регистра в ячейку памяти - результат"""
if OPERATORS[self.operator.op] == 'div':
now2 = f"""mov EAX, {r1}; переносим значение первого выражения в регистр
mov EBX, {r2} ; переносим значение второго выражения в регистр
mov EDX, 0; вы все боты - хаха
idiv EBX ; делим - результат в EAX
mov DWORD PTR {res_address}, EAX ; переносим из регистра в ячейку памяти - результат"""
return begin, now1 + '\n' + now2, 'DWORD PTR ' + res_address
def assemble_final(self, res_address):
b1, n1, r1 = self.statement1.assemble()
b2, n2, r2 = self.statement2.assemble()
begin = b1 + '\n' + b2
now1 = n1 + '\n' * min(len(n1), 1) + n2 + '\n' * min(len(n1), 1)
now2 = ''
if OPERATORS[self.operator.op] == 'add':
now2 = f"""mov EAX, {r1}; переносим значение первого выражения в регистр
mov EBX, {r2} ; переносим значение второго выражения в регистр
add EAX, EBX ; складываем
mov {res_address}, EAX ; переносим из регистра в ячейку памяти - результат"""
if OPERATORS[self.operator.op] == 'mul':
now2 = f"""mov EAX, {r1}; переносим значение первого выражения в регистр
mov EBX, {r2} ; переносим значение второго выражения в регистр
imul EBX ; перемножаем - результат в EAX
mov {res_address}, EAX ; переносим из регистра в ячейку памяти - результат"""
if OPERATORS[self.operator.op] == 'sub':
now2 = f"""mov EAX, {r1}; переносим значение первого выражения в регистр
mov EBX, {r2} ; переносим значение второго выражения в регистр
sub EAX, EBX ; вычитаем
mov {res_address}, EAX ; переносим из регистра в ячейку памяти - результат"""
if OPERATORS[self.operator.op] == 'div':
now2 = f"""mov EAX, {r1}; переносим значение первого выражения в регистр
mov EBX, {r2} ; переносим значение второго выражения в регистр
mov EDX, 0; вы все боты
idiv EBX ; делим - результат в EAX
mov {res_address}, EAX ; переносим из регистра в ячейку памяти - результат"""
return begin, now1 + '\n' + now2, res_address
"""
class ItemStatement:
def __init__(self, tokens):
self.tokens = tokens
self.name = tokens[0][0]
self.condition = tokens[2:-1]
self.condition = ConditionStatement(self.condition)
def get_vars(self):
return [self.name] + self.condition.get_vars()
def get_numbers(self):
return self.condition.get_numbers()
def send_GA(self, GA):
self.condition.send_GA(GA)
self.GA = GA
def assemble(self):
b, n, ra = self.condition.assemble()
class ArrayStatement:
def __init__(self, tokens):
self.name = tokens[1][0]
self.length = int(tokens[2][0])
self.tokens = tokens
def get_vars(self):
return [self.name]
def get_numbers(self):
return [self.length]
def send_GA(self, GA):
self.GA = GA
self.address = self.GA.array_address(self.length)
"""
def ConditionStatement(tokens):
# print('DEBUG:ConditionStatement:tokens:', tokens)
if len(tokens) == 1:
return PreStatement(tokens)
while ('(', RESERVED) in tokens:
i = 0
nesting_depth = 0
while nesting_depth > 0 or i == 0:
if tokens[i][0] == '(':
nesting_depth += 1
if tokens[i][0] == ')':
nesting_depth -= 1
i += 1
if i == len(tokens):
tokens = tokens[1:-1]
else:
break
if len(tokens) == 1:
return PreStatement(tokens)
# if tokens[-1][0] == ']':
# return ItemStatement(tokens)
return ArithmeticStatement(tokens)
def isBoolOperatorIn(tokens):
if (EQUALS, RESERVED) in tokens:
return True
if (GOE, RESERVED) in tokens:
return True
if (LOE, RESERVED) in tokens:
return True
if (GREATER, RESERVED) in tokens:
return True
if (LESS, RESERVED) in tokens:
return True
if (AND, RESERVED) in tokens:
return True
if (OR, RESERVED) in tokens:
return True
if ('!=', 'RESERVED') in tokens:
return True
return False
def BoolConditionStatement(tokens):
while ('(', RESERVED) in tokens:
i = 0
nesting_depth = 0
while nesting_depth > 0 or i == 0:
if tokens[i][0] == '(':
nesting_depth += 1
if tokens[i][0] == ')':
nesting_depth -= 1
i += 1
if i == len(tokens):
tokens = tokens[1:-1]
else:
break
if (OR, RESERVED) in tokens:
return BoolLogicConditionStatement(tokens)
if (AND, RESERVED) in tokens:
return BoolLogicConditionStatement(tokens)
if (EQUALS, RESERVED) in tokens:
return BoolCMPConditionStatement(tokens)
if (GOE, RESERVED) in tokens:
return BoolCMPConditionStatement(tokens)
if (LOE, RESERVED) in tokens:
return BoolCMPConditionStatement(tokens)
if (GREATER, RESERVED) in tokens:
return BoolCMPConditionStatement(tokens)
if (LESS, RESERVED) in tokens:
return BoolCMPConditionStatement(tokens)
if ('!=', 'RESERVED') in tokens:
return BoolCMPConditionStatement(tokens)
return False
class BoolLogicConditionStatement:
def __init__(self, tokens):
print('gg')
self.tokens = tokens
# if not isBoolOperatorIn(tokens):
# raise RuntimeWarning('нету операторов логических', tokens)
self.statements = []
i = 0
nesting_depth = 0
statement_begin = 0
while i < len(self.tokens):
if self.tokens[i] == ('(', RESERVED):
nesting_depth += 1
if self.tokens[i] == (')', RESERVED):
nesting_depth -= 1
if self.tokens[i] == ('(', RESERVED) and nesting_depth == 1:
statement_begin = i
if self.tokens[i] == (')', RESERVED) and nesting_depth == 0:
self.statements.append(tokens[statement_begin:i + 1])
elif nesting_depth == 0:
self.statements.append(tokens[i])
i += 1
index_or = index(self.statements, (OR, RESERVED))
index_and = index(self.statements, (AND, RESERVED))
if index_or != -1:
# print('DEBUG:OR:')
self.statement1 = union(self.statements[:index_or])
self.statement2 = union(self.statements[index_or + 1:])
self.operator = self.statements[index_or]
elif index_and != -1:
# print('DEBUG:AND:')
self.statement1 = union(self.statements[:index_and])
self.statement2 = union(self.statements[index_and + 1:])
self.operator = self.statements[index_and]
else:
raise RuntimeError('нету операторов', self.tokens)
if isBoolOperatorIn(self.statement1):
self.statement1 = BoolConditionStatement(self.statement1)
else:
self.statement1 = ConditionStatement(self.statement1)
if isBoolOperatorIn(self.statement2):
self.statement2 = BoolConditionStatement(self.statement2)
else:
self.statement2 = ConditionStatement(self.statement2)
self.operator = OperatorStatement(self.operator)
def build_tree(self):
return 'BoolConditionStatement(' + self.statement1.build_tree() + '~' + self.operator.build_tree() + '~' + self.statement2.build_tree() + ')'
def get_vars(self):
return self.statement1.get_vars() + self.statement2.get_vars()
def get_numbers(self):
return self.statement1.get_numbers() + self.statement2.get_numbers()
def send_GA(self, GA):
self.statement1.send_GA(GA)
self.statement2.send_GA(GA)
self.GA = GA
def assemble(self):
b1, n1, r1 = self.statement1.assemble()
b2, n2, r2 = self.statement2.assemble()
res_address = self.GA.bool_address
begin = b1 + '\n' + b2 + f'\n{res_address} dq ?'
op = self.operator.op
nb = n1 + '\n' * min(len(n1), 1) + n2 + '\n' * min(len(n1), 1)
now = ''
print('DEBUG:OP', op)
if op == 'или':
now = f'''mov EAX, {r1}
mov EBX, {r2}
or EAX, EBX
mov DWORD PTR {res_address}, EAX'''
elif op == 'и':
now = f'''mov EAX, {r1}
mov EBX, {r2}
and EAX, EBX
mov DWORD PTR {res_address}, EAX'''
now = nb + now
return begin, now, 'DWORD PTR ' + res_address
def assemble_final(self, res_address):
b1, n1, r1 = self.statement1.assemble()
b2, n2, r2 = self.statement2.assemble()
begin = b1 + '\n' + b2 + f''
op = self.operator.op
nb = n1 + '\n' * min(len(n1), 1) + n2 + '\n' * min(len(n1), 1)
now = ''
# print('DEBUG:OP', op)
if op == 'или':
now = f'''mov EAX, {r1}
mov EBX, {r2}
or EAX, EBX
mov {res_address}, EAX'''
elif op == 'и':
now = f'''mov EAX, {r1}
mov EBX, {r2}
and EAX, EBX
mov {res_address}, EAX'''
now = nb + now
return begin, now, res_address
class BoolCMPConditionStatement:
def __init__(self, tokens):
self.tokens = tokens
if not isBoolOperatorIn(tokens):
raise RuntimeWarning('нету операторов логических', tokens)
self.statements = []
i = 0
nesting_depth = 0
statement_begin = 0
while i < len(self.tokens):
if self.tokens[i] == ('(', RESERVED):
nesting_depth += 1
if self.tokens[i] == (')', RESERVED):
nesting_depth -= 1
if self.tokens[i] == ('(', RESERVED) and nesting_depth == 1:
statement_begin = i
if self.tokens[i] == (')', RESERVED) and nesting_depth == 0:
self.statements.append(tokens[statement_begin:i + 1])
elif nesting_depth == 0:
self.statements.append(tokens[i])
i += 1
index_ls = index(self.statements, (LESS, RESERVED))
index_gr = index(self.statements, (GREATER, RESERVED))
index_goe = index(self.statements, (GOE, RESERVED))
index_loe = index(self.statements, (LOE, RESERVED))
index_eq = index(self.statements, (EQUALS, RESERVED))
index_not_eq = index(self.statements, (NOT_EQUALS, RESERVED))
if index_gr != -1:
# print('DEBUG:GR:')
self.statement1 = union(self.statements[:index_gr])
self.statement2 = union(self.statements[index_gr + 1:])
self.operator = self.statements[index_gr]
elif index_ls != -1:
# print('DEBUG:LS:')
self.statement1 = union(self.statements[:index_ls])
self.statement2 = union(self.statements[index_ls + 1:])
self.operator = self.statements[index_ls]
elif index_goe != -1:
# print('DEBUG:GOE:')
self.statement1 = union(self.statements[:index_goe])
self.statement2 = union(self.statements[index_goe + 1:])
self.operator = self.statements[index_goe]
elif index_loe != -1:
# print('DEBUG:LOE:')
self.statement1 = union(self.statements[:index_loe])
self.statement2 = union(self.statements[index_loe + 1:])
self.operator = self.statements[index_loe]
elif index_eq != -1:
# print('DEBUG:EQ:')
self.statement1 = union(self.statements[:index_eq])
self.statement2 = union(self.statements[index_eq + 1:])
self.operator = self.statements[index_eq]
elif index_not_eq != -1:
# print('DEBUG:EQ:')
self.statement1 = union(self.statements[:index_not_eq])
self.statement2 = union(self.statements[index_not_eq + 1:])
self.operator = self.statements[index_not_eq]
else:
raise RuntimeError('нету операторов', self.tokens)
if isBoolOperatorIn(self.statement1):
self.statement1 = BoolConditionStatement(self.statement1)
else:
self.statement1 = ConditionStatement(self.statement1)
if isBoolOperatorIn(self.statement2):
self.statement2 = BoolConditionStatement(self.statement2)
else:
self.statement2 = ConditionStatement(self.statement2)
self.operator = OperatorStatement(self.operator)
def build_tree(self):
return 'BoolConditionStatement(' + self.statement1.build_tree() + '~' + self.operator.build_tree() + '~' + self.statement2.build_tree() + ')'
def get_vars(self):
return self.statement1.get_vars() + self.statement2.get_vars()
def get_numbers(self):
return self.statement1.get_numbers() + self.statement2.get_numbers()
def send_GA(self, GA):
self.statement1.send_GA(GA)
self.statement2.send_GA(GA)
self.GA = GA
def assemble(self):
b1, n1, r1 = self.statement1.assemble()
b2, n2, r2 = self.statement2.assemble()
res_address = self.GA.bool_address
begin = b1 + '\n' + b2 + '\n' + f'{res_address} dq ?'
op = self.operator.op
now = ''
nb1 = n1 + '\n' * min(len(n1), 1) + n2 + '\n' * min(len(n1), 1)
# print('DEBUG:OP', op)
n1 = self.GA.bool_address
n2 = self.GA.bool_address
if op == '==':
now = f'''mov EAX, {r1}
mov EBX, {r2}
cmp EAX, EBX
jne next_{n1}
mov ECX, 1
jmp next_{n2}
next_{n1}:
mov ECX, 0
next_{n2}:
mov DWORD PTR {res_address}, ECX'''
elif op == '!=':
now = f'''mov EAX, {r1}
mov EBX, {r2}
cmp EAX, EBX
je next_{n1}
mov ECX, 1
jmp next_{n2}
next_{n1}:
mov ECX, 0
next_{n2}:
mov DWORD PTR {res_address}, ECX'''
elif op == '>':
now = f'''mov EAX, {r1}
mov EBX, {r2}
cmp EAX, EBX
jng next_{n1}
mov ECX, 1
jmp next_{n2}
next_{n1}:
mov ECX, 0
next_{n2}:
mov DWORD PTR {res_address}, ECX'''
elif op == '<':
now = f'''mov EAX, {r1}
mov EBX, {r2}
cmp EAX, EBX
jnl next_{n1}
mov ECX, 1
jmp next_{n2}
next_{n1}:
mov ECX, 0
next_{n2}:
mov DWORD PTR {res_address}, ECX'''
elif op == '>=':
now = f'''mov EAX, {r1}
mov EBX, {r2}
cmp EAX, EBX
jnge next_{n1}
mov ECX, 1
jmp next_{n2}
next_{n1}:
mov ECX, 0
next_{n2}:
mov DWORD PTR {res_address}, ECX'''
elif op == '<=':
now = f'''mov EAX, {r1}
mov EBX, {r2}
cmp EAX, EBX
jnle next_{n1}
mov ECX, 1
jmp next_{n2}
next_{n1}:
mov ECX, 0
next_{n2}:
mov DWORD PTR {res_address}, ECX'''
return begin, nb1 + now, 'DWORD PTR ' + res_address
def assemble_final(self, res_address):
b1, n1, r1 = self.statement1.assemble()
b2, n2, r2 = self.statement2.assemble()
begin = b1 + '\n' + b2
op = self.operator.op
now = ''
nb1 = n1 + '\n' * min(len(n1), 1) + n2 + '\n' * min(len(n1), 1)
# print('DEBUG:OP', op)
n1 = self.GA.bool_address
n2 = self.GA.bool_address
if op == '==':
now = f'''mov EAX, {r1}
mov EBX, {r2}
cmp EAX, EBX
jne next_{n1}
mov ECX, 1
jmp next_{n2}
next_{n1}:
mov ECX, 0
next_{n2}:
mov {res_address}, ECX'''
elif op == '!=':
now = f'''mov EAX, {r1}
mov EBX, {r2}
cmp EAX, EBX
je next_{n1}
mov ECX, 1
jmp next_{n2}
next_{n1}:
mov ECX, 0
next_{n2}:
mov {res_address}, ECX'''
elif op == '>':
now = f'''mov EAX, {r1}
mov EBX, {r2}
cmp EAX, EBX
jng next_{n1}
mov ECX, 1
jmp next_{n2}
next_{n1}:
mov ECX, 0
next_{n2}:
mov {res_address}, ECX'''
elif op == '<':
now = f'''mov EAX, {r1}
mov EBX, {r2}
cmp EAX, EBX
jnl next_{n1}
mov ECX, 1
jmp next_{n2}
next_{n1}:
mov ECX, 0
next_{n2}:
mov {res_address}, ECX'''
elif op == '>=':
now = f'''mov EAX, {r1}
mov EBX, {r2}
cmp EAX, EBX
jnge next_{n1}
mov ECX, 1
jmp next_{n2}
next_{n1}:
mov ECX, 0
next_{n2}:
mov {res_address}, ECX'''
elif op == '<=':
now = f'''mov EAX, {r1}
mov EBX, {r2}
cmp EAX, EBX
jnle next_{n1}
mov ECX, 1
jmp next_{n2}
next_{n1}:
mov ECX, 0
next_{n2}:
mov {res_address}, ECX'''
return begin, nb1 + now, res_address
class BlockStatement:
def __init__(self, tokens):
self.block_begin = tokens.index((BLOCK_BEGIN, RESERVED))
self.block_tokens = tokens[self.block_begin + 1:len(tokens) - 1]
self.condition = tokens[1:self.block_begin]
self.condition = BoolConditionStatement(self.condition)
self.statement = Statement(self.block_tokens)