This commit is contained in:
NunoSempere 2023-05-07 12:49:22 -04:00
parent 1dfba9f4a1
commit 2f9edb67e6

View File

@ -131,7 +131,8 @@ lispval* lispval_builtin_func(lispbuiltin func, char* builtin_func_name)
} }
lispenv* new_lispenv(); lispenv* new_lispenv();
lispval* lispval_lambda_func(lispval* variables, lispval* manipulation){ lispval* lispval_lambda_func(lispval* variables, lispval* manipulation)
{
lispval* v = malloc(sizeof(lispval)); lispval* v = malloc(sizeof(lispval));
v->type = LISPVAL_USER_FUNC; v->type = LISPVAL_USER_FUNC;
v->builtin_func = NULL; v->builtin_func = NULL;
@ -205,8 +206,8 @@ void delete_lispval(lispval* v)
printfln("Freed sym"); printfln("Freed sym");
break; break;
case LISPVAL_BUILTIN_FUNC: case LISPVAL_BUILTIN_FUNC:
if (v->builtin_func_name != NULL){ if (v->builtin_func_name != NULL) {
if (VERBOSE){ if (VERBOSE) {
printfln("Freeing builtin func"); printfln("Freeing builtin func");
} }
free(v->builtin_func_name); free(v->builtin_func_name);
@ -223,15 +224,15 @@ void delete_lispval(lispval* v)
case LISPVAL_USER_FUNC: case LISPVAL_USER_FUNC:
if (VERBOSE) if (VERBOSE)
printfln("Freeing user-defined func"); printfln("Freeing user-defined func");
if (v->env != NULL){ if (v->env != NULL) {
free(v->env); free(v->env);
v->env = NULL; v->env = NULL;
} }
if (v->variables != NULL){ if (v->variables != NULL) {
free(v->variables); free(v->variables);
v->variables = NULL; v->variables = NULL;
} }
if (v->manipulation != NULL){ if (v->manipulation != NULL) {
free(v->manipulation); free(v->manipulation);
v->manipulation = NULL; v->manipulation = NULL;
} }
@ -266,7 +267,6 @@ void delete_lispval(lispval* v)
free(v->cell); free(v->cell);
v->cell = NULL; v->cell = NULL;
if (VERBOSE) if (VERBOSE)
printfln("Freeing the v pointer"); printfln("Freeing the v pointer");
if (v != NULL) if (v != NULL)
@ -317,8 +317,6 @@ void destroy_lispenv(lispenv* env)
// so it isn't destroyed // so it isn't destroyed
} }
lispval* clone_lispval(lispval* old); lispval* clone_lispval(lispval* old);
lispval* get_from_lispenv(char* sym, lispenv* env) lispval* get_from_lispenv(char* sym, lispenv* env)
{ {
@ -329,7 +327,7 @@ lispval* get_from_lispenv(char* sym, lispenv* env)
} }
} }
if(env->parent != NULL){ if (env->parent != NULL) {
return get_from_lispenv(sym, env->parent); return get_from_lispenv(sym, env->parent);
} else { } else {
return lispval_err("Error: unbound symbol"); return lispval_err("Error: unbound symbol");
@ -735,7 +733,7 @@ lispval* builtin_def(lispval* v, lispenv* env)
lispval* symbols = source->cell[0]; lispval* symbols = source->cell[0];
lispval* values = source->cell[1]; lispval* values = source->cell[1];
for (int i=0; i < symbols->count; i++) { for (int i = 0; i < symbols->count; i++) {
LISPVAL_ASSERT(symbols->cell[i]->type == LISPVAL_SYM, "Error: in function def, the first list of items should be of type symbol: def { { a b } { 1 2 } }"); LISPVAL_ASSERT(symbols->cell[i]->type == LISPVAL_SYM, "Error: in function def, the first list of items should be of type symbol: def { { a b } { 1 2 } }");
if (VERBOSE) if (VERBOSE)
print_lispval_tree(symbols, 0); print_lispval_tree(symbols, 0);
@ -749,19 +747,20 @@ lispval* builtin_def(lispval* v, lispenv* env)
} }
// A builtin for defining a function // A builtin for defining a function
lispval* builtin_define_lambda(lispval* v, lispenv* env){ lispval* builtin_define_lambda(lispval* v, lispenv* env)
{
// @ { {x y} { + x y } } // @ { {x y} { + x y } }
// def { {plus} {{@ {x y} {+ x y}} }} // def { {plus} {{@ {x y} {+ x y}} }}
// (eval plus) 1 2 // (eval plus) 1 2
// (@ { {x y} { + x y } }) 1 2 // (@ { {x y} { + x y } }) 1 2
LISPVAL_ASSERT( v->count == 2, "Lambda definition requires two arguments; try @ { {x y} { + x y } }"); LISPVAL_ASSERT(v->count == 2, "Lambda definition requires two arguments; try @ { {x y} { + x y } }");
LISPVAL_ASSERT(v->cell[0]->type == LISPVAL_QEXPR, "Lambda definition (@) requires that the first sub-arg be a q-expression; try @ { {x y} { + x y } }"); LISPVAL_ASSERT(v->cell[0]->type == LISPVAL_QEXPR, "Lambda definition (@) requires that the first sub-arg be a q-expression; try @ { {x y} { + x y } }");
LISPVAL_ASSERT(v->cell[1]->type == LISPVAL_QEXPR, "Lambda definition (@) requires that the second sub-arg be a q-expression; try @ { {x y} { + x y } }"); LISPVAL_ASSERT(v->cell[1]->type == LISPVAL_QEXPR, "Lambda definition (@) requires that the second sub-arg be a q-expression; try @ { {x y} { + x y } }");
lispval* variables = clone_lispval(v->cell[0]); lispval* variables = clone_lispval(v->cell[0]);
lispval* manipulation = clone_lispval(v->cell[1]); lispval* manipulation = clone_lispval(v->cell[1]);
for(int i=0; i>variables->count; i++){ for (int i = 0; i > variables->count; i++) {
LISPVAL_ASSERT(variables->cell[i]->type == LISPVAL_SYM, "First argument in function definition must only be symbols. Try @ { {x y} { + x y } }"); LISPVAL_ASSERT(variables->cell[i]->type == LISPVAL_SYM, "First argument in function definition must only be symbols. Try @ { {x y} { + x y } }");
} }
@ -931,7 +930,7 @@ lispval* evaluate_lispval(lispval* l, lispenv* env)
// Check if the first element is an operation. // Check if the first element is an operation.
if (VERBOSE) if (VERBOSE)
printfln("Checking is first element is a function"); printfln("Checking is first element is a function");
if (l->count >= 2 && ((l->cell[0])->type == LISPVAL_BUILTIN_FUNC)){ if (l->count >= 2 && ((l->cell[0])->type == LISPVAL_BUILTIN_FUNC)) {
if (VERBOSE) if (VERBOSE)
printfln("Passed check"); printfln("Passed check");
@ -945,9 +944,8 @@ lispval* evaluate_lispval(lispval* l, lispenv* env)
printfln("Constructing function and operands"); printfln("Constructing function and operands");
lispval* f = clone_lispval(l->cell[0]); lispval* f = 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]);
} }
if (VERBOSE) if (VERBOSE)
printfln("Applying function to operands"); printfln("Applying function to operands");
@ -1046,7 +1044,7 @@ int main(int argc, char** argv)
if (input == NULL) { if (input == NULL) {
break; break;
} else { } else {
if(modify_verbosity(input)){ if (modify_verbosity(input)) {
continue; continue;
} }
/* Attempt to Parse the user Input */ /* Attempt to Parse the user Input */