1
1
from syntree import *
2
- from analyzer import LabelFactory
3
2
from transasm_recipe import templates
4
3
5
4
def transasm (n ):
@@ -22,37 +21,32 @@ def stat(n):
22
21
case Print ():
23
22
match n .expr .deco ['type' ]:
24
23
case Type .INT :
25
- asm = templates ['print_int' ].format (expr = expr (n .expr ))
24
+ asm = templates ['print_int' ].format (expr = stat (n .expr ))
26
25
case Type .BOOL :
27
- asm = templates ['print_bool' ].format (expr = expr (n .expr ))
26
+ asm = templates ['print_bool' ].format (expr = stat (n .expr ))
28
27
case Type .STRING :
29
28
asm = templates ['print_string' ].format (label = n .expr .deco ['label' ])
30
29
case other : raise Exception ('Unknown expression type' , n .expr )
31
30
return asm + (templates ['print_linebreak' ] if n .newline else '' )
32
31
case Return ():
33
- return (expr (n .expr ) if n .expr is not None and n .expr .deco ['type' ] != Type .VOID else '' ) + '\t ret\n '
32
+ return (stat (n .expr ) if n .expr is not None and n .expr .deco ['type' ] != Type .VOID else '' ) + '\t ret\n '
34
33
case Assign ():
35
- return templates ['assign' ].format (expression = expr (n .expr ),
34
+ return templates ['assign' ].format (expression = stat (n .expr ),
36
35
scope = n .deco ['scope' ]* 4 ,
37
36
variable = n .deco ['offset' ]* 4 )
38
- case FunCall (): return expr (n )
39
37
case While ():
40
- return templates ['while' ].format (condition = expr (n .expr ),
38
+ return templates ['while' ].format (condition = stat (n .expr ),
41
39
label1 = LabelFactory .new_label (),
42
40
label2 = LabelFactory .new_label (),
43
41
body = '' .join ([stat (s ) for s in n .body ]))
44
42
case IfThenElse ():
45
- return templates ['ifthenelse' ].format (condition = expr (n .expr ),
43
+ return templates ['ifthenelse' ].format (condition = stat (n .expr ),
46
44
label1 = LabelFactory .new_label (),
47
45
label2 = LabelFactory .new_label (),
48
46
ibody = '' .join ([stat (s ) for s in n .ibody ]),
49
47
ebody = '' .join ([stat (s ) for s in n .ebody ]))
50
- case other : raise Exception ('Unknown statement type' , n )
51
-
52
- def expr (n ): # convention: all expressions save their results to eax
53
- match n :
54
48
case ArithOp () | LogicOp ():
55
- args = expr (n .left ) + '\t pushl %eax\n ' + expr (n .right ) + '\t movl %eax, %ebx\n \t popl %eax\n '
49
+ args = stat (n .left ) + '\t pushl %eax\n ' + stat (n .right ) + '\t movl %eax, %ebx\n \t popl %eax\n '
56
50
pyeq1 = {'+' :'addl' , '-' :'subl' , '*' :'imull' , '||' :'orl' , '&&' :'andl' }
57
51
pyeq2 = {'<=' :'jle' , '<' :'jl' , '>=' :'jge' , '>' :'jg' , '==' :'je' , '!=' :'jne' }
58
52
if n .op in pyeq1 :
@@ -69,9 +63,9 @@ def expr(n): # convention: all expressions save their results to eax
69
63
case Var ():
70
64
return templates ['var' ].format (scope = n .deco ['scope' ]* 4 , variable = n .deco ['offset' ]* 4 )
71
65
case FunCall ():
72
- return templates ['funcall' ].format (allocargs = '' .join (['%s\t pushl %%eax\n ' % expr (a ) for a in n .args ]),
66
+ return templates ['funcall' ].format (allocargs = '' .join (['%s\t pushl %%eax\n ' % stat (a ) for a in n .args ]),
73
67
varsize = n .deco ['var_cnt' ]* 4 ,
74
68
disphead = n .deco ['var_cnt' ]* 4 + len (n .args )* 4 - 4 ,
75
69
scope = n .deco ['scope' ]* 4 ,
76
70
funlabel = n .deco ['label' ])
77
- case other : raise Exception ('Unknown expression type ' , n )
71
+ case other : raise Exception ('Unknown instruction ' , n )
0 commit comments