Add simple evaluation

This commit is contained in:
2022-10-28 22:29:37 -04:00
parent ed173bd17a
commit 26a0d17074
6 changed files with 75 additions and 17 deletions

View File

@@ -2,26 +2,48 @@
#include <stddef.h>
#include "nihilispm.h"
#include "nihilispm_internal.h"
#include "nihilispm_utility.h"
#include "nihilispm_state.h"
struct nl_object *nl_evaluate_list(struct nl_object *list) {
return NULL;
struct nl_object *nl_evaluate_list(struct nl_state *state, struct nl_object *list) {
// TODO: Recursively eval args
struct nl_object *evaluated_list = nl_nil_create();
FOREACH_LIST(list, iter, item) {
struct nl_object *obj = nl_evaluate(state, item);
nl_list_append(evaluated_list, obj);
};
struct nl_object *fun = nl_car(evaluated_list);
struct nl_object *args = nl_cdr(evaluated_list);
struct nl_object *result = NULL;
assert(fun->type == NL_TYPE_BUILTIN);
if (fun->type == NL_TYPE_BUILTIN) {
result = fun->builtin(args);
}
// TODO: Non-builtins
// TODO: cleanup
return result;
}
struct nl_object *nl_evaluate_symbol(struct nl_object *symbol) {
return NULL;
}
struct nl_object *nl_evaluate(struct nl_object *obj) {
struct nl_object *nl_evaluate(struct nl_state *state, struct nl_object *obj) {
assert(obj != NULL);
switch (obj->type) {
case NL_TYPE_CELL:
return nl_evaluate_list(obj);
return nl_evaluate_list(state, obj);
case NL_TYPE_SYMBOL:
return NULL; //nl_evalute_symbol(obj);
return nl_state_get(state, obj->symbol);
case NL_TYPE_INT:
case NL_TYPE_STRING:
case NL_TYPE_ERROR:
return obj;
case NL_TYPE_BUILTIN:
case NL_TYPE_COUNT:
assert(0);
return NULL;