diff options
author | ubq323 <ubq323@ubq323.website> | 2024-06-20 23:26:18 +0100 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2024-06-20 23:26:18 +0100 |
commit | f331192861d8ba02af7fd47f2e0c6d6db7515007 (patch) | |
tree | 9752cdea672d64f3ebfd3ee142185468510708fd /vm.c | |
parent | fa8891ffe017ae890f0ef07915cf8b52acd7304a (diff) |
add (if cond if-true if-false) builtin
Diffstat (limited to 'vm.c')
-rw-r--r-- | vm.c | 25 |
1 files changed, 24 insertions, 1 deletions
@@ -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 |