diff options
author | ubq323 <ubq323@ubq323.website> | 2024-06-20 18:11:09 +0100 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2024-06-20 18:11:09 +0100 |
commit | 600eaef90f9f0507635fec4cf98f7fa1d1779bd1 (patch) | |
tree | d057e694a4b9a6a0109523f61ad788a125a7f479 | |
parent | b8d0ee2e105727021f9466790ec07ecbfee8dff6 (diff) |
add readable globals
-rw-r--r-- | com.c | 19 | ||||
-rw-r--r-- | dis.c | 11 | ||||
-rw-r--r-- | state.h | 1 | ||||
-rw-r--r-- | vm.c | 16 | ||||
-rw-r--r-- | vm.h | 2 |
5 files changed, 44 insertions, 5 deletions
@@ -9,9 +9,11 @@ static void compile_node(State *S, Chunk *ch, AstNode a) { switch (a.ty) { - case AST_IDENT: - printf("can't compile ident\n"); - exit(1); + case AST_IDENT:; + size_t len = strlen(a.as.str); + ObjString *o = objstring_copy(S, a.as.str, len); + chunk_wbc(S, ch, OP_GETGLOBAL); + chunk_wbc(S, ch, chunk_wconst(S, ch, VAL_OBJ(o))); break; case AST_NUM: chunk_wbc(S, ch, OP_LOADK); @@ -67,6 +69,17 @@ int main() { Chunk ch = chunk_new(S); th.ch = &ch; + char n1[] = "foo"; + char n2[] = "bar"; + char n3[] = "baz"; + ObjString *o1 = objstring_copy(S, n1, 3); + ObjString *o2 = objstring_copy(S, n2, 3); + ObjString *o3 = objstring_copy(S, n3, 3); + + ht_put(S, &st.globals, o1, VAL_NUM(69)); + ht_put(S, &st.globals, o2, VAL_NUM(2)); + ht_put(S, &st.globals, o3, VAL_NUM(3)); + AstNode an = read(); compile_node(S, &ch, an); @@ -14,13 +14,22 @@ void disasm_chunk(Chunk *ch) { case opcode: puts(str); break; switch (instr) { SIMPLE_INSTR(OP_RET, "ret") - case OP_LOADK:; + case OP_LOADK: { uint8_t ix = ch->bc.d[ip++]; printf("loadk #%d\t; ",ix); Val k = ch->consts.d[ix]; printf("%-4s : ",typename_str(k)); println_val(k); break; + } + case OP_GETGLOBAL: { + uint8_t ix = ch->bc.d[ip++]; + printf("getglobal #%d\t; ",ix); + Val k = ch->consts.d[ix]; + printf("%-4s : ",typename_str(k)); + println_val(k); + break; + } SIMPLE_INSTR(OP_PRINT, "print") SIMPLE_INSTR(OP_ADD, "add") SIMPLE_INSTR(OP_SUB, "sub") @@ -9,6 +9,7 @@ typedef struct _state State; struct _state { Thread *th; Ht strings; + Ht globals; }; State state_new(); @@ -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(); \ @@ -44,6 +44,8 @@ typedef enum { OP_SUB, OP_MUL, OP_DIV, + + OP_GETGLOBAL, } Op; void runvm(State *S); |