From 2ac8aa7b1c7e64af86214a5eda680cd8e8b254c2 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Sat, 18 Mar 2023 14:57:52 -0600 Subject: [PATCH] Macro-time permissive Helpers.argList() and bindingList() --- src/kiss/Helpers.hx | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/kiss/Helpers.hx b/src/kiss/Helpers.hx index 003b03c..64df155 100644 --- a/src/kiss/Helpers.hx +++ b/src/kiss/Helpers.hx @@ -539,7 +539,11 @@ class Helpers { interp.variables.set("eval", innerRunAtCompileTimeDynamic); interp.variables.set("Helpers", { evalUnquotes: evalUnquotes.bind(_, innerRunAtCompileTime), - runAtCompileTime: innerRunAtCompileTime + runAtCompileTime: innerRunAtCompileTime, + // TODO it is bad that Helpers functions have to manually be included here: + explicitTypeString: Helpers.explicitTypeString, + argList: Helpers.argList, + bindingList: Helpers.bindingList }); interp.variables.set("__interp__", interp); @@ -777,6 +781,12 @@ class Helpers { public static function argList(exp:ReaderExp, forThis:String, allowEmpty = true):Array { return switch (exp.def) { + // At macro-time, a list of exps could be passed instead of a ListExp. Handle + // that tricky case: + case null if (Std.isOfType(exp, Array)): + var expList = cast(exp, Array); + var expDynamic:Dynamic = exp; + argList({pos:expList[0].pos, def: ListExp(expDynamic)}, forThis, allowEmpty); case ListExp([]) if (allowEmpty): []; case ListExp([]) if (!allowEmpty): @@ -784,16 +794,22 @@ class Helpers { case ListExp(argExps): argExps; default: - throw KissError.fromExp(exp, '$forThis arg list should be a list expression'); + throw KissError.fromExp(exp, '$forThis arg list should be a list or list expression'); }; } public static function bindingList(exp:ReaderExp, forThis:String, allowEmpty = false):Array { return switch (exp.def) { + // At macro-time, a list of exps could be passed instead of a ListExp. Handle + // that tricky case: + case null if (Std.isOfType(exp, Array)): + var expList = cast(exp, Array); + var expDynamic:Dynamic = exp; + bindingList({pos:expList[0].pos, def: ListExp(expDynamic)}, forThis, allowEmpty); case ListExp(bindingExps) if ((allowEmpty || bindingExps.length > 0) && bindingExps.length % 2 == 0): bindingExps; default: - throw KissError.fromExp(exp, '$forThis bindings should be a list expression with an even number of sub expressions (at least 2)'); + throw KissError.fromExp(exp, '$forThis bindings should be a list or list expression with an even number of sub expressions (at least 2)'); }; } }