91 lines
2.4 KiB
C
91 lines
2.4 KiB
C
#include <assert.h>
|
|
#include <stddef.h>
|
|
|
|
#include "uclisp.h"
|
|
#include "internal.h"
|
|
#include "utility.h"
|
|
#include "state.h"
|
|
|
|
// TODO: remove string.h
|
|
#include <string.h>
|
|
|
|
struct ucl_object *ucl_evaluate_builtin_form(struct ucl_state *state, struct ucl_object *list) {
|
|
// TODO: Recursively eval args
|
|
struct ucl_object *evaluated_list = ucl_nil_create();
|
|
|
|
FOREACH_LIST(list, iter, item) {
|
|
struct ucl_object *obj = ucl_evaluate(state, item);
|
|
ucl_list_append(evaluated_list, obj);
|
|
};
|
|
|
|
struct ucl_object *fun = ucl_car(evaluated_list);
|
|
struct ucl_object *args = ucl_cdr(evaluated_list);
|
|
struct ucl_object *result = NULL;
|
|
|
|
assert(fun->type == UCL_TYPE_BUILTIN);
|
|
if (fun->type == UCL_TYPE_BUILTIN) {
|
|
result = fun->builtin(state, args);
|
|
}
|
|
|
|
// TODO: cleanup
|
|
|
|
return result;
|
|
}
|
|
|
|
struct ucl_object *ucl_evaluate_special_form(struct ucl_state *state, struct ucl_object *list) {
|
|
// TODO: Recursively eval args
|
|
const char *fun_sym = ucl_car(list)->symbol;
|
|
|
|
struct ucl_object *fun = ucl_state_get(state, ucl_car(list)->symbol);
|
|
struct ucl_object *args = ucl_cdr(list);
|
|
struct ucl_object *result = NULL;
|
|
|
|
result = fun->special(state, args);
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
|
|
struct ucl_object *ucl_evaluate_list(struct ucl_state *state, struct ucl_object *list) {
|
|
if (list->cell.car == NULL) {
|
|
return list;
|
|
}
|
|
|
|
struct ucl_object *fun_sym = ucl_car(list);
|
|
if (fun_sym->type != UCL_TYPE_SYMBOL) {
|
|
return ucl_error_create("Unknown function symbol");
|
|
}
|
|
struct ucl_object *fun = ucl_state_get(state, fun_sym->symbol);
|
|
UCL_RET_IF_ERROR(fun);
|
|
|
|
if (fun->type == UCL_TYPE_SPECIAL) {
|
|
return ucl_evaluate_special_form(state, list);
|
|
} else if (fun->type == UCL_TYPE_BUILTIN) {
|
|
return ucl_evaluate_builtin_form(state, list);
|
|
} else {
|
|
assert(0);
|
|
// TODO: Lisp functions and other errors
|
|
}
|
|
}
|
|
|
|
struct ucl_object *ucl_evaluate(struct ucl_state *state, struct ucl_object *obj) {
|
|
assert(obj != NULL);
|
|
|
|
switch (obj->type) {
|
|
case UCL_TYPE_CELL:
|
|
return ucl_evaluate_list(state, obj);
|
|
case UCL_TYPE_SYMBOL:
|
|
return ucl_state_get(state, 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;
|
|
}
|
|
}
|