diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..b333dd1 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,5 @@ +all: + gcc -g -O0 -D STANDALONE pebblisp.c tokens.c object.c + +debug: + gcc -g -O0 -D STANDALONE -D DEBUG pebblisp.c tokens.c object.c diff --git a/src/object.c b/src/object.c index 7920f30..fbdfe09 100644 --- a/src/object.c +++ b/src/object.c @@ -105,6 +105,14 @@ char* stringObj(char *dest, const Object *obj) int depth = 0; +void debugObj(const Object *obj) +{ +#ifdef DEBUG + printObj(obj); +#endif + return; +} + // Prints the given object void printObj(const Object *obj) { diff --git a/src/object.h b/src/object.h index 6d5ba0c..6bb5c93 100644 --- a/src/object.h +++ b/src/object.h @@ -12,7 +12,8 @@ enum errorCode { BUILT_IN_NOT_FOUND, NULL_PARSE, NULL_LAMBDA_LIST, - LAMBDA_ARGS_NOT_LIST + LAMBDA_ARGS_NOT_LIST, + DID_NOT_FIND_SYMBOL }; #ifdef STANDALONE @@ -23,7 +24,8 @@ enum errorCode { "BUILT_IN_NOT_FOUND", "NULL_PARSE", "NULL_LAMBDA_LIST", - "LAMBDA_ARGS_NOT_LIST" + "LAMBDA_ARGS_NOT_LIST", + "DID_NOT_FIND_SYMBOL" }; #endif @@ -62,6 +64,7 @@ struct Lambda { char* stringObj(char *dest, const Object *obj); void printList(const Object *list); void printObj(const Object *obj); +void debugObj(const Object *obj); Object *tail(const Object *listObj); void addToList(Object *dest, Object src); diff --git a/src/pebblisp.c b/src/pebblisp.c index a7ed573..4172763 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -4,10 +4,6 @@ #include #include -#ifndef STANDALONE -#define printf(...) copySlice(NULL, NULL) -#endif - void copySlice(char * dest, struct Slice *src) { if(!dest || !src) @@ -18,12 +14,12 @@ void copySlice(char * dest, struct Slice *src) void debugSlice(struct Slice *s) { - printf("Debug Slice\n text:'"); + printd("Debug Slice\n text:'"); for(int i = 0; i < s->length; i++) { - printf("%c", s->text[i]); + printd("%c", s->text[i]); } - printf("'\n"); - printf(" length: %d\n", s->length); + printd("'\n"); + printd(" length: %d\n", s->length); } Object fetchFromEnvironment(const char *name, struct Environment *env) @@ -31,7 +27,7 @@ Object fetchFromEnvironment(const char *name, struct Environment *env) if(!env) return errorObject(NULL_ENV); - printf("Fetching '%s' from env\n", name); + printd("Fetching '%s' from env\n", name); printEnv(env); int i = 0; const char *next = env->strings[i]; @@ -41,14 +37,12 @@ Object fetchFromEnvironment(const char *name, struct Environment *env) } next = env->strings[++i]; } - printf("Trying outer\n"); + printd("Trying outer\n"); if(env->outer) { return fetchFromEnvironment(name, env->outer); } - printf("DID NOT FIND SYMBOL\n"); - Object o; - o.type = TYPE_ERROR; - return o; + + return errorObject(DID_NOT_FIND_SYMBOL); } Result parse(struct Slice *slices) @@ -136,8 +130,8 @@ Object evalLambdaArgs(const Object *arg_forms) struct Environment envForLambda(const Object *params, const Object *arg_forms, struct Environment *outer) { - printf("\n#####################\nenvForLambda()\n"); - printObj(arg_forms); + printd("\n#####################\nenvForLambda()\n"); + debugObj(arg_forms); struct Environment env; env.outer = outer; env.strings = NULL; @@ -160,10 +154,10 @@ struct Environment envForLambda(const Object *params, const Object *arg_forms, } // Something is segfaulting, anyway env.strings[length] = NULL; - printf("envForLambda env IT BROKE HERE()\n"); - printf("envForLambda length=%d\n", length); + printd("envForLambda env IT BROKE HERE()\n"); + printd("envForLambda length=%d\n", length); printEnv(&env); - printf("END envForLambda()\n\n"); + printd("END envForLambda()\n\n"); return env; } @@ -175,10 +169,10 @@ Object evalBuiltIns(const Object *first, const Object *rest, }else if(strcmp(first->name, "if") == 0) { return evalIfArgs(rest, env); }else if(strcmp(first->name, "fn") == 0) { - // printf("EVALBUILTINS first:\n"); - // printObj(first); - // printf("\nEVALBUILTINS rest:\n"); - // printObj(rest); + // printd("EVALBUILTINS first:\n"); + // debugObj(first); + // printd("\nEVALBUILTINS rest:\n"); + // debugObj(rest); return evalLambdaArgs(rest); } @@ -195,8 +189,8 @@ void eval_forms(Object *destList, const Object *src, struct Environment *env) Object eval(const Object *obj, struct Environment *env) { - printf("eval():\n"); - printObj(obj); + printd("eval():\n"); + debugObj(obj); switch(obj->type) { case TYPE_NUMBER: case TYPE_BOOL: @@ -230,15 +224,15 @@ Object eval(const Object *obj, struct Environment *env) Object rest[length]; eval_forms(rest, obj, env); - printf("Evaluating func\n"); Object func_eval = first_eval.func(rest[0], rest[1]); // deleteList(obj); return func_eval; + } else if (first_eval.type == TYPE_LAMBDA) { - printf("lammy\n"); struct Environment newEnv = envForLambda(&first_eval.lambda->params, first_form->forward, env); return eval(&first_eval.lambda->body, &newEnv); + } else { return errorObject(TYPE_LIST_NOT_CAUGHT); } @@ -287,8 +281,8 @@ void printEnv(struct Environment *env) for(int i = 0; i < MAX_ENV_ELM; i++) { if(env->strings[i] == NULL) return; - printf("env[%d]: '%s'\n ", i, env->strings[i]); - printObj(&env->objects[i]); + printd("env[%d]: '%s'\n ", i, env->strings[i]); + debugObj(&env->objects[i]); } } @@ -395,20 +389,20 @@ struct Environment defaultEnv() { Object parseEval(const char *input, struct Environment *env) { struct Slice *tokens = nf_tokenize(input); -#ifdef STANDALONE +#ifdef DEBUG struct Slice *debug = tokens; if(debug) { while(debug->text) { char tok[MAX_TOK_LEN]; copySlice(tok, debug); - //printf("slice: '%s'\n", tok); + //printd("slice: '%s'\n", tok); debug++; } } #endif Object parsed = parse(tokens).obj; // printf("PARSEEVAL() PRINTLIST():\n"); - // printObj(&parsed); + // debugObject(&parsed); // printf("end PARSEEVAL() PRINTLIST():\n\n"); free(tokens); return eval(&parsed, env); diff --git a/src/pebblisp.h b/src/pebblisp.h index 931ba9d..dcd29cd 100644 --- a/src/pebblisp.h +++ b/src/pebblisp.h @@ -1,11 +1,20 @@ #ifndef PEBBLISP_H #define PEBBLISP_H -// #define STANDALONE -// #define NO_REPL - #include "object.h" +#ifndef STANDALONE +#include +// #define printf(...) copySlice(NULL, NULL) +#define printd(...) copySlice(NULL, NULL) +#endif + +#ifdef DEBUG +#define printd(...) printf(__VA_ARGS__) +#else +#define printd(...) copySlice(NULL, NULL) +#endif + struct Slice { const char *text; char length; diff --git a/src/tokens.c b/src/tokens.c index de35992..f4bfd80 100644 --- a/src/tokens.c +++ b/src/tokens.c @@ -4,7 +4,8 @@ #ifdef STANDALONE #include #else -#define printf(...) nf_tokenize(NULL) +#include +// #define printf(...) nf_tokenize(NULL) #endif /*