allow catch in defMacro without :Dynamic
This commit is contained in:
@@ -21,6 +21,7 @@ using kiss.Reader;
|
||||
using kiss.Helpers;
|
||||
using kiss.Kiss;
|
||||
using StringTools;
|
||||
using haxe.macro.ExprTools;
|
||||
|
||||
/**
|
||||
* Compile-time helper functions for Kiss. Don't import or reference these at runtime.
|
||||
@@ -332,14 +333,58 @@ class Helpers {
|
||||
// When we ARE running at compiletime already, the pre-existing interp will be used
|
||||
static var interps:kiss.List<Interp> = [];
|
||||
|
||||
public static function removeTypeAnnotations(exp:ReaderExp):ReaderExp {
|
||||
var def = switch (exp.def) {
|
||||
case Symbol(_) | StrExp(_) | RawHaxe(_) | Quasiquote(_):
|
||||
exp.def;
|
||||
case CallExp(func, callArgs):
|
||||
CallExp(removeTypeAnnotations(func), callArgs.map(removeTypeAnnotations));
|
||||
case ListExp(elements):
|
||||
ListExp(elements.map(removeTypeAnnotations));
|
||||
case TypedExp(type, innerExp):
|
||||
innerExp.def;
|
||||
case MetaExp(meta, innerExp):
|
||||
MetaExp(meta, removeTypeAnnotations(innerExp));
|
||||
case FieldExp(field, innerExp):
|
||||
FieldExp(field, removeTypeAnnotations(innerExp));
|
||||
case KeyValueExp(keyExp, valueExp):
|
||||
KeyValueExp(removeTypeAnnotations(keyExp), removeTypeAnnotations(valueExp));
|
||||
default:
|
||||
throw CompileError.fromExp(exp, 'cannot remove type annotations');
|
||||
};
|
||||
return def.withPosOf(exp);
|
||||
}
|
||||
// hscript.Interp is very finicky about some edge cases.
|
||||
// This function handles them
|
||||
private static function mapForInterp(expr:Expr):Expr {
|
||||
return expr.map(subExp -> {
|
||||
switch (subExp.expr) {
|
||||
case ETry(e, catches):
|
||||
catches = [for (c in catches) {
|
||||
// hscript.Parser expects :Dynamic after the catch varname
|
||||
{
|
||||
type: Helpers.parseComplexType("Dynamic"),
|
||||
name: c.name,
|
||||
expr: c.expr
|
||||
};
|
||||
}];
|
||||
{
|
||||
pos: subExp.pos,
|
||||
expr: ETry(e, catches)
|
||||
};
|
||||
default: subExp;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static function runAtCompileTimeDynamic(exp:ReaderExp, k:KissState, ?args:Map<String, Dynamic>):Dynamic {
|
||||
var code = k.forHScript().convert(exp).toString(); // tink_macro to the rescue
|
||||
var hscriptExp = mapForInterp(k.forHScript().convert(exp));
|
||||
var code = hscriptExp.toString(); // tink_macro to the rescue
|
||||
#if macrotest
|
||||
Prelude.print("Compile-time hscript: " + code);
|
||||
#end
|
||||
// Need parser external to the KissInterp to wrap parsing in an informative try-catch
|
||||
var parser = new Parser();
|
||||
parser.allowTypes = true;
|
||||
if (interps.length == 0) {
|
||||
var interp = new KissInterp();
|
||||
interp.variables.set("read", Reader.assertRead.bind(_, k));
|
||||
|
@@ -283,6 +283,9 @@ class Kiss {
|
||||
// Bind the table arguments of this function for easy recursive calling/passing
|
||||
var convert = readerExpToHaxeExpr.bind(_, k);
|
||||
|
||||
if (k.hscript)
|
||||
exp = Helpers.removeTypeAnnotations(exp);
|
||||
|
||||
var none = EBlock([]).withMacroPosOf(exp);
|
||||
|
||||
var expr = switch (exp.def) {
|
||||
|
@@ -2,8 +2,11 @@ package kiss;
|
||||
|
||||
import hscript.Parser;
|
||||
import hscript.Interp;
|
||||
import hscript.Expr;
|
||||
import kiss.Prelude;
|
||||
|
||||
using hscript.Tools;
|
||||
|
||||
/**
|
||||
* Specialized hscript interpreter for hscript generated from Kiss expressions.
|
||||
* When macrotest is defined by the compiler, many functions run without
|
||||
@@ -19,8 +22,6 @@ class KissInterp extends Interp {
|
||||
public function new(nullForUnknownVar = false) {
|
||||
super();
|
||||
|
||||
parser.allowTypes = true;
|
||||
|
||||
this.nullForUnknownVar = nullForUnknownVar;
|
||||
|
||||
variables.set("Reflect", Reflect);
|
||||
|
@@ -198,7 +198,7 @@ class Macros {
|
||||
#if test
|
||||
Prelude.print("#case hscript: " + hscriptStr);
|
||||
#end
|
||||
return caseInterp.evalHaxe(caseStr);
|
||||
return caseInterp.evalHaxe(hscriptStr);
|
||||
} catch (e) {
|
||||
throw CompileError.fromExp(caseExp, '#case evaluation threw error $e');
|
||||
}
|
||||
|
@@ -44,4 +44,8 @@ class MacroTestCase extends Test {
|
||||
function testSetMacroVar() {
|
||||
_testSetMacroVar();
|
||||
}
|
||||
|
||||
function testTryCatchWithoutDynamic () {
|
||||
_testTryCatchWithoutDynamic();
|
||||
}
|
||||
}
|
||||
|
@@ -90,3 +90,9 @@
|
||||
|
||||
(function _testIfLetDisabled []
|
||||
(_testIfLetDisabledMacro))
|
||||
|
||||
(defMacro _testTryCatchWithoutDynamicMacro []
|
||||
(try (throw "intended") (catch [e] (ReaderExp.StrExp e))))
|
||||
|
||||
(function _testTryCatchWithoutDynamic []
|
||||
(Assert.equals "intended" (_testTryCatchWithoutDynamicMacro)))
|
Reference in New Issue
Block a user