Clean-up. Added startList(). Fixed `T` and `F`
This commit is contained in:
parent
1da347e612
commit
29aa001de8
|
@ -389,6 +389,13 @@ inline Object listObject()
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline Object startList(const Object start)
|
||||||
|
{
|
||||||
|
Object list = listObject();
|
||||||
|
nf_addToList(&list, start);
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
inline Object numberObject(int num)
|
inline Object numberObject(int num)
|
||||||
{
|
{
|
||||||
Object o = newObject(TYPE_NUMBER);
|
Object o = newObject(TYPE_NUMBER);
|
||||||
|
|
|
@ -96,6 +96,7 @@ void allocObject(Object **spot, const Object src);
|
||||||
|
|
||||||
Object newObject(Type type);
|
Object newObject(Type type);
|
||||||
Object listObject();
|
Object listObject();
|
||||||
|
Object startList(const Object start);
|
||||||
Object lambdaObject();
|
Object lambdaObject();
|
||||||
Object symbolObject();
|
Object symbolObject();
|
||||||
Object boolObject(int b);
|
Object boolObject(int b);
|
||||||
|
|
|
@ -35,7 +35,7 @@ Object fetchFromEnvironment(const char *name, struct Environment *env)
|
||||||
return errorObject(NULL_ENV);
|
return errorObject(NULL_ENV);
|
||||||
|
|
||||||
printd("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++) {
|
||||||
if(strcmp(name, env->strings[i]) == 0) {
|
if(strcmp(name, env->strings[i]) == 0) {
|
||||||
return env->objects[i];
|
return env->objects[i];
|
||||||
|
@ -72,7 +72,7 @@ Result readSeq(struct Slice *tokens)
|
||||||
{
|
{
|
||||||
Object res = listObject();
|
Object res = listObject();
|
||||||
for(;;) {
|
for(;;) {
|
||||||
struct Slice *next = &tokens[0];
|
struct Slice *next = tokens;
|
||||||
struct Slice *rest = next->text? &next[1] : NULL;
|
struct Slice *rest = next->text? &next[1] : NULL;
|
||||||
if(next->text[0] == ')') {
|
if(next->text[0] == ')') {
|
||||||
return result(res, rest);
|
return result(res, rest);
|
||||||
|
@ -93,10 +93,10 @@ Object parseAtom(struct Slice *s)
|
||||||
}
|
}
|
||||||
return numberObject(num);
|
return numberObject(num);
|
||||||
|
|
||||||
} else if (s->text[0] == 'T' && s->text[1] == '\0') {
|
} else if (s->text[0] == 'T' && s->length == 1) {
|
||||||
return boolObject(1);
|
return boolObject(1);
|
||||||
|
|
||||||
} else if (s->text[0] == 'F' && s->text[1] == '\0') {
|
} else if (s->text[0] == 'F' && s->length == 1) {
|
||||||
return boolObject(0);
|
return boolObject(0);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
@ -108,7 +108,7 @@ Object parseAtom(struct Slice *s)
|
||||||
|
|
||||||
Object evalDefArgs(const Object *arg_forms, struct Environment *env)
|
Object evalDefArgs(const Object *arg_forms, struct Environment *env)
|
||||||
{
|
{
|
||||||
const Object *first_form = &arg_forms[0];
|
const Object *first_form = arg_forms;
|
||||||
const char *name = first_form->name;
|
const char *name = first_form->name;
|
||||||
|
|
||||||
// Immediately adding the function to the env might allow recursion?
|
// Immediately adding the function to the env might allow recursion?
|
||||||
|
@ -121,6 +121,15 @@ Object evalDefArgs(const Object *arg_forms, struct Environment *env)
|
||||||
|
|
||||||
Object evalIfArgs(const Object *arg_forms, struct Environment *env)
|
Object evalIfArgs(const Object *arg_forms, struct Environment *env)
|
||||||
{
|
{
|
||||||
|
printf("eval(arg_forms):\n ");
|
||||||
|
Object o = eval(arg_forms, env);
|
||||||
|
printObj(&o);
|
||||||
|
printf("eval(arg_forms->forward):\n ");
|
||||||
|
o = eval(arg_forms->forward, env);
|
||||||
|
printObj(&o);
|
||||||
|
printf("eval(arg_forms->forward->forward):\n ");
|
||||||
|
o = eval(arg_forms->forward->forward, env);
|
||||||
|
printObj(&o);
|
||||||
return eval(arg_forms, env).number?
|
return eval(arg_forms, env).number?
|
||||||
eval(arg_forms->forward, env) :
|
eval(arg_forms->forward, env) :
|
||||||
eval(arg_forms->forward->forward, env);
|
eval(arg_forms->forward->forward, env);
|
||||||
|
@ -137,8 +146,8 @@ Object evalMapArgs(const Object *arg_forms, struct Environment *env)
|
||||||
if(!arg_forms)
|
if(!arg_forms)
|
||||||
return errorObject(NULL_MAP_ARGS);
|
return errorObject(NULL_MAP_ARGS);
|
||||||
|
|
||||||
const Object lambda = eval(&arg_forms[0], env);
|
const Object lambda = eval(arg_forms, env);
|
||||||
Object *oldList = (&arg_forms[0])->forward;
|
Object *oldList = arg_forms->forward;
|
||||||
|
|
||||||
if(lambda.type != TYPE_LAMBDA || oldList->type != TYPE_LIST)
|
if(lambda.type != TYPE_LAMBDA || oldList->type != TYPE_LIST)
|
||||||
return errorObject(BAD_TYPE);
|
return errorObject(BAD_TYPE);
|
||||||
|
@ -149,10 +158,7 @@ Object evalMapArgs(const Object *arg_forms, struct Environment *env)
|
||||||
while(oldElement != NULL) {
|
while(oldElement != NULL) {
|
||||||
// Create a new list for each element in the list,
|
// Create a new list for each element in the list,
|
||||||
// since lambda evaluation looks for a list
|
// since lambda evaluation looks for a list
|
||||||
Object tempList = listObject();
|
const Object tempList = startList(*oldElement);
|
||||||
Object listBack[1] = {*oldElement};
|
|
||||||
listBack[0].forward = NULL;
|
|
||||||
tempList.list = listBack;
|
|
||||||
|
|
||||||
struct Environment newEnv =
|
struct Environment newEnv =
|
||||||
envForLambda(&lambda.lambda->params, &tempList, env);
|
envForLambda(&lambda.lambda->params, &tempList, env);
|
||||||
|
@ -173,11 +179,12 @@ struct Environment envForLambda(const Object *params, const Object *arg_forms,
|
||||||
|
|
||||||
int length = listLength(params);
|
int length = listLength(params);
|
||||||
|
|
||||||
struct Environment env;
|
struct Environment env = {
|
||||||
env.outer = outer;
|
.outer = outer,
|
||||||
env.strings = NULL;
|
.strings = NULL,
|
||||||
env.objects = NULL;
|
.objects = NULL,
|
||||||
env.size = length;
|
.size = length
|
||||||
|
};
|
||||||
|
|
||||||
if(length == 0)
|
if(length == 0)
|
||||||
return env;
|
return env;
|
||||||
|
@ -185,15 +192,12 @@ struct Environment envForLambda(const Object *params, const Object *arg_forms,
|
||||||
env.strings = calloc(sizeof(char*), length + 1);
|
env.strings = calloc(sizeof(char*), length + 1);
|
||||||
env.objects = malloc(sizeof(Object) * length + 1);
|
env.objects = malloc(sizeof(Object) * length + 1);
|
||||||
|
|
||||||
Object vs[length];
|
const Object *o = arg_forms;
|
||||||
eval_forms(vs, arg_forms, outer);
|
|
||||||
|
|
||||||
for(int i = 0; i < length; i++) {
|
for(int i = 0; i < length; i++) {
|
||||||
const char *n = itemAt(params, i)->name;
|
const char *n = itemAt(params, i)->name;
|
||||||
addToEnv(&env, n, eval(&arg_forms[i], outer)); // May not need eval?
|
addToEnv(&env, n, eval(o, outer)); // Could use eval_forms?
|
||||||
// SHOULD BE `vs` not arg_forms???
|
o = o->forward;
|
||||||
} // Something is segfaulting, anyway
|
}
|
||||||
// env.strings[length] = NULL;
|
|
||||||
|
|
||||||
return env;
|
return env;
|
||||||
}
|
}
|
||||||
|
@ -271,6 +275,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);
|
||||||
deleteEnv(&newEnv);
|
deleteEnv(&newEnv);
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
|
@ -338,8 +343,8 @@ void printEnv(struct Environment *env)
|
||||||
for(int i = 0; i < env->size; i++) {
|
for(int i = 0; i < env->size; i++) {
|
||||||
if(env->strings[i] == NULL)
|
if(env->strings[i] == NULL)
|
||||||
return;
|
return;
|
||||||
printd("env[%d]: '%s'\n ", i, env->strings[i]);
|
printf("env[%d]: '%s'\n ", i, env->strings[i]);
|
||||||
debugObj(&env->objects[i]);
|
printObj(&env->objects[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -384,9 +389,7 @@ bopf(lth, '<');
|
||||||
void addFunc(const char *name, Object (*func)(Object, Object),
|
void addFunc(const char *name, Object (*func)(Object, Object),
|
||||||
struct Environment *env)
|
struct Environment *env)
|
||||||
{
|
{
|
||||||
Object o;
|
Object o = newObject(TYPE_FUNC);
|
||||||
o.type = TYPE_FUNC;
|
|
||||||
o.forward = NULL;
|
|
||||||
o.func = func;
|
o.func = func;
|
||||||
addToEnv(env, name, o);
|
addToEnv(env, name, o);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue