From 3ed2c60da6ef2e18d7e273cf39056833c5b41c13 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Wed, 13 Oct 2021 20:16:31 +0200 Subject: Add the define `def` procedure --- src/bootstrap/primitives.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src/bootstrap/primitives.c') diff --git a/src/bootstrap/primitives.c b/src/bootstrap/primitives.c index 39229ff..af8e9da 100644 --- a/src/bootstrap/primitives.c +++ b/src/bootstrap/primitives.c @@ -601,6 +601,38 @@ proc_equal(Environment *env, Object *obj) { return obj_eq(a, b) ? obj_true : obj_false; } +// +// Variables and declarations. +// + +Object * +proc_define(Environment *env, Object *obj) { + if (obj == obj_nil || obj->cdr == obj_nil) { + error_push((Error){ + .type = ERR_TYPE_RUNTIME, + .value = ERR_NOT_ENOUGH_ARGS, + }); + return obj_err; + } + + Object *symbol = obj->car; + if (symbol->type != OBJ_TYPE_SYMBOL) { + error_push((Error){ + .type = ERR_TYPE_RUNTIME, + .value = ERR_WRONG_ARG_TYPE, + }); + return obj_err; + } + + Object *value = eval(env, obj->cdr->car); + if (value == obj_err) { + return obj_err; + } + + // Make a copy of the symbol and to make them permanent in the environment. + env_update_symbol(env, obj_duplicate(symbol), obj_duplicate(value)); + return obj_nil; +} // TODO: fixnum left/right shift, mask, invert // TODO: add primitives for type transforms: string->symbol, symbol->string, etc -- cgit v1.2.1