-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshapescript.py
58 lines (47 loc) · 1.02 KB
/
shapescript.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
import sys
def interpret(code, stack = None):
if stack == None:
stack = ['' if sys.stdin.isatty() else sys.stdin.read()]
tail_call = True
while tail_call:
if len(code) and code[-1] == '!':
code = code[:-1]
else:
tail_call = False
stringing = 0
for char in code:
quote = 1 + '\'\"'.find(char)
if quote or stringing:
if not stringing:
string = ''
stringing = quote
elif stringing == quote:
stack.append(string)
stringing = 0
else:
string += char
elif char in '0123456789':
stack.append(int(char))
else:
x = stack.pop()
if char == '!':
interpret(x, stack)
else:
if char == '?':
y = stack[~x]
elif char == '_':
y = len(x)
else:
y = stack.pop()
if char == '@':
stack.append(x)
elif char == '$':
y = y.split(x)
elif char == '~':
y = x.join(map(str, y))
else:
y = eval(repr(y) + char + repr(x))
stack.append(y)
if tail_call:
code = stack.pop()
return stack