Skip to content

Commit

Permalink
Got globals working
Browse files Browse the repository at this point in the history
  • Loading branch information
ysthakur committed Jul 12, 2021
1 parent dfec530 commit 76cfdd6
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 105 deletions.
14 changes: 7 additions & 7 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,25 +109,25 @@ def test_deep_vectorise():


def test_quit():
from vyxal import interpreter
real_print = vyxal.interpreter.vy_print
real_print = vyxal.builtins.vy_print

def shouldnt_print(first, *args):
raise ValueError("Shouldn't print anything")

vyxal.interpreter.vy_print = shouldnt_print
vyxal.builtins.vy_print = shouldnt_print
run_code("69 Q")
run_code("69 Q", flags="O")
run_code("69 Q", flags=["O"])

trip = []

def should_print(first, *args):
nonlocal trip
trip.append(first)

vyxal.interpreter.vy_print = should_print
run_code("69 Q", flags="o")
vyxal.builtins.vy_print = should_print
run_code("69 Q", flags=["o"])
assert trip
vyxal.interpreter.vy_print = real_print
vyxal.builtins.vy_print = real_print


"""
Expand Down
1 change: 1 addition & 0 deletions tests/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import vyxal
import vyxal.interpreter
import vyxal.builtins

# from from https://codegolf.stackexchange.com/a/210307
fizzbuzz_output = [
Expand Down
8 changes: 4 additions & 4 deletions vyxal/builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,8 +942,8 @@ def ncr(lhs, rhs):
(Number, Number): lambda: unsympy(
sympy.functions.combinatorial.numbers.nC(int(lhs), int(rhs))
),
(str, Number): lambda: [random.choice(lhs) for c in range(rhs)],
(Number, str): lambda: [random.choice(rhs) for c in range(lhs)],
(str, Number): lambda: [random.choice(lhs) for _ in range(rhs)],
(Number, str): lambda: [random.choice(rhs) for _ in range(lhs)],
(str, str): lambda: int(set(lhs) == set(rhs)),
}.get(types, lambda: vectorise(ncr, lhs, rhs))()

Expand Down Expand Up @@ -1092,7 +1092,7 @@ def pop(vector, num=1, wrap=False):
if vy_globals.retain_items:
vector += ret[::-1]

last_popped = ret
vy_globals.last_popped = ret
if num == 1 and not wrap:
return ret[0]

Expand Down Expand Up @@ -1842,7 +1842,7 @@ def vy_oct(item):


def vy_print(item, end="\n", raw=False):
printed = True
vy_globals.printed = True
t_item = type(item)
if t_item is Generator:
item._print(end)
Expand Down
2 changes: 1 addition & 1 deletion vyxal/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"J": ("rhs, lhs = pop(vy_globals.stack, 2); vy_globals.stack.append(join(lhs, rhs))", 2),
"K": ("vy_globals.stack.append(divisors_of(pop(vy_globals.stack)))", 1),
"L": ("top = pop(vy_globals.stack); vy_globals.stack.append(len(iterable(top)))", 1),
"M": ("fn, vector = pop(vy_globals.stack, 2); vy_globals.stack.append(vy_map(fn, vector))", 2),
"M": ("fn, vector = pop(vy_globals.stack, 2); temp = vy_map(fn, vector); vy_globals.stack.append(temp)", 2),
"N": ("vy_globals.stack.append(negate(pop(vy_globals.stack)))", 1),
"O": (
"needle, haystack = pop(vy_globals.stack, 2); vy_globals.stack.append(iterable(haystack).count(needle))",
Expand Down
79 changes: 0 additions & 79 deletions vyxal/foo.py

This file was deleted.

21 changes: 10 additions & 11 deletions vyxal/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,9 @@ def vy_compile(program, header=""):

def execute(code, flags, input_list, output_variable):
vy_globals.online_version = True
output = output_variable
output[1] = ""
output[2] = ""
vy_globals.output = output_variable
vy_globals.output[1] = ""
vy_globals.output[2] = ""
flags = flags

if input_list:
Expand All @@ -325,7 +325,7 @@ def execute(code, flags, input_list, output_variable):
vy_globals.set_globals(flags)

if "h" in flags:
output[
vy_globals.output[
1
] = """
ALL flags should be used as is (no '-' prefix)
Expand Down Expand Up @@ -375,7 +375,7 @@ def execute(code, flags, input_list, output_variable):
)
vy_globals.context_level = 0
if flags and "c" in flags:
output[2] = code
vy_globals.output[2] = code

try:
print(code)
Expand All @@ -384,12 +384,12 @@ def execute(code, flags, input_list, output_variable):
if "o" not in flags:
return
except Exception as e:
output[2] += "\n" + str(e)
output[
vy_globals.output[2] += "\n" + str(e)
vy_globals.output[
2
] += f"\nMost recently popped arguments: {[deref(i, limit=10) for i in vy_globals.last_popped]}"
output[2] += f"\nFinal stack: {[deref(i, limit=10) for i in vy_globals.stack]}"
raise e
vy_globals.output[2] += f"\nFinal stack: {[deref(i, limit=10) for i in vy_globals.stack]}"
print(e)

if (not vy_globals.printed and "O" not in flags) or "o" in flags:
if flags and "s" in flags:
Expand Down Expand Up @@ -421,8 +421,7 @@ def execute(code, flags, input_list, output_variable):
elif vy_globals._vertical_join:
vy_print(vertical_join(pop(vy_globals.stack)))
elif vy_globals._join:
if vy_globals.stack:
vy_print("\n".join([vy_str(n) for n in pop(vy_globals.stack)]))
vy_print("\n".join([vy_str(n) for n in pop(vy_globals.stack)]))
elif flags and "J" in flags:
vy_print("\n".join([vy_str(n) for n in vy_globals.stack]))
else:
Expand Down
6 changes: 3 additions & 3 deletions vyxal/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ def swapped_expl(l, r):
ret = {
(types[0], types[1]): (
lambda: safe_apply(fn, left, right),
lambda: expl(vyxal.array_builtins.iterable(left), right),
lambda: expl(iterable(left), right),
),
(list, types[1]): (
lambda: [safe_apply(fn, x, right) for x in left],
Expand Down Expand Up @@ -483,7 +483,7 @@ def swapped_expl(l, r):
ret = {
(types[0], types[1]): (
lambda: safe_apply(fn, left, right),
lambda: expl(vyxal.array_builtins.iterable(left), right),
lambda: expl(iterable(left), right),
),
(list, types[1]): (
lambda: [safe_apply(fn, x, right) for x in left],
Expand Down Expand Up @@ -522,7 +522,7 @@ def gen():

return gen()
elif vy_type(left) in (str, Number):
return safe_apply(fn, list(vyxal.array_builtins.iterable(left)))
return safe_apply(fn, list(iterable(left)))
else:
ret = [safe_apply(fn, x) for x in left]
return ret
Expand Down

0 comments on commit 76cfdd6

Please sign in to comment.