#include #include #include "uclisp.h" #include "common.h" #include "scope.h" #include struct ucl_object *ucl_evaluate_builtin_form(struct ucl *state, struct ucl_scope *scope, struct ucl_object *list) { // TODO: Reasonably split builtin and non-builtin evaluation struct ucl_object *evaluated_list = ucl_nil_create(state); struct ucl_object *evaluated_list_tail = evaluated_list; FOREACH_LIST(list, iter, item) { struct ucl_object *obj = ucl_evaluate_in_scope(state, scope, item); UCL_RET_IF_ERROR(obj); evaluated_list_tail = ucl_list_append(state, evaluated_list_tail, obj); }; struct ucl_object *fun = ucl_car(state, evaluated_list); struct ucl_object *result = NULL; if (fun->type == UCL_TYPE_BUILTIN) { struct ucl_object *args = ucl_cdr(state, evaluated_list); result = fun->builtin(state, scope, args); } else if (fun->type == UCL_TYPE_CELL) { struct ucl_scope *fun_scope = ucl_scope_create_child(state, scope); struct ucl_object *fun_arg_syms = ucl_car(state, fun); struct ucl_object *fun_forms = ucl_cdr(state, fun); int i = 0; FOREACH_LIST(fun_arg_syms, iter, sym) { ucl_scope_put(state, fun_scope, sym->symbol, ucl_list_nth(evaluated_list, i + 1)); i++; } result = ucl_progn(state, fun_scope, fun_forms); ucl_scope_delete(state, fun_scope); } else { assert(0); } return (result == NULL) ? ucl_nil_create(state) : result; } struct ucl_object *ucl_evaluate_special_form(struct ucl* state, struct ucl_scope *scope, struct ucl_object *list) { const char *fun_sym = ucl_car(state, list)->symbol; struct ucl_object *fun = ucl_scope_get(state, scope, fun_sym); struct ucl_object *args = ucl_cdr(state, list); struct ucl_object *result = NULL; result = fun->special(state, scope, args); return result; } struct ucl_object *ucl_evaluate_list(struct ucl* state, struct ucl_scope *scope, struct ucl_object *list) { if (list->cell.car == NULL) { return list; } struct ucl_object *fun = ucl_evaluate_in_scope(state, scope, ucl_car(state, list)); UCL_RET_IF_ERROR(fun); if (fun->type == UCL_TYPE_SPECIAL) { return ucl_evaluate_special_form(state, scope, list); } else if (fun->type == UCL_TYPE_BUILTIN || fun->type == UCL_TYPE_CELL) { return ucl_evaluate_builtin_form(state, scope, list); } else { assert(0); UCL_COND_OR_RET_ERROR(false, "Unreachable error in ucl_evaluate_list"); } } struct ucl_object *ucl_evaluate_in_scope(struct ucl *state, struct ucl_scope *scope, struct ucl_object *obj) { assert(obj != NULL); switch (obj->type) { case UCL_TYPE_CELL: return ucl_evaluate_list(state, scope, obj); case UCL_TYPE_SYMBOL: return ucl_scope_get(state, scope, obj->symbol); case UCL_TYPE_INT: case UCL_TYPE_STRING: case UCL_TYPE_ERROR: return obj; case UCL_TYPE_BUILTIN: case UCL_TYPE_SPECIAL: case UCL_TYPE_COUNT: assert(0); return NULL; } assert(0); return ucl_error_create(state, "Unreachable error in ucl_evaluate"); } struct ucl_object *ucl_evaluate(struct ucl *state, struct ucl_object *obj) { return ucl_evaluate_in_scope(state, state->global_scope, obj); } struct ucl_object *ucl_evaluate_progn(struct ucl *state, struct ucl_object *forms) { return ucl_progn(state, state->global_scope, forms); }