-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtxtzyme.py
More file actions
61 lines (54 loc) · 1.01 KB
/
txtzyme.py
File metadata and controls
61 lines (54 loc) · 1.01 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
"""
Skeleton of Ward Cunningham's txtzyme language for embedded controllers.
"""
import re
## run('42p 5p')
#. 42
#. 5
#.
## run('5{3p}')
#. 3
#. 3
#. 3
#. 3
#. 3
#.
## run('5{kp}')
#. 4
#. 3
#. 2
#. 1
#. 0
#.
## tokenize('5{kp} 42p')
#. ['5', '{', 'k', 'p', '}', '42', 'p']
def tokenize(s):
return [token
for token in re.findall(r'\d+|\s+|.', s)
if not token.isspace()]
def run(s):
tokens = tokenize(s)
acc = i = 0
pc = 0
loop_start = 0
while pc < len(tokens):
c = tokens[pc]
if c.isdigit():
acc = int(c)
elif c == 'p':
print acc
elif c == '{':
i = acc
loop_start = pc + 1
pc = tokens.index('}', pc)
continue
elif c == '}':
if 0 < i:
i -= 1
pc = loop_start
continue
elif c == 'k':
acc = i
else:
raise Exception('Unexpected character: %s' % c)
pc += 1