-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstructure.py
76 lines (54 loc) · 1.76 KB
/
structure.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
from tokenizer import Token, TokenType, tokenize
class ForLoop:
"""
For loop structure class.
Syntax:
↹number{body}
(Borrowed from SYNTAX.md)
"""
def __init__(self, number: Token, body: list):
self.number = number
self.body = body
def __repr__(self):
return f"ForLoop(number={self.number}, body={self.body})"
def get_bracket_index(token_list):
balance = 1
index = 0
for index in range(len(token_list)):
if token_list[index].value == "{":
balance += 1
if token_list[index].value == "}":
balance -= 1
if balance == 0:
return index
return index
def structure_forLoop(token_list: list[Token], main=True):
if main:
tokens_copy = list(token_list)
for index, token in enumerate(token_list):
if token.value == "↹" and token_list[index + 1].value == "{":
tokens_copy.insert(index + 1, Token(TokenType.NUMBER, 10))
token_list = list(tokens_copy)
output_lst = []
index = 0
while index < len(token_list):
token = token_list[index]
if token.value == "↹":
bracket_index = get_bracket_index(token_list[3 + index :])
output_lst += [
ForLoop(
token_list[index + 1],
structure_forLoop(
token_list[3 + index : bracket_index + 3 + index], main=False
),
)
]
index += bracket_index + 3
else:
output_lst.append(token)
index += 1
return output_lst
def structure(tokens):
return structure_forLoop(tokens)
if __name__ == "__main__":
print(structure(tokenize("3↹2{1↹{9}}5")))