From 3626828ceb96dedb118a2c4f8ff12a81006327dd Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Mon, 22 Mar 2021 01:45:19 +0100 Subject: [PATCH] decided --- .../src/prog8/ast/statements/AstStatements.kt | 5 ++-- compilerAst/src/prog8/ast/walk/AstWalker.kt | 15 +++++++++-- examples/test.p8 | 27 ++++++++++--------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/compilerAst/src/prog8/ast/statements/AstStatements.kt b/compilerAst/src/prog8/ast/statements/AstStatements.kt index 3152acb22..800072f2b 100644 --- a/compilerAst/src/prog8/ast/statements/AstStatements.kt +++ b/compilerAst/src/prog8/ast/statements/AstStatements.kt @@ -227,8 +227,9 @@ open class VarDecl(val type: VarDeclType, } override fun replaceChildNode(node: Node, replacement: Node) { - // TODO the check that node===value is too strict sometimes, but leaving it out allows for bugs to creep through ... :( Perhaps check when adding the replace if there is already a replace on the same node? - require(replacement is Expression) + require(replacement is Expression && node===value) + // NOTE: ideally you also want to check that node===value but this sometimes crashes the optimizer when queueing multiple node replacements + // just accept the risk of having the wrong node specified in the IAstModification object... value = replacement replacement.parent = this } diff --git a/compilerAst/src/prog8/ast/walk/AstWalker.kt b/compilerAst/src/prog8/ast/walk/AstWalker.kt index 09b6c0594..08a7d1ecf 100644 --- a/compilerAst/src/prog8/ast/walk/AstWalker.kt +++ b/compilerAst/src/prog8/ast/walk/AstWalker.kt @@ -56,7 +56,7 @@ interface IAstModification { } } - class ReplaceNode(private val node: Node, private val replacement: Node, private val parent: Node) : + class ReplaceNode(val node: Node, private val replacement: Node, private val parent: Node) : IAstModification { override fun perform() { parent.replaceChildNode(node, replacement) @@ -158,9 +158,19 @@ abstract class AstWalker { open fun after(whileLoop: WhileLoop, parent: Node): Iterable = emptyList() private val modifications = mutableListOf>() + // private val modificationsReplacedNodes = mutableSetOf>() private fun track(mods: Iterable, node: Node, parent: Node) { - for (it in mods) modifications += Triple(it, node, parent) + for (it in mods) { +// if(it is IAstModification.ReplaceNode) { +// val replaceKey = Pair(it.node, it.node.position) +// if(replaceKey in modificationsReplacedNodes) +// throw FatalAstException("there already is a node replacement for $replaceKey - optimizer can't deal with multiple replacements for same node yet. Split the ast modification?") +// else +// modificationsReplacedNodes.add(replaceKey) +// } + modifications += Triple(it, node, parent) + } } fun applyModifications(): Int { @@ -169,6 +179,7 @@ abstract class AstWalker { } val amount = modifications.size modifications.clear() +// modificationsReplacedNodes.clear() return amount } diff --git a/examples/test.p8 b/examples/test.p8 index 9a201779b..dbc862141 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,23 +1,24 @@ -%target cx16 +;%target cx16 %import textio %zeropage basicsafe main { sub start() { + txt.print("hello") - str filename = "titlescreen.bin" - ubyte success = cx16.vload(filename, 8, 0, $0000) - if success { - txt.print("load ok") - cx16.VERA_DC_HSCALE = 64 - cx16.VERA_DC_VSCALE = 64 - cx16.VERA_L1_CONFIG = %00011111 ; 256c bitmap mode - cx16.VERA_L1_MAPBASE = 0 - cx16.VERA_L1_TILEBASE = 0 - } else { - txt.print("load fail") - } +; str filename = "titlescreen.bin" +; ubyte success = cx16.vload(filename, 8, 0, $0000) +; if success { +; txt.print("load ok") +; cx16.VERA_DC_HSCALE = 64 +; cx16.VERA_DC_VSCALE = 64 +; cx16.VERA_L1_CONFIG = %00011111 ; 256c bitmap mode +; cx16.VERA_L1_MAPBASE = 0 +; cx16.VERA_L1_TILEBASE = 0 +; } else { +; txt.print("load fail") +; } } }