source: git/modules/openmathserver/objects.py @ 68549d

spielwiese
Last change on this file since 68549d was 68549d, checked in by Michael Brickenstein <bricken@…>, 19 years ago
*bricken: initial version git-svn-id: file:///usr/local/Singular/svn/trunk@8358 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 3.4 KB
Line 
1from omexceptions import *
2from cd import *
3class OMobject(object):
4    def __init__(self):
5        self.attributes={}
6    pass
7class OMvar(OMobject):
8    def __init__(self,name):
9        super(OMvar,self).__init__()
10        self.name=name
11    def evaluate(self,context):
12        try:
13            return context[self.name]
14        except OutOfScopeError:
15            return self
16    def __str__(self):
17        return "OMV(" + self.name +")"
18class OMapplication(OMobject):
19    def __init__(self, func, args):
20        super(OMapplication,self).__init__()
21        self.func=func
22        self.args=args
23    def evaluate(self,context):
24        efunc=context.evaluate(self.func)
25        eargs=[context.evaluate(a) for a in self.args]
26        if callable(efunc):
27            try:
28                return context.apply(efunc, eargs)
29            except EvaluationFailedError, NotImplementedError:
30                return self
31        else:
32            return OMapplication(efunc, eargs)
33class OMsymbol(OMobject):
34    def __init__(self,name,cd=None):
35        super(OMsymbol,self).__init__()
36        self.cd=cd
37        self.name=name
38    def __eq__(self, other):
39        return bool(other.name==self.name and self.cd==other.cd)
40    def __str__(self):
41        return "OMS("+self.name+", "+self.cd.name + ")"
42    def __hash__(self):#
43        return hash((self.name,self.cd.__hash__()))
44    def evaluate(self,context):
45        return context.evaluateSymbol(self)
46class SimpleValue(OMobject):
47    def __init__(self,value):
48        super(SimpleValue,self).__init__()
49        if (isinstance(value, str)):
50            value=self.parse(value)
51        self.value=value
52    def evaluate(self, context):
53        return self
54    def getValue(self):
55        return self.value
56    def __str__(self):
57        return "OM("+repr(self.value)+")"
58
59class OMint(SimpleValue):
60    def __init__(self,value):
61        super(OMint,self).__init__(value)
62    def parse(self,value):
63        """FIXME: Not fully standard compliant,
64        -> hex encodings"""
65        return int(value,10)
66    def __str__(self):
67        return "OMint("+repr(self.value)+")"
68   
69
70class OMfloat(SimpleValue):
71    def __init__(self,value):
72        super(OMfloat,self).__init__(value)
73    def parse(self, value):
74        """FIXME: Not fully standard compliant,
75        -> hex encodings"""
76        return float(value)
77    def __str__(self):
78        return "OMfloat("+repr(self.value)+")"
79       
80if __name__=='__main__':
81    from context import *
82
83    from binding import *
84
85    context=Context()
86
87    context.push({})
88
89    context["x"]=OMint(1)
90
91    x=OMvar("x")
92
93    y=OMvar("y")
94
95    print context.evaluate(x)
96    print context.evaluate(y)
97    firstArg=OMbinding(lambdasym,[OMvar("x"), OMvar("y")], OMvar("x"))
98    #print context.evaluate(firstArg)
99    application=OMapplication(firstArg, [x,y])
100    print context.evaluate(application)
101    application=OMapplication(firstArg,[y,x])
102    print context.evaluate(application)
103    import arith1
104    context.addCDImplementation(arith1.implementation)
105    #print type(context.lookupImplementation(arith1.plussym))
106    #application=OMapplication(arith1.plussym,[x])
107    #application=OMapplication(arith1.plussym,[x,x])
108    application=OMapplication(OMsymbol("plus",arith1.content),[x,x])
109   
110    print context.evaluate(application)
111    application=OMapplication(OMsymbol("plus",arith1.content),[x,x,x])
112   
113    print context.evaluate(application)
114   
115   
Note: See TracBrowser for help on using the repository browser.