From 9a7d1b1d41f4b3bb3387e7bbe77105d0089803d0 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Thu, 20 Jun 2024 18:43:01 +0100 Subject: globals are now variable --- vm.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'vm.c') diff --git a/vm.c b/vm.c index 00ba397..9f6ece6 100644 --- a/vm.c +++ b/vm.c @@ -56,6 +56,7 @@ void runvm(State *S) { #define PUSH(v) th->stack[th->sp++] = v; #define POP() (th->stack[--th->sp]) +#define PEEK() (th->stack[th->sp-1]) puts("---"); while (1) { @@ -75,7 +76,10 @@ void runvm(State *S) { break; } case OP_PRINT: - println_val(POP()); + println_val(PEEK()); + break; + case OP_DROP: + --th->sp; break; case OP_GETGLOBAL: { @@ -90,6 +94,19 @@ void runvm(State *S) { PUSH(v); break; } + case OP_SETGLOBAL: { + 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 = PEEK(); + + ht_put(S, &S->globals, AS_STRING(varname), v); + break; + } #define ARITH_OP(opcode, OP) \ case opcode: { \ -- cgit v1.2.3