From fa5479ee5fbe250a71c175f81876223a336ae69a Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 13 Oct 2024 04:30:46 +0200 Subject: [PATCH] fix ast printing of arrays with duplicate elements --- codeCore/src/prog8/code/ast/AstPrinter.kt | 11 ++++++++++- compilerAst/src/prog8/ast/AstToSourceTextConverter.kt | 8 ++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/codeCore/src/prog8/code/ast/AstPrinter.kt b/codeCore/src/prog8/code/ast/AstPrinter.kt index e96cc6945..47d47eff3 100644 --- a/codeCore/src/prog8/code/ast/AstPrinter.kt +++ b/codeCore/src/prog8/code/ast/AstPrinter.kt @@ -21,7 +21,16 @@ fun printAst(root: PtNode, skipLibraries: Boolean, output: (text: String) -> Uni else "&" } - is PtArray -> "array len=${node.children.size} ${type(node.type)}" + is PtArray -> { + val valuelist = node.children.map { + when (it) { + is PtNumber -> it.number.toString() + is PtIdentifier -> it.name + else -> "?" + } + }.joinToString(", ") + "array len=${node.children.size} ${type(node.type)} [ $valuelist ]" + } is PtArrayIndexer -> " ${type(node.type)} ${if(node.splitWords) "[splitwords]" else ""}" is PtBinaryExpression -> " ${node.operator} ${type(node.type)}" is PtBuiltinFunctionCall -> { diff --git a/compilerAst/src/prog8/ast/AstToSourceTextConverter.kt b/compilerAst/src/prog8/ast/AstToSourceTextConverter.kt index 03c64a586..b164580a9 100644 --- a/compilerAst/src/prog8/ast/AstToSourceTextConverter.kt +++ b/compilerAst/src/prog8/ast/AstToSourceTextConverter.kt @@ -315,16 +315,16 @@ class AstToSourceTextConverter(val output: (text: String) -> Unit, val program: } override fun visit(array: ArrayLiteral) { - outputListMembers(array.value.asSequence()) + outputListMembers(array.value) } - private fun outputListMembers(array: Sequence) { + private fun outputListMembers(array: Array) { var counter = 0 output("[") scopelevel++ - for (v in array) { + for ((idx, v) in array.withIndex()) { v.accept(this) - if (v !== array.last()) + if (idx != array.size-1) output(", ") counter++ if (counter > 16) {