94 lines
2.8 KiB
C
94 lines
2.8 KiB
C
#include <assert.h>
|
|
#include <stddef.h>
|
|
|
|
#include "uclisp.h"
|
|
#include "internal.h"
|
|
#include "utility.h"
|
|
#include "scope.h"
|
|
|
|
// TODO: remove string.h
|
|
#include <string.h>
|
|
|
|
struct ucl_object *ucl_evaluate_builtin_form(struct ucl_scope *scope, struct ucl_object *list) {
|
|
// TODO: Reasonably split builtin and non-builtin evaluation
|
|
struct ucl_object *evaluated_list = ucl_nil_create();
|
|
|
|
FOREACH_LIST(list, iter, item) {
|
|
struct ucl_object *obj = ucl_evaluate(scope, item);
|
|
UCL_RET_IF_ERROR(obj);
|
|
ucl_list_append(evaluated_list, obj);
|
|
};
|
|
|
|
struct ucl_object *fun = ucl_car(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);
|
|
} 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);
|
|
int i = 0;
|
|
FOREACH_LIST(fun_arg_syms, iter, sym) {
|
|
ucl_scope_put(fun_scope, sym->symbol, ucl_list_nth(evaluated_list, i + 1));
|
|
i++;
|
|
}
|
|
result = ucl_progn(fun_scope, fun_forms);
|
|
ucl_scope_delete(fun_scope);
|
|
} else {
|
|
assert(0);
|
|
}
|
|
|
|
return (result == NULL) ? ucl_nil_create() : 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 *fun = ucl_scope_get(scope, fun_sym);
|
|
struct ucl_object *args = ucl_cdr(list);
|
|
struct ucl_object *result = NULL;
|
|
|
|
result = fun->special(scope, args);
|
|
|
|
return result;
|
|
}
|
|
|
|
struct ucl_object *ucl_evaluate_list(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));
|
|
UCL_RET_IF_ERROR(fun);
|
|
|
|
if (fun->type == UCL_TYPE_SPECIAL) {
|
|
return ucl_evaluate_special_form(scope, list);
|
|
} else if (fun->type == UCL_TYPE_BUILTIN || fun->type == UCL_TYPE_CELL) {
|
|
return ucl_evaluate_builtin_form(scope, list);
|
|
} else {
|
|
assert(0);
|
|
}
|
|
}
|
|
|
|
struct ucl_object *ucl_evaluate(struct ucl_scope *scope, struct ucl_object *obj) {
|
|
assert(obj != NULL);
|
|
|
|
switch (obj->type) {
|
|
case UCL_TYPE_CELL:
|
|
return ucl_evaluate_list(scope, obj);
|
|
case UCL_TYPE_SYMBOL:
|
|
return ucl_scope_get(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;
|
|
}
|
|
}
|