From 0cc8e1fd89aacd54926b61ac3b9c4a2810024b4a Mon Sep 17 00:00:00 2001 From: ubq323 Date: Tue, 6 Aug 2024 18:17:55 +0100 Subject: implement arrlit and quote forms --- com.c | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) (limited to 'com.c') diff --git a/com.c b/com.c index 000ad63..3b2260d 100644 --- a/com.c +++ b/com.c @@ -535,6 +535,23 @@ static void defn_form(Compiler *C, ObjArr *a, Op _, int flags) { cpl_op(C, OP_NIL); } +static void quote_form(Compiler *C, ObjArr *a, Op _, int flags) { + CHECK(IS_STRING(a->d[1]), "can only quote strings for now"); + cpl_constop(C, OP_LOADK, a->d[1]); +} + +static void arrlit_form(Compiler *C, ObjArr *a, Op _, int flags) { + CHECK(IS_ARR(a->d[1]), "can only arrlit a list"); + ObjArr *exprs = AS_ARR(a->d[1]); + + cpl_op(C, OP_ARRNEW); + for (int i = 0; i < exprs->len; i++) { + cpl_expr(C, exprs->d[i], 0); + cpl_op(C, OP_ARRAPPEND); + } +} + + typedef void (*form_compiler)(Compiler *C, ObjArr *a, Op op, int flags); typedef struct { char *name; @@ -545,16 +562,18 @@ typedef struct { } BuiltinForm; static BuiltinForm builtin_forms[] = { - { "set!", 2, false, set_form, 0 }, - { "do", 1, true, do_form, 0 }, - { "if", 3, false, if_form, 0 }, - { "while", 2, true, while_form, 0 }, - { "for", 2, true, for_form, 0 }, - { "each", 2, true, each_form, 0 }, - { "fn", 2, true, fn_form, 0 }, - { "let", 2, true, let_form, 0 }, - { "def", 2, false, def_form, 0 }, - { "defn", 2, true, defn_form, 0 }, + { "set!", 2, false, set_form, 0 }, + { "do", 1, true, do_form, 0 }, + { "if", 3, false, if_form, 0 }, + { "while", 2, true, while_form, 0 }, + { "for", 2, true, for_form, 0 }, + { "each", 2, true, each_form, 0 }, + { "fn", 2, true, fn_form, 0 }, + { "let", 2, true, let_form, 0 }, + { "def", 2, false, def_form, 0 }, + { "defn", 2, true, defn_form, 0 }, + { "quote", 1, false, quote_form, 0 }, + { "arrlit", 1, false, arrlit_form, 0 }, #define ARITH_OP(str, op) \ { str, 2, false, arith_form, op }, ARITH_OP("+", OP_ADD) -- cgit v1.2.3