Make previous changes more compiler-safe

This commit is contained in:
2024-12-10 20:02:51 -06:00
parent 4a457f57a7
commit 70464e4bce
5 changed files with 42 additions and 24 deletions

View File

@@ -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<Int>, max:Null<Int>, ?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');
}
}
}

View File

@@ -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<Int>, max:Null<Int>, ?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

View File

@@ -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<ReaderExp>, message:String) {
return new KissError(exps, message);

View File

@@ -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

View File

@@ -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