1 | from omexceptions import * |
---|
2 | from exceptions import * |
---|
3 | #from cd import * |
---|
4 | class XMLattribute(object): |
---|
5 | def __init__(self, name, value): |
---|
6 | self.name=name |
---|
7 | self.value=value |
---|
8 | def encode(self, context): |
---|
9 | return "".join([self.name,"=\"",self.value,"\""]) |
---|
10 | class OMObjectBase(object): |
---|
11 | """ at the moment only a base class""" |
---|
12 | def __init__(self): |
---|
13 | self.attributes={} |
---|
14 | def __getChildren(self): |
---|
15 | try: |
---|
16 | return self.getChildren() |
---|
17 | except AttributeError: |
---|
18 | try: |
---|
19 | return self.__children |
---|
20 | except AttributeError: |
---|
21 | return [] |
---|
22 | def __delChildren(self): |
---|
23 | try: |
---|
24 | self.delChildren() |
---|
25 | return |
---|
26 | except AttributeError: |
---|
27 | try: |
---|
28 | del self.__children |
---|
29 | except AttributeError: |
---|
30 | pass |
---|
31 | def __setChildren(self,children): |
---|
32 | try: |
---|
33 | self.setChildren(children) |
---|
34 | except AttributeError: |
---|
35 | raise UnsupportedOperationError |
---|
36 | def __getBody(self): |
---|
37 | try: |
---|
38 | return self.getBody() |
---|
39 | except AttributeError: |
---|
40 | try: |
---|
41 | return self.__body |
---|
42 | except AttributeError: |
---|
43 | return None |
---|
44 | def __delBody(self): |
---|
45 | try: |
---|
46 | self.delBody() |
---|
47 | return |
---|
48 | except AttributeError: |
---|
49 | try: |
---|
50 | del self.__body |
---|
51 | except AttributeError: |
---|
52 | pass |
---|
53 | def __setBody(self,body): |
---|
54 | try: |
---|
55 | self.setBody(body) |
---|
56 | except AttributeError: |
---|
57 | raise UnsupportedOperationError |
---|
58 | def __getXMLattributes(self): |
---|
59 | try: |
---|
60 | return self.getXMLattributes() |
---|
61 | except AttributeError: |
---|
62 | try: |
---|
63 | return self.__XMLattributes |
---|
64 | except AttributeError: |
---|
65 | #do return None, cause if modifiying a new list, changes will not be saved |
---|
66 | return [] |
---|
67 | def __delXMLattributes(self): |
---|
68 | try: |
---|
69 | self.delXMLattributes() |
---|
70 | return |
---|
71 | except AttributeError: |
---|
72 | try: |
---|
73 | del self.__XMLattributes |
---|
74 | except AttributeError: |
---|
75 | pass |
---|
76 | def __setXMLattributes(self,XMLattributes): |
---|
77 | try: |
---|
78 | self.setBody(XMLattributes) |
---|
79 | except AttributeError: |
---|
80 | raise UnsupportedOperationError |
---|
81 | children=property(__getChildren, __setChildren,__delChildren,\ |
---|
82 | """ children in an OMtree""") |
---|
83 | body=property(__getBody,__setBody,__delBody,\ |
---|
84 | "xml body,FIXME: at the moment only char data") |
---|
85 | XMLattributes=property(__getXMLattributes,__setXMLattributes,__delXMLattributes,\ |
---|
86 | "xml attributes") |
---|
87 | def XMLencode(self, context): |
---|
88 | |
---|
89 | attr=self.XMLattributes |
---|
90 | if attr: |
---|
91 | attrstr=" "+" ".join([a.encode(context) for a in attr]) |
---|
92 | else: |
---|
93 | attrstr="" |
---|
94 | opening="".join(["<", self.XMLtag, attrstr,">"]) |
---|
95 | children=self.children |
---|
96 | if children: |
---|
97 | body="".join([context.XMLEncodeObject(c) for c in children]) |
---|
98 | else: |
---|
99 | body=self.body |
---|
100 | if not body: |
---|
101 | body="" |
---|
102 | assert body!=None |
---|
103 | body=context.XMLEncodeBody(body) |
---|
104 | assert body!=None |
---|
105 | closing="".join(["</"+self.XMLtag+">"]) |
---|
106 | return "".join([opening,body,closing]) |
---|
107 | class OMvar(OMObjectBase): |
---|
108 | def __init__(self,name): |
---|
109 | super(OMvar,self).__init__() |
---|
110 | self.name=name |
---|
111 | def evaluate(self,context): |
---|
112 | try: |
---|
113 | return context[self.name] |
---|
114 | except OutOfScopeError: |
---|
115 | return self |
---|
116 | def __str__(self): |
---|
117 | return "OMV(" + self.name +")" |
---|
118 | XMLtag="OMV" |
---|
119 | def getXMLattributes(self): |
---|
120 | return [XMLattribute("name", self.name)] |
---|
121 | |
---|
122 | class OMapplication(OMObjectBase): |
---|
123 | def __init__(self, func, args): |
---|
124 | super(OMapplication,self).__init__() |
---|
125 | self.func=func |
---|
126 | self.args=args |
---|
127 | def evaluate(self,context): |
---|
128 | efunc=context.evaluate(self.func) |
---|
129 | eargs=[context.evaluate(a) for a in self.args] |
---|
130 | if callable(efunc): |
---|
131 | try: |
---|
132 | return context.apply(efunc, eargs) |
---|
133 | except EvaluationFailedError, NotImplementedError: |
---|
134 | return OMapplication(efunc, eargs) |
---|
135 | #return self |
---|
136 | else: |
---|
137 | return OMapplication(efunc, eargs) |
---|
138 | XMLtag="OMA" |
---|
139 | def getChildren(self): |
---|
140 | return [self.func]+self.args |
---|
141 | def setChildren(self): |
---|
142 | raise UnsupportedOperationError |
---|
143 | |
---|
144 | class OMsymbol(OMObjectBase): |
---|
145 | def __init__(self,name,cd=None): |
---|
146 | super(OMsymbol,self).__init__() |
---|
147 | self.cd=cd |
---|
148 | self.name=name |
---|
149 | def __eq__(self, other): |
---|
150 | try: |
---|
151 | return bool(other.name==self.name and self.cd==other.cd) |
---|
152 | except: |
---|
153 | return False |
---|
154 | def __str__(self): |
---|
155 | return "OMS("+self.name+", "+self.cd.name + ")" |
---|
156 | def __hash__(self):# |
---|
157 | return hash((self.name,self.cd.__hash__())) |
---|
158 | def evaluate(self,context): |
---|
159 | return context.evaluateSymbol(self) |
---|
160 | XMLtag="OMS" |
---|
161 | def getXMLattributes(self): |
---|
162 | return [XMLattribute("name", self.name),\ |
---|
163 | XMLattribute("cdbase",self.cd.base),\ |
---|
164 | XMLattribute("cd",self.cd.name)] |
---|
165 | def setXMLattributes(self): |
---|
166 | raise UnsupportedOperationError |
---|
167 | class SimpleValue(OMObjectBase): |
---|
168 | def __init__(self,value): |
---|
169 | super(SimpleValue,self).__init__() |
---|
170 | if (isinstance(value, str)): |
---|
171 | value=self.parse(value) |
---|
172 | self.value=value |
---|
173 | def evaluate(self, context): |
---|
174 | return self |
---|
175 | def getValue(self): |
---|
176 | return self.value |
---|
177 | def __str__(self): |
---|
178 | return "OM("+repr(self.value)+")" |
---|
179 | |
---|
180 | class OMint(SimpleValue): |
---|
181 | def __init__(self,value): |
---|
182 | if not isinstance(value,int): |
---|
183 | value=self.parse(value) |
---|
184 | super(OMint,self).__init__(value) |
---|
185 | def parse(self,value): |
---|
186 | """FIXME: Not fully standard compliant, |
---|
187 | -> hex encodings""" |
---|
188 | return int(value,10) |
---|
189 | def __str__(self): |
---|
190 | return "OMint("+repr(self.value)+")" |
---|
191 | def getBody(self): |
---|
192 | return str(self.value) |
---|
193 | def setBody(self, value): |
---|
194 | raise UnsupportedOperationError |
---|
195 | XMLtag="OMI" |
---|
196 | class OMfloat(SimpleValue): |
---|
197 | def __init__(self,value): |
---|
198 | super(OMfloat,self).__init__(value) |
---|
199 | def parse(self, value): |
---|
200 | """FIXME: Not fully standard compliant, |
---|
201 | -> hex encodings""" |
---|
202 | return float(value) |
---|
203 | def __str__(self): |
---|
204 | return "OMfloat("+repr(self.value)+")" |
---|
205 | XMLtag="OMF" |
---|
206 | def getXMLattributes(self): |
---|
207 | return [XMLattribute("dec",str(self.value))] |
---|
208 | class OMref(OMObjectBase): |
---|
209 | def __init__(self, ref): |
---|
210 | self.ref=ref |
---|
211 | def evaluate(self, context): |
---|
212 | return context.evaluate(self.ref) |
---|
213 | def XMLencode(self, context): |
---|
214 | "FIXME: maybe it should also be able to encode as reference" |
---|
215 | return context.XMLEncodeObject(self.ref) |
---|
216 | if __name__=='__main__': |
---|
217 | from context import * |
---|
218 | |
---|
219 | from binding import * |
---|
220 | |
---|
221 | context=Context() |
---|
222 | |
---|
223 | context.push({}) |
---|
224 | |
---|
225 | context["x"]=OMint(1) |
---|
226 | |
---|
227 | x=OMvar("x") |
---|
228 | |
---|
229 | y=OMvar("y") |
---|
230 | |
---|
231 | print context.evaluate(x) |
---|
232 | print context.evaluate(y) |
---|
233 | firstArg=OMbinding(lambdasym,[OMvar("x"), OMvar("y")], OMvar("x")) |
---|
234 | #print context.evaluate(firstArg) |
---|
235 | application=OMapplication(firstArg, [x,y]) |
---|
236 | print context.evaluate(application) |
---|
237 | application=OMapplication(firstArg,[y,x]) |
---|
238 | print context.evaluate(application) |
---|
239 | import arith1 |
---|
240 | context.addCDImplementation(arith1.implementation) |
---|
241 | #print type(context.lookupImplementation(arith1.plussym)) |
---|
242 | #application=OMapplication(arith1.plussym,[x]) |
---|
243 | #application=OMapplication(arith1.plussym,[x,x]) |
---|
244 | application=OMapplication(OMsymbol("plus",arith1.content),[x,x]) |
---|
245 | |
---|
246 | print context.evaluate(application) |
---|
247 | application=OMapplication(OMsymbol("plus",arith1.content),[x,x,x]) |
---|
248 | |
---|
249 | print context.evaluate(application) |
---|
250 | i=OMint(22482489) |
---|
251 | print i.body |
---|
252 | print i.XMLencode(context) |
---|
253 | #i.body="dshj" |
---|
254 | |
---|