1 | from objects import OMobject, OMsymbol |
---|
2 | from cd import * |
---|
3 | from omexceptions import * |
---|
4 | from itertools import izip |
---|
5 | cdFns1=OMcd("fns1") |
---|
6 | lambdasym=OMsymbol("lambda",cdFns1) |
---|
7 | |
---|
8 | def islambda(sym): |
---|
9 | return lambdasym==sym |
---|
10 | |
---|
11 | class OMbinding(OMobject): |
---|
12 | def __init__(self, binder,variables,block): |
---|
13 | super(OMbinding,self).__init__() |
---|
14 | self.block=block |
---|
15 | self.binder=binder |
---|
16 | self.variables=variables |
---|
17 | def evaluate(self,context): |
---|
18 | assert islambda(self.binder) |
---|
19 | self.scope=context.scopeFromCurrentScope() |
---|
20 | return self |
---|
21 | def bind(self, args): |
---|
22 | #print args, self.variables |
---|
23 | assert len(args)==len(self.variables) |
---|
24 | varBindings=dict(izip([i.name for i in self.variables],args)) |
---|
25 | self.scope.push(varBindings) |
---|
26 | def unbind(self): |
---|
27 | self.scope.pop() |
---|
28 | |
---|
29 | def calcErg(self,context): |
---|
30 | return context.evaluateInScope(self.block,self.scope) |
---|
31 | def __call__(self, context,*args): |
---|
32 | self.bind(args) |
---|
33 | erg=self.calcErg(context) |
---|
34 | self.unbind() |
---|
35 | #print "__call__ erg is", erg |
---|
36 | return erg |
---|
37 | |
---|
38 | XMLtag="OMBIND" |
---|
39 | def getChildren(self): |
---|
40 | return [self.binder]+self.variables+[self.block] |
---|
41 | def setChildren(self): |
---|
42 | raise UnsupportedOperationError |
---|
43 | |
---|