diff options
author | ubq323 <ubq323@ubq323.website> | 2024-06-22 18:22:24 +0100 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2024-06-22 18:22:24 +0100 |
commit | 8c3037662ba572a6935170dbd4cb8cc8a3636417 (patch) | |
tree | 1d585a5f0eab8eed96d0741421656f11c99c7c33 /vm.c | |
parent | 1d496f5f7f01b20fb34e63d1c85a33d8decd1894 (diff) |
compilation of functions, and some parts of interpreting them
Diffstat (limited to 'vm.c')
-rw-r--r-- | vm.c | 41 |
1 files changed, 37 insertions, 4 deletions
@@ -41,12 +41,13 @@ int runvm(State *S) { #define PEEK() (th->stack[th->sp-1]) while (1) { + ch = th->ch; + if (S->do_trace) { + printf("%p ",(void *)ch); + disasm_instr(ch, th->ip); + } uint8_t instr = RBYTE(); switch (instr) { - case OP_RET: - status = 0; - goto done; - break; case OP_LOADK: { uint8_t cix = RBYTE(); Val v = ch->consts.d[cix]; @@ -155,7 +156,39 @@ int runvm(State *S) { case OP_TRUE: PUSH(VAL_TRUE); break; case OP_FALSE: PUSH(VAL_FALSE); break; + case OP_CALL: { + uint8_t len = RBYTE(); + // ignore arguments for now + th->sp -= len-1; + Val callee = POP(); + if (!IS_FUNC(callee)) { + fprintf(stderr, "can only call functions\n"); + goto done; + } + ObjFunc *func = AS_FUNC(callee); + + StackFrame *sf = &th->rstack[th->rsp++]; + sf->ch = th->ch; + sf->ip = th->ip; + + th->ch = &func->ch; + th->ip = 0; } + break; + + case OP_RET: { + StackFrame *sf = &th->rstack[--th->rsp]; + th->ch = sf->ch; + th->ip = sf->ip; + break; + } + case OP_HALT: + status = 0; + goto done; + break; + + } + } done:; return status; |