Skip to content

Commit e8af1a2

Browse files
committed
[ops] Move internal operators; make inc/dec support mul. variables
1 parent 41b0ae9 commit e8af1a2

File tree

4 files changed

+47
-40
lines changed

4 files changed

+47
-40
lines changed

main.xpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
jmp run
1+
var @x 1
2+
var @y 2
3+
var @z 3
4+
rep 10 loop
25

3-
:woah a b c
4-
prt "Hello, world!"
5-
prt a b c
6-
ret b
7-
8-
:run
9-
prt "Hi. How are you?"
10-
jmp woah 1 2 3 ?response
11-
prt response "should be 2"
12-
rem response
13-
prt response "should be None"
6+
:loop
7+
prt @x @y @z
8+
inc @x @y @z

xpp/modules/ops/stdlib/internal.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
from copy import copy as copyobj
66
from xpp import load_sections, config
77
from xpp.modules.ops.shared import (
8-
ensure_arguments, InvalidArgument
8+
fetch_io_args, ensure_arguments,
9+
InvalidArgument
910
)
1011

1112
# Initialization
@@ -84,5 +85,19 @@ def ret(ctx) -> None:
8485
if ctx.args:
8586
section.return_value = [a.value for a in ctx.args]
8687

88+
def var(ctx) -> None:
89+
ensure_arguments("var", "var <name> <value>", ["name", "value"], ctx.args)
90+
ctx.args[0].set(ctx.args[1].value)
91+
92+
def jmp(ctx) -> None:
93+
ain, aout = fetch_io_args("jmp", "jmp <section> [args...] [?output]", ["section"], ctx.args)
94+
results = ctx.mem.interpreter.run_section(ain[0].value if isinstance(ain[0].value, str) else ain[0].raw, [a.value for a in ain[1:]])
95+
outn = len(aout)
96+
for i, r in enumerate(results):
97+
if i > outn:
98+
break
99+
100+
ctx.args[-(outn - i)].set(r)
101+
87102
def rem(ctx) -> None:
88103
[arg.delete() for arg in ctx.args]

xpp/modules/ops/stdlib/math.py

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,34 @@ def pow(ctx) -> int | float:
6666
return perform_operation(ain[0].value, ain[1:], operator.pow, aout)
6767

6868
def inc(ctx) -> int | float:
69-
ain, aout = fetch_io_args("inc", "inc <value> [?output]", ["value"], ctx.args)
70-
if not isinstance(ain[0].value, (int, float)):
71-
raise InvalidArgument("inc: value must be either an integer or a float!")
69+
ain, aout = fetch_io_args("inc", "inc <args...> [?output]", ["args"], ctx.args)
70+
if aout and len(ain) > 1:
71+
raise InvalidArgument("inc: can only process one input if output variable is specified!")
7272

73-
ain[0].set(ain[0].value + 1)
74-
[out.set(ain[0].value) for out in aout]
73+
for val in ain:
74+
if not isinstance(val.value, (int, float)):
75+
raise InvalidArgument("inc: all values must be either an integer or a float!")
76+
77+
val.set(val.value + 1)
78+
79+
if aout:
80+
[out.set(val.value) for out in aout]
81+
return val.value
7582

7683
def dec(ctx) -> int | float:
77-
ain, aout = fetch_io_args("dec", "dec <value> [?output]", ["value"], ctx.args)
78-
if not isinstance(ain[0].value, (int, float)):
79-
raise InvalidArgument("dec: value must be either an integer or a float!")
84+
ain, aout = fetch_io_args("dec", "dec <args...> [?output]", ["args"], ctx.args)
85+
if aout and len(ain) > 1:
86+
raise InvalidArgument("dec: can only process one input if output variable is specified!")
87+
88+
for val in ain:
89+
if not isinstance(val.value, (int, float)):
90+
raise InvalidArgument("dec: all values must be either an integer or a float!")
91+
92+
val.set(val.value - 1)
8093

81-
ain[0].set(ain[0].value - 1)
82-
[out.set(ain[0].value) for out in aout]
94+
if aout:
95+
[out.set(val.value) for out in aout]
96+
return val.value
8397

8498
def rnd(ctx) -> int:
8599
ain, aout = fetch_io_args("rnd", "rnd <value> [precision] [?output]", ["value"], ctx.args)

xpp/modules/ops/stdlib/stdio.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
# Modules
44
import sys
5-
from xpp.modules.ops.shared import (
6-
fetch_io_args, ensure_arguments
7-
)
85

96
# Operators class
107
class XOperators:
@@ -14,20 +11,6 @@ class XOperators:
1411
def prt(ctx) -> None:
1512
print(*[v.value for v in ctx.args])
1613

17-
def var(ctx) -> None:
18-
ensure_arguments("var", "var <name> <value>", ["name", "value"], ctx.args)
19-
ctx.args[0].set(ctx.args[1].value)
20-
21-
def jmp(ctx) -> None:
22-
ain, aout = fetch_io_args("jmp", "jmp <section> [args...] [?output]", ["section"], ctx.args)
23-
results = ctx.mem.interpreter.run_section(ain[0].value if isinstance(ain[0].value, str) else ain[0].raw, [a.value for a in ain[1:]])
24-
outn = len(aout)
25-
for i, r in enumerate(results):
26-
if i > outn:
27-
break
28-
29-
ctx.args[-(outn - i)].set(r)
30-
3114
def cls(ctx) -> None:
3215
print("\033c\033[3J", end = "")
3316

0 commit comments

Comments
 (0)