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 <[email protected]>
This commit is contained in:
Gavin Howard 2022-12-27 23:00:55 -07:00
parent 78bc9d0c74
commit 2484c9b600
Signed by: gavin
GPG Key ID: 93D31C8CA4AB6C63
2 changed files with 12 additions and 1 deletions

View File

@ -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.

View File

@ -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;