# source:git/modules/openmathserver/binding.py@e25c5e

spielwiese
Last change on this file since e25c5e was e25c5e, checked in by Michael Brickenstein <bricken@…>, 18 years ago
• Property mode set to `100644`
File size: 1.9 KB
Line
1"""Definition of the OMBinding object"""
2from objects import OMObjectBase, OMSymbol
3from cd import OMCD
4from omexceptions import UnsupportedOperationError
5from itertools import izip
6from copy import copy
7cdFns1 = OMCD("fns1")
8lambdasym = OMSymbol("lambda", cdFns1)
9
10def islambda(sym):
11    "return True, iff sym is the lambda binder"
12    return lambdasym == sym
13
14class OMBinding(OMObjectBase):
15    """hopefully fixed possible problems: reevaluation writes new scope,
16       if it isn't
17       meant so, references do not work correctly because of scopes
18       solve this by first evaluation to bounded OMBinding"""
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):
26        "evaluate the OMbinding in context"
27        assert islambda(self.binder)
28        if not self.bounded:
29            mycopy = copy(self)
30            mycopy.scope = context.scopeFromCurrentScope()
31            mycopy.bounded = True
32            return mycopy
33        else:
34            return self
35    def bind(self, args):
36        "bind arguments to values"
37        #print args, self.variables
38        assert len(args) == len(self.variables)
39        varBindings = dict(izip([i.name for i in self.variables], args))
40        self.scope.push(varBindings)
41    def unbind(self):
42        "unbind the arguments"
43        self.scope.pop()
44
45    def calcErg(self, context):
46        "do the actual computation"
47        return context.evaluateInScope(self.block, self.scope)
48    def __call__(self, context, *args):
49        self.bind(args)
50        erg = self.calcErg(context)
51        self.unbind()
52        #print "__call__ erg is", erg
53        return erg
54
55    XMLtag = "OMBIND"
56    def getChildren(self):
57        "get children for (XML) representation"
58        return [self.binder]+self.variables+[self.block]
59
Note: See TracBrowser for help on using the repository browser.