diff --git a/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt b/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt index 00ec6a9c4..3eb5472e5 100644 --- a/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt +++ b/compiler/src/prog8/compiler/astprocessing/CodeDesugarer.kt @@ -340,6 +340,18 @@ _after: } if(expr.operator==".") { + if(expr.left is IdentifierReference) { + require(expr.right !is IdentifierReference) + val ri = expr.right as? ArrayIndexedExpression + if(ri!=null && ri.plainarrayvar!=null) { + // a.b . c.d[i] -> a.b.c.d[i] + val joined = (expr.left as IdentifierReference).nameInSource + ri.plainarrayvar!!.nameInSource + val ai = ArrayIndexedExpression(IdentifierReference(joined, expr.position), null, ri.indexer, expr.position) + return listOf(IAstModification.ReplaceNode(expr, ai, parent)) + } + } + + val left = expr.left as? ArrayIndexedExpression val right = expr.right as? PtrDereference if(left!=null && right!=null) { @@ -629,12 +641,13 @@ _after: if(deref.chain.last().second!=null && deref.derefLast && deref.chain.dropLast(1).all { it.second==null } ) { - // parent could be Assigment directly, or a binexpr chained pointer expression (with '.' operator)_ + // parent could be Assigment directly, or a binexpr chained pointer expression (with '.' operator) if(parent is Assignment) { val dt = deref.inferType(program).getOrUndef() require(dt.isNumericOrBool) if (parent.value isSameAs deref) { - // x = z[i]^^ --> peekX(z[i]) + // get rid of ArrayIndexedPtrDereference in the assignment value + // x = z[i]^^ --> x = peekX(z[i]) val (peekFunc, cast) = if(dt.isBool) "peekbool" to null else if (dt.isUnsignedByte) "peek" to null @@ -672,6 +685,7 @@ _after: TODO("translate deref $deref here ${deref.position}") } else if(parent is AssignTarget) { + // get rid of ArrayIndexedPtrDereference in the assignment target // z[i]^^ = value --> pokeX(z[i], value) val dt = deref.inferType(program).getOrUndef() require(dt.isNumericOrBool) diff --git a/compilerAst/src/prog8/ast/AstToSourceTextConverter.kt b/compilerAst/src/prog8/ast/AstToSourceTextConverter.kt index 5853a186c..e9a3fa26f 100644 --- a/compilerAst/src/prog8/ast/AstToSourceTextConverter.kt +++ b/compilerAst/src/prog8/ast/AstToSourceTextConverter.kt @@ -435,6 +435,7 @@ class AstToSourceTextConverter(val output: (text: String) -> Unit, val program: assignTarget.identifier?.accept(this) assignTarget.arrayindexed?.accept(this) assignTarget.pointerDereference?.accept(this) + assignTarget.arrayIndexedDereference?.accept(this) val multi = assignTarget.multi if (multi != null) { multi.dropLast(1).forEach { target ->