From e9b99a90510309ac4f5d91d4a5138e7a84904057 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Mon, 24 Jun 2024 20:20:06 +0100 Subject: add local variables and (let) form --- vm.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'vm.c') diff --git a/vm.c b/vm.c index 4e412c1..b5b66d3 100644 --- a/vm.c +++ b/vm.c @@ -111,6 +111,12 @@ int runvm(State *S) { break; } + case OP_GETLOCAL: { + uint8_t lidx = RBYTE(); + PUSH(th->stack[lidx]); + break; + } + #define BINARY_OP(opcode, OP, RET_TYPE) \ case opcode: { \ Val b = POP(); \ @@ -148,8 +154,8 @@ int runvm(State *S) { goto done; } PUSH(VAL_NUM(fmod(AS_NUM(a), AS_NUM(b)))); - } break; + } case OP_NIL: PUSH(VAL_NIL); break; @@ -173,8 +179,16 @@ int runvm(State *S) { th->ch = &func->ch; th->ip = 0; + break; + } + + case OP_ENDSCOPE: { + uint8_t nlocals = RBYTE(); + Val retval = POP(); + th->sp -= nlocals; + PUSH(retval); + break; } - break; case OP_RET: { StackFrame *sf = &th->rstack[--th->rsp]; -- cgit v1.2.3