-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnek.py
426 lines (369 loc) · 12.5 KB
/
snek.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import functools
import logging
import operator
import random
import time
import pyparsing as pp
UNFINISHED = "const unfinished"
DONE = "const done"
pp.ParserElement.enable_packrat()
logger = logging.getLogger(__name__)
class SnekCommand:
def __iter__(self):
return self
def __next__(self):
return self.get_value()
def get_value(self):
return None
class Wait(SnekCommand):
def __init__(self, amount=1000):
self.time = amount
self.start = time.time() * 1000
def get_value(self):
now = time.time() * 1000
if now - self.start >= self.time:
return 1
return UNFINISHED
class Any:
def __eq__(self, other):
return True
def __lt__(self, other):
return False
def __gt__(self, other):
return False
def __le__(self, other):
return True
def __ge__(self, other):
return False
def __repr__(self):
return f"SNEK const ANY"
def snek_command(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
yield func(*args, **kwargs)
return wrapper
def unfinished_run():
yield UNFINISHED
yield 1
def neq(*args):
last_arg = args[0]
for arg in args:
if arg != last_arg:
return True
return False
class Lexer:
"""Holds all lexing primitives and a function to use them"""
cache = {}
# argument primitives
comment = "#" + pp.rest_of_line
number = pp.common.number
string = pp.QuotedString("'", esc_char="\\") | pp.QuotedString('"', esc_char="\\")
varname = pp.common.identifier
literal = number | string | varname
expression = pp.Forward()
# commands and keywords
command = (
varname
+ "("
+ pp.Opt(pp.DelimitedList(expression, allow_trailing_delim=True))
+ ")"
)
control = pp.one_of("if switch case while", as_keyword=True)
# expressions
assignment = varname + "="
expression <<= pp.Group(
pp.infix_notation(
pp.Group(command) | literal,
[
# almost all the same operators as python, with the same precedences
# https://www.w3schools.com/python/python_operators.asp
("**", 2, pp.opAssoc.LEFT),
(pp.one_of("+ - ~"), 1, pp.opAssoc.RIGHT),
(pp.one_of("* / // %"), 2, pp.OpAssoc.LEFT),
(pp.one_of("+ -"), 2, pp.OpAssoc.LEFT),
(pp.one_of("<< >>"), 2, pp.OpAssoc.LEFT),
("&", 2, pp.OpAssoc.LEFT),
("^", 2, pp.OpAssoc.LEFT),
("|", 2, pp.OpAssoc.LEFT),
(pp.one_of("== != > >= < <= in"), 2, pp.OpAssoc.LEFT),
("not in", 2, pp.OpAssoc.LEFT),
(pp.one_of("! not"), 1, pp.opAssoc.RIGHT),
("and", 2, pp.opAssoc.LEFT),
("or", 2, pp.opAssoc.LEFT),
],
)
)
grammar = pp.ZeroOrMore(
pp.Group(pp.Literal("}"))
| pp.Group(control + pp.Opt(expression) + pp.Literal("{"))
| pp.Group(pp.Opt(assignment) + expression + pp.Literal(";").suppress())
).ignore(comment)
@classmethod
def tokenize(cls, string):
if string in cls.cache:
return cls.cache[string]
result = cls.grammar.parse_string(string, parse_all=True)
cls.cache[string] = result
return result
@classmethod
def _test(cls):
for test in (
"a = 1; \nb = 2;",
"a = !bool(bool(1));",
"-1 + 3 * -4;",
"a = bool() + 1 - 3;",
"b(a, 1 + -1);",
"bool(a + 45, -5 * (3 + -b) - 6);",
"if x==0 {",
"if x {",
"}",
"while 1 {",
"_='Hi there!';",
):
result = cls.tokenize(test)
print(test)
print(result)
print()
class SNEKProgram:
def __init__(self, script, start_variables=None, api=None):
self.api = {
"upper": snek_command(lambda arg: arg.upper()),
"lower": snek_command(lambda arg: arg.lower()),
"title": snek_command(lambda arg: arg.title()),
"print": self.print,
"wait": Wait,
"randint": snek_command(random.randint),
"input": snek_command(input),
"bool": snek_command(lambda *args: bool(*args)),
"abs": snek_command(abs),
"sub": snek_command(lambda a, *args: a - sum(args)),
"div": snek_command(lambda *args: functools.reduce(operator.truediv, args)),
"fdiv": snek_command(
lambda *args: functools.reduce(operator.floordiv, args)
),
"getitem": snek_command(lambda x, y: x[y]),
"time": snek_command(time.time() * 1000),
}
if api is not None:
self.api.update(api)
# variables go here
self.namespace = {
"ANY": Any(),
}
if start_variables is not None:
self.namespace.update(start_variables)
self.operators = {
"**": pow,
"+": lambda x, y=None: x + y if y != None else +x,
"-": lambda x, y=None: x - y if y != None else -x,
"~": operator.invert,
"*": operator.mul,
"/": operator.truediv,
"//": operator.floordiv,
"%": operator.mod,
"<<": operator.lshift,
">>": operator.rshift,
"&": operator.and_,
"^": operator.xor,
"|": operator.or_,
"==": operator.eq,
"!=": operator.ne,
">": operator.gt,
">=": operator.ge,
"<": operator.lt,
"<=": operator.le,
"!": lambda x: not x,
"not": lambda x: not x,
"in": lambda x, y: x in y,
"not in": lambda x, y: x not in y,
# TODO: add short circuiting on logical operators
"and": lambda x, y: x and y,
"or": lambda x, y: x or y,
}
self.kwds = {
"if": self._if,
"switch": self._switch,
"case": self._case,
"while": self._while,
"count": self._count,
}
self.operand_evaluators = {
int: lambda x: x,
float: lambda x: x,
str: lambda x: self.namespace.get(x, x),
}
self.script = Lexer.tokenize(script)
self.index = 0
self.running = True
self.call_stack = []
self.runner = self._run()
def _evaluate_expression(self, expression):
# Try to evaluate all subgroups first
fixed = []
for token in expression:
if isinstance(token, pp.ParseResults):
evaluator = self._evaluate_expression(token)
value = next(evaluator)
while value == UNFINISHED:
yield UNFINISHED
value = next(evaluator)
token = value
if isinstance(token, str) and token in self.namespace:
token = self.namespace[token]
fixed.append(token)
match fixed:
# single term
case [op]:
yield op
# function call (always grouped by itself)
case [func_name, "(", *args, ")"]:
yield from self.api[func_name](*args)
# unary operator (also always grouped by itself)
case [op, arg]:
yield self.operators[op](arg)
# binary operator
case [arg1, op, arg2]:
yield self.operators[op](arg1, arg2)
# multiple binary operators strung together
# currently we do the leftmost operation and then rrecurse again
case [arg1, op, arg2, *rest]:
yield from self._evaluate_expression(
[self.operators[op](arg1, arg2), *rest]
)
def _skip_to_end(self, index):
brace_count = 1
while brace_count:
line = tuple(self.script[index])
if "{" in line:
brace_count += 1
if "}" in line:
brace_count -= 1
index += 1
return index
def _if(self, expression):
evaluator = self._evaluate_expression(expression)
value = next(evaluator)
while value == UNFINISHED:
yield UNFINISHED
value = next(evaluator)
if not value:
self.index = self._skip_to_end(self.index + 1) - 1
else:
self.call_stack.append(("if", value, None))
yield
def _switch(self, expression):
evaluator = self._evaluate_expression(expression)
value = next(evaluator)
while value == UNFINISHED:
yield UNFINISHED
value = next(evaluator)
self.call_stack.append(["switch", value, False])
yield
def _case(self, expression):
evaluator = self._evaluate_expression(expression)
value = next(evaluator)
while value == UNFINISHED:
yield UNFINISHED
value = next(evaluator)
owner = None
for data in reversed(self.call_stack):
if data[0] == "switch":
owner = data
break
if owner is None:
raise ValueError("case statement without switch", self.call_stack)
owner_value = owner[1]
if owner_value != value:
self.index = self._skip_to_end(self.index + 1) - 1
else:
owner[-1] = True
self.call_stack.append(("case", value, None))
yield
def _end_block(self):
data = self.call_stack.pop()
if data[0] == "while":
self.index = data[-1]
def _while(self, expression):
evaluator = self._evaluate_expression(expression)
value = next(evaluator)
while value == UNFINISHED:
yield UNFINISHED
value = next(evaluator)
if value:
self.call_stack.append(("while", expression, self.index - 1))
else:
self.index = self._skip_to_end(self.index + 1) - 1
yield
def _count(self):
yield
def _run(self):
self.index = 0
self.running = True
while self.running:
line = self.script[self.index]
match line:
case [kwd, *args, "{"] if kwd in self.kwds:
evaluator = self.kwds[kwd](*args)
value = next(evaluator)
while value == UNFINISHED:
yield UNFINISHED
value = next(evaluator)
case ["}"]:
self._end_block()
case [varname, "=", expr]:
evaluator = self._evaluate_expression(expr)
value = next(evaluator)
while value == UNFINISHED:
yield value
value = next(evaluator)
self.namespace[varname] = value
case [expr]:
evaluator = self._evaluate_expression(expr)
value = next(evaluator)
while value == UNFINISHED:
yield value
value = next(evaluator)
self.index += 1
if self.index >= len(self.script):
self.running = False
yield DONE
def cycle(self):
if self.running:
value = next(self.runner)
if value == DONE:
self.running = False
def run(self, delay=0.05):
# just iterate through the whole thing and kill all the async
for _ in self.runner:
time.sleep(delay)
def done(self):
return not self.running
@classmethod
def _eval_test(cls):
instance = cls("a = 0;")
for test in (
"1 + 1;",
"-1 + 3 * -4 // (wait(0) + 1);",
"(3 * 3 * 3 * -1);",
"(((((0))))) + -5;",
"2 >= 1 - 2;",
"1 // 8 + 2;",
):
print(test)
print(
"result:",
list(instance._evaluate_expression(Lexer.tokenize(test)[0][0])),
)
def print(self, *args):
logger.info(f"SNEK says: {' '.join(repr(arg) for arg in args)}")
yield 1
if __name__ == "__main__":
print("TOKENIZER TEST")
Lexer._test()
# print("EVALUATOR TEST")
# SNEKProgram._eval_test()
print("PROGRAM TEST")
with open("test.snk") as file:
script = file.read()
program = SNEKProgram(script)
program.run()