source: git/modules/openmathserver/context.py @ 50225a

spielwiese
Last change on this file since 50225a was 50225a, checked in by Michael Brickenstein <bricken@…>, 18 years ago
*bricken: having fun git-svn-id: file:///usr/local/Singular/svn/trunk@8359 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 2.7 KB
Line 
1from exceptions import *
2from copy import copy
3from cd import *
4from omexceptions import *
5from objects import *
6class Context(object):
7    #TODO: Referenzen durch scope richtig behandeln
8    def __init__(self):
9        self.scope=Scope()
10        self.implementations={}
11    def addCDImplementation(self, implementation):
12        self.implementations[implementation.cd]=implementation
13    def lookupImplementation(self, oms):
14        try:
15            return self.implementations[oms.cd][oms]
16        except KeyError:
17            raise NotImplementedError
18    def __getitem__(self,itemname):
19        return self.scope[itemname]
20    def __setitem__(self,itemname, item):
21        self.scope[itemname]=item
22    def push(self, dict):
23        """push a lexical context in form of a dictionary"""
24        self.scope.push(dict)
25    def pop(self, dict):
26        """pop a lexical context"""
27        return self.scope.pop(dict)
28    def scopeFromCurrentScope(self):
29        """returns a new Scope object, sharing the dictionaries,
30           which will represent the current scope"""
31        return self.scope.derriveScope()
32    def toGeneric(self, o):
33        return o.getValue()
34
35    def evaluateSymbol(self, oms):
36        try:
37            impl=self.lookupImplementation(oms)
38            if len(oms.attributes)>0:
39                impl=copy(impl)
40                impl.attributes=copy(oms.attributes)
41            return impl
42        except NotImplementedError:
43            print "not found"
44            return oms
45    def evaluate(self,omobject):
46        return omobject.evaluate(self)
47    def evaluateInScope(self,omobject, scope):
48        bak=self.scope
49        self.scope=scope
50        erg=self.evaluate(omobject)
51        self.scope=bak
52        #print "my erg is", erg
53        return erg
54    def package(self, val):
55        if isinstance(val, OMobject):
56            return val
57        else:
58            if isinstance(val, int):
59                return OMint(val)
60    def apply(self,func,args):
61        return func(self,*args)
62       
63class Scope(object):
64    def __init__(self):
65        self.dicts=[]
66    def push(self, dict):
67        self.dicts.append(dict)
68    def pop(self):
69        return self.dicts.pop()
70    def __getitem__(self,itemname):
71        i=len(self.dicts)-1
72        while i>=0:
73            try:
74                return self.dicts[i][itemname]
75            except KeyError:
76                pass
77            i=i-1
78        raise OutOfScopeError
79    def  __setitem__(self, itemname, item):
80        try:
81            self.dicts[len(self.dicts)-1][itemname]=item
82        except IndexError:
83            print "scope has no layers"
84    def derriveScope(self):
85        erg=Scope()
86        erg.dicts=copy(self.dicts)
87        return erg
Note: See TracBrowser for help on using the repository browser.