step: some more scaffolding for funcs.

This commit is contained in:
NunoSempere 2023-05-02 11:10:19 -04:00
parent 2854b79f62
commit 0a9f5702be

View File

@ -646,16 +646,18 @@ lispval* builtin_functions(char* func, lispval* v, lispenv* env)
// Evaluate the lispval // Evaluate the lispval
lispval* evaluate_lispval(lispval* l, lispenv* env) lispval* evaluate_lispval(lispval* l, lispenv* env)
{ {
// Check if this is neither an s-expression nor a symbol; otherwise return as is.
if (l->type != LISPVAL_SEXPR && l->type != LISPVAL_SYM)
return l;
// Check if this is a symbol // Check if this is a symbol
if(l->type == LISPVAL_SYM){ if(l->type == LISPVAL_SYM){
get_from_lispenv(l->sym, env); get_from_lispenv(l->sym, env);
} }
// Check if this is an s-expression
if (l->type != LISPVAL_SEXPR) // Evaluate the children if needed
return l;
// Evaluate the children if needed
for (int i = 0; i < l->count; i++) { for (int i = 0; i < l->count; i++) {
if (l->cell[i]->type == LISPVAL_SEXPR) { if (l->cell[i]->type == LISPVAL_SEXPR || l->cell[i]->type == LISPVAL_SYM) {
l->cell[i] = evaluate_lispval(l->cell[i], env); l->cell[i] = evaluate_lispval(l->cell[i], env);
} }
} }
@ -667,17 +669,18 @@ lispval* evaluate_lispval(lispval* l, lispenv* env)
} }
// Check if the first element is an operation. // Check if the first element is an operation.
if (l->count >= 2 && ((l->cell[0])->type == LISPVAL_SYM)) { if (l->count >= 2 && ((l->cell[0])->type == LISPVAL_FUNC)) {
lispval* temp = clone_lispval(l->cell[0]); lispval* temp = clone_lispval(l->cell[0]);
lispval* operation = pop_lispval(l, 0); lispval* f = pop_lispval(l, 0);
lispval* operands = temp; lispval* operands = temp;
// lispval* operation = clone_lispval(l->cell[0]); // lispval* operation = clone_lispval(l->cell[0]);
// lispval* operands = lispval_sexpr(); // lispval* operands = lispval_sexpr();
// for (int i = 1; i < l->count; i++) { // for (int i = 1; i < l->count; i++) {
// lispval_append_child(operands, l->cell[i]); // lispval_append_child(operands, l->cell[i]);
// } // }
lispval* answer = builtin_functions(operation->sym, l, env); lispval* answer = f->func(env, operands);
delete_lispval(operation); // builtin_functions(operation->sym, l, env);
delete_lispval(f);
delete_lispval(operands); delete_lispval(operands);
return answer; return answer;
} }