source: git/modules/openmathserver/binding.py @ 057e4d3

spielwiese
Last change on this file since 057e4d3 was 057e4d3, checked in by Michael Brickenstein <bricken@…>, 18 years ago
*bricken: beautified git-svn-id: file:///usr/local/Singular/svn/trunk@8390 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 1.7 KB
Line 
1from objects import OMObjectBase, OMSymbol
2from cd import OMCD
3from omexceptions import UnsupportedOperationError
4from itertools import izip
5from copy import copy
6cdFns1 = OMCD("fns1")
7lambdasym = OMSymbol("lambda", cdFns1)
8
9def islambda(sym):
10    return lambdasym == sym
11   
12class 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       
Note: See TracBrowser for help on using the repository browser.