From 1c10839c14859555e5afafe7fcaafbf98fe82888 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Mon, 27 Dec 2021 02:08:47 +0100 Subject: [PATCH] moved peek/poke desugaring to other walker --- .../compiler/astprocessing/CodeDesugarer.kt | 25 +++++++++++++++++- .../compiler/astprocessing/VariousCleanups.kt | 26 ------------------- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt b/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt index 2b9bd0cbe..ad817ec35 100644 --- a/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt +++ b/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt @@ -1,11 +1,13 @@ package prog8.compiler.astprocessing -import com.github.michaelbull.result.toResultOr +import prog8.ast.IFunctionCall import prog8.ast.IStatementContainer import prog8.ast.Node import prog8.ast.Program import prog8.ast.base.ParentSentinel import prog8.ast.base.Position +import prog8.ast.expressions.DirectMemoryRead +import prog8.ast.expressions.FunctionCall import prog8.ast.expressions.IdentifierReference import prog8.ast.expressions.PrefixExpression import prog8.ast.statements.* @@ -111,4 +113,25 @@ _whilecond: ), pos) return listOf(IAstModification.ReplaceNode(whileLoop, replacement, parent)) } + + override fun before(functionCallStatement: FunctionCallStatement, parent: Node) = + before(functionCallStatement as IFunctionCall, parent, functionCallStatement.position) + + override fun before(functionCall: FunctionCall, parent: Node) = + before(functionCall as IFunctionCall, parent, functionCall.position) + + private fun before(functionCall: IFunctionCall, parent: Node, position: Position): Iterable { + if(functionCall.target.nameInSource==listOf("peek")) { + // peek(a) is synonymous with @(a) + val memread = DirectMemoryRead(functionCall.args.single(), position) + return listOf(IAstModification.ReplaceNode(functionCall as Node, memread, parent)) + } + if(functionCall.target.nameInSource==listOf("poke")) { + // poke(a, v) is synonymous with @(a) = v + val tgt = AssignTarget(null, null, DirectMemoryWrite(functionCall.args[0], position), position) + val assign = Assignment(tgt, functionCall.args[1], position) + return listOf(IAstModification.ReplaceNode(functionCall as Node, assign, parent)) + } + return noModifications + } } diff --git a/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt b/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt index fe562f47e..5b329737b 100644 --- a/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt +++ b/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt @@ -1,11 +1,9 @@ package prog8.compiler.astprocessing -import prog8.ast.IFunctionCall import prog8.ast.IStatementContainer import prog8.ast.Node import prog8.ast.Program import prog8.ast.base.FatalAstException -import prog8.ast.base.Position import prog8.ast.expressions.* import prog8.ast.statements.* import prog8.ast.walk.AstWalker @@ -37,30 +35,6 @@ internal class VariousCleanups(val program: Program, val errors: IErrorReporter) } } - override fun before(functionCallStatement: FunctionCallStatement, parent: Node) = - before(functionCallStatement as IFunctionCall, parent, functionCallStatement.position) - - override fun before(functionCall: FunctionCall, parent: Node) = - before(functionCall as IFunctionCall, parent, functionCall.position) - - private fun before(functionCall: IFunctionCall, parent: Node, position: Position): Iterable { - - // TODO move to CodeDesugarer - - if(functionCall.target.nameInSource==listOf("peek")) { - // peek(a) is synonymous with @(a) - val memread = DirectMemoryRead(functionCall.args.single(), position) - return listOf(IAstModification.ReplaceNode(functionCall as Node, memread, parent)) - } - if(functionCall.target.nameInSource==listOf("poke")) { - // poke(a, v) is synonymous with @(a) = v - val tgt = AssignTarget(null, null, DirectMemoryWrite(functionCall.args[0], position), position) - val assign = Assignment(tgt, functionCall.args[1], position) - return listOf(IAstModification.ReplaceNode(functionCall as Node, assign, parent)) - } - return noModifications - } - override fun after(typecast: TypecastExpression, parent: Node): Iterable { if(typecast.expression is NumericLiteralValue) { val value = (typecast.expression as NumericLiteralValue).cast(typecast.type)