Skip to content

Commit d617131

Browse files
committed
polishing
1 parent e5c36d1 commit d617131

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

transasm.py

+9-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from syntree import *
2-
from analyzer import LabelFactory
32
from transasm_recipe import templates
43

54
def transasm(n):
@@ -22,37 +21,32 @@ def stat(n):
2221
case Print():
2322
match n.expr.deco['type']:
2423
case Type.INT:
25-
asm = templates['print_int'].format(expr = expr(n.expr))
24+
asm = templates['print_int'].format(expr = stat(n.expr))
2625
case Type.BOOL:
27-
asm = templates['print_bool'].format(expr = expr(n.expr))
26+
asm = templates['print_bool'].format(expr = stat(n.expr))
2827
case Type.STRING:
2928
asm = templates['print_string'].format(label = n.expr.deco['label'])
3029
case other: raise Exception('Unknown expression type', n.expr)
3130
return asm + (templates['print_linebreak'] if n.newline else '')
3231
case Return():
33-
return (expr(n.expr) if n.expr is not None and n.expr.deco['type'] != Type.VOID else '') + '\tret\n'
32+
return (stat(n.expr) if n.expr is not None and n.expr.deco['type'] != Type.VOID else '') + '\tret\n'
3433
case Assign():
35-
return templates['assign'].format(expression = expr(n.expr),
34+
return templates['assign'].format(expression = stat(n.expr),
3635
scope = n.deco['scope']*4,
3736
variable = n.deco['offset']*4)
38-
case FunCall(): return expr(n)
3937
case While():
40-
return templates['while'].format(condition = expr(n.expr),
38+
return templates['while'].format(condition = stat(n.expr),
4139
label1 = LabelFactory.new_label(),
4240
label2 = LabelFactory.new_label(),
4341
body = ''.join([stat(s) for s in n.body]))
4442
case IfThenElse():
45-
return templates['ifthenelse'].format(condition = expr(n.expr),
43+
return templates['ifthenelse'].format(condition = stat(n.expr),
4644
label1 = LabelFactory.new_label(),
4745
label2 = LabelFactory.new_label(),
4846
ibody = ''.join([stat(s) for s in n.ibody]),
4947
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:
5448
case ArithOp() | LogicOp():
55-
args = expr(n.left) + '\tpushl %eax\n' + expr(n.right) + '\tmovl %eax, %ebx\n\tpopl %eax\n'
49+
args = stat(n.left) + '\tpushl %eax\n' + stat(n.right) + '\tmovl %eax, %ebx\n\tpopl %eax\n'
5650
pyeq1 = {'+':'addl', '-':'subl', '*':'imull', '||':'orl', '&&':'andl'}
5751
pyeq2 = {'<=':'jle', '<':'jl', '>=':'jge', '>':'jg', '==':'je', '!=':'jne'}
5852
if n.op in pyeq1:
@@ -69,9 +63,9 @@ def expr(n): # convention: all expressions save their results to eax
6963
case Var():
7064
return templates['var'].format(scope = n.deco['scope']*4, variable = n.deco['offset']*4)
7165
case FunCall():
72-
return templates['funcall'].format(allocargs = ''.join(['%s\tpushl %%eax\n' % expr(a) for a in n.args]),
66+
return templates['funcall'].format(allocargs = ''.join(['%s\tpushl %%eax\n' % stat(a) for a in n.args]),
7367
varsize = n.deco['var_cnt']*4,
7468
disphead = n.deco['var_cnt']*4 + len(n.args)*4 - 4,
7569
scope = n.deco['scope']*4,
7670
funlabel = n.deco['label'])
77-
case other: raise Exception('Unknown expression type', n)
71+
case other: raise Exception('Unknown instruction', n)

0 commit comments

Comments
 (0)