forked from IUCompilerCourse/python-student-support-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterp_Clambda.py
31 lines (28 loc) · 882 Bytes
/
interp_Clambda.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
from ast import *
from interp_Cfun import InterpCfun
from interp_Llambda import ClosureTuple
from utils import *
from interp_Lfun import Function
class InterpClambda(InterpCfun):
def arity(self, v):
match v:
case Function(name, params, body, env):
return len(params)
case ClosureTuple(args, arity):
return arity
case _:
raise Exception('Cany arity unexpected ' + repr(v))
def interp_exp(self, e, env):
match e:
case Call(Name('arity'), [fun]):
f = self.interp_exp(fun, env)
return self.arity(f)
case Uninitialized(ty):
return None
case AllocateClosure(length, typ, arity):
array = [None] * length
return ClosureTuple(array, arity)
case UncheckedCast(exp, ty):
return self.interp_exp(exp, env)
case _:
return super().interp_exp(e, env)