Better handling of raw lists
This commit is contained in:
parent
7906e55220
commit
4bc3337dcd
74
src/object.c
74
src/object.c
|
@ -3,6 +3,12 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
#define printd(...) printf(__VA_ARGS__)
|
||||||
|
#else
|
||||||
|
#define printd(...) stringObj(NULL, NULL)
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef STANDALONE
|
#ifndef STANDALONE
|
||||||
#define printf(...) stringObj(NULL, NULL)
|
#define printf(...) stringObj(NULL, NULL)
|
||||||
#endif
|
#endif
|
||||||
|
@ -177,61 +183,75 @@ void debugObj(const Object *obj)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints the given object
|
void _printList(const Object *list, int newline);
|
||||||
void printObj(const Object *obj)
|
void _printObj(const Object *obj, int newline)
|
||||||
{
|
{
|
||||||
for(int i = 0; i < depth; i++)
|
for(int i = 0; i < depth; i++)
|
||||||
printf(" ");
|
printd(" ");
|
||||||
depth++;
|
depth++;
|
||||||
|
|
||||||
if(obj->type == TYPE_NUMBER) {
|
if(obj->type == TYPE_NUMBER) {
|
||||||
printf("TYPE_NUMBER");
|
printd("TYPE_NUMBER");
|
||||||
} else if(obj->type == TYPE_BOOL) {
|
} else if(obj->type == TYPE_BOOL) {
|
||||||
printf("TYPE_BOOL");
|
printd("TYPE_BOOL");
|
||||||
} else if(obj->type == TYPE_LIST) {
|
} else if(obj->type == TYPE_LIST) {
|
||||||
printf("TYPE_LIST\n");
|
printd("TYPE_LIST\n");
|
||||||
printList(obj);
|
_printList(obj, newline);
|
||||||
depth--;
|
depth--;
|
||||||
return;
|
return;
|
||||||
} else if(obj->type == TYPE_FUNC) {
|
} else if(obj->type == TYPE_FUNC) {
|
||||||
printf("TYPE_FUNC");
|
printd("TYPE_FUNC");
|
||||||
} else if(obj->type == TYPE_SYMBOL) {
|
} else if(obj->type == TYPE_SYMBOL) {
|
||||||
printf("TYPE_SYMBOL");
|
printd("TYPE_SYMBOL");
|
||||||
} else if(obj->type == TYPE_LAMBDA) {
|
} else if(obj->type == TYPE_LAMBDA) {
|
||||||
printf("TYPE_LAMBDA Params:\n");
|
printd("TYPE_LAMBDA Params:\n");
|
||||||
depth++;
|
depth++;
|
||||||
printObj(&obj->lambda->params);
|
printObj(&obj->lambda->params);
|
||||||
for(int i = 1; i < depth; i++)
|
for(int i = 1; i < depth; i++)
|
||||||
printf(" ");
|
printf(" ");
|
||||||
printf("Lambda Body: \n");
|
printd("Lambda Body: \n");
|
||||||
printObj(&obj->lambda->body);
|
printObj(&obj->lambda->body);
|
||||||
} else if(obj->type == TYPE_ERROR) {
|
} else if(obj->type == TYPE_ERROR) {
|
||||||
printf("TYPE_ERROR: ");
|
printd("TYPE_ERROR: ");
|
||||||
} else {
|
} else {
|
||||||
printf("TYPE_OTHER (as int)");
|
printd("TYPE_OTHER (as int)");
|
||||||
}
|
}
|
||||||
depth--;
|
depth--;
|
||||||
char temp[20] = "";
|
char temp[20] = "";
|
||||||
stringObj(temp, obj);
|
stringObj(temp, obj);
|
||||||
printf(": %s\n", temp);
|
if(newline)
|
||||||
|
printf("%s\n", temp);
|
||||||
|
else
|
||||||
|
printf("%s", temp);
|
||||||
printErr(obj);
|
printErr(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prints the given object
|
||||||
|
void printObj(const Object *obj)
|
||||||
|
{
|
||||||
|
_printObj(obj, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _printList(const Object *list, int newline)
|
||||||
|
{
|
||||||
|
printf("(");
|
||||||
|
const Object *tail = list->list;
|
||||||
|
while(tail != NULL) {
|
||||||
|
printf(" ");
|
||||||
|
depth++;
|
||||||
|
_printObj(tail, 0);
|
||||||
|
depth--;
|
||||||
|
tail = tail->forward;
|
||||||
|
}
|
||||||
|
printf(" )");
|
||||||
|
|
||||||
|
if(newline)
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
void printList(const Object *list)
|
void printList(const Object *list)
|
||||||
{
|
{
|
||||||
const Object *tail = list->list;
|
_printList(list, 1);
|
||||||
if(tail == NULL) {
|
|
||||||
for(int i = 0; i < depth; i++)
|
|
||||||
printf(" ");
|
|
||||||
printf("EMPTY LIST\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
while(tail != NULL) {
|
|
||||||
depth++;
|
|
||||||
printObj(tail);
|
|
||||||
depth--;
|
|
||||||
tail = tail->forward;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void cleanObject(Object *target)
|
void cleanObject(Object *target)
|
||||||
|
|
|
@ -126,6 +126,11 @@ Object evalLambdaArgs(const Object *arg_forms)
|
||||||
return constructLambda(arg_forms, arg_forms->forward);
|
return constructLambda(arg_forms, arg_forms->forward);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Object evalMapArgs(const Object *arg_forms)
|
||||||
|
{
|
||||||
|
return listObject();
|
||||||
|
}
|
||||||
|
|
||||||
struct Environment envForLambda(const Object *params, const Object *arg_forms,
|
struct Environment envForLambda(const Object *params, const Object *arg_forms,
|
||||||
struct Environment *outer)
|
struct Environment *outer)
|
||||||
{
|
{
|
||||||
|
@ -169,6 +174,8 @@ Object evalBuiltIns(const Object *first, const Object *rest,
|
||||||
return evalIfArgs(rest, env);
|
return evalIfArgs(rest, env);
|
||||||
} else if(strcmp(first->name, "fn") == 0) {
|
} else if(strcmp(first->name, "fn") == 0) {
|
||||||
return evalLambdaArgs(rest);
|
return evalLambdaArgs(rest);
|
||||||
|
} else if(strcmp(first->name, "mp") == 0) {
|
||||||
|
return evalMapArgs(rest);
|
||||||
}
|
}
|
||||||
|
|
||||||
return errorObject(BUILT_IN_NOT_FOUND);
|
return errorObject(BUILT_IN_NOT_FOUND);
|
||||||
|
@ -232,7 +239,7 @@ Object eval(const Object *obj, struct Environment *env)
|
||||||
return eval(&first_eval.lambda->body, &newEnv);
|
return eval(&first_eval.lambda->body, &newEnv);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return errorObject(TYPE_LIST_NOT_CAUGHT);
|
return *obj;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case TYPE_LAMBDA:
|
case TYPE_LAMBDA:
|
||||||
|
|
Loading…
Reference in New Issue