Skip to content

Commit f76822f

Browse files
committed
check
1 parent 8f54efc commit f76822f

11 files changed

+196
-0
lines changed

tests/var/add.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
42

tests/var/add.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

tests/var/add.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print(40 + 2)

tests/var/input.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
42

tests/var/input.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
42

tests/var/input.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print(input_int())

tests/var/zero.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0

tests/var/zero.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

tests/var/zero.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print(0)

type_check_Pvar.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from ast import *
2+
3+
def check_type_equal(t1, t2, e):
4+
if t1 != t2:
5+
raise Exception('error: ' + repr(t1) + ' != ' + repr(t2) + ' in ' + repr(e))
6+
7+
class TypeCheckPvar:
8+
9+
def type_check_exp(self, e, env):
10+
match e:
11+
case BinOp(left, Add(), right):
12+
l = self.type_check_exp(left, env)
13+
check_type_equal(l, int, left)
14+
r = self.type_check_exp(right, env)
15+
check_type_equal(r, int, right)
16+
return int
17+
case UnaryOp(USub(), v):
18+
t = self.type_check_exp(v, env)
19+
check_type_equal(t, int, v)
20+
return int
21+
case Name(id):
22+
return env[id]
23+
case Constant(value) if isinstance(value, int):
24+
return int
25+
case Call(Name('input_int'), []):
26+
return int
27+
case _:
28+
raise Exception('error in TypeCheckPvar.type_check_exp, unhandled ' + repr(e))
29+
30+
def type_check_stmts(self, ss, env):
31+
if len(ss) == 0:
32+
return
33+
match ss[0]:
34+
case Assign([lhs], value):
35+
t = self.type_check_exp(value, env)
36+
if lhs.id in env:
37+
check_type_equal(env[lhs.id], t, value)
38+
else:
39+
env[lhs.id] = t
40+
return self.type_check_stmts(ss[1:], env)
41+
case Expr(Call(Name('print'), [arg])):
42+
t = self.type_check_exp(arg, env)
43+
check_type_equal(t, int, arg)
44+
return self.type_check_stmts(ss[1:], env)
45+
case Expr(value):
46+
self.type_check_exp(value, env)
47+
return self.type_check_stmts(ss[1:], env)
48+
case _:
49+
raise Exception('error in TypeCheckPvar.type_check_stmt, unhandled ' + repr(s))
50+
51+
def type_check_P(self, p):
52+
match p:
53+
case Module(body):
54+
self.type_check_stmts(body, {})

x86_ast.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
from typing import List
2+
3+
class X86Program:
4+
__match_args__ = ("body",)
5+
def __init__(self, body):
6+
self.body = body
7+
def __repr__(self):
8+
result = ''
9+
if type(self.body) == dict:
10+
for (l,ss) in self.body.items():
11+
result += l + ':\n'
12+
result += '\n'.join([repr(s) for s in ss]) + '\n\n'
13+
else:
14+
result = '\n'.join([repr(s) for s in self.body])
15+
return result
16+
17+
class instr: ...
18+
class arg: ...
19+
class location(arg): ...
20+
21+
class Instr(instr):
22+
instr: str
23+
args: List[arg]
24+
25+
__match_args__ = ("instr", "args")
26+
def __init__(self, instr, args):
27+
self.instr = instr
28+
self.args = args
29+
def source(self):
30+
return self.args[0]
31+
def target(self):
32+
return self.args[-1]
33+
def __repr__(self):
34+
return self.instr + ' ' + ', '.join(repr(a) for a in self.args)
35+
36+
class Callq(instr):
37+
__match_args__ = ("func", "num_args")
38+
def __init__(self, func, num_args):
39+
self.func = func
40+
self.num_args = num_args
41+
def __repr__(self):
42+
return 'callq' + ' ' + self.func
43+
44+
class Variable(location):
45+
__match_args__ = ("id",)
46+
def __init__(self, id):
47+
self.id = id
48+
def __repr__(self):
49+
return self.id
50+
def __eq__(self, other):
51+
if isinstance(other, Variable):
52+
return self.id == other.id
53+
else:
54+
return False
55+
def __hash__(self):
56+
return hash(self.id)
57+
58+
class Immediate(arg):
59+
__match_args__ = ("value",)
60+
def __init__(self, value):
61+
self.value = value
62+
def __repr__(self):
63+
return repr(self.value)
64+
def __eq__(self, other):
65+
if isinstance(other, Immediate):
66+
return self.value == other.value
67+
else:
68+
return False
69+
def __hash__(self):
70+
return hash(self.value)
71+
72+
class Reg(location):
73+
__match_args__ = ("id",)
74+
def __init__(self, id):
75+
self.id = id
76+
def __repr__(self):
77+
return '%' + self.id
78+
def __eq__(self, other):
79+
if isinstance(other, Reg):
80+
return self.id == other.id
81+
else:
82+
return False
83+
def __hash__(self):
84+
return hash(self.id)
85+
86+
class ByteReg(arg):
87+
__match_args__ = ("id",)
88+
def __init__(self, id):
89+
self.id = id
90+
def __repr__(self):
91+
return '%' + self.id
92+
def __eq__(self, other):
93+
if isinstance(other, ByteReg):
94+
return self.id == other.id
95+
else:
96+
return False
97+
def __hash__(self):
98+
return hash(self.id)
99+
100+
class Deref(arg):
101+
__match_args__ = ("reg", "offset")
102+
def __init__(self, reg, offset):
103+
self.reg = reg
104+
self.offset = offset
105+
def __repr__(self):
106+
return repr(self.offset) + '(%' + self.reg + ')'
107+
def __eq__(self, other):
108+
if isinstance(other, Deref):
109+
return self.reg == other.reg and self.offset == other.offset
110+
else:
111+
return False
112+
def __hash__(self):
113+
return hash((self.reg, self.offset))
114+
115+
class JumpIf(instr):
116+
cc: str
117+
label: str
118+
119+
__match_args__ = ("cc", "label")
120+
def __init__(self, cc, label):
121+
self.cc = cc
122+
self.label = label
123+
def __repr__(self):
124+
return 'j' + self.cc + ' ' + self.label
125+
126+
class Jump(instr):
127+
label: str
128+
129+
__match_args__ = ("label",)
130+
def __init__(self, label):
131+
self.label = label
132+
def __repr__(self):
133+
return 'jmp ' + self.label

0 commit comments

Comments
 (0)