From 8c3037662ba572a6935170dbd4cb8cc8a3636417 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sat, 22 Jun 2024 18:22:24 +0100 Subject: compilation of functions, and some parts of interpreting them --- vm.c | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) (limited to 'vm.c') diff --git a/vm.c b/vm.c index 3410e1c..4e412c1 100644 --- a/vm.c +++ b/vm.c @@ -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; -- cgit v1.2.3