source: git/modules/openmath/context.py @ e88c64

fieker-DuValspielwiese
Last change on this file since e88c64 was 913156, checked in by Michael Brickenstein <bricken@…>, 18 years ago
*bricken: initial version git-svn-id: file:///usr/local/Singular/svn/trunk@8995 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 5.4 KB
Line 
1from exceptions import *
2from copy import copy
3#from cd import *
4from omexceptions import *
5from objects import *
6from re import compile
7import StringIO
8from xml.sax.saxutils import XMLGenerator
9raiseException=True
10class Context(object):
11    #TODO: Referenzen durch scope richtig behandeln
12    def __init__(self):
13        self.scope=Scope()
14        self.scope.push({})
15        self.implementations={}
16        self.XMLEncoder=SimpleXMLEncoder()
17        self.errorHandler=SimpleErrorHandler()
18    def addCDImplementation(self, implementation):
19        self.implementations[implementation.cd]=implementation
20    def lookupImplementation(self, oms):
21        #print self.implementations
22        try:
23           
24            return getattr(self.implementations[oms.cd],oms.name)
25        except ValueError:
26            raise NotImplementedError
27    def __getitem__(self,itemname):
28        return self.scope[itemname]
29    def __setitem__(self,itemname, item):
30        self.scope[itemname]=item
31    def push(self, dict):
32        """push a lexical context in form of a dictionary"""
33        self.scope.push(dict)
34    def pop(self, dict):
35        """pop a lexical context"""
36        return self.scope.pop(dict)
37    def scopeFromCurrentScope(self):
38        """returns a new Scope object, sharing the dictionaries,
39           which will represent the current scope"""
40        return self.scope.derriveScope()
41    def toGeneric(self, o):
42        return o.getValue()
43
44    def evaluateSymbol(self, oms):
45        try:
46            impl=self.lookupImplementation(oms)
47            #if len(oms.attributes)>0:
48            #    impl=copy(impl)
49            #    impl.attributes=copy(oms.attributes)
50            return impl
51        except NotImplementedError:
52            #TODO log this, report it
53            return oms
54    def evaluateVariable(self, omv):
55        try:
56            return self[omv.name]
57        except OutOfScopeError:
58            return omv
59    def evaluateApplication(self, app):
60        efunc = self.evaluate(app.func)
61        eargs = [self.evaluate(a) for a in app.args]
62        if callable(efunc):
63            if raiseException:
64                return self.apply(efunc,eargs)
65            else:
66                try:
67                    return self.apply(efunc, eargs)
68                except EvaluationFailedError, NotImplementedError:
69                    return OMA(efunc, eargs)
70                #return self
71        else:
72            #print efunc
73            return OMA(efunc, eargs)
74    def evaluate(self,omobject):
75       
76        if isinstance(omobject,OMV):
77           
78            return self.evaluateVariable(omobject)
79        if isinstance(omobject,OMS):
80            return self.evaluateSymbol(omobject)
81        if isinstance(omobject,OMA):
82            return self.evaluateApplication(omobject)
83        if isinstance(omobject,OMBIND):
84            return omobject.evaluate(self)
85        return omobject
86    def evaluateInScope(self,omobject, scope):
87        bak=self.scope
88        self.scope=scope
89        erg=self.evaluate(omobject)
90        self.scope=bak
91        #print "my erg is", erg
92        return erg
93    def package(self, val):
94        return val
95        if isinstance(val, int):
96            return OMint(val)
97        if isinstance(val, float):
98            return OMfloat(val)
99           
100            raise NotImplementedError
101    def apply(self,func,args):
102        try:
103            return func(self,*args)
104        except:
105            from traceback import print_exc
106            print_exc()
107            raise EvaluationFailedError
108    def XMLEncodeBody(self,body):
109        return self.XMLEncoder.encode(body)
110    def XMLEncodeObject(self, obj):
111        #TODO: Make Attribution List attributes
112        #TODO: Make all objects __hash__ and __eq__
113        out=StringIO.StringIO()
114        G=XMLGenerator(out)
115        obj.XMLSAXEncode(self,G)
116        return out.getvalue()
117        if (len(obj.attributes)==0):
118            return "".join(obj.XMLPreEncode(self))
119        else:
120            toencode=copy(obj)
121            toencode.attributes={}
122            #FIXME: look on order
123            attribution=OMAttribution(*([OMAttributePair(k,obj.attributes[k])\
124                for k in obj.attributes])+[toencode])
125            return attribution.XMLEncode(self)
126class SimpleErrorHandler(object):
127    def __init__(self):
128        super(SimpleErrorHandler,self).__init__()
129    def handle_unexpected_symbol(self, symbol):
130        pass
131    def handle_unsupported_cd(self, symbol):
132        pass
133    def handle_unexpected_symbol(self, symbol):
134        pass 
135class SimpleXMLEncoder(object):
136    def __init__(self):
137      self.re_inner=compile("&")
138      self.re_outer=compile("<")
139    def encode(self, string):
140        #return sub("<","&lt;",sub("&","&amp;",string))
141      return self.re_outer.sub("&lt", self.re_inner.sub("&amp", string)) 
142class Scope(object):
143    def __init__(self):
144        self.dicts=[]
145    def push(self, dict):
146        self.dicts.append(dict)
147    def pop(self):
148        return self.dicts.pop()
149    def __getitem__(self,itemname):
150        i=len(self.dicts)-1
151        while i>=0:
152            try:
153                return self.dicts[i][itemname]
154            except KeyError:
155                pass
156            i=i-1
157        raise OutOfScopeError
158    def  __setitem__(self, itemname, item):
159        try:
160            self.dicts[len(self.dicts)-1][itemname]=item
161        except IndexError:
162            print "scope has no layers"
163    def derriveScope(self):
164        erg=Scope()
165        erg.dicts=copy(self.dicts)
166        return erg
Note: See TracBrowser for help on using the repository browser.