multi-target convertToHScript function in Prelude
This commit is contained in:
@@ -5,6 +5,14 @@ using Std;
|
|||||||
import kiss.Operand;
|
import kiss.Operand;
|
||||||
import haxe.ds.Either;
|
import haxe.ds.Either;
|
||||||
import haxe.Constraints;
|
import haxe.Constraints;
|
||||||
|
#if (!macro && hxnodejs)
|
||||||
|
import js.node.ChildProcess;
|
||||||
|
import js.node.buffer.Buffer;
|
||||||
|
#elseif sys
|
||||||
|
import sys.io.Process;
|
||||||
|
#end
|
||||||
|
|
||||||
|
using StringTools;
|
||||||
|
|
||||||
/** What functions that process Lists should do when there are more elements than expected **/
|
/** What functions that process Lists should do when there are more elements than expected **/
|
||||||
enum ExtraElementHandling {
|
enum ExtraElementHandling {
|
||||||
@@ -267,4 +275,31 @@ class Prelude {
|
|||||||
#end
|
#end
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* On Sys targets and nodejs, Kiss can be converted to hscript at runtime
|
||||||
|
* 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'});
|
||||||
|
if (kissProcess.status != 0) {
|
||||||
|
var error:String = kissProcess.stderr;
|
||||||
|
throw 'failed to convert ${kissStr} to hscript: ${error}';
|
||||||
|
}
|
||||||
|
var output:String = kissProcess.stdout;
|
||||||
|
return output;
|
||||||
|
#elseif sys
|
||||||
|
var kissProcess = new Process("haxelib", ["run", "kiss"]);
|
||||||
|
kissProcess.stdin.writeString('${kissStr.replace("\n", " ")}\n');
|
||||||
|
var output = kissProcess.stdout.readLine();
|
||||||
|
if (output.startsWith(">>> ")) {
|
||||||
|
output = output.substr(4);
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
#else
|
||||||
|
throw "Can't convert Kiss to HScript on this target.";
|
||||||
|
#end
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -6,8 +6,6 @@ import kiss.Operand;
|
|||||||
import kiss.Stream;
|
import kiss.Stream;
|
||||||
import vscode.*;
|
import vscode.*;
|
||||||
import js.lib.Promise;
|
import js.lib.Promise;
|
||||||
import js.node.ChildProcess;
|
|
||||||
import js.node.buffer.Buffer;
|
|
||||||
import hscript.Parser;
|
import hscript.Parser;
|
||||||
import hscript.Interp;
|
import hscript.Interp;
|
||||||
import hscript.Expr;
|
import hscript.Expr;
|
||||||
|
@@ -58,17 +58,11 @@
|
|||||||
* Functionality
|
* Functionality
|
||||||
*/
|
*/
|
||||||
|
|
||||||
(defun :String convertToHScript [kissStr]
|
|
||||||
(let [kissProcess (ChildProcess.spawnSync "haxelib" ["run" "kiss" "--all"] (object input "${kissStr}\n"))]
|
|
||||||
(if !(= kissProcess.status 0)
|
|
||||||
{(errorMessage "failed to compile ${kissStr}: $(.toString (the Buffer .stderr kissProcess))") ""}
|
|
||||||
(.toString (the Buffer .stdout kissProcess)))))
|
|
||||||
|
|
||||||
(defun :Dynamic evalString [:String kissStr]
|
(defun :Dynamic evalString [:String kissStr]
|
||||||
(try
|
(try
|
||||||
(interp.execute
|
(interp.execute
|
||||||
(parser.parseString
|
(parser.parseString
|
||||||
(convertToHScript kissStr)))
|
(Prelude.convertToHScript kissStr)))
|
||||||
(catch [e]
|
(catch [e]
|
||||||
(errorMessage "Error `${e}` from $kissStr")
|
(errorMessage "Error `${e}` from $kissStr")
|
||||||
null)))
|
null)))
|
||||||
|
Reference in New Issue
Block a user