-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproper.py
executable file
·372 lines (292 loc) · 8.2 KB
/
proper.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env python3
"""
Proper: The world's first Tore-oriented programming language.
"""
### Primitives #############################################
def drop(stack):
"""Discards the top stack item."""
stack.pop()
def swap(stack):
"""Reverses the top two stack items."""
a = stack.pop()
b = stack.pop()
stack.append(a)
stack.append(b)
def dup(stack):
"""Duplicates the top stack item."""
a = stack.pop()
stack.append(a)
stack.append(a)
def rot(stack):
"""Rotates the third item to the top."""
c = stack.pop()
b = stack.pop()
a = stack.pop()
stack.append(b)
stack.append(c)
stack.append(a)
def over(stack):
"""Makes a copy of the second item and pushes it on top."""
a = stack.pop()
b = stack.pop()
stack.append(b)
stack.append(a)
stack.append(b)
def inc(stack):
"""Increment."""
a = stack.pop()
stack.append(a + 1)
def dec(stack):
"""Decrement."""
a = stack.pop()
stack.append(a - 1)
def add(stack):
"""Addition."""
b = stack.pop()
a = stack.pop()
stack.append(a + b)
def sub(stack):
"""Subtraction."""
b = stack.pop()
a = stack.pop()
stack.append(a - b)
def mul(stack):
"""Multiplication."""
b = stack.pop()
a = stack.pop()
stack.append(a * b)
def _pow(stack):
"""Power."""
b = stack.pop()
a = stack.pop()
stack.append(a ** b)
def div(stack):
"""Division without remainder."""
b = stack.pop()
a = stack.pop()
stack.append(a // b)
def truediv(stack):
"""Division as float."""
b = stack.pop()
a = stack.pop()
stack.append(a / b)
def mod(stack):
"""Remainder of division."""
b = stack.pop()
a = stack.pop()
stack.append(a % b)
def eq(stack):
"""Equality."""
a = stack.pop()
b = stack.pop()
stack.append(1 if a == b else 0)
def lt(stack):
"""Less than."""
b = stack.pop()
a = stack.pop()
stack.append(1 if a < b else 0)
def _pop(stack):
print(stack.pop())
def _list(stack):
a = stack.pop()
stack.append([a])
def append(stack):
"""Concatenate two lists."""
b = stack.pop()
a = stack.pop()
stack.append(a + b)
def first(stack):
"""Find first element of list."""
a = stack.pop()
stack.append(a[0])
def rest(stack):
"""Return list without first element."""
a = stack.pop()
stack.append(a[1:])
def _print(stack):
print(stack[-1])
def print_stack(stack):
print("[", " ".join(str(e) for e in stack), "]", sep="")
def print_dictionary(stack):
for k in dictionary:
print(k)
def _quit(stack):
exit(1)
### Dictionary #############################################
dictionary = {
"drop": drop,
"swap": swap,
"dup": dup,
"rot": rot,
"over": over,
"inc": inc,
"dec": dec,
"+": add,
"-": sub,
"*": mul,
"^": _pow,
"/": truediv,
"//": div,
"%": mod,
"=": eq,
"<": lt,
"pop": _pop,
"list": _list,
"append": append,
"first": first,
"rest": rest,
"print": _print,
"print_stack": print_stack,
"print_dictionary": print_dictionary,
"quit": _quit,
}
### Parsing ################################################
def tokenize(inp):
"""Tokenize string to list of tokens."""
tokens = []
for line in inp.split("\n"):
if line:
if line.find("---") >= 0:
line = line[:line.find("---")]
tokens += [x for x in line.split(" ") if x]
return tokens
def is_integer(token):
return token.isnumeric()
def is_float(token):
try:
float(token)
return True
except ValueError:
return False
def is_string(token):
return token[0] == token[-1] == '"'
def is_quoted(token):
return token[0] == "'"
def parse(tokens):
"""Parse list of tokens to structure."""
result = []
i = 0
while i < len(tokens):
if is_integer(tokens[i]):
result.append(int(tokens[i]))
elif is_float(tokens[i]):
result.append(float(tokens[i]))
elif is_string(tokens[i]):
result.append(tokens[i])
elif is_quoted(tokens[i]):
result.append(tokens[i])
elif tokens[i] == "{":
depth = 0
lst = []
j = i + 1
while True:
if tokens[j] == "{":
depth += 1
if tokens[j] == "}":
depth -= 1
if depth == -1:
break
lst.append(tokens[j])
j += 1
i = j
result.append(parse(lst))
elif tokens[i] == "[":
depth = 0
lst = []
j = i + 1
while True:
if tokens[j] == "[":
depth += 1
if tokens[j] == "]":
depth -= 1
if depth == -1:
break
lst.append(tokens[j])
j += 1
i = j
result.append(parse(lst))
else:
try:
result.append(tokens[i])
except KeyError:
print("Error: Undefined procedure:", tokens[i])
i += 1
return result
### Eval ###################################################
def _eval(inp, stack):
while len(inp) > 0:
token = inp.pop(0)
try:
if isinstance(token, str) \
and not is_string(token) \
and not is_quoted(token):
if token == "define":
func = stack.pop()
if not isinstance(func, list):
func = [func]
name = stack.pop()
dictionary[name[1:]] = func
elif token == "load":
filename = stack.pop()
inp = parse(tokenize(open(filename[1:-1]).read())) + inp
elif token == "map":
func = stack.pop()
lst = stack.pop()
_inp = []
for e in lst:
_inp += [e] + [func[1:]]
_stack = []
_eval(_inp, _stack)
stack.append(_stack)
elif token == "reduce":
func = stack.pop()
lst = stack.pop()
inp = lst + [func[1:]] * (len(lst) - 1) + inp
elif token == "zip":
func = stack.pop()
lst2 = stack.pop()
lst1 = stack.pop()
_inp = []
for a, b in zip(lst1, lst2):
_inp += [a, b] + [func[1:]]
_stack = []
_eval(_inp, _stack)
stack.append(_stack)
elif token == "times":
n = stack.pop()
func = stack.pop()
_inp = []
for _ in range(n):
_inp += [func[1:]]
inp = _inp + inp
elif token == "ifelse":
alternative = stack.pop()
consequent = stack.pop()
condition = stack.pop()
if condition != 0:
inp = consequent + inp
else:
inp = alternative + inp
elif token in dictionary:
# Primitive
if callable(dictionary[token]):
dictionary[token](stack)
# Non-primitive
else:
func = dictionary[token][:]
inp = func + inp
else:
print("Error: Undefined procedure:", token)
else:
raise TypeError
except TypeError as e:
stack.append(token)
### Main ###################################################
if __name__ == '__main__':
import sys
STACK = []
INP = parse(tokenize(open("library.proper").read()))
_eval(INP, STACK)
if len(sys.argv) == 2:
INP = parse(tokenize(open(sys.argv[1]).read()))
_eval(INP, STACK)