1 | from exceptions import * |
---|
2 | from copy import copy |
---|
3 | from cd import * |
---|
4 | from omexceptions import * |
---|
5 | from objects import * |
---|
6 | from re import sub |
---|
7 | class Context(object): |
---|
8 | #TODO: Referenzen durch scope richtig behandeln |
---|
9 | def __init__(self): |
---|
10 | self.scope=Scope() |
---|
11 | self.implementations={} |
---|
12 | self.XMLEncoder=SimpleXMLEncoder() |
---|
13 | def addCDImplementation(self, implementation): |
---|
14 | self.implementations[implementation.cd]=implementation |
---|
15 | def lookupImplementation(self, oms): |
---|
16 | try: |
---|
17 | return self.implementations[oms.cd][oms] |
---|
18 | except KeyError: |
---|
19 | raise NotImplementedError |
---|
20 | def __getitem__(self,itemname): |
---|
21 | return self.scope[itemname] |
---|
22 | def __setitem__(self,itemname, item): |
---|
23 | self.scope[itemname]=item |
---|
24 | def push(self, dict): |
---|
25 | """push a lexical context in form of a dictionary""" |
---|
26 | self.scope.push(dict) |
---|
27 | def pop(self, dict): |
---|
28 | """pop a lexical context""" |
---|
29 | return self.scope.pop(dict) |
---|
30 | def scopeFromCurrentScope(self): |
---|
31 | """returns a new Scope object, sharing the dictionaries, |
---|
32 | which will represent the current scope""" |
---|
33 | return self.scope.derriveScope() |
---|
34 | def toGeneric(self, o): |
---|
35 | return o.getValue() |
---|
36 | |
---|
37 | def evaluateSymbol(self, oms): |
---|
38 | try: |
---|
39 | impl=self.lookupImplementation(oms) |
---|
40 | if len(oms.attributes)>0: |
---|
41 | impl=copy(impl) |
---|
42 | impl.attributes=copy(oms.attributes) |
---|
43 | return impl |
---|
44 | except NotImplementedError: |
---|
45 | #TODO log this, report it |
---|
46 | return oms |
---|
47 | def evaluate(self,omobject): |
---|
48 | return omobject.evaluate(self) |
---|
49 | def evaluateInScope(self,omobject, scope): |
---|
50 | bak=self.scope |
---|
51 | self.scope=scope |
---|
52 | erg=self.evaluate(omobject) |
---|
53 | self.scope=bak |
---|
54 | #print "my erg is", erg |
---|
55 | return erg |
---|
56 | def package(self, val): |
---|
57 | if isinstance(val, OMobject): |
---|
58 | return val |
---|
59 | else: |
---|
60 | if isinstance(val, int): |
---|
61 | return OMint(val) |
---|
62 | def apply(self,func,args): |
---|
63 | try: |
---|
64 | return func(self,*args) |
---|
65 | except: |
---|
66 | raise EvaluationFailedError |
---|
67 | def XMLEncodeBody(self,body): |
---|
68 | return self.XMLEncoder.encode(body) |
---|
69 | def XMLEncodeObject(self, obj): |
---|
70 | return obj.XMLencode(self) |
---|
71 | class SimpleXMLEncoder(object): |
---|
72 | def encode(self, string): |
---|
73 | return sub("<","<",sub("&","&",string)) |
---|
74 | |
---|
75 | class Scope(object): |
---|
76 | def __init__(self): |
---|
77 | self.dicts=[] |
---|
78 | def push(self, dict): |
---|
79 | self.dicts.append(dict) |
---|
80 | def pop(self): |
---|
81 | return self.dicts.pop() |
---|
82 | def __getitem__(self,itemname): |
---|
83 | i=len(self.dicts)-1 |
---|
84 | while i>=0: |
---|
85 | try: |
---|
86 | return self.dicts[i][itemname] |
---|
87 | except KeyError: |
---|
88 | pass |
---|
89 | i=i-1 |
---|
90 | raise OutOfScopeError |
---|
91 | def __setitem__(self, itemname, item): |
---|
92 | try: |
---|
93 | self.dicts[len(self.dicts)-1][itemname]=item |
---|
94 | except IndexError: |
---|
95 | print "scope has no layers" |
---|
96 | def derriveScope(self): |
---|
97 | erg=Scope() |
---|
98 | erg.dicts=copy(self.dicts) |
---|
99 | return erg |
---|