Refactor to encapsulate all state in a 'ucl'

This commit is contained in:
2022-11-22 15:50:46 -05:00
parent 13062a5b86
commit 5320c70dea
23 changed files with 641 additions and 663 deletions

View File

@@ -7,78 +7,78 @@
#include <string.h>
struct ucl_object *ucl_evaluate_builtin_form(struct ucl_scope *scope, struct ucl_object *list) {
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();
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(scope, item);
struct ucl_object *obj = ucl_evaluate_in_scope(state, scope, item);
UCL_RET_IF_ERROR(obj);
evaluated_list_tail = ucl_list_append(evaluated_list_tail, obj);
evaluated_list_tail = ucl_list_append(state, evaluated_list_tail, obj);
};
struct ucl_object *fun = ucl_car(evaluated_list);
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(evaluated_list);
result = fun->builtin(scope, args);
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(scope);
struct ucl_object *fun_arg_syms = ucl_car(fun);
struct ucl_object *fun_forms = ucl_cdr(fun);
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(fun_scope, sym->symbol, ucl_list_nth(evaluated_list, i + 1));
ucl_scope_put(state, fun_scope, sym->symbol, ucl_list_nth(state, evaluated_list, i + 1));
i++;
}
result = ucl_progn(fun_scope, fun_forms);
ucl_scope_delete(fun_scope);
result = ucl_progn(state, fun_scope, fun_forms);
ucl_scope_delete(state, fun_scope);
} else {
assert(0);
}
return (result == NULL) ? ucl_nil_create() : result;
return (result == NULL) ? ucl_nil_create(state) : result;
}
struct ucl_object *ucl_evaluate_special_form(struct ucl_scope *scope, struct ucl_object *list) {
const char *fun_sym = ucl_car(list)->symbol;
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(scope, fun_sym);
struct ucl_object *args = ucl_cdr(list);
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(scope, args);
result = fun->special(state, scope, args);
return result;
}
struct ucl_object *ucl_evaluate_list(struct ucl_scope *scope, struct ucl_object *list) {
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(scope, ucl_car(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(scope, list);
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(scope, list);
return ucl_evaluate_builtin_form(state, scope, list);
} else {
assert(0);
}
}
struct ucl_object *ucl_evaluate(struct ucl_scope *scope, struct ucl_object *obj) {
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(scope, obj);
return ucl_evaluate_list(state, scope, obj);
case UCL_TYPE_SYMBOL:
return ucl_scope_get(scope, obj->symbol);
return ucl_scope_get(state, scope, obj->symbol);
case UCL_TYPE_INT:
case UCL_TYPE_STRING:
case UCL_TYPE_ERROR:
@@ -91,5 +91,9 @@ struct ucl_object *ucl_evaluate(struct ucl_scope *scope, struct ucl_object *obj)
}
assert(0);
return ucl_error_create("Unreachable error in ucl_evaluate");
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);
}