Implement FOREACH (#658)

This acts like `REPT` with a variable automatically
incremented across a range of values

Fixes #432
This commit is contained in:
Rangi
2020-12-29 15:30:42 -05:00
committed by GitHub
parent 3690546795
commit 6874f694e5
8 changed files with 239 additions and 14 deletions

48
test/asm/foreach.asm Normal file
View File

@@ -0,0 +1,48 @@
foreach n, 10
printt "{d:n} "
endr
printt "-> {d:n}\n"
foreach v, 0
printt "unreached"
endr
foreach v, 2, 1
printt "unreached"
endr
foreach v, 1, 2, 0
printt "unreached"
endr
foreach x, 1, 5+1
printt "{d:x} "
endr
printt "-> {d:x}\n"
foreach v, 10, -1, -1
printt "{d:v} "
v = 42
endr
printt "-> {d:v}\n"
foreach q, 5, 21, 5
printt "{d:q} "
purge q
endr
printt "-> {d:q}\n"
s EQUS "x"
foreach s, 3, 30, 3
printt "{d:x} "
endr
printt "-> {d:x}\n"
foreach v, 10
printt "{d:v}\n"
if v == 3
purge v
v equ 42 ; causes a fatal error
endc
endr
printt "-> {d:v}\n"

6
test/asm/foreach.err Normal file
View File

@@ -0,0 +1,6 @@
ERROR: foreach.asm(16):
FOREACH cannot have a step value of 0
ERROR: foreach.asm(41) -> foreach.asm::REPT~5(47):
'v' already defined as constant at foreach.asm(41) -> foreach.asm::REPT~4(45)
FATAL: foreach.asm(41) -> foreach.asm::REPT~5(47):
Failed to update FOREACH symbol value

9
test/asm/foreach.out Normal file
View File

@@ -0,0 +1,9 @@
0 1 2 3 4 5 6 7 8 9 -> 10
1 2 3 4 5 -> 6
10 9 8 7 6 5 4 3 2 1 0 -> -1
5 10 15 20 -> 25
3 6 9 12 15 18 21 24 27 -> 30
0
1
2
3