remove type annotations from expressions for hscript consumption
This commit is contained in:
@@ -354,6 +354,26 @@ class Helpers {
|
||||
return def.withPosOf(exp);
|
||||
}
|
||||
|
||||
public static function removeTypeAnnotations(exp:ReaderExp):ReaderExp {
|
||||
var def = switch (exp.def) {
|
||||
case Symbol(_) | StrExp(_) | RawHaxe(_):
|
||||
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 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
|
||||
// position in source code
|
||||
public static function expBuilder(posRef:ReaderExp) {
|
||||
|
@@ -32,7 +32,8 @@ typedef KissState = {
|
||||
callAliases:Map<String, ReaderExpDef>,
|
||||
identAliases:Map<String, ReaderExpDef>,
|
||||
fields:Array<Field>,
|
||||
loadingDirectory:String
|
||||
loadingDirectory:String,
|
||||
hscript:Bool
|
||||
};
|
||||
|
||||
class Kiss {
|
||||
@@ -63,7 +64,8 @@ class Kiss {
|
||||
],
|
||||
identAliases: new Map(),
|
||||
fields: [],
|
||||
loadingDirectory: ""
|
||||
loadingDirectory: "",
|
||||
hscript: false
|
||||
};
|
||||
|
||||
// Helpful aliases
|
||||
@@ -156,6 +158,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 expr = switch (exp.def) {
|
||||
case Symbol(alias) if (k.identAliases.exists(alias)):
|
||||
readerExpToHaxeExpr(k.identAliases[alias].withPosOf(exp), k);
|
||||
|
@@ -23,10 +23,21 @@ class Main {
|
||||
static macro function macroMain():Expr {
|
||||
var k = Kiss.defaultKissState();
|
||||
k.wrapListExps = false;
|
||||
if (Sys.args().indexOf("--all") != -1) {
|
||||
var args = Sys.args();
|
||||
var pretty = args.indexOf("--pretty") != -1;
|
||||
k.hscript = args.indexOf("--hscript") != -1;
|
||||
|
||||
function print(s:String) {
|
||||
if (!pretty)
|
||||
s = s.replace("\n", " ");
|
||||
|
||||
Sys.println(s);
|
||||
}
|
||||
|
||||
if (args.indexOf("--all") != -1) {
|
||||
var kissInputStream = Stream.fromString(Sys.stdin().readAll().toString());
|
||||
Reader.readAndProcess(kissInputStream, k, (readerExp) -> {
|
||||
Sys.println(Kiss.readerExpToHaxeExpr(readerExp, k).toString());
|
||||
print(Kiss.readerExpToHaxeExpr(readerExp, k).toString());
|
||||
});
|
||||
} else {
|
||||
var line = "";
|
||||
@@ -47,7 +58,7 @@ class Main {
|
||||
|
||||
var kissInputStream = Stream.fromString(line);
|
||||
Reader.readAndProcess(kissInputStream, k, (readerExp) -> {
|
||||
Sys.println(Kiss.readerExpToHaxeExpr(readerExp, k).toString());
|
||||
print(Kiss.readerExpToHaxeExpr(readerExp, k).toString());
|
||||
});
|
||||
|
||||
line = "";
|
||||
|
@@ -276,14 +276,20 @@ class Prelude {
|
||||
return v;
|
||||
}
|
||||
|
||||
#if sys
|
||||
private static var kissProcess:Process = null;
|
||||
#end
|
||||
|
||||
/**
|
||||
* On Sys targets and nodejs, Kiss can be converted to hscript at runtime
|
||||
* NOTE on non-nodejs targets, after the first time calling this function,
|
||||
* it will be much faster
|
||||
* NOTE on non-nodejs sys targets, newlines in raw strings will be stripped away.
|
||||
* So don't use raw string literals in Kiss you want parsed and evaluated at runtime.
|
||||
*/
|
||||
public static function convertToHScript(kissStr:String):String {
|
||||
#if (!macro && hxnodejs)
|
||||
var kissProcess = ChildProcess.spawnSync("haxelib", ["run", "kiss", "--all"], {input: '${kissStr}\n'});
|
||||
var kissProcess = ChildProcess.spawnSync("haxelib", ["run", "kiss", "--all", "--hscript"], {input: '${kissStr}\n'});
|
||||
if (kissProcess.status != 0) {
|
||||
var error:String = kissProcess.stderr;
|
||||
throw 'failed to convert ${kissStr} to hscript: ${error}';
|
||||
@@ -291,13 +297,20 @@ class Prelude {
|
||||
var output:String = kissProcess.stdout;
|
||||
return output;
|
||||
#elseif sys
|
||||
var kissProcess = new Process("haxelib", ["run", "kiss"]);
|
||||
if (kissProcess == null)
|
||||
kissProcess = new Process("haxelib", ["run", "kiss", "--hscript"]);
|
||||
|
||||
kissProcess.stdin.writeString('${kissStr.replace("\n", " ")}\n');
|
||||
try {
|
||||
var output = kissProcess.stdout.readLine();
|
||||
if (output.startsWith(">>> ")) {
|
||||
output = output.substr(4);
|
||||
}
|
||||
return output;
|
||||
} catch (e) {
|
||||
var error = kissProcess.stderr.readAll().toString();
|
||||
throw 'failed to convert ${kissStr} to hscript: ${error}';
|
||||
}
|
||||
#else
|
||||
throw "Can't convert Kiss to HScript on this target.";
|
||||
#end
|
||||
|
Reference in New Issue
Block a user