From f331192861d8ba02af7fd47f2e0c6d6db7515007 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Thu, 20 Jun 2024 23:26:18 +0100 Subject: add (if cond if-true if-false) builtin --- vm.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'vm.c') diff --git a/vm.c b/vm.c index 205e0fd..70aca3d 100644 --- a/vm.c +++ b/vm.c @@ -55,6 +55,7 @@ int runvm(State *S) { disasm_chunk(ch); #define RBYTE() (ch->bc.d[th->ip++]) +#define RSHORT() (th->ip += 2, (uint16_t)( ch->bc.d[th->ip-2] | ch->bc.d[th->ip-1] << 8 )) #define PUSH(v) th->stack[th->sp++] = v; #define POP() (th->stack[--th->sp]) @@ -85,6 +86,24 @@ int runvm(State *S) { --th->sp; break; + case OP_SKIP: { + uint16_t offset = RSHORT(); + th->ip += offset; + break; + } + case OP_REDO: { + uint16_t offset = RSHORT(); + th->ip -= offset; + break; + } + case OP_0BRANCH: { + uint16_t offset = RSHORT(); + bool cond = is_truthy(POP()); + if (!cond) + th->ip += offset; + break; + } + case OP_GETGLOBAL: { uint8_t cix = RBYTE(); Val varname = ch->consts.d[cix]; @@ -134,12 +153,16 @@ int runvm(State *S) { case OP_TRUE: PUSH(VAL_TRUE); break; case OP_FALSE: PUSH(VAL_FALSE); break; - } } done:; return status; } +#undef RBYTE +#undef RSHORT +#undef PUSH +#undef POP +#undef PEEK -- cgit v1.2.3