-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.py
337 lines (298 loc) · 11.6 KB
/
main.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
"""The main interface to the project - you run this file to run Vyxal programs
offline.
"""
import os
import sys
import traceback
import types
import vyxal.encoding
from vyxal.context import Context, TranspilationOptions
from vyxal.elements import *
from vyxal.helpers import *
from vyxal.transpile import transpile
THIS_FOLDER = os.path.dirname(os.path.abspath(__file__)) + "/.."
sys.path.insert(1, THIS_FOLDER)
__all__ = ["execute_vyxal", "repl", "cli"]
FLAG_STRING = """ALL flags should be used as is (no '-' prefix)
H Preset stack to 100
j Print top of stack joined by newlines on end of execution
L Print top of stack joined by newlines (Vertically) on end of execution
s Sum/concatenate top of stack on end of execution
M Make implicit range generation and while loop counter start at 0 instead of 1
m Make implicit range generation end at n-1 instead of n
Ṁ Equivalent to having both m and M flags
v Use Vyxal encoding for input file
c Output compiled code
f Get input from file instead of arguments
a Treat newline seperated values as a list
d Print deep sum of top of stack on end of execution
r Makes all operations happen with reverse arguments
S Print top of stack joined by spaces on end of execution
C Centre the output and join on newlines on end of execution
O Disable implicit output
o Force implicit output
l Print length of top of stack on end of execution
G Print the maximum item of the top of stack on end of execution
g Print the minimum item of the top of the stack on end of execution
W Print the entire stack on end of execution
Ṡ Treat all inputs as strings
R Treat numbers as ranges if ever used as an iterable
D Treat all strings as raw strings (don't decompress strings)
U Treat all strings as UTF-8 byte sequences (also don't decompress strings)
Ṫ Print the sum of the entire stack
ṡ Print the entire stack, joined on spaces
Z With four argument vectorization where all arguments are lists, use zip(zip(a, b), zip(c, d)) instead of zip(a, b, c, d)
J Print the entire stack, separated by newlines
t Vectorise boolify on Lists
P Print lists as their python representation
ḋ Print rationals in their decimal form
V Variables are one character long
E Evaluate stdout as JavaScript (online interpreter only)
Ḣ Render stdout as HTML (online interpreter only)
e Use the file name as the program source (offline interpreter only)
? If there is empty input, treat it as 0 instead of empty string.
2 Make the default arity of lambdas 2
3 Make the default arity of lambdas 3
A Run test cases on all inputs
… Limit list output to the first 100 items of that list
5 Make the interpreter timeout after 5 seconds (online interpreter only)
b Make the interpreter timeout after 15 seconds (online interpreter only)
B Make the interpreter timeout after 30 seconds (online interpreter only)
T Make the interpreter timeout after 60 seconds (online interpreter only)
"""
def execute_vyxal(file_name, flags, inputs, output_var=None, online_mode=False):
ctx = Context()
stack = []
ctx.online_output = output_var
ctx.online = online_mode
if online_mode:
inputs = inputs.split("\n") # have to do this here because file writing
if inputs[0] == "":
inputs = inputs[1:]
# Handle input handling flags
if "h" in flags: # Help flag
vy_print(FLAG_STRING, ctx=ctx)
sys.exit(0)
if "A" in flags:
for inp in inputs:
try:
inps = ast.literal_eval(inp)
if isinstance(inps, tuple):
inps = list(inps)
else:
inps = [inps]
inps = [vyxalify(x) for x in inps]
except:
inps = inp.split(", ")
repred_inps = [repr(x) for x in inps]
if online_mode:
ctx.online_output[1] += ", ".join(repred_inps) + " => "
else:
print(", ".join(repred_inps), end=" => ")
execute_vyxal(
file_name,
flags.replace("A", ""),
"\n".join(repred_inps) if online_mode else inps,
output_var,
online_mode,
)
return
if "e" in flags: # Program is file name
code = file_name
elif "v" in flags: # Open file using Vyxal encoding
with open(file_name, "rb") as f:
code = f.read()
code = vyxal.encoding.vyxal_to_utf8(code)
else: # Open file using UTF-8 encoding:
with open(file_name, "r", encoding="utf-8") as f:
code = f.read()
# Handle input handling flags
if "f" in flags: # Read inputs from file
with open(inputs[0], "r", encoding="utf-8") as f:
inputs = [x.replace("\r", "") for x in f.readlines()]
if "Ṡ" in flags: # All inputs as strings
inputs = list(map(str, inputs))
ctx.inputs_as_strings = True
else:
inputs = list(map(lambda x: vy_eval(x, ctx), inputs))
if "H" in flags: # Pre-initalise stack to 100
stack = [100]
else:
stack = []
ctx.inputs[0][0] = inputs
ctx.stacks.append(stack)
# Handle runtime flags
if "Ṁ" in flags: # Implicit ranges are [0, n)
ctx.range_start = 0
ctx.range_end = 0
elif "M" in flags: # Implicit ranges are [0, n]
ctx.range_start = 0
elif "m" in flags: # Implicit ranges are [1, n)
ctx.range_end = 0
ctx.reverse_flag = "r" in flags
ctx.number_as_range = "R" in flags
ctx.vectorise_boolify = "t" in flags # see boolify in elements.py
ctx.vyxal_lists = "P" not in flags
ctx.print_decimals = "ḋ" in flags
ctx.empty_input_is_zero = "?" not in flags
ctx.array_inputs = "a" in flags
ctx.double_zip_vectorize = "Z" in flags
ctx.utf8strings = "U" in flags
options = TranspilationOptions()
options.dict_compress = "D" not in flags
options.variables_as_digraphs = "V" in flags
options.utf8strings = ctx.utf8strings
ctx.transpilation_options = options
if "2" in flags:
ctx.default_arity = 2
elif "3" in flags:
ctx.default_arity = 3
else:
ctx.default_arity = 1
try:
code = transpile(code, options)
except Exception as e: # skipcq: PYL-W0703
if ctx.online:
ctx.online_output[2] += "\n" + traceback.format_exc()
sys.exit(1)
else:
raise e
if "c" in flags: # Show transpiled code
if ctx.online:
ctx.online_output[2] += code
else:
print(code + "\n")
ctx.stacks.append(stack)
try:
exec(code, locals() | globals())
except Exception as e: # skipcq: PYL-W0703
if ctx.online:
ctx.online_output[2] += "\n" + traceback.format_exc()
sys.exit(1)
else:
raise
if not ctx.printed and ctx.canvas.canvas != [[" "]]:
vy_print(str(ctx.canvas), ctx=ctx)
return
originally_empty = not stack
output = pop(stack, 1, ctx)
if not (ctx.printed or "O" in flags) or "o" in flags:
for flag in flags:
if flag == "j":
if isinstance(output, LazyList):
for item in output:
vy_print(item, ctx=ctx)
break
else:
output = join(output, "\n", ctx=ctx)
elif flag == "s":
if not isinstance(output, LazyList) or not output.has_ind(0):
output = vy_sum(output, ctx)
else:
acc = None
is_str = False
for elem in output:
if is_str:
vy_print(elem, end="", ctx=ctx)
continue
if acc is None:
# For the first element
acc = elem
else:
acc = add(acc, elem, ctx)
if isinstance(acc, str):
# We've encountered a string, now print that
# Everything else will also be immediately
# printed
vy_print(acc, end="", ctx=ctx)
is_str = True
output = acc
if not is_str:
vy_print(output, ctx=ctx)
break
elif flag == "d":
output = vy_sum(deep_flatten(output, ctx), ctx)
elif flag == "Ṫ":
if originally_empty:
output = []
else:
stack.append(output)
output = vy_sum(stack, ctx)
stack = [output]
elif flag == "L":
output = vertical_join(output, ctx=ctx)
elif flag == "S":
if isinstance(output, LazyList):
for item in output:
vy_print(item, end=" ", ctx=ctx)
break
else:
output = join(output, " ", ctx)
elif flag == "C":
output = center(output, ctx)
output = join(output, "\n", ctx)
elif flag == "G":
output = monadic_maximum(output, ctx)
elif flag == "g":
output = monadic_minimum(output, ctx)
elif flag == "W":
if originally_empty:
output = []
else:
stack.append(output)
output = vy_str(stack, ctx)
elif flag == "ṡ":
if originally_empty:
output = []
else:
stack.append(output)
output = join(stack, " ", ctx)
elif flag == "J":
if originally_empty:
output = []
else:
stack.append(output)
output = join(stack, "\n", ctx)
elif flag == "…":
if vy_type(output, simple=True) is list:
output = output[:100]
elif flag == "l":
output = length(output, ctx)
else:
pass
else:
vy_print(output, ctx=ctx)
def repl():
ctx, stack = Context(), []
# This is called if a file isn't given, just like it used to.
ctx.repl_mode = True
while True:
# Vyxal REPL ftw
# Empty options is still required
line = transpile(input(">>> "), TranspilationOptions())
stack = []
ctx.stacks.append(stack) # Finally, a use case for assignment by
# reference. Never thought I'd fine a time
# when it wouldn't be an actual pain.
print(line)
exec(line, locals() | globals())
res = []
while stack:
top = stack.pop()
if isinstance(top, types.FunctionType):
res.append(top(stack, top, ctx=ctx)[-1])
else:
res.append(top)
res = res[::-1]
vy_print(res, ctx=ctx)
ctx.stacks.pop()
def cli():
if len(sys.argv) > 1:
file_name = sys.argv[1]
flags = ""
inputs = []
if len(sys.argv) > 2:
flags, inputs = sys.argv[2], sys.argv[3:]
execute_vyxal(file_name, flags, inputs)
else:
repl()