commit f49b0fd2b449ad6a82a627dcab0c6afaded6ab32
parent cd75a060c3c6edcfb800c1ca2f3b383a2b2e6553
Author: NunoSempere <nuno.sempere@protonmail.com>
Date: Sun, 7 May 2023 13:28:34 -0400
add function evaluation? attempt 1
Diffstat:
3 files changed, 26 insertions(+), 1 deletion(-)
diff --git a/README.md b/README.md
@@ -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
- A slightly different approach to evaluating functions
- 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.
diff --git a/mumble b/mumble
Binary files differ.
diff --git a/src/mumble.c b/src/mumble.c
@@ -167,6 +167,7 @@ lispval* lispval_qexpr(void)
// Destructor
void print_lispval_tree(lispval* v, int indent_level);
+void destroy_lispenv(lispenv* env);
void delete_lispval(lispval* v)
{
if (v == NULL || v->type > LARGEST_LISPVAL)
@@ -225,14 +226,17 @@ void delete_lispval(lispval* v)
if (VERBOSE)
printfln("Freeing user-defined func");
if (v->env != NULL) {
+ destroy_lispenv(v->env);
free(v->env);
v->env = NULL;
}
if (v->variables != NULL) {
+ delete_lispval(v->variables);
free(v->variables);
v->variables = NULL;
}
if (v->manipulation != NULL) {
+ delete_lispval(v->manipulation);
free(v->manipulation);
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)) {
// 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;