From 1da347e612e9cc16cad6e89957b260c259a744ca Mon Sep 17 00:00:00 2001 From: = <=> Date: Fri, 8 May 2020 07:29:06 +0100 Subject: [PATCH] Added more error-catching --- src/Makefile | 3 +++ src/object.h | 2 ++ src/pebblisp.c | 19 ++++++++++--------- src/tokens.c | 4 ++++ 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/Makefile b/src/Makefile index 95badf3..280a9b6 100644 --- a/src/Makefile +++ b/src/Makefile @@ -6,3 +6,6 @@ debug: phrase: gcc -g -O0 -o pebblisp -D STANDALONE -D DEBUG -D NO_REPL pebblisp.c tokens.c object.c + +run: + ./pebblisp diff --git a/src/object.h b/src/object.h index 80470c3..f2956ae 100644 --- a/src/object.h +++ b/src/object.h @@ -6,6 +6,7 @@ #define MAX_ENV_ELM 15 // 50 enum errorCode { + MISMATCHED_PARENS, BAD_LIST_OF_SYMBOL_STRINGS, TYPE_LIST_NOT_CAUGHT, NULL_ENV, @@ -21,6 +22,7 @@ enum errorCode { //#ifdef STANDALONE static const char *errorText[] = { + "MISMATCHED_PARENS", "BAD_LIST_OF_SYMBOL_STRINGS", "TYPE_LIST_NOT_CAUGHT", "NULL_ENV", diff --git a/src/pebblisp.c b/src/pebblisp.c index 4fe24cb..3578ab5 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -22,6 +22,8 @@ void debugSlice(struct Slice *s) printd("Debug Slice\n text:'"); for(int i = 0; i < s->length; i++) { printd("%c", s->text[i]); + if(s->text[i] == '\0') + printd("NULLCHAR\n"); } printd("'\n"); printd(" length: %d\n", s->length); @@ -61,7 +63,7 @@ Result parse(struct Slice *slices) if(token->text[0] == '(') { // todo check for null rest return readSeq(rest); - } else { // todo error on closed paren + } else { // todo error on missing close paren return result(parseAtom(token), rest); } } @@ -109,6 +111,7 @@ Object evalDefArgs(const Object *arg_forms, struct Environment *env) const Object *first_form = &arg_forms[0]; const char *name = first_form->name; + // Immediately adding the function to the env might allow recursion? Object second_eval = eval(first_form->forward, env); addToEnv(env, name, second_eval); @@ -125,8 +128,8 @@ Object evalIfArgs(const Object *arg_forms, struct Environment *env) Object evalLambdaArgs(const Object *arg_forms) { - // params // body - return constructLambda(arg_forms, arg_forms->forward); + // params // body + return constructLambda(arg_forms, arg_forms? arg_forms->forward : NULL); } Object evalMapArgs(const Object *arg_forms, struct Environment *env) @@ -137,14 +140,12 @@ Object evalMapArgs(const Object *arg_forms, struct Environment *env) const Object lambda = eval(&arg_forms[0], env); Object *oldList = (&arg_forms[0])->forward; - if(lambda.type != TYPE_LAMBDA || oldList->type != TYPE_LIST) { + if(lambda.type != TYPE_LAMBDA || oldList->type != TYPE_LIST) return errorObject(BAD_TYPE); - } Object list = listObject(); - int i = 0; - Object *oldElement = oldList->list; + const Object *oldElement = oldList->list; while(oldElement != NULL) { // Create a new list for each element in the list, // since lambda evaluation looks for a list @@ -152,7 +153,6 @@ Object evalMapArgs(const Object *arg_forms, struct Environment *env) Object listBack[1] = {*oldElement}; listBack[0].forward = NULL; tempList.list = listBack; - printObj(&tempList); struct Environment newEnv = envForLambda(&lambda.lambda->params, &tempList, env); @@ -192,7 +192,6 @@ struct Environment envForLambda(const Object *params, const Object *arg_forms, const char *n = itemAt(params, i)->name; addToEnv(&env, n, eval(&arg_forms[i], outer)); // May not need eval? // SHOULD BE `vs` not arg_forms??? - } // Something is segfaulting, anyway // env.strings[length] = NULL; @@ -432,6 +431,8 @@ struct Environment defaultEnv() { Object parseEval(const char *input, struct Environment *env) { struct Slice *tokens = nf_tokenize(input); + if(!tokens) + return errorObject(MISMATCHED_PARENS); #ifdef DEBUG struct Slice *debug = tokens; if(debug) { diff --git a/src/tokens.c b/src/tokens.c index 5bbfac1..c8eb88d 100644 --- a/src/tokens.c +++ b/src/tokens.c @@ -63,6 +63,10 @@ struct Slice *nf_tokenize(const char *input) parens++; } else if (input[i] == ')') { parens--; + if(parens < 0) { + free(slices); + return NULL; + } } if(isSingle(input[i])) {