-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontest.py
217 lines (154 loc) · 5.19 KB
/
contest.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
import sys
from typing import List, Dict, Any
from loguru import logger as log
from collections import namedtuple
Return = namedtuple("Return", "stdout retval error")
def solve(data):
tokens = data["tokens"]
assert tokens[0] == "start"
assert tokens[-1] == "end"
stdout = ""
funcs = parse_functions(tokens)
for func in funcs:
log.debug("--- new function ---")
result = interpret(func, {})
stdout += result.stdout if not result.error else "ERROR"
stdout += "\n"
return stdout
def parse_functions(tokens):
assert len(tokens) > 0
assert tokens[0] == "start"
assert tokens[-1] == "end"
functions = []
function = []
# consume first start token
i = 1
while i < len(tokens):
while i < len(tokens) and tokens[i] != "start":
function.append(tokens[i])
i += 1
# pop last end
assert function.pop() == "end"
# append copy and re-use list
functions.append(function[:])
function.clear()
i += 1
return functions
def interpret(tokens: List[str], variables: Dict[str, Any]):
log.debug("---")
stdout = ""
retval = None
error = False
def _get(name):
if name not in variables:
return name
return variables[name]
i = 0
while i < len(tokens):
tok = tokens[i]
if tok == "start":
log.error(f"token invalid here, fail-fast: {tok}")
sys.exit(1)
elif tok == "end":
log.error(f"token invalid here, fail: {tok}")
sys.exit(1)
elif tok == "return":
i += 1
retval = _get(tokens[i])
break
elif tok == "var":
i += 1
varname = tokens[i]
i += 1
varval = _get(tokens[i])
if varname in variables:
log.warning(f"tried creating variable that exists: {varname}")
error = True
break
variables[varname] = varval
elif tok == "set":
i += 1
varname = tokens[i]
i += 1
varval = _get(tokens[i])
if varname not in variables:
log.warning(f"tried setting variable that does not exists: {varname}")
error = True
break
variables[varname] = varval
elif tok == "if":
# consume boolean value
i += 1
boolval = _get(tokens[i])
if boolval != "true" and boolval != "false":
log.warning(f"invalid boolean value encountered: {boolval}")
error = True
break
# advance
i += 1
# scan inner block (if) --- START
inner_true = []
# skip multiple ends
parsed_ends, goal_ends = 0, 1
is_end = False
while not is_end:
tok = tokens[i]
# check end condition
if tok == "end":
parsed_ends += 1
is_end = parsed_ends == goal_ends
if not is_end:
inner_true.append(tok)
i += 1
# detected an inner if, skip one end
if tok == "if":
# we need to skip the next two end's now
goal_ends += 2
# scan inner block (if) --- END
# consume else
assert tokens[i] == "else"
i += 1
# scan inner block (if) --- START
inner_false = []
# skip multiple ends
parsed_ends, goal_ends = 0, 1
is_end = False
while not is_end:
tok = tokens[i]
# check end condition
if tok == "end":
parsed_ends += 1
is_end = parsed_ends == goal_ends
if not is_end:
inner_false.append(tok)
i += 1
# detected an inner if, skip one end
if tok == "if":
# we need to skip the next two end's now
goal_ends += 2
# scan inner block (if) --- END
# undo, we already advanced before
i += -1
# execute correct block
next_block = inner_true if boolval == "true" else inner_false
inner_func = interpret(next_block, variables)
stdout += inner_func.stdout
# check if no error occurred
if inner_func.error:
assert inner_func.retval is None
return Return(stdout, None, True)
# check if no return occurred
if inner_func.retval is not None:
return Return(stdout, inner_func.retval, inner_func.error)
elif tok == "print":
i += 1
stdout += _get(tokens[i])
else:
log.error(f"unknown token, fail {tok}")
sys.exit(1)
i += 1
# there can be no return if an error occurred before
if error:
assert retval is None
log.debug(f"return value: {retval}")
return Return(stdout, retval, error)