Make previous changes more compiler-safe
This commit is contained in:
@@ -3,6 +3,7 @@ package kiss;
|
|||||||
import kiss.ReaderExp;
|
import kiss.ReaderExp;
|
||||||
using kiss.Helpers;
|
using kiss.Helpers;
|
||||||
using kiss.Reader;
|
using kiss.Reader;
|
||||||
|
using kiss.ExpBuilder;
|
||||||
|
|
||||||
class ExpBuilder {
|
class ExpBuilder {
|
||||||
// Return convenient functions for succinctly making new ReaderExps that link back to an original exp's
|
// 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),
|
haxeExpr: (e:haxe.macro.Expr) -> Helpers.withMacroPosOf(e.expr, posRef),
|
||||||
|
#end
|
||||||
none: () -> None.withPosOf(posRef)
|
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');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
package kiss;
|
package kiss;
|
||||||
|
#if macro
|
||||||
|
|
||||||
import haxe.macro.Expr;
|
import haxe.macro.Expr;
|
||||||
import haxe.macro.Context;
|
import haxe.macro.Context;
|
||||||
@@ -435,30 +436,7 @@ class Helpers {
|
|||||||
Throw a KissError if the given expression has the wrong number of arguments
|
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) {
|
public static function checkNumArgs(wholeExp:ReaderExp, min:Null<Int>, max:Null<Int>, ?expectedForm:String) {
|
||||||
if (expectedForm == null) {
|
ExpBuilder.checkNumArgs(wholeExp, min, max, expectedForm);
|
||||||
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');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function removeTypeAnnotations(exp:ReaderExp):ReaderExp {
|
public static function removeTypeAnnotations(exp:ReaderExp):ReaderExp {
|
||||||
@@ -861,3 +839,5 @@ class Helpers {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#end
|
||||||
@@ -26,6 +26,7 @@ class KissError {
|
|||||||
return new KissError([exp], message);
|
return new KissError([exp], message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if macro
|
||||||
public static function fromExpStr(pos:Position, expStr:String, message:String) {
|
public static function fromExpStr(pos:Position, expStr:String, message:String) {
|
||||||
switch (Reader.read(Stream.fromString(expStr), Kiss.defaultKissState())) {
|
switch (Reader.read(Stream.fromString(expStr), Kiss.defaultKissState())) {
|
||||||
case Some(exp):
|
case Some(exp):
|
||||||
@@ -34,6 +35,7 @@ class KissError {
|
|||||||
throw 'bad'; // TODO better message
|
throw 'bad'; // TODO better message
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#end
|
||||||
|
|
||||||
public static function fromArgs(exps:Array<ReaderExp>, message:String) {
|
public static function fromArgs(exps:Array<ReaderExp>, message:String) {
|
||||||
return new KissError(exps, message);
|
return new KissError(exps, message);
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
package kiss;
|
package kiss;
|
||||||
|
#if macro
|
||||||
|
|
||||||
import haxe.macro.Expr;
|
import haxe.macro.Expr;
|
||||||
import haxe.macro.Context;
|
import haxe.macro.Context;
|
||||||
@@ -1708,3 +1709,5 @@ class Macros {
|
|||||||
macros[formName] = cond;
|
macros[formName] = cond;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#end
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package kiss;
|
package kiss;
|
||||||
|
|
||||||
|
#if macro
|
||||||
import haxe.macro.Expr;
|
import haxe.macro.Expr;
|
||||||
import haxe.macro.Context;
|
import haxe.macro.Context;
|
||||||
import kiss.Reader;
|
import kiss.Reader;
|
||||||
@@ -799,3 +800,5 @@ class SpecialForms {
|
|||||||
return b.haxeExpr(m);
|
return b.haxeExpr(m);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#end
|
||||||
Reference in New Issue
Block a user