From 70464e4bce59e5d559d3c2366a3f7783a29d8b8f Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Tue, 10 Dec 2024 20:02:51 -0600 Subject: [PATCH] Make previous changes more compiler-safe --- src/kiss/ExpBuilder.hx | 30 ++++++++++++++++++++++++++++++ src/kiss/Helpers.hx | 28 ++++------------------------ src/kiss/KissError.hx | 2 ++ src/kiss/Macros.hx | 3 +++ src/kiss/SpecialForms.hx | 3 +++ 5 files changed, 42 insertions(+), 24 deletions(-) diff --git a/src/kiss/ExpBuilder.hx b/src/kiss/ExpBuilder.hx index 03ca133..bfbfc0a 100644 --- a/src/kiss/ExpBuilder.hx +++ b/src/kiss/ExpBuilder.hx @@ -3,6 +3,7 @@ package kiss; import kiss.ReaderExp; using kiss.Helpers; using kiss.Reader; +using kiss.ExpBuilder; class ExpBuilder { // Return convenient functions for succinctly making new ReaderExps that link back to an original exp's @@ -126,8 +127,37 @@ class ExpBuilder { ]) ]); }, + #if macro haxeExpr: (e:haxe.macro.Expr) -> Helpers.withMacroPosOf(e.expr, posRef), + #end none: () -> None.withPosOf(posRef) }; } + + public static function checkNumArgs(wholeExp:ReaderExp, min:Null, max:Null, ?expectedForm:String) { + if (expectedForm == null) { + expectedForm = if (max == min) { + '$min arguments'; + } else if (max == null) { + 'at least $min arguments'; + } else if (min == null) { + 'no more than $max arguments'; + } else if (min == null && max == null) { + throw 'checkNumArgs() needs a min or a max'; + } else { + 'between $min and $max arguments'; + }; + } + + var args = switch (wholeExp.def) { + case CallExp(_, args): args; + default: throw KissError.fromExp(wholeExp, "Can only check number of args in a CallExp"); + }; + + if (min != null && args.length < min) { + throw KissError.fromExp(wholeExp, 'Not enough arguments. Expected $expectedForm'); + } else if (max != null && args.length > max) { + throw KissError.fromExp(wholeExp, 'Too many arguments. Expected $expectedForm'); + } + } } diff --git a/src/kiss/Helpers.hx b/src/kiss/Helpers.hx index febb49e..70c2ef3 100644 --- a/src/kiss/Helpers.hx +++ b/src/kiss/Helpers.hx @@ -1,4 +1,5 @@ package kiss; +#if macro import haxe.macro.Expr; import haxe.macro.Context; @@ -435,30 +436,7 @@ class Helpers { Throw a KissError if the given expression has the wrong number of arguments **/ public static function checkNumArgs(wholeExp:ReaderExp, min:Null, max:Null, ?expectedForm:String) { - if (expectedForm == null) { - expectedForm = if (max == min) { - '$min arguments'; - } else if (max == null) { - 'at least $min arguments'; - } else if (min == null) { - 'no more than $max arguments'; - } else if (min == null && max == null) { - throw 'checkNumArgs() needs a min or a max'; - } else { - 'between $min and $max arguments'; - }; - } - - var args = switch (wholeExp.def) { - case CallExp(_, args): args; - default: throw KissError.fromExp(wholeExp, "Can only check number of args in a CallExp"); - }; - - if (min != null && args.length < min) { - throw KissError.fromExp(wholeExp, 'Not enough arguments. Expected $expectedForm'); - } else if (max != null && args.length > max) { - throw KissError.fromExp(wholeExp, 'Too many arguments. Expected $expectedForm'); - } + ExpBuilder.checkNumArgs(wholeExp, min, max, expectedForm); } public static function removeTypeAnnotations(exp:ReaderExp):ReaderExp { @@ -861,3 +839,5 @@ class Helpers { }; } } + +#end \ No newline at end of file diff --git a/src/kiss/KissError.hx b/src/kiss/KissError.hx index a906c2b..dc969c6 100644 --- a/src/kiss/KissError.hx +++ b/src/kiss/KissError.hx @@ -26,6 +26,7 @@ class KissError { return new KissError([exp], message); } + #if macro public static function fromExpStr(pos:Position, expStr:String, message:String) { switch (Reader.read(Stream.fromString(expStr), Kiss.defaultKissState())) { case Some(exp): @@ -34,6 +35,7 @@ class KissError { throw 'bad'; // TODO better message } } + #end public static function fromArgs(exps:Array, message:String) { return new KissError(exps, message); diff --git a/src/kiss/Macros.hx b/src/kiss/Macros.hx index 825952f..e1d9de0 100644 --- a/src/kiss/Macros.hx +++ b/src/kiss/Macros.hx @@ -1,4 +1,5 @@ package kiss; +#if macro import haxe.macro.Expr; import haxe.macro.Context; @@ -1708,3 +1709,5 @@ class Macros { macros[formName] = cond; } } + +#end \ No newline at end of file diff --git a/src/kiss/SpecialForms.hx b/src/kiss/SpecialForms.hx index e173a0f..2f9a8ea 100644 --- a/src/kiss/SpecialForms.hx +++ b/src/kiss/SpecialForms.hx @@ -1,5 +1,6 @@ package kiss; +#if macro import haxe.macro.Expr; import haxe.macro.Context; import kiss.Reader; @@ -799,3 +800,5 @@ class SpecialForms { return b.haxeExpr(m); }; } + +#end \ No newline at end of file