-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.py
255 lines (239 loc) · 5.76 KB
/
runtime.py
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# import time
# the pounce language runtime
def deepcopy(l):
newlist = []
for e in l:
if isArray(e):
newlist.append(deepcopy(e))
else:
newlist.append(e)
return newlist
def numBool(boolVal) :
if boolVal:
return 1
return 0
# def evalNumBool(numVal):
# if numVal == 0 :
# return False
# return True
def pounceReplace(id, val):
def r (ele):
if ele == id:
return val
return ele
return r
def _compose(s, pl):
global words
# usage: [phrase] [new-word-name] compose
new_word = s.pop()
new_definition = s.pop()
words[new_word] = new_definition
return [s, pl]
def _pounce(s, pl):
# [param1 param2] [phrase]
phrase = deepcopy(s.pop())
params = s.pop().copy()
while len(params) > 0 :
name = params.pop()
val = s.pop()
rep = pounceReplace(name, val)
phrase = list(map(rep, phrase))
pl = phrase+pl
return [s, pl]
def _crouch(s, pl):
# [param1 param2] [phrase]
phrase = deepcopy(s.pop())
params = s.pop().copy()
while len(params) > 0 :
id = params.pop()
val = s.pop()
rep = pounceReplace(id, val)
phrase = list(map(rep, phrase))
s.append(phrase)
return [s, pl]
def _dup(s, pl):
a = s[-1]
if isArray(a):
s.append(deepcopy(a))
else:
s.append(a)
return [s, pl]
def _add(s, pl):
a = s.pop()
b = s.pop()
s.append(a + b)
return [s, pl]
def _sub(s, pl):
a = s.pop()
b = s.pop()
s.append(b - a)
return [s, pl]
def _mult(s, pl):
a = s.pop()
b = s.pop()
s.append(a * b)
return [s, pl]
def _divide(s, pl):
a = s.pop()
b = s.pop()
s.append(b / a)
return [s, pl]
def _modulo(s, pl):
a = s.pop()
b = s.pop()
s.append(b % a)
return [s, pl]
def _n_prod(s, pl):
if len(s) >= 2:
a = s.pop()
b = s.pop()
if isNumber(a) and isNumber(b):
s.append(a * b)
pl.insert(0, 'n*')
return [s, pl]
else:
s.append(b)
s.append(a)
return [s, pl];
def _eq(s, pl):
a = s.pop()
b = s.pop()
s.append(numBool(a == b))
return [s, pl]
def _gt(s, pl):
a = s.pop()
b = s.pop()
s.append(numBool(b > a))
return [s, pl]
def _lt(s, pl):
a = s.pop()
b = s.pop()
s.append(numBool(b < a))
return [s, pl]
def _ift(s, pl):
then_block = s.pop()
expression = s.pop()
if expression:
if isArray(then_block):
pl = then_block+pl
else:
pl.insert(0, then_block)
return [s, pl]
def _ifte (s, pl):
else_block = s.pop()
then_block = s.pop()
expression = s.pop()
if expression:
if isArray(then_block):
#print(then_block)
pl = then_block+pl
else:
pl.insert(0, then_block)
else:
if isArray(else_block):
pl = else_block+pl
else:
pl.insert(0, else_block)
return [s, pl]
def _get(s, l): # (dict key -- dict value)
key = s.pop()
dictionary = s[-1]
s.append(dictionary[key])
return [s, l]
def _set(s, l): # (dict value key -- dict)
key = s.pop()
value = s.pop()
dictionary = s[-1]
dictionary[key] = value
return [s, l]
def _leap(s, l):
phrase = s.pop()
l = phrase+l # concat arrays so that the program list (l) has words on it, not as a list
return [s, l]
def _swap(s, l):
a = s.pop()
b = s.pop()
s.append(a)
s.append(b)
return [s, l]
def _drop(s, l):
a = s.pop()
return [s, l]
def _dip(s, l):
f = s.pop()
a = s.pop()
l.insert(0, a)
l = f+l
return [s, l]
words = {
'compose': _compose,
'pounce': _pounce,
'crouch': _crouch,
'dup': _dup,
'+': _add,
'-': _sub,
'*': _mult,
'/': _divide,
'%': _modulo,
'n*': _n_prod,
'==': _eq,
'<': _lt,
'>': _gt,
'if': _ift,
'ifte': _ifte,
'get': _get,
'set': _set,
'leap': _leap,
'swap': _swap,
'drop': _drop,
'dip': _dip
}
def isValue(e, fun):
return (isinstance(e, int)
or isinstance(e, float)
or isinstance(e, bool)
or (isinstance(e, str) and not e in fun.keys()))
def isNumber(e):
return isinstance(e, int) or isinstance(e, float)
def isArray(a):
return isinstance(a, (list,))
def isRecord(a):
return isinstance(a, (dict,))
#from inspect import isfunction
def isfunction(candidate):
return not (isinstance(candidate, str) or isinstance(candidate, (list,)))
# purr is the interpreter
def purr(pl, words=words, debug = False, stack = []):
vs = []
while pl != None and len(pl) > 0:
next = pl[0]
pl = pl[1:]
# if debug:
# print('about to', vs, next)
# time.sleep(0.3)
if isValue(next, words) or isArray(next) or isRecord(next):
if next == 'true':
vs.append(True)
elif next == 'false':
vs.append(False)
else:
vs.append(next)
elif next in words.keys():
# if debug:
# print('applying', vs, next, pl)
# time.sleep(0.3)
if isfunction(words[next]):
(vs, pl) = words[next](vs, pl)
elif isinstance(words[next], str):
pl = jp.parse(words[next]) + pl
elif isRecord(words[next]):
if 'args' in words[next].keys():
arg_rec = {}
while len(words[next].args) > 0:
arg_rec[words[next].args.pop()] = s.pop()
(vs, pl) = words[next].func(vs, pl, arg_rec)
else:
pl = words[next] + pl
else:
print('unknown term or word:', next)
return vs