-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngine.py
More file actions
174 lines (149 loc) · 6.4 KB
/
Engine.py
File metadata and controls
174 lines (149 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from expression.ImmediateValue import ImmediateValue
from expression.CallOperator import CallOperator
from expression.IExpression import IExpression
from operators.IOperator import IOperator
from Closure import Closure
from operators.AddOperator import AddOperator
from operators.SubOperator import SubOperator
from operators.MulOperator import MulOperator
from operators.DivOperator import DivOperator
from operators.ModOperator import ModOperator
from operators.EqualOperator import EqualOperator, NotEqualOperator, LessOperator, GreatOperator, LEqOperator, GEqOperator
from operators.IOperator import IOperator
from operators.GetOperator import GetOperator
from operators.SetOperator import SetOperator
from operators.StepOperator import StepOperator
from operators.UntilOperator import UntilOperator
from operators.IfOperator import IfOperator
from operators.LogicOperator import NotOperator, AndOperator, OrOperator
from operators.PrintOperator import PrintOperator
from operators.PrintlnOperator import PrintlnOperator
from operators.DeffunOperator import DeffunOperator
from operators.WhileOperator import WhileOperator
from operators.GetfunOperator import GetfunOperator
from operators.DynamicOperator import DynamicOperator
from operators.LambdaOperator import LambdaOperator
from operators.MapOperator import MapOperator
from operators.SetIdxOperator import SetIdxOperator
from operators.AsIVOperator import AsIVOperator
from operators.DefvarOperator import DefvarOperator
from operators.SeqOperator import SeqOperator
from operators.FoldOperator import FoldOperator
from operators.LengthOperator import LengthOperator
from operators.CarOperator import CarOperator
from operators.CdrOperator import CdrOperator
from operators.LoadOperator import LoadOperator
from operators.CondOperator import CondOperator
from operators.AliasOperator import AliasOperator
from operators.ConsOperator import ConsOperator
from operators.ForeachOperator import ForeachOperator
from operators.LetOperator import LetOperator
from operators.WhenOperator import WhenOperator
from operators.IsListOperator import IsListOperator
from operators.RemoveOperator import RemoveOperator
class Engine:
def __init__(self):
self.variables = {}
self.variables["+"] = AddOperator()
self.variables["-"] = SubOperator()
self.variables["*"] = MulOperator()
self.variables["/"] = DivOperator()
self.variables["%"] = ModOperator()
self.variables["="] = EqualOperator()
self.variables["<"] = LessOperator()
self.variables[">"] = GreatOperator()
self.variables["<="] = LEqOperator()
self.variables[">="] = GEqOperator()
self.variables["set"] = SetOperator()
self.variables["get"] = GetOperator()
self.variables["until"] = UntilOperator()
self.variables["step"] = StepOperator()
self.variables["if"] = IfOperator()
self.variables["!"] = NotOperator()
self.variables["not"] = self.variables["!"]
self.variables["&&"] = AndOperator()
self.variables["and"] = self.variables["&&"]
self.variables["||"] = OrOperator()
self.variables["or"] = self.variables["||"]
self.variables["print"] = PrintOperator()
self.variables["println"] = PrintlnOperator()
self.variables["def"] = DeffunOperator()
self.variables["while"] = WhileOperator()
self.variables["get-fun"] = GetfunOperator()
self.variables["lambda"] = LambdaOperator()
self.variables["map"] = MapOperator()
self.variables["set-idx"] = SetIdxOperator()
self.variables["as-iv"] = AsIVOperator()
self.variables["def-var"] = DefvarOperator()
self.variables["seq"] = SeqOperator()
self.variables["fold"] = FoldOperator()
self.variables["length"] = LengthOperator()
self.variables["car"] = CarOperator()
self.variables["cdr"] = CdrOperator()
self.variables["load"] = LoadOperator()
self.variables["cond"] = CondOperator()
self.variables["alias"] = AliasOperator()
self.variables["cons"] = ConsOperator()
self.variables["for-each"] = ForeachOperator()
self.variables["let"] = LetOperator()
self.variables["when"] = WhenOperator()
self.variables["list?"] = IsListOperator()
self.variables["remove"] = RemoveOperator()
self.super_ = None
def clone(self):
newEngine = Engine()
newEngine.super_ = self
for key, value in self.variables.items():
newEngine.variables[key] = value
return newEngine
def defineVariable(self, name, value):
self.variables[name] = value
return value
def setVariable(self, name, value):
engine = self
while True:
if name in engine.variables:
engine.variables[name] = value
return value
elif engine.super_ is not None:
engine = engine.super_
else:
return engine.defineVariable(name, value)
def getVariable(self, name):
engine = self
while True:
if name in self.variables:
return self.variables[name]
elif engine.super_ is not None:
engine = engine.super_
else:
return None
def eval(self, script):
ret = self.getExpression(script)
if isinstance(ret, IOperator):
return ret
ret = ret.eval(self)
if isinstance(ret, IOperator):
return Closure(self, ret)
else:
return ret
def getExpression(self, script):
if isinstance(script, ImmediateValue):
return script
if isinstance(script, list):
if isinstance(script[0], list):
ret = CallOperator(self.variables[script[0][0]], script[0][1:])
tmp = ret.eval(self)
if isinstance(tmp, Closure):
return ImmediateValue(tmp.eval(script[1:]))
elif isinstance(tmp, IOperator):
return ImmediateValue(tmp.call(self, script[1:]))
return CallOperator(self.variables[script[0]], script[1:])
else:
tmp = self.getVariable(script)
if tmp is not None:
if isinstance(tmp, IOperator):
return tmp
else:
return ImmediateValue(tmp)
return ImmediateValue(script)