add function evaluation? attempt 1

This commit is contained in:
NunoSempere 2023-05-07 13:28:34 -04:00
parent cd75a060c3
commit f49b0fd2b4
3 changed files with 26 additions and 1 deletions

View File

@ -9,6 +9,13 @@ This is a Lisp written in C. It follows the outline in this [Build Your Own Lisp
- Different and perhaps slightly more elegant printing functions - Different and perhaps slightly more elegant printing functions
- A slightly different approach to evaluating functions - A slightly different approach to evaluating functions
- Capturing Ctrl+D - Capturing Ctrl+D
- Float instead of ints
Conversely, it doesn't have:
- Function currying
- strings
- Variable arguments
- ...
Overall this might be mostly of interest as a pointer to the book that this is originally based on. And to readers of that same book, I'd be curious to see what you ended up with. Overall this might be mostly of interest as a pointer to the book that this is originally based on. And to readers of that same book, I'd be curious to see what you ended up with.

BIN
mumble

Binary file not shown.

View File

@ -167,6 +167,7 @@ lispval* lispval_qexpr(void)
// Destructor // Destructor
void print_lispval_tree(lispval* v, int indent_level); void print_lispval_tree(lispval* v, int indent_level);
void destroy_lispenv(lispenv* env);
void delete_lispval(lispval* v) void delete_lispval(lispval* v)
{ {
if (v == NULL || v->type > LARGEST_LISPVAL) if (v == NULL || v->type > LARGEST_LISPVAL)
@ -225,14 +226,17 @@ void delete_lispval(lispval* v)
if (VERBOSE) if (VERBOSE)
printfln("Freeing user-defined func"); printfln("Freeing user-defined func");
if (v->env != NULL) { if (v->env != NULL) {
destroy_lispenv(v->env);
free(v->env); free(v->env);
v->env = NULL; v->env = NULL;
} }
if (v->variables != NULL) { if (v->variables != NULL) {
delete_lispval(v->variables);
free(v->variables); free(v->variables);
v->variables = NULL; v->variables = NULL;
} }
if (v->manipulation != NULL) { if (v->manipulation != NULL) {
delete_lispval(v->manipulation);
free(v->manipulation); free(v->manipulation);
v->manipulation = NULL; v->manipulation = NULL;
} }
@ -963,7 +967,21 @@ lispval* evaluate_lispval(lispval* l, lispenv* env)
if (l->count >= 2 && ((l->cell[0])->type == LISPVAL_USER_FUNC)) { if (l->count >= 2 && ((l->cell[0])->type == LISPVAL_USER_FUNC)) {
// Do something with user-defined functions here // Do something with user-defined functions here
return lispval_err("Error: User-defined functions not yet implemented"); lispval* f = clone_lispval(l->cell[0]);
// check whether function takes as many arguments as given
LISPVAL_ASSERT(f->variables->count == (l->count - 1), "Error: Incorrect number of variables given to user-defined function");
for (int i = 1; i < l->count; i++) {
// lispval_append_child(operands, l->cell[i]);
insert_in_current_lispenv(
f->variables->cell[i]->sym, l->cell[i], f->env);
}
lispval* answer = evaluate_lispval(f->manipulation, f->env);
destroy_lispenv(f->env);
return answer;
// return lispval_err("Error: User-defined functions not yet implemented");
} }
return l; return l;