diff --git a/eval.c b/eval.c index 60b7ade..507e773 100644 --- a/eval.c +++ b/eval.c @@ -5,6 +5,9 @@ #include "util.h" #include "internals.h" +int currentRecursionDepth = 0; +#define MAXRECURSIONDEPTH 1000 + object apply(object function, object parameters, env currentEnv); object eval(object input, env currentEnv) @@ -97,6 +100,11 @@ object apply(object procedure, object parameters, env currentEnv) return result; } + if (++currentRecursionDepth > MAXRECURSIONDEPTH) + { + --currentRecursionDepth; + SIGERR(maxRecursionDepthError); + } object args = copyObject(PROC_COMP_ARGS(procedure)); object body = copyObject(PROC_COMP_BODY(procedure)); env definitionEnv = PROC_COMP_ENV(procedure); @@ -123,13 +131,14 @@ object apply(object procedure, object parameters, env currentEnv) CAR(*currentSubProc) = eval(CAR(*currentSubProc), procEnv); if (TYPE(CDR(*currentSubProc)) == nilObject) { - result = CAR(*currentSubProc); + result = copyObject(CAR(*currentSubProc)); } currentSubProc = &CDR(*currentSubProc); } deleteObject(args); deleteObject(body); removeEnvironment(procEnv); + --currentRecursionDepth; return result; }