-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdebug.py
More file actions
65 lines (60 loc) · 2.17 KB
/
debug.py
File metadata and controls
65 lines (60 loc) · 2.17 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
62
63
64
65
def tokenize(prog_line):
prog_line = prog_line.strip("[]").replace(" ", "")
prog_line = iter(prog_line)
in_loop = False
curr_token = ""
open_parenthesis = 0
result = []
for c in prog_line:
if not in_loop:
if c == ",":
if curr_token != "":
result.append(curr_token)
curr_token = ""
# the case where it just come out of the loop
else:
pass
elif curr_token == "loop":
# c must be "("
result.append("loop")
curr_token = ""
in_loop = True
open_parenthesis += 1
else:
curr_token += c
else:
# read loop count until the first "," in loop
while c != ",":
curr_token += c
c = next(prog_line)
result.append(curr_token) # save loop count
curr_token = ""
# read until there are no open parenthesis, then we are out of the loop
for c in prog_line:
if c == "(":
open_parenthesis += 1
elif c == ")":
open_parenthesis -= 1
if open_parenthesis > 0:
curr_token += c
else:
result.append(curr_token)
curr_token = ""
in_loop = False
break
return result
def is_loop_body(token):
# checks if token is loop body, i.e. []
return token.startswith('[') and token.endswith(']')
def list_all_pulses(tokens_set, tokens_lst):
for t in tokens_lst:
# loop body is not tokenize, so it needs to be further tokenized
if is_loop_body(t):
list_all_pulses(tokens_set, tokenize(t))
else:
# if t is a number, this means to wait, so don't save it in the set
if not t.isnumeric() and t != "loop":
tokens_set.update(t)
tokens = set()
tokens_lst = ['loop', '10', '[Dog,Cat,DOg,cat,DO,CA,DO,AC, cA]', 'CA']
list_all_pulses(tokens, tokens_lst)