From b8d0ee2e105727021f9466790ec07ecbfee8dff6 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Thu, 20 Jun 2024 17:23:01 +0100 Subject: string interning and print statement --- com.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'com.c') diff --git a/com.c b/com.c index 64aee2e..b97ffc0 100644 --- a/com.c +++ b/com.c @@ -27,9 +27,15 @@ static void compile_node(State *S, Chunk *ch, AstNode a) { case AST_LIST: { AstVec l = a.as.list; #define CK(cond, msg) if (!(cond)) { puts(msg); exit(1); } - CK(l.len == 3, "can only compile binary ops"); - CK(l.vals[0].ty == AST_IDENT, "can only call ops"); - #undef CK + CK(l.vals[0].ty == AST_IDENT, "can only call ops"); + + if (0 == strcmp(l.vals[0].as.str, "print")) { + CK(l.len == 2, "print requires exactly 1 argument"); + compile_node(S, ch, l.vals[1]); + chunk_wbc(S, ch, OP_PRINT); + break; + } + CK(l.len == 3, "can only compile binary ops"); char opchar = l.vals[0].as.str[0]; Op op; switch (opchar) { @@ -47,6 +53,7 @@ static void compile_node(State *S, Chunk *ch, AstNode a) { compile_node(S, ch, l.vals[1]); compile_node(S, ch, l.vals[2]); chunk_wbc(S, ch, op); + #undef CK } } } -- cgit v1.2.3