1 | from omexceptions import * |
---|
2 | from exceptions import * |
---|
3 | from cd import * |
---|
4 | |
---|
5 | class 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 | body=context.xmlEncodeBody(body) |
---|
71 | closing="".join(["</"+self.xmltag+">"]) |
---|
72 | return "".join([opening,body,closing]) |
---|
73 | class OMvar(OMobject): |
---|
74 | def __init__(self,name): |
---|
75 | super(OMvar,self).__init__() |
---|
76 | self.name=name |
---|
77 | def evaluate(self,context): |
---|
78 | try: |
---|
79 | return context[self.name] |
---|
80 | except OutOfScopeError: |
---|
81 | return self |
---|
82 | def __str__(self): |
---|
83 | return "OMV(" + self.name +")" |
---|
84 | class OMapplication(OMobject): |
---|
85 | def __init__(self, func, args): |
---|
86 | super(OMapplication,self).__init__() |
---|
87 | self.func=func |
---|
88 | self.args=args |
---|
89 | def evaluate(self,context): |
---|
90 | efunc=context.evaluate(self.func) |
---|
91 | eargs=[context.evaluate(a) for a in self.args] |
---|
92 | if callable(efunc): |
---|
93 | try: |
---|
94 | return context.apply(efunc, eargs) |
---|
95 | except EvaluationFailedError, NotImplementedError: |
---|
96 | return self |
---|
97 | else: |
---|
98 | return OMapplication(efunc, eargs) |
---|
99 | class OMsymbol(OMobject): |
---|
100 | def __init__(self,name,cd=None): |
---|
101 | super(OMsymbol,self).__init__() |
---|
102 | self.cd=cd |
---|
103 | self.name=name |
---|
104 | def __eq__(self, other): |
---|
105 | return bool(other.name==self.name and self.cd==other.cd) |
---|
106 | def __str__(self): |
---|
107 | return "OMS("+self.name+", "+self.cd.name + ")" |
---|
108 | def __hash__(self):# |
---|
109 | return hash((self.name,self.cd.__hash__())) |
---|
110 | def evaluate(self,context): |
---|
111 | return context.evaluateSymbol(self) |
---|
112 | class SimpleValue(OMobject): |
---|
113 | def __init__(self,value): |
---|
114 | super(SimpleValue,self).__init__() |
---|
115 | if (isinstance(value, str)): |
---|
116 | value=self.parse(value) |
---|
117 | self.value=value |
---|
118 | def evaluate(self, context): |
---|
119 | return self |
---|
120 | def getValue(self): |
---|
121 | return self.value |
---|
122 | def __str__(self): |
---|
123 | return "OM("+repr(self.value)+")" |
---|
124 | |
---|
125 | class OMint(SimpleValue): |
---|
126 | def __init__(self,value): |
---|
127 | super(OMint,self).__init__(value) |
---|
128 | def parse(self,value): |
---|
129 | """FIXME: Not fully standard compliant, |
---|
130 | -> hex encodings""" |
---|
131 | return int(value,10) |
---|
132 | def __str__(self): |
---|
133 | return "OMint("+repr(self.value)+")" |
---|
134 | def getBody(self): |
---|
135 | return str(self.value) |
---|
136 | def setBody(self, value): |
---|
137 | raise OperationNotPossibleError |
---|
138 | class OMfloat(SimpleValue): |
---|
139 | def __init__(self,value): |
---|
140 | super(OMfloat,self).__init__(value) |
---|
141 | def parse(self, value): |
---|
142 | """FIXME: Not fully standard compliant, |
---|
143 | -> hex encodings""" |
---|
144 | return float(value) |
---|
145 | def __str__(self): |
---|
146 | return "OMfloat("+repr(self.value)+")" |
---|
147 | |
---|
148 | if __name__=='__main__': |
---|
149 | from context import * |
---|
150 | |
---|
151 | from binding import * |
---|
152 | |
---|
153 | context=Context() |
---|
154 | |
---|
155 | context.push({}) |
---|
156 | |
---|
157 | context["x"]=OMint(1) |
---|
158 | |
---|
159 | x=OMvar("x") |
---|
160 | |
---|
161 | y=OMvar("y") |
---|
162 | |
---|
163 | print context.evaluate(x) |
---|
164 | print context.evaluate(y) |
---|
165 | firstArg=OMbinding(lambdasym,[OMvar("x"), OMvar("y")], OMvar("x")) |
---|
166 | #print context.evaluate(firstArg) |
---|
167 | application=OMapplication(firstArg, [x,y]) |
---|
168 | print context.evaluate(application) |
---|
169 | application=OMapplication(firstArg,[y,x]) |
---|
170 | print context.evaluate(application) |
---|
171 | import arith1 |
---|
172 | context.addCDImplementation(arith1.implementation) |
---|
173 | #print type(context.lookupImplementation(arith1.plussym)) |
---|
174 | #application=OMapplication(arith1.plussym,[x]) |
---|
175 | #application=OMapplication(arith1.plussym,[x,x]) |
---|
176 | application=OMapplication(OMsymbol("plus",arith1.content),[x,x]) |
---|
177 | |
---|
178 | print context.evaluate(application) |
---|
179 | application=OMapplication(OMsymbol("plus",arith1.content),[x,x,x]) |
---|
180 | |
---|
181 | print context.evaluate(application) |
---|
182 | i=OMint(22482489) |
---|
183 | print i.body |
---|
184 | i.body="dshj" |
---|
185 | |
---|