source: git/modules/openmathserver/context.py @ 559396

spielwiese
Last change on this file since 559396 was 559396, checked in by Michael Brickenstein <bricken@…>, 19 years ago
*bricken: refactored git-svn-id: file:///usr/local/Singular/svn/trunk@8378 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 3.6 KB
Line 
1from exceptions import *
2from copy import copy
3from cd import *
4from omexceptions import *
5from objects import *
6from re import sub
7class Context(object):
8    #TODO: Referenzen durch scope richtig behandeln
9    def __init__(self):
10        self.scope=Scope()
11        self.implementations={}
12        self.XMLEncoder=SimpleXMLEncoder()
13    def addCDImplementation(self, implementation):
14        self.implementations[implementation.cd]=implementation
15    def lookupImplementation(self, oms):
16        try:
17            return self.implementations[oms.cd][oms]
18        except KeyError:
19            raise NotImplementedError
20    def __getitem__(self,itemname):
21        return self.scope[itemname]
22    def __setitem__(self,itemname, item):
23        self.scope[itemname]=item
24    def push(self, dict):
25        """push a lexical context in form of a dictionary"""
26        self.scope.push(dict)
27    def pop(self, dict):
28        """pop a lexical context"""
29        return self.scope.pop(dict)
30    def scopeFromCurrentScope(self):
31        """returns a new Scope object, sharing the dictionaries,
32           which will represent the current scope"""
33        return self.scope.derriveScope()
34    def toGeneric(self, o):
35        return o.getValue()
36
37    def evaluateSymbol(self, oms):
38        try:
39            impl=self.lookupImplementation(oms)
40            if len(oms.attributes)>0:
41                impl=copy(impl)
42                impl.attributes=copy(oms.attributes)
43            return impl
44        except NotImplementedError:
45            #TODO log this, report it
46            return oms
47    def evaluate(self,omobject):
48        return omobject.evaluate(self)
49    def evaluateInScope(self,omobject, scope):
50        bak=self.scope
51        self.scope=scope
52        erg=self.evaluate(omobject)
53        self.scope=bak
54        #print "my erg is", erg
55        return erg
56    def package(self, val):
57        if isinstance(val, OMObjectBase):
58            return val
59        else:
60            if isinstance(val, int):
61                return OMint(val)
62            if isinstance(val, float):
63                return OMfloat(val)
64            raise NotImplementedError
65    def apply(self,func,args):
66        try:
67            return func(self,*args)
68        except:
69            raise EvaluationFailedError
70    def XMLEncodeBody(self,body):
71        return self.XMLEncoder.encode(body)
72    def XMLEncodeObject(self, obj):
73        #TODO: Make Attribution List attributes
74        #TODO: Make all objects __hash__ and __eq__
75        if (len(obj.attributes)==0):
76            return obj.XMLEncode(self)
77        else:
78            toencode=copy(obj)
79            toencode.attributes={}
80            #FIXME: look on order
81            attribution=OMAttribution(*([OMAttributePair(k,obj.attributes[k])\
82                for k in obj.attributes])+[toencode])
83            return attribution.XMLEncode(self)
84           
85class SimpleXMLEncoder(object):
86    def encode(self, string):
87        return sub("<","&lt;",sub("&","&amp;",string))
88       
89class Scope(object):
90    def __init__(self):
91        self.dicts=[]
92    def push(self, dict):
93        self.dicts.append(dict)
94    def pop(self):
95        return self.dicts.pop()
96    def __getitem__(self,itemname):
97        i=len(self.dicts)-1
98        while i>=0:
99            try:
100                return self.dicts[i][itemname]
101            except KeyError:
102                pass
103            i=i-1
104        raise OutOfScopeError
105    def  __setitem__(self, itemname, item):
106        try:
107            self.dicts[len(self.dicts)-1][itemname]=item
108        except IndexError:
109            print "scope has no layers"
110    def derriveScope(self):
111        erg=Scope()
112        erg.dicts=copy(self.dicts)
113        return erg
Note: See TracBrowser for help on using the repository browser.