From 600eaef90f9f0507635fec4cf98f7fa1d1779bd1 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Thu, 20 Jun 2024 18:11:09 +0100 Subject: add readable globals --- com.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'com.c') diff --git a/com.c b/com.c index b97ffc0..ba45975 100644 --- a/com.c +++ b/com.c @@ -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); -- cgit v1.2.3