diff options
author | ubq323 <ubq323@ubq323.website> | 2024-08-17 19:14:47 +0100 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2024-08-17 19:15:39 +0100 |
commit | 41989a2ad51b15bf512b81f2c147070ba8a28ef8 (patch) | |
tree | 2c4b1d473ba65ded89c5bd8f9fbc57c4f12a9e9a | |
parent | 7d93de8e6d778c0f5e5aa9401f1acd5f7f092800 (diff) |
each returns array of loop values
-rw-r--r-- | com.c | 13 | ||||
-rw-r--r-- | tests/each2.bth | 1 | ||||
-rw-r--r-- | tests/each2.out | 4 |
3 files changed, 17 insertions, 1 deletions
@@ -399,6 +399,9 @@ static void each_form(Compiler *C, ObjArr *a, Op _, int flags) { cpl_op(C, OP_ARRLEN); uint8_t mslot = declare_local(C, "__max__"); + cpl_op(C, OP_ARRNEW); + uint8_t oslot = declare_local(C, "__out__"); + cpl_op(C, OP_NIL); uint8_t vslot = declare_local(C, ivar); @@ -412,7 +415,10 @@ static void each_form(Compiler *C, ObjArr *a, Op _, int flags) { // getlocal idx // call 2 // setlocal ivar + // getlocal out // body ... + // arrappend + // drop // incr idx // redo -> A // B: @@ -436,7 +442,11 @@ static void each_form(Compiler *C, ObjArr *a, Op _, int flags) { cpl_byte(C, vslot); cpl_op(C, OP_DROP); + cpl_op(C, OP_GETLOCAL); + cpl_byte(C, oslot); + cpl_body(C, a, 2, flags & ~F_tail); + cpl_op(C, OP_ARRAPPEND); cpl_op(C, OP_DROP); cpl_op(C, OP_GETLOCAL); @@ -451,7 +461,8 @@ static void each_form(Compiler *C, ObjArr *a, Op _, int flags) { size_t ph_A = placeholder(C); size_t dest_B = BYTECODE(C).len; - cpl_op(C, OP_NIL); + cpl_op(C, OP_GETLOCAL); + cpl_byte(C, oslot); patch(C, ph_A, ph_A - dest_A + 2); patch(C, ph_B, dest_B - ph_B - 2); diff --git a/tests/each2.bth b/tests/each2.bth new file mode 100644 index 0000000..100db39 --- /dev/null +++ b/tests/each2.bth @@ -0,0 +1 @@ +(say (each (a [1 2 3]) (say a) a)) diff --git a/tests/each2.out b/tests/each2.out new file mode 100644 index 0000000..7777a40 --- /dev/null +++ b/tests/each2.out @@ -0,0 +1,4 @@ +1 +2 +3 +[1 2 3] |