diff options
-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) |