diff options
author | ubq323 <ubq323@ubq323.website> | 2024-08-06 18:17:55 +0100 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2024-08-06 18:17:55 +0100 |
commit | 0cc8e1fd89aacd54926b61ac3b9c4a2810024b4a (patch) | |
tree | bd0b3b42bd317a3b6fa3c5a37cef2f51af40798f /com.c | |
parent | 5b522325bdc0af283ca6d3ec7d71908858d91b33 (diff) |
implement arrlit and quote forms
Diffstat (limited to 'com.c')
-rw-r--r-- | com.c | 39 |
1 files changed, 29 insertions, 10 deletions
@@ -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) |