From fa8891ffe017ae890f0ef07915cf8b52acd7304a Mon Sep 17 00:00:00 2001 From: ubq323 Date: Thu, 20 Jun 2024 21:45:57 +0100 Subject: add true, false, nil keywords --- com.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'com.c') diff --git a/com.c b/com.c index 4252ecd..3d891f7 100644 --- a/com.c +++ b/com.c @@ -10,10 +10,18 @@ static void compile_node(State *S, Chunk *ch, AstNode a) { switch (a.ty) { 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))); + char *ident = a.as.str; + + if (0 == strcmp(ident, "true")) chunk_wbc(S, ch, OP_TRUE); + else if (0 == strcmp(ident, "false")) chunk_wbc(S, ch, OP_FALSE); + else if (0 == strcmp(ident, "nil")) chunk_wbc(S, ch, OP_NIL); + else { + // global read + 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); @@ -104,6 +112,6 @@ int main() { chunk_wbc(S, &ch, OP_PRINT); chunk_wbc(S, &ch, OP_RET); - runvm(S); + return runvm(S); } -- cgit v1.2.3