From dd71476626b762dc9d66b5c139b0f1e38de6900a Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Sat, 10 Feb 2024 11:32:24 -0700 Subject: [PATCH] auto-add missing cc calls in AsyncEmbeddedScript2 --- src/kiss/AsyncEmbeddedScript2.hx | 28 ++++++++++++++++++++- src/test/cases/AsyncDSLScriptWithAutoCC.dsl | 3 +++ src/test/cases/DSLTestCase.hx | 20 ++++++++++++++- 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 src/test/cases/AsyncDSLScriptWithAutoCC.dsl diff --git a/src/kiss/AsyncEmbeddedScript2.hx b/src/kiss/AsyncEmbeddedScript2.hx index 136b764..78a15d9 100644 --- a/src/kiss/AsyncEmbeddedScript2.hx +++ b/src/kiss/AsyncEmbeddedScript2.hx @@ -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 = []; + public var autoCC(default,default):Bool = true; + private var parser = new Parser(); private var interp:ObjectInterp2; public var interpVariables(get, null):Map; @@ -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) { diff --git a/src/test/cases/AsyncDSLScriptWithAutoCC.dsl b/src/test/cases/AsyncDSLScriptWithAutoCC.dsl new file mode 100644 index 0000000..c24fa1f --- /dev/null +++ b/src/test/cases/AsyncDSLScriptWithAutoCC.dsl @@ -0,0 +1,3 @@ +(prop &mut finished null) +(set finished false) +(set finished true) \ No newline at end of file diff --git a/src/test/cases/DSLTestCase.hx b/src/test/cases/DSLTestCase.hx index 8d7866d..f170fcc 100644 --- a/src/test/cases/DSLTestCase.hx +++ b/src/test/cases/DSLTestCase.hx @@ -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 {} \ No newline at end of file +class AsyncDSLScriptThatWillCache2 extends AsyncEmbeddedScript {} + +// Auto-call cc when the scripter forgets to: + +@:build(kiss.AsyncEmbeddedScript2.build("", "DSL.kiss", "AsyncDSLScriptWithAutoCC.dsl")) +class AsyncDSLScriptWithAutoCC extends AsyncEmbeddedScript2 {} \ No newline at end of file