From 2484c9b600158331bca86138c066eb6dd85ba8ce Mon Sep 17 00:00:00 2001 From: Gavin Howard Date: Tue, 27 Dec 2022 23:00:55 -0700 Subject: [PATCH] Fix the bug mentioned in the previous commit So the fix was much easier than I thought. I just had to add an extra field to BcLoc for the stack index and set it, and then when actually grabbing the element, I needed to grab the vector at the index. Adding an element to BcLoc was free, by the way. The reason is that BcLoc is only used in a union with a BcNum and a BcVec, both of which are bigger, so adding the extra field doesn't take any more space. Signed-off-by: Gavin Howard --- include/lang.h | 5 +++++ src/program.c | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/include/lang.h b/include/lang.h index 4ad6df88..1fdbe932 100644 --- a/include/lang.h +++ b/include/lang.h @@ -355,6 +355,11 @@ typedef struct BcLoc /// The index of the array element. Only used for array elements. size_t idx; + /// The index of the array in the array stack. Only used for array elements. + /// This is to prevent a bug with getting the wrong array element. See the + /// tests/bc/scripts/array.bc test. + size_t stack_idx; + } BcLoc; /// An entry for a constant. diff --git a/src/program.c b/src/program.c index 5424b715..f1468ad1 100644 --- a/src/program.c +++ b/src/program.c @@ -346,7 +346,7 @@ bc_program_num(BcProgram* p, BcResult* r) { size_t idx = r->d.loc.idx; - v = bc_vec_top(v); + v = bc_vec_item(v, r->d.loc.stack_idx); #if BC_ENABLED // If this is true, we have a reference vector, so dereference @@ -1674,6 +1674,7 @@ bc_program_pushArray(BcProgram* p, const char* restrict code, BcResult* operand; BcNum* num; BcBigDig temp; + BcVec* v; // Get the index of the array. r.d.loc.loc = bc_program_index(code, bgn); @@ -1690,9 +1691,14 @@ bc_program_pushArray(BcProgram* p, const char* restrict code, bc_program_prep(p, &operand, &num, 0); temp = bc_num_bigdig(num); + v = bc_program_vec(p, r.d.loc.loc, BC_TYPE_ARRAY); + + assert(v != NULL); + // Set the result. r.t = BC_RESULT_ARRAY_ELEM; r.d.loc.idx = (size_t) temp; + r.d.loc.stack_idx = v->len - 1; BC_SIG_LOCK;