From f3196f4f422584f9444664141dd3ae33b697a34c Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Sun, 8 Aug 2021 11:42:24 -0600 Subject: [PATCH] (macroDepth) and generativetestcase depth limit --- src/kiss/Helpers.hx | 1 + src/test/cases/GenerativeTestCase.kiss | 62 +++++++++++++++----------- 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/src/kiss/Helpers.hx b/src/kiss/Helpers.hx index 7ccf9d0..90b7462 100644 --- a/src/kiss/Helpers.hx +++ b/src/kiss/Helpers.hx @@ -337,6 +337,7 @@ class Helpers { } // This is kind of a big deal: interp.variables.set("eval", Helpers.runAtCompileTimeDynamic.bind(_, k)); + interp.variables.set("macroDepth", () -> interps.length); interps.push(interp); } else { diff --git a/src/test/cases/GenerativeTestCase.kiss b/src/test/cases/GenerativeTestCase.kiss index 00bc052..7cb08df 100644 --- a/src/test/cases/GenerativeTestCase.kiss +++ b/src/test/cases/GenerativeTestCase.kiss @@ -1,6 +1,7 @@ (defMacroVar maxInt 1000000000) (defMacroVar maxStringLength 20) (defMacroVar maxExps 4) +(defMacroVar maxDepth 15) (defMacroFunction _macroList [func length] (for _ (range length) (func))) @@ -22,37 +23,46 @@ (defMacroFunction _randomFalsyExp [] ((chooseRandom - [ - ->{`null} - ->{`false} - ->{`""} - ->{`[]} - ->{`(or ,@(_randomLengthMacroList _randomFalsyExp))} - ->{`(and - ,@(_randomLengthMacroList _randomUncertainExp) - ,(_randomFalsyExp) - ,@(_randomLengthMacroList _randomUncertainExp))} - ]))) + (concat + [ + ->{`null} + ->{`false} + ->{`""} + ->{`[]} + ] + (if (< (macroDepth) maxDepth) + [ + ->{`(or ,@(_randomLengthMacroList _randomFalsyExp))} + ->{`(and + ,@(_randomLengthMacroList _randomUncertainExp) + ,(_randomFalsyExp) + ,@(_randomLengthMacroList _randomUncertainExp))} + ] + []))))) (defMacro randomFalsyExp [] (printExp (_randomFalsyExp) "Falsy")) (defMacroFunction _randomTruthyExp [] ((chooseRandom - [ - ->{`true} - ->(_randomLetterString) - ->(_randomInt) - ->(_randomFloat) - // These cause stack overflows because it's hard to implement a recursion limit at macro time - /*->{`[,@(_randomLengthMacroList _randomLetterString)]} - ->{`[,@(_randomLengthMacroList _randomInt)]} - ->{`[,@(_randomLengthMacroList _randomFloat)]} - ->{`(and ,@(_randomLengthMacroList _randomTruthyExp))} - ->{`(or - ,@(_randomLengthMacroList _randomUncertainExp) - ,(_randomTruthyExp) - ,@(_randomLengthMacroList _randomUncertainExp))}*/ - ]))) + (concat + [ + ->{`true} + ->(_randomLetterString) + ->(_randomInt) + ->(_randomFloat) + ] + (if (< (macroDepth) maxDepth) + [ + ->{`[,@(_randomLengthMacroList _randomLetterString)]} + ->{`[,@(_randomLengthMacroList _randomInt)]} + ->{`[,@(_randomLengthMacroList _randomFloat)]} + ->{`(and ,@(_randomLengthMacroList _randomTruthyExp))} + ->{`(or + ,@(_randomLengthMacroList _randomUncertainExp) + ,(_randomTruthyExp) + ,@(_randomLengthMacroList _randomUncertainExp))} + ] + []))))) (defMacro randomTruthyExp [] (printExp (_randomTruthyExp) "Truthy"))