From 79ffe32866cd6f939b4569992d1d3632a5c65252 Mon Sep 17 00:00:00 2001 From: kappa Date: Tue, 5 Feb 2019 20:08:25 +0100 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D1=81=D1=82=D0=B0=D0=B2=D1=99?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=B3=D1=80=D0=B0=D0=BD=D0=B8=D1=86=D0=B0?= =?UTF-8?q?=20=D1=80=D0=B5=D0=BA=D1=83=D1=80=D0=B7=D0=B8=D1=98=D0=B5=20?= =?UTF-8?q?=D0=B7=D0=B0=20=D1=81=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=B5=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D1=86=D0=B5=D0=B4=D1=83=D1=80=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- eval.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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; }