multi-target convertToHScript function in Prelude

This commit is contained in:
2021-05-18 15:35:48 -06:00
parent 0989aa06d3
commit dfba66b900
3 changed files with 36 additions and 9 deletions

View File

@@ -5,6 +5,14 @@ using Std;
import kiss.Operand;
import haxe.ds.Either;
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 **/
enum ExtraElementHandling {
@@ -267,4 +275,31 @@ class Prelude {
#end
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
}
}

View File

@@ -6,8 +6,6 @@ import kiss.Operand;
import kiss.Stream;
import vscode.*;
import js.lib.Promise;
import js.node.ChildProcess;
import js.node.buffer.Buffer;
import hscript.Parser;
import hscript.Interp;
import hscript.Expr;

View File

@@ -58,17 +58,11 @@
* 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]
(try
(interp.execute
(parser.parseString
(convertToHScript kissStr)))
(Prelude.convertToHScript kissStr)))
(catch [e]
(errorMessage "Error `${e}` from $kissStr")
null)))