From 76b05cb5fd045da7eeb33ce7d59d57ce1a14d016 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Mon, 28 Oct 2024 18:35:23 +0100 Subject: [PATCH] fix chained aliasing --- .../compiler/astprocessing/AstExtensions.kt | 4 +- .../astprocessing/LiteralsToAutoVars.kt | 16 +++++- compiler/test/ast/TestVariousCompilerAst.kt | 4 ++ examples/test.p8 | 56 +------------------ 4 files changed, 23 insertions(+), 57 deletions(-) diff --git a/compiler/src/prog8/compiler/astprocessing/AstExtensions.kt b/compiler/src/prog8/compiler/astprocessing/AstExtensions.kt index 15edb8051..f45770688 100644 --- a/compiler/src/prog8/compiler/astprocessing/AstExtensions.kt +++ b/compiler/src/prog8/compiler/astprocessing/AstExtensions.kt @@ -119,8 +119,8 @@ internal fun Program.checkIdentifiers(errors: IErrorReporter, options: Compilati if(errors.noErrors()) { val lit2decl = LiteralsToAutoVars(this, errors) lit2decl.visit(this) - if(errors.noErrors()) - lit2decl.applyModifications() + while(errors.noErrors() && lit2decl.applyModifications()>0) + lit2decl.visit(this) } } diff --git a/compiler/src/prog8/compiler/astprocessing/LiteralsToAutoVars.kt b/compiler/src/prog8/compiler/astprocessing/LiteralsToAutoVars.kt index 25d9ce2ec..08a835faf 100644 --- a/compiler/src/prog8/compiler/astprocessing/LiteralsToAutoVars.kt +++ b/compiler/src/prog8/compiler/astprocessing/LiteralsToAutoVars.kt @@ -107,10 +107,22 @@ internal class LiteralsToAutoVars(private val program: Program, private val erro return noModifications } + override fun after(alias: Alias, parent: Node): Iterable { + val target = alias.target.targetStatement(program) + if(target is Alias) { + val newAlias = Alias(alias.alias, target.target, alias.position) + return listOf(IAstModification.ReplaceNode(alias, newAlias, parent)) + } + return noModifications + } + override fun after(identifier: IdentifierReference, parent: Node): Iterable { val target = identifier.targetStatement(program) - if(target is Alias) { - return listOf(IAstModification.ReplaceNode(identifier, target.target.copy(position = identifier.position), parent)) + + // don't replace an identifier in an Alias or when the alias points to another alias (that will be resolved first elsewhere) + if(target is Alias && parent !is Alias) { + if(target.target.targetStatement(program) !is Alias) + return listOf(IAstModification.ReplaceNode(identifier, target.target.copy(position = identifier.position), parent)) } // experimental code to be able to alias blocks too: diff --git a/compiler/test/ast/TestVariousCompilerAst.kt b/compiler/test/ast/TestVariousCompilerAst.kt index 4573eed3b..ed445ff5b 100644 --- a/compiler/test/ast/TestVariousCompilerAst.kt +++ b/compiler/test/ast/TestVariousCompilerAst.kt @@ -741,6 +741,10 @@ main { print2("two") txt.print_ub(width) txt.print_ub(width2) + + ; chained aliases + alias chained = print2 + chained("chained") } } diff --git a/examples/test.p8 b/examples/test.p8 index 08c72ca80..2bd76e070 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,61 +1,11 @@ %import textio -%import string %option no_sysinit %zeropage basicsafe main { sub start() { - str name1 = "alfred" - str name2 = "aldrik" - str name3 = "aldrik" - - uword block1 = memory("block1", 1000, 0) - uword block2 = memory("block2", 1000, 0) - uword block3 = memory("block3", 1000, 0) - - sys.memset(block1, 1000, 0) - sys.memset(block2, 1000, 0) - sys.memset(block3, 1000, 0) - void string.copy(name1, block1+900) - void string.copy(name2, block2+900) - void string.copy(name3, block3+900) - - txt.print_b(string.compare(name1, name2)) - txt.spc() - txt.print_b(string.compare(name2, name3)) - txt.spc() - txt.print_b(string.compare(name2, name1)) - txt.nl() - txt.print_b(sys.memcmp(name1, name2, len(name1))) - txt.spc() - txt.print_b(sys.memcmp(name2, name3, len(name1))) - txt.spc() - txt.print_b(sys.memcmp(name2, name1, len(name1))) - txt.nl() - txt.nl() - - name1[1] = 0 - name2[1] = 0 - name3[1] = 0 - txt.print_b(string.compare(name1, name2)) - txt.spc() - txt.print_b(string.compare(name2, name3)) - txt.spc() - txt.print_b(string.compare(name2, name1)) - txt.nl() - - txt.print_b(sys.memcmp(name1, name2, len(name1))) - txt.spc() - txt.print_b(sys.memcmp(name2, name3, len(name1))) - txt.spc() - txt.print_b(sys.memcmp(name2, name1, len(name1))) - txt.nl() - - txt.print_b(sys.memcmp(block1, block2, 1000)) - txt.spc() - txt.print_b(sys.memcmp(block2, block3, 1000)) - txt.spc() - txt.print_b(sys.memcmp(block2, block1, 1000)) - txt.nl() + alias print = txt.print + alias zz=print + zz("chained") } }