1 | from objects import OMObjectBase, OMSymbol |
---|
2 | from cd import OMCD |
---|
3 | from omexceptions import UnsupportedOperationError |
---|
4 | from itertools import izip |
---|
5 | from copy import copy |
---|
6 | cdFns1 = OMCD("fns1") |
---|
7 | lambdasym = OMSymbol("lambda", cdFns1) |
---|
8 | |
---|
9 | def islambda(sym): |
---|
10 | return lambdasym == sym |
---|
11 | |
---|
12 | class OMBinding(OMObjectBase): |
---|
13 | """hopefully fixed possible problems: reevaluation writes new scope, if it isn't |
---|
14 | meant so, references do not work correctly because of scopes |
---|
15 | solve this by first evaluation to bounded OMBinding""" |
---|
16 | def __init__(self, binder, variables, block): |
---|
17 | super(OMBinding, self).__init__() |
---|
18 | self.block = block |
---|
19 | self.binder = binder |
---|
20 | self.variables = variables |
---|
21 | self.bounded = False |
---|
22 | def evaluate(self, context): |
---|
23 | assert islambda(self.binder) |
---|
24 | if not self.bounded: |
---|
25 | mycopy = copy(self) |
---|
26 | mycopy.scope = context.scopeFromCurrentScope() |
---|
27 | mycopy.bounded = True |
---|
28 | return mycopy |
---|
29 | else: |
---|
30 | return self |
---|
31 | def bind(self, args): |
---|
32 | #print args, self.variables |
---|
33 | assert len(args) == len(self.variables) |
---|
34 | varBindings = dict(izip([i.name for i in self.variables], args)) |
---|
35 | self.scope.push(varBindings) |
---|
36 | def unbind(self): |
---|
37 | self.scope.pop() |
---|
38 | |
---|
39 | def calcErg(self, context): |
---|
40 | return context.evaluateInScope(self.block, self.scope) |
---|
41 | def __call__(self, context, *args): |
---|
42 | self.bind(args) |
---|
43 | erg = self.calcErg(context) |
---|
44 | self.unbind() |
---|
45 | #print "__call__ erg is", erg |
---|
46 | return erg |
---|
47 | |
---|
48 | XMLtag = "OMBIND" |
---|
49 | def getChildren(self): |
---|
50 | return [self.binder]+self.variables+[self.block] |
---|
51 | def setChildren(self): |
---|
52 | raise UnsupportedOperationError |
---|
53 | |
---|