From 600eaef90f9f0507635fec4cf98f7fa1d1779bd1 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Thu, 20 Jun 2024 18:11:09 +0100 Subject: add readable globals --- vm.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'vm.c') diff --git a/vm.c b/vm.c index eb041a0..00ba397 100644 --- a/vm.c +++ b/vm.c @@ -65,7 +65,7 @@ void runvm(State *S) { printf("done!\n"); goto done; break; - case OP_LOADK:; + case OP_LOADK: { uint8_t cix = RBYTE(); Val v = ch->consts.d[cix]; PUSH(v); @@ -73,10 +73,24 @@ void runvm(State *S) { // print_val(v); // printf(")\n"); break; + } case OP_PRINT: println_val(POP()); break; + case OP_GETGLOBAL: { + uint8_t cix = RBYTE(); + Val varname = ch->consts.d[cix]; + if (!IS_STRING(varname)) { + printf("global names must be string, not %s\n", + typename_str(varname)); + goto done; + } + Val v = ht_get(S, &S->globals, AS_STRING(varname)); + PUSH(v); + break; + } + #define ARITH_OP(opcode, OP) \ case opcode: { \ Val b = POP(); \ -- cgit v1.2.3