-
Notifications
You must be signed in to change notification settings - Fork 1
/
interp_Lfun.py
70 lines (64 loc) · 2.19 KB
/
interp_Lfun.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
65
66
67
68
69
70
from ast import *
from interp_Ltup import InterpLtup
from utils import *
class Function:
__match_args__ = ("name", "params", "body", "env")
def __init__(self, name, params, body, env):
self.name = name
self.params = params
self.body = body
self.env = env
def __repr__(self):
return 'Function(' + self.name + ', ...)'
class InterpLfun(InterpLtup):
def apply_fun(self, fun, args, e):
match fun:
case Function(name, xs, body, env):
trace(f"apply {fun} {args=} {e} {name} {xs=} {body} {env}")
new_env = {x: v for (x,v) in env.items()}
for (x,arg) in zip(xs, args):
new_env[x] = arg
return self.interp_stmts(body, new_env)
# case Tagged(val, tag):
# #print("YYYY {}".format(v))
# return val
case _:
raise Exception('apply_fun: unexpected: ' + repr(fun))
def interp_exp(self, e, env):
match e:
case Call(Name(f), args) if (f == 'input_int') or (f == 'len') or (f == 'print'):
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]
t = self.apply_fun(f, vs, e)
return t
case FunRef(id, arity):
return env[id]
case _:
return super().interp_exp(e, env)
def interp_stmts(self, ss, env):
if len(ss) == 0:
return
match ss[0]:
case Return(value):
return self.interp_exp(value, env)
case FunctionDef(name, params, bod, dl, returns, comment):
if isinstance(params, ast.arguments):
ps = [p.arg for p in params.args]
else:
ps = [x for (x,t) in params]
env[str(name)] = Function(name, ps, bod, env)
return self.interp_stmts(ss[1:], env)
case _:
return super().interp_stmts(ss, env)
def interp(self, p):
match p:
case Module(ss):
env = {}
self.interp_stmts(ss, env)
#trace('interp global env: ' + repr(env))
if 'main' in env.keys():
self.apply_fun(env['main'], [], None)
case _:
raise Exception('interp: unexpected ' + repr(p))