Seem to have resolved bad nesting issue
copyList() now properly deep-copies. Recursive functions still not working.
This commit is contained in:
parent
9fe35450df
commit
cb945a1e41
32
src/object.c
32
src/object.c
|
@ -361,20 +361,36 @@ void deleteList(Object *dest)
|
||||||
dest->list = NULL;
|
dest->list = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _copyList(Object *dest, const Object *src, int delete) {
|
||||||
|
if(!dest || !src) {
|
||||||
|
printd("NULL\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(dest->type != TYPE_LIST || src->type != TYPE_LIST) {
|
||||||
|
printd("NOT A LIST\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(delete)
|
||||||
|
deleteList(dest);
|
||||||
|
|
||||||
|
FOR_POINTER_IN_LIST(src) {
|
||||||
|
debugObj(POINTER);
|
||||||
|
nf_addToList(dest, *POINTER);
|
||||||
|
if(POINTER->type == TYPE_LIST) {
|
||||||
|
tail(dest)->list = NULL;
|
||||||
|
_copyList(tail(dest), POINTER, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Copies all items from `src` to `dest`
|
* Does a deep copy of all items from `src` to `dest`
|
||||||
* Does nothing if either is NULL, or not a list type
|
* Does nothing if either is NULL, or not a list type
|
||||||
* Deletes all items from `dest` before copying
|
* Deletes all items from `dest` before copying
|
||||||
*/
|
*/
|
||||||
void copyList(Object *dest, const Object *src)
|
void copyList(Object *dest, const Object *src)
|
||||||
{
|
{
|
||||||
if(!dest || !src || dest->type != TYPE_LIST || src->type != TYPE_LIST)
|
_copyList(dest, src, 1);
|
||||||
return;
|
|
||||||
|
|
||||||
deleteList(dest);
|
|
||||||
FOR_POINTER_IN_LIST(src) {
|
|
||||||
nf_addToList(dest, *POINTER);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a basic object with NULL forward and the given `type`
|
// Returns a basic object with NULL forward and the given `type`
|
||||||
|
|
|
@ -37,10 +37,10 @@ Object fetchFromEnvironment(const char *name, struct Environment *env)
|
||||||
if(env->size == 0)
|
if(env->size == 0)
|
||||||
return errorObject(EMPTY_ENV);
|
return errorObject(EMPTY_ENV);
|
||||||
|
|
||||||
printf("Fetching '%s' from env\n", name);
|
printd("Fetching '%s' from env\n", name);
|
||||||
// printEnv(env);
|
// printEnv(env);
|
||||||
for(int i = 0; i < env->size; i++) {
|
for(int i = 0; i < env->size; i++) {
|
||||||
printf("Try %d\n", i);
|
printd("Try %d\n", i);
|
||||||
if(env->strings[i] == NULL)
|
if(env->strings[i] == NULL)
|
||||||
break;
|
break;
|
||||||
if(strcmp(name, env->strings[i]) == 0) {
|
if(strcmp(name, env->strings[i]) == 0) {
|
||||||
|
@ -48,9 +48,9 @@ Object fetchFromEnvironment(const char *name, struct Environment *env)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Trying outer %p\n", env->outer);
|
printd("Trying outer %p\n", env->outer);
|
||||||
if(env->outer) {
|
if(env->outer) {
|
||||||
printEnv(env->outer);
|
// printEnv(env->outer);
|
||||||
return fetchFromEnvironment(name, env->outer);
|
return fetchFromEnvironment(name, env->outer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -200,12 +200,12 @@ struct Environment envForLambda(const Object *params, const Object *arg_forms,
|
||||||
env.objects = malloc(sizeof(Object) * paramCount + 1);
|
env.objects = malloc(sizeof(Object) * paramCount + 1);
|
||||||
|
|
||||||
const Object *march = arg_forms;
|
const Object *march = arg_forms;
|
||||||
printf("Param count: %d\n", paramCount);
|
printd("Param count: %d\n", paramCount);
|
||||||
for(int i = 0; i < paramCount; i++) {
|
for(int i = 0; i < paramCount; i++) {
|
||||||
const char *newObjName = itemAt(params, i)->name;
|
const char *newObjName = itemAt(params, i)->name;
|
||||||
const Object newEnvObj = eval(march, outer);
|
const Object newEnvObj = eval(march, outer);
|
||||||
printf("Adding new object '%s' to lambda env: ", newObjName);
|
printd("Adding new object '%s' to lambda env: ", newObjName);
|
||||||
printObj(&newEnvObj);
|
debugObj(&newEnvObj);
|
||||||
addToEnv(&env, newObjName, newEnvObj); // Could use eval_forms?
|
addToEnv(&env, newObjName, newEnvObj); // Could use eval_forms?
|
||||||
march = march->forward;
|
march = march->forward;
|
||||||
}
|
}
|
||||||
|
@ -238,8 +238,6 @@ void eval_forms(Object *destList, const Object *src, struct Environment *env)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int depth = 0;
|
|
||||||
|
|
||||||
Object eval(const Object *obj, struct Environment *env)
|
Object eval(const Object *obj, struct Environment *env)
|
||||||
{
|
{
|
||||||
printd("eval():\n");
|
printd("eval():\n");
|
||||||
|
@ -250,13 +248,7 @@ Object eval(const Object *obj, struct Environment *env)
|
||||||
return *obj; // Return as is
|
return *obj; // Return as is
|
||||||
|
|
||||||
case TYPE_SYMBOL:
|
case TYPE_SYMBOL:
|
||||||
{
|
return fetchFromEnvironment(obj->name, env);
|
||||||
depth++;
|
|
||||||
printf("depth: %d\n", depth);
|
|
||||||
const Object oi = fetchFromEnvironment(obj->name, env);
|
|
||||||
depth--;
|
|
||||||
return oi;
|
|
||||||
}
|
|
||||||
|
|
||||||
case TYPE_LIST:
|
case TYPE_LIST:
|
||||||
{
|
{
|
||||||
|
@ -295,7 +287,7 @@ Object eval(const Object *obj, struct Environment *env)
|
||||||
struct Environment newEnv =
|
struct Environment newEnv =
|
||||||
envForLambda(&first_eval.lambda->params, first_form->forward, env);
|
envForLambda(&first_eval.lambda->params, first_form->forward, env);
|
||||||
Object ret = eval(&first_eval.lambda->body, &newEnv);
|
Object ret = eval(&first_eval.lambda->body, &newEnv);
|
||||||
printEnv(&newEnv);
|
//printEnv(&newEnv);
|
||||||
deleteEnv(&newEnv);
|
deleteEnv(&newEnv);
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
|
@ -452,6 +444,8 @@ struct Environment defaultEnv() {
|
||||||
addFunc(">", >h, &e);
|
addFunc(">", >h, &e);
|
||||||
addFunc("<", <h, &e);
|
addFunc("<", <h, &e);
|
||||||
addFunc("len", &len, &e);
|
addFunc("len", &len, &e);
|
||||||
|
parseEval("(def max (fn (a b) (if (> a b) a b)))", &e);
|
||||||
|
parseEval("(def min (fn (a b) (if (< a b) a b)))", &e);
|
||||||
|
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue