instead of removing type annotations from hscript, allow them in the parser
This commit is contained in:
@@ -337,7 +337,9 @@ class Helpers {
|
|||||||
#if macrotest
|
#if macrotest
|
||||||
Prelude.print("Compile-time hscript: " + code);
|
Prelude.print("Compile-time hscript: " + code);
|
||||||
#end
|
#end
|
||||||
|
// Need parser external to the KissInterp to wrap parsing in an informative try-catch
|
||||||
var parser = new Parser();
|
var parser = new Parser();
|
||||||
|
parser.allowTypes = true;
|
||||||
if (interps.length == 0) {
|
if (interps.length == 0) {
|
||||||
var interp = new KissInterp();
|
var interp = new KissInterp();
|
||||||
interp.variables.set("read", Reader.assertRead.bind(_, k));
|
interp.variables.set("read", Reader.assertRead.bind(_, k));
|
||||||
@@ -484,28 +486,6 @@ class Helpers {
|
|||||||
return def.withPosOf(exp);
|
return def.withPosOf(exp);
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
||||||
// position in source code
|
// position in source code
|
||||||
public static function expBuilder(posRef:ReaderExp) {
|
public static function expBuilder(posRef:ReaderExp) {
|
||||||
|
@@ -283,8 +283,6 @@ class Kiss {
|
|||||||
// Bind the table arguments of this function for easy recursive calling/passing
|
// Bind the table arguments of this function for easy recursive calling/passing
|
||||||
var convert = readerExpToHaxeExpr.bind(_, k);
|
var convert = readerExpToHaxeExpr.bind(_, k);
|
||||||
|
|
||||||
if (k.hscript)
|
|
||||||
exp = Helpers.removeTypeAnnotations(exp);
|
|
||||||
|
|
||||||
var none = EBlock([]).withMacroPosOf(exp);
|
var none = EBlock([]).withMacroPosOf(exp);
|
||||||
|
|
||||||
|
@@ -19,6 +19,8 @@ class KissInterp extends Interp {
|
|||||||
public function new(nullForUnknownVar = false) {
|
public function new(nullForUnknownVar = false) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
|
parser.allowTypes = true;
|
||||||
|
|
||||||
this.nullForUnknownVar = nullForUnknownVar;
|
this.nullForUnknownVar = nullForUnknownVar;
|
||||||
|
|
||||||
variables.set("Reflect", Reflect);
|
variables.set("Reflect", Reflect);
|
||||||
|
@@ -130,7 +130,6 @@ class Macros {
|
|||||||
var thenExp = exps.shift();
|
var thenExp = exps.shift();
|
||||||
var elseExp = if (exps.length > 0) exps.shift(); else b.none();
|
var elseExp = if (exps.length > 0) exps.shift(); else b.none();
|
||||||
|
|
||||||
var parser = new Parser();
|
|
||||||
var conditionInterp = new KissInterp(true);
|
var conditionInterp = new KissInterp(true);
|
||||||
var conditionStr = Reader.toString(conditionExp.def);
|
var conditionStr = Reader.toString(conditionExp.def);
|
||||||
for (flag => value in Context.getDefines()) {
|
for (flag => value in Context.getDefines()) {
|
||||||
@@ -144,8 +143,7 @@ class Macros {
|
|||||||
#if test
|
#if test
|
||||||
Prelude.print("#if condition hscript: " + hscriptStr);
|
Prelude.print("#if condition hscript: " + hscriptStr);
|
||||||
#end
|
#end
|
||||||
var conditionHScript = parser.parseString(hscriptStr);
|
return if (Prelude.truthy(conditionInterp.evalHaxe(hscriptStr))) {
|
||||||
return if (Prelude.truthy(conditionInterp.execute(conditionHScript))) {
|
|
||||||
#if test
|
#if test
|
||||||
Prelude.print("using thenExp");
|
Prelude.print("using thenExp");
|
||||||
#end
|
#end
|
||||||
@@ -186,7 +184,6 @@ class Macros {
|
|||||||
|
|
||||||
var caseExp = b.callSymbol("case", caseArgs);
|
var caseExp = b.callSymbol("case", caseArgs);
|
||||||
|
|
||||||
var parser = new Parser();
|
|
||||||
var caseInterp = new KissInterp();
|
var caseInterp = new KissInterp();
|
||||||
var caseStr = Reader.toString(caseExp.def);
|
var caseStr = Reader.toString(caseExp.def);
|
||||||
for (matchBodySymbol in matchBodySymbols) {
|
for (matchBodySymbol in matchBodySymbols) {
|
||||||
@@ -201,8 +198,7 @@ class Macros {
|
|||||||
#if test
|
#if test
|
||||||
Prelude.print("#case hscript: " + hscriptStr);
|
Prelude.print("#case hscript: " + hscriptStr);
|
||||||
#end
|
#end
|
||||||
var caseHScript = parser.parseString(hscriptStr);
|
return caseInterp.evalHaxe(caseStr);
|
||||||
return caseInterp.execute(caseHScript);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw CompileError.fromExp(caseExp, '#case evaluation threw error $e');
|
throw CompileError.fromExp(caseExp, '#case evaluation threw error $e');
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user