summaryrefslogtreecommitdiff
path: root/vm.c
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2024-06-20 16:00:34 +0100
committerubq323 <ubq323@ubq323.website>2024-06-20 16:00:34 +0100
commit60b3369ab24f9bd2a4a6d638ab1b3013ebc29814 (patch)
tree3ff8ec7e95ef314266673b1b3d7bedb880a4a5b0 /vm.c
parent9ee73a8459eb2bb58adc29da02de312b7e4e7dca (diff)
pass State *S everywhere
contains changes from a million years ago that i don't remember much about
Diffstat (limited to 'vm.c')
-rw-r--r--vm.c65
1 files changed, 29 insertions, 36 deletions
diff --git a/vm.c b/vm.c
index acbfaa3..eb041a0 100644
--- a/vm.c
+++ b/vm.c
@@ -10,59 +10,52 @@
#include "dis.h"
-Chunk chunk_new() {
- return (Chunk){
- .blen = 0, .bcap = 0, .b = NULL,
- .clen = 0, .ccap = 0, .c = NULL,
- };
+Chunk chunk_new(State *S) {
+ return (Chunk){ 0 };
}
-
-size_t chunk_wbc(Chunk *ch, uint8_t byte) {
- if (ch->blen == ch->bcap) {
- size_t newsz = (ch->bcap == 0 ? 8 : ch->bcap * 2);
- ch->b = RENEW_ARR(ch->b, uint8_t, ch->bcap, newsz);
- ch->bcap = newsz;
+size_t chunk_wbc(State *S, Chunk *ch, uint8_t byte) {
+ if (ch->bc.len == ch->bc.cap) {
+ size_t newsz = (ch->bc.cap == 0 ? 8 : ch->bc.cap * 2);
+ ch->bc.d = RENEW_ARR(S, ch->bc.d, uint8_t, ch->bc.cap, newsz);
+ ch->bc.cap = newsz;
}
- size_t ix = ch->blen;
- ch->b[ix] = byte;
- ch->blen ++;
+ size_t ix = ch->bc.len;
+ ch->bc.d[ix] = byte;
+ ch->bc.len ++;
return ix;
}
-size_t chunk_wconst(Chunk *ch, Val v) {
- if (ch->clen == ch->ccap) {
- size_t newsz = (ch->ccap == 0 ? 8 : ch->ccap *2);
- ch->c = RENEW_ARR(ch->c, Val, ch->ccap, newsz);
- ch->ccap = newsz;
+size_t chunk_wconst(State *S, Chunk *ch, Val v) {
+ 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->clen;
- ch->c[ix] = v;
- ch->clen ++;
+ size_t ix = ch->consts.len;
+ ch->consts.d[ix] = v;
+ ch->consts.len ++;
return ix;
}
-Vm vm_new(Chunk *ch) {
- Vm vm = (Vm){
- .ch = ch,
- .ip = 0,
- .sp = 0
- };
+Thread thread_new(State *S) {
+ Thread th = (Thread){ 0 };
for (int i = 0; i < STACKSIZE; i++) {
- vm.stack[i] = VAL_NIL;
+ th.stack[i] = VAL_NIL;
}
- return vm;
+ return th;
}
-void runvm(Chunk *ch) {
+void runvm(State *S) {
+ Thread *th = S->th;
+ Chunk *ch = th->ch;
disasm_chunk(ch);
- Vm vm = vm_new(ch);
-#define RBYTE() (vm.ch->b[vm.ip++])
+#define RBYTE() (ch->bc.d[th->ip++])
-#define PUSH(v) vm.stack[vm.sp++] = v;
-#define POP() (vm.stack[--vm.sp])
+#define PUSH(v) th->stack[th->sp++] = v;
+#define POP() (th->stack[--th->sp])
puts("---");
while (1) {
@@ -74,7 +67,7 @@ void runvm(Chunk *ch) {
break;
case OP_LOADK:;
uint8_t cix = RBYTE();
- Val v = vm.ch->c[cix];
+ Val v = ch->consts.d[cix];
PUSH(v);
// printf(" (pushing ");
// print_val(v);