Skip to content

Commit ba9c879

Browse files
committed
[ops] Implement repeat function
1 parent e8af1a2 commit ba9c879

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

main.xpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
var @x 1
2-
var @y 2
3-
var @z 3
4-
rep 10 loop
1+
var y 1
2+
rep 5 loop y ?y
3+
prt y
54

6-
:loop
7-
prt @x @y @z
8-
inc @x @y @z
5+
:loop x
6+
ret (add x 1)

xpp/modules/ops/stdlib/internal.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,18 @@ def jmp(ctx) -> None:
101101

102102
def rem(ctx) -> None:
103103
[arg.delete() for arg in ctx.args]
104+
105+
def rep(ctx) -> None:
106+
ain, aout = fetch_io_args("rep", "rep <amount> <section> [args...] [?output]", ["amount", "section"], ctx.args)
107+
if not isinstance(ain[0].value, int):
108+
raise InvalidArgument("rep: amount must be an integer!")
109+
110+
for _ in range(ain[0].value):
111+
[a.refresh() for a in ain[2:]]
112+
results = ctx.mem.interpreter.run_section(ain[1].value if isinstance(ain[1].value, str) else ain[1].raw, [a.value for a in ain[2:]])
113+
outn = len(aout)
114+
for i, r in enumerate(results):
115+
if i > outn:
116+
break
117+
118+
aout[-i].set(r)

0 commit comments

Comments
 (0)