Add lambda special form

This commit is contained in:
2022-11-04 21:51:25 -04:00
parent c37e46e354
commit 2344a7d498
3 changed files with 16 additions and 2 deletions

View File

@@ -16,6 +16,7 @@ int main(int argc, const char **argv) {
ucl_state_put(state, "let", ucl_special_create(ucl_special_let)); ucl_state_put(state, "let", ucl_special_create(ucl_special_let));
ucl_state_put(state, "if", ucl_special_create(ucl_special_if)); ucl_state_put(state, "if", ucl_special_create(ucl_special_if));
ucl_state_put(state, "defun", ucl_special_create(ucl_special_defun)); ucl_state_put(state, "defun", ucl_special_create(ucl_special_defun));
ucl_state_put(state, "lambda", ucl_special_create(ucl_special_lambda));
ucl_state_put(state, "setq", ucl_special_create(ucl_special_setq)); ucl_state_put(state, "setq", ucl_special_create(ucl_special_setq));
ucl_state_put(state, "progn", ucl_special_create(ucl_special_progn)); ucl_state_put(state, "progn", ucl_special_create(ucl_special_progn));
@@ -23,7 +24,6 @@ int main(int argc, const char **argv) {
// - progn // - progn
// - quote // - quote
// - iteration // - iteration
// - lambda
ucl_state_put(state, "print", ucl_builtin_create(ucl_builtin_print)); ucl_state_put(state, "print", ucl_builtin_create(ucl_builtin_print));
ucl_state_put(state, "list", ucl_builtin_create(ucl_builtin_list)); ucl_state_put(state, "list", ucl_builtin_create(ucl_builtin_list));
@@ -55,6 +55,7 @@ int main(int argc, const char **argv) {
// - car // - car
// - cdr // - cdr
// - nth // - nth
// - booleans (e.g. not)
if (argc < 2) { if (argc < 2) {
while (1) { while (1) {

View File

@@ -71,12 +71,24 @@ struct ucl_object *ucl_special_defun(struct ucl_state *state, struct ucl_object
} }
struct ucl_object *ucl_special_lambda(struct ucl_state *state, struct ucl_object *args) {
// TODO: Check arguments
struct ucl_object *fun_args = ucl_list_nth(args, 0);
if (fun_args->type != UCL_TYPE_CELL) {
// TODO: Check that the list contains only symbols
return ucl_error_create("First argument to lambda must be a list of symbols");
}
return args;
}
struct ucl_object *ucl_special_setq(struct ucl_state *state, struct ucl_object *args) { struct ucl_object *ucl_special_setq(struct ucl_state *state, struct ucl_object *args) {
// TODO: Check arguments // TODO: Check arguments
struct ucl_object *sym = ucl_car(args); struct ucl_object *sym = ucl_car(args);
struct ucl_state *root_state = ucl_state_get_root(state); struct ucl_state *root_state = ucl_state_get_root(state);
if (sym->type != UCL_TYPE_SYMBOL) { if (sym->type != UCL_TYPE_SYMBOL) {
return ucl_error_create("First argument to defun must be a symbol"); return ucl_error_create("First argument to setq must be a symbol");
} }
struct ucl_object *value = ucl_evaluate(state, ucl_list_nth(args, 1)); struct ucl_object *value = ucl_evaluate(state, ucl_list_nth(args, 1));

View File

@@ -6,6 +6,7 @@
struct ucl_object *ucl_special_let(struct ucl_state *state, struct ucl_object *args); struct ucl_object *ucl_special_let(struct ucl_state *state, struct ucl_object *args);
struct ucl_object *ucl_special_if(struct ucl_state *state, struct ucl_object *args); struct ucl_object *ucl_special_if(struct ucl_state *state, struct ucl_object *args);
struct ucl_object *ucl_special_defun(struct ucl_state *state, struct ucl_object *args); struct ucl_object *ucl_special_defun(struct ucl_state *state, struct ucl_object *args);
struct ucl_object *ucl_special_lambda(struct ucl_state *state, struct ucl_object *args);
struct ucl_object *ucl_special_setq(struct ucl_state *state, struct ucl_object *args); struct ucl_object *ucl_special_setq(struct ucl_state *state, struct ucl_object *args);
struct ucl_object *ucl_special_progn(struct ucl_state *state, struct ucl_object *args); struct ucl_object *ucl_special_progn(struct ucl_state *state, struct ucl_object *args);