source: git/modules/openmathserver/context.py @ 68549d

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