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