Skip to content

Commit 587bd8f

Browse files
committed
add grammars
1 parent 1441b56 commit 587bd8f

File tree

3 files changed

+88
-13
lines changed

3 files changed

+88
-13
lines changed

compiler.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,27 @@ class Compiler:
1515
# Remove Complex Operands
1616
############################################################################
1717

18-
def rco_exp(self, e: expr, need_atomic: bool) -> Tuple[expr, Temporaries]:
19-
# YOUR CODE HERE
20-
raise Exception('rco_exp not implemented')
18+
def rco_exp(self, e: expr) -> Tuple[expr, Temporaries]:
19+
match e:
20+
case Constant(int):
21+
22+
case Call(Name('input_int'),[]):
23+
24+
case UnaryOp(USub(), exp):
25+
(new_exp, temps) = self.rco_exp(exp)
26+
27+
case BinOp(exp1, Add(), exp2):
28+
(new_exp1, temps1) = self.rco_exp(exp1)
29+
(new_exp2, temps2) = self.rco_exp(exp2)
30+
31+
case BinOp(exp1, Sub(), exp2):
32+
(new_exp1, temps1) = self.rco_exp(exp1)
33+
(new_exp2, temps2) = self.rco_exp(exp2)
34+
35+
case Name(var):
36+
37+
case _:
38+
raise Exception('rco_exp unexpected ' + repr(e))
2139

2240
def rco_stmt(self, s: stmt) -> List[stmt]:
2341
# YOUR CODE HERE

grammmars.txt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
L_var
3+
4+
exp ::= Constant(int)
5+
| Call(Name('input_int'),[])
6+
| UnaryOp(USub(), exp)
7+
| BinOp(exp, Add(), exp)
8+
| BinOp(exp, Sub(), exp)
9+
| Name(var)
10+
stmt ::= Expr(Call(Name('print'), [exp]))
11+
| Expr(exp)
12+
| Assign([Name(var)], exp)
13+
LVar ::= Module(stmt∗)
14+
15+
L_var^mon
16+
17+
atm ::= Constant(int)
18+
| Name(var)
19+
exp ::= atm
20+
| Call(Name('input_int'),[])
21+
| UnaryOp(USub(), atm)
22+
| BinOp(atm, Add(), atm)
23+
| BinOp(atm, Sub(), atm)
24+
stmt ::= Expr(Call(Name('print'), [atm]))
25+
| Expr(exp)
26+
| Assign([Name(var)], exp)
27+
LVar ::= Module(stmt∗)
28+
29+
X86_Var
30+
31+
reg ::= 'rsp' | 'rbp' | 'rax' | 'rbx' | 'rcx' | 'rdx' | 'rsi' | 'rdi'
32+
| 'r8' | 'r9' | 'r10' | 'r11' | 'r12' | 'r13' | 'r14' | 'r15'
33+
arg ::= Immediate(int)
34+
| Reg(reg)
35+
| Deref(reg,int)
36+
| Variable(var)
37+
instr ::= Instr('addq',[arg,arg])
38+
| Instr('subq',[arg,arg])
39+
| Instr('movq',[arg,arg])
40+
| Instr('negq',[arg])
41+
| Instr('pushq',[arg])
42+
| Instr('popq',[arg])
43+
| Callq(label,int)
44+
| Instr('retq',[])
45+
| Jump(label)
46+
x86_Var ::= X86Program(instr∗)
47+
48+
49+
X86_Int
50+
51+
Remove Variable(var) from arg.
52+
53+
arg ::= Immediate(int)
54+
| Reg(reg)
55+
| Deref(reg,int)
56+

simple-test.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,20 @@ def execute_and_check(program_filename, program_ast):
9696
print(repr(program_ast))
9797
print()
9898

99+
# Remove Complex Operands
100+
program_ast = C.remove_complex_operands(program_ast)
101+
print('After RCO')
102+
print('Concrete Syntax:')
103+
print(program_ast)
104+
print('Abstract Syntax:')
105+
print(repr(program_ast))
106+
interp_and_check(program_filename, program_ast,
107+
interp_Lvar, 'remove complex operands')
108+
print()
109+
99110
if False:
100111
# Move up each pass once it is completed
101112

102-
# Remove Complex Operands
103-
program_ast = C.remove_complex_operands(program_ast)
104-
print('After RCO')
105-
print('Concrete Syntax:')
106-
print(program_ast)
107-
print('Abstract Syntax:')
108-
print(repr(program_ast))
109-
interp_and_check(program_filename, program_ast,
110-
interp_Lvar, 'remove complex operands')
111-
print()
112113

113114
# Select Instructions
114115
program_ast = C.select_instructions(program_ast)

0 commit comments

Comments
 (0)