source: git/modules/openmathserver/objects.py @ 9a95b3

spielwiese
Last change on this file since 9a95b3 was 9a95b3, checked in by Michael Brickenstein <bricken@…>, 19 years ago
*bricken: having even more fun git-svn-id: file:///usr/local/Singular/svn/trunk@8360 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 5.6 KB
Line 
1from omexceptions import *
2from exceptions import *
3from cd import *
4
5class OMobject(object):
6    def __init__(self):
7        self.attributes={}
8    def __getChildren(self):
9        try:
10            return self.getChildren()
11        except AttributeError:
12            try:
13                return self.__children
14            except AttributeError:
15                return []
16    def __delChildren(self):
17        try:
18            self.delChildren()
19            return
20        except AttributeError:
21            try:
22                del self.__children
23            except AttributeError:
24                pass
25    def __setChildren(self,children):
26        try:
27            self.setChildren(children)
28        except AttributeError:
29                self.__children=children
30    def __getBody(self):
31        try:
32            return self.getBody()
33        except AttributeError:
34            try:
35                return self.__body
36            except AttributeError:
37                return None
38    def __delBody(self):
39        try:
40            self.delBody()
41            return
42        except AttributeError:
43            try:
44                del self.__body
45            except AttributeError:
46                pass
47    def __setBody(self,body):
48        try:
49            self.setBody(body)
50        except AttributeError:
51                self.__body=body
52    children=property(__getChildren, __setChildren,__delChildren,\
53                      """ children in an OMtree""")
54    body=property(__getBody,__setBody,__delBody,\
55        "xml body,FIXME: at the moment only char data")
56    def XMLencode(self, context):
57        try:
58            attr=self.XMLAttributes()
59            attrstr=" "+" ".join([a.encode(context) for a in attr])
60        except:
61            attrstr=""
62        opening="".join(["<", self.XMLtag, attrstr,">"])
63        children=self.children
64        if children:
65            body="".join([c.XMLencode(context) for i in children])
66        else:
67            body=self.body
68            if not body:
69                body=""
70            assert body!=None
71            body=context.XMLEncodeBody(body)
72            assert body!=None
73        closing="".join(["</"+self.XMLtag+">"])
74        return "".join([opening,body,closing])
75class OMvar(OMobject):
76    def __init__(self,name):
77        super(OMvar,self).__init__()
78        self.name=name
79    def evaluate(self,context):
80        try:
81            return context[self.name]
82        except OutOfScopeError:
83            return self
84    def __str__(self):
85        return "OMV(" + self.name +")"
86class OMapplication(OMobject):
87    def __init__(self, func, args):
88        super(OMapplication,self).__init__()
89        self.func=func
90        self.args=args
91    def evaluate(self,context):
92        efunc=context.evaluate(self.func)
93        eargs=[context.evaluate(a) for a in self.args]
94        if callable(efunc):
95            try:
96                return context.apply(efunc, eargs)
97            except EvaluationFailedError, NotImplementedError:
98                return self
99        else:
100            return OMapplication(efunc, eargs)
101class OMsymbol(OMobject):
102    def __init__(self,name,cd=None):
103        super(OMsymbol,self).__init__()
104        self.cd=cd
105        self.name=name
106    def __eq__(self, other):
107        return bool(other.name==self.name and self.cd==other.cd)
108    def __str__(self):
109        return "OMS("+self.name+", "+self.cd.name + ")"
110    def __hash__(self):#
111        return hash((self.name,self.cd.__hash__()))
112    def evaluate(self,context):
113        return context.evaluateSymbol(self)
114class SimpleValue(OMobject):
115    def __init__(self,value):
116        super(SimpleValue,self).__init__()
117        if (isinstance(value, str)):
118            value=self.parse(value)
119        self.value=value
120    def evaluate(self, context):
121        return self
122    def getValue(self):
123        return self.value
124    def __str__(self):
125        return "OM("+repr(self.value)+")"
126
127class OMint(SimpleValue):
128    def __init__(self,value):
129        super(OMint,self).__init__(value)
130    def parse(self,value):
131        """FIXME: Not fully standard compliant,
132        -> hex encodings"""
133        return int(value,10)
134    def __str__(self):
135        return "OMint("+repr(self.value)+")"
136    def getBody(self):
137        return str(self.value)
138    def setBody(self, value):
139        raise OperationNotPossibleError
140    XMLtag="OMI"
141class OMfloat(SimpleValue):
142    def __init__(self,value):
143        super(OMfloat,self).__init__(value)
144    def parse(self, value):
145        """FIXME: Not fully standard compliant,
146        -> hex encodings"""
147        return float(value)
148    def __str__(self):
149        return "OMfloat("+repr(self.value)+")"
150       
151if __name__=='__main__':
152    from context import *
153
154    from binding import *
155
156    context=Context()
157
158    context.push({})
159
160    context["x"]=OMint(1)
161
162    x=OMvar("x")
163
164    y=OMvar("y")
165
166    print context.evaluate(x)
167    print context.evaluate(y)
168    firstArg=OMbinding(lambdasym,[OMvar("x"), OMvar("y")], OMvar("x"))
169    #print context.evaluate(firstArg)
170    application=OMapplication(firstArg, [x,y])
171    print context.evaluate(application)
172    application=OMapplication(firstArg,[y,x])
173    print context.evaluate(application)
174    import arith1
175    context.addCDImplementation(arith1.implementation)
176    #print type(context.lookupImplementation(arith1.plussym))
177    #application=OMapplication(arith1.plussym,[x])
178    #application=OMapplication(arith1.plussym,[x,x])
179    application=OMapplication(OMsymbol("plus",arith1.content),[x,x])
180   
181    print context.evaluate(application)
182    application=OMapplication(OMsymbol("plus",arith1.content),[x,x,x])
183   
184    print context.evaluate(application)
185    i=OMint(22482489)
186    print i.body
187    print i.XMLencode(context)
188    #i.body="dshj"
189   
Note: See TracBrowser for help on using the repository browser.