summaryrefslogtreecommitdiff
path: root/vm.c
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2024-06-21 01:05:00 +0100
committerubq323 <ubq323@ubq323.website>2024-06-21 01:05:00 +0100
commit5298940fc7798455d701d075b910e0545d3f6048 (patch)
treeebdded23f23468d164dd3c073c27185360b3f66c /vm.c
parenta03973653262fbbfed7ce42dfa39646d16bdc98f (diff)
proper equality for values; deduplicate constants in compilation
Diffstat (limited to 'vm.c')
-rw-r--r--vm.c11
1 files changed, 10 insertions, 1 deletions
diff --git a/vm.c b/vm.c
index e969692..1dc40e0 100644
--- a/vm.c
+++ b/vm.c
@@ -27,11 +27,15 @@ size_t chunk_wbc(State *S, Chunk *ch, uint8_t byte) {
return ix;
}
size_t chunk_wconst(State *S, Chunk *ch, Val v) {
+ for (int i = 0; i < ch->consts.len; i ++)
+ if (val_equal(v, ch->consts.d[i])) return i;
+
if (ch->consts.len == ch->consts.cap) {
size_t newsz = (ch->consts.cap == 0 ? 8 : ch->consts.cap *2);
ch->consts.d = RENEW_ARR(S, ch->consts.d, Val, ch->consts.cap, newsz);
ch->consts.cap = newsz;
}
+
size_t ix = ch->consts.len;
ch->consts.d[ix] = v;
ch->consts.len ++;
@@ -149,11 +153,16 @@ int runvm(State *S) {
ARITH_OP(OP_SUB, -)
ARITH_OP(OP_MUL, *)
ARITH_OP(OP_DIV, /)
- BOOL_OP(OP_EQU, ==)
BOOL_OP(OP_CMP, <)
#undef BINARY_OP
#undef ARITH_OP
#undef BOOL_OP
+ case OP_EQU: {
+ Val b = POP();
+ Val a = POP();
+ PUSH(VAL_BOOL(val_equal(a,b)));
+ break;
+ }
case OP_MOD: {
Val b = POP();
Val a = POP();