-
Notifications
You must be signed in to change notification settings - Fork 1
/
interp_Cfun.py
64 lines (58 loc) · 2.05 KB
/
interp_Cfun.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
from ast import *
from interp_Ctup import InterpCtup
from utils import *
from interp_Lfun import Function
class InterpCfun(InterpCtup):
def apply_fun(self, fun, args, e):
match fun:
case Function(name, xs, blocks, env):
old_blocks = self.blocks
self.blocks = blocks
trace('apply_fun ' + name)
# trace(blocks.keys())
new_env = {x: v for (x,v) in env.items()}
for (x,arg) in zip(xs, args):
new_env[x] = arg
ret = self.interp_stmts(blocks[label_name(name + 'start')], new_env)
self.blocks = old_blocks
return ret
case _:
raise Exception('apply_fun: unexpected: ' + repr(fun))
def interp_exp(self, e, env):
match e:
case Call(Name('input_int'), []):
return super().interp_exp(e, env)
case Call(Name('len'), [tup]):
return super().interp_exp(e, env)
case Call(func, args):
f = self.interp_exp(func, env)
vs = [self.interp_exp(arg, env) for arg in args]
return self.apply_fun(f, vs, e)
case TailCall(func, args):
f = self.interp_exp(func, env)
vs = [self.interp_exp(arg, env) for arg in args]
return self.apply_fun(f, vs, e)
case FunRef(id, arity):
return env[id]
case _:
return super().interp_exp(e, env)
def interp_stmts(self, ss, env):
if len(ss) == 0:
raise Exception('interp_stmts function ended without return')
match ss[0]:
case TailCall(func, args):
return self.interp_exp(Call(func, args), env)
case _:
return super().interp_stmts(ss, env)
def interp(self, p):
match p:
case CProgramDefs(defs):
env = {}
for d in defs:
match d:
case FunctionDef(name, params, blocks, dl, returns, comment):
env[name] = Function(name, [x for (x,t) in params], blocks, env)
self.blocks = {}
self.apply_fun(env['main'], [], None)
case _:
raise Exception('interp: unexpected ' + repr(p))