-
Notifications
You must be signed in to change notification settings - Fork 38
/
interp_Cfun.py
54 lines (48 loc) · 1.66 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
from ast import *
from interp_Carray import InterpCarray
from utils import *
from interp_Lfun import Function
class InterpCfun(InterpCarray):
def apply_fun(self, fun, args, e):
match fun:
case Function(name, xs, blocks, env):
old_blocks = self.blocks
self.blocks = blocks
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[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(f), args) if f in builtin_functions:
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 FunRef(id, arity):
return env[id]
case _:
return super().interp_exp(e, env)
def interp_tail(self, s, env):
match s:
case TailCall(func, args):
return self.interp_exp(Call(func, args), env)
case _:
return super().interp_tail(s, 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))