auto-add missing cc calls in AsyncEmbeddedScript2

This commit is contained in:
2024-02-10 11:32:24 -07:00
parent 55770fc848
commit dd71476626
3 changed files with 49 additions and 2 deletions

View File

@@ -4,6 +4,7 @@ package kiss;
import haxe.macro.Expr;
import haxe.macro.Context;
import haxe.macro.PositionTools;
using haxe.macro.ExprTools;
import sys.io.File;
import haxe.io.Path;
using haxe.io.Path;
@@ -103,6 +104,8 @@ class AsyncEmbeddedScript2 {
public var lastInstructionPointer(default,null):Int = -1;
private var labels:Map<String,Int> = [];
public var autoCC(default,default):Bool = true;
private var parser = new Parser();
private var interp:ObjectInterp2<AsyncEmbeddedScript2>;
public var interpVariables(get, null):Map<String,Dynamic>;
@@ -477,12 +480,35 @@ class AsyncEmbeddedScript2 {
return;
default:
}
var exprString = Reader.toString(nextExp.def);
var fieldCount = k.fieldList.length;
var expr = Kiss.readerExpToHaxeExpr(nextExp, k);
if (expr == null || Kiss.isEmpty(expr))
return;
// Detect whether the scripter forgot to reference cc. If they did, insert a cc call.
var referencesCC = false;
function checkSubExps(exp: Expr) {
switch (exp.expr) {
case EConst(CIdent("cc")):
referencesCC = true;
default:
exp.iter(checkSubExps);
}
}
expr.iter(checkSubExps);
if (!referencesCC) {
expr = macro {
$expr;
if (autoCC) {
cc();
}
};
}
expr = macro { if (printCurrentInstruction) Prelude.print($v{exprString}); $expr; };
expr = expr.expr.withMacroPosOf(nextExp);
if (expr != null) {

View File

@@ -0,0 +1,3 @@
(prop &mut finished null)
(set finished false)
(set finished true)

View File

@@ -4,6 +4,7 @@ import utest.Test;
import utest.Assert;
import kiss.EmbeddedScript;
import kiss.AsyncEmbeddedScript;
import kiss.AsyncEmbeddedScript2;
import kiss.Prelude;
import kiss.FuzzyMap;
import kiss.FuzzyMapTools;
@@ -38,6 +39,18 @@ class DSLTestCase extends Test {
Assert.isTrue(script2.wholeScriptDone);
}
#end
function testAsyncAutoCC() {
var scriptWithAutoCC = new AsyncDSLScriptWithAutoCC();
var scriptWithoutAutoCC = new AsyncDSLScriptWithAutoCC();
scriptWithoutAutoCC.autoCC = false;
scriptWithAutoCC.run();
scriptWithoutAutoCC.run();
Assert.isTrue(scriptWithAutoCC.finished);
Assert.isFalse(scriptWithoutAutoCC.finished);
}
}
@:build(kiss.EmbeddedScript.build("DSL.kiss", "DSLScript.dsl"))
@@ -53,4 +66,9 @@ class AsyncDSLScript extends AsyncEmbeddedScript {}
class AsyncDSLScriptThatWillCache extends AsyncEmbeddedScript {}
@:build(kiss.AsyncEmbeddedScript.build("", "DSL.kiss", "AsyncDSLScriptThatWillCache.dsl"))
class AsyncDSLScriptThatWillCache2 extends AsyncEmbeddedScript {}
class AsyncDSLScriptThatWillCache2 extends AsyncEmbeddedScript {}
// Auto-call cc when the scripter forgets to:
@:build(kiss.AsyncEmbeddedScript2.build("", "DSL.kiss", "AsyncDSLScriptWithAutoCC.dsl"))
class AsyncDSLScriptWithAutoCC extends AsyncEmbeddedScript2 {}