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