reintroduce expressions for array indexing

This commit is contained in:
Irmen de Jong 2020-10-18 13:33:11 +02:00
parent 8f2e166a22
commit 8aeb8a9bb7
3 changed files with 18 additions and 21 deletions

View File

@ -1053,9 +1053,8 @@ internal class AstChecker(private val program: Program,
if(dtxVar!=null && dtxVar != DataType.UBYTE && dtxVar != DataType.BYTE)
errors.err("array indexing is limited to byte size 0..255", arrayIndexedExpression.position)
// check type of array indexer
if(arrayIndexedExpression.indexer.indexVar==null && arrayIndexedExpression.indexer.indexNum==null)
errors.err("array indexing can only be done with a number or a variable, or a simple binary expression, but not an arbitrary expression. Use a temp var?", arrayIndexedExpression.indexer.position)
if(arrayIndexedExpression.indexer.origExpression!=null)
throw FatalAstException("array indexer should have been replaced with a temp var @ ${arrayIndexedExpression.indexer.position}")
super.visit(arrayIndexedExpression)
}

View File

@ -72,18 +72,19 @@ internal class StatementReorderer(val program: Program, val errors: ErrorReporte
}
override fun after(arrayIndexedExpression: ArrayIndexedExpression, parent: Node): Iterable<IAstModification> {
val expr = arrayIndexedExpression.indexer.origExpression
if (expr is NumericLiteralValue) {
arrayIndexedExpression.indexer.indexNum = expr
arrayIndexedExpression.indexer.origExpression = null
}
else if (expr is IdentifierReference) {
arrayIndexedExpression.indexer.indexVar = expr
arrayIndexedExpression.indexer.origExpression = null
}
else if(expr is BinaryExpression) {
if((expr.left is NumericLiteralValue || expr.left is IdentifierReference) &&
(expr.right is NumericLiteralValue || expr.right is IdentifierReference)) {
when (val expr = arrayIndexedExpression.indexer.origExpression) {
is NumericLiteralValue -> {
arrayIndexedExpression.indexer.indexNum = expr
arrayIndexedExpression.indexer.origExpression = null
return noModifications
}
is IdentifierReference -> {
arrayIndexedExpression.indexer.indexVar = expr
arrayIndexedExpression.indexer.origExpression = null
return noModifications
}
is Expression -> {
// replace complex indexing with a temp variable
val modifications = mutableListOf<IAstModification>()
val indexerVarName = "prog8_autovar_index"
val block = expr.definingBlock()
@ -107,9 +108,8 @@ internal class StatementReorderer(val program: Program, val errors: ErrorReporte
return modifications
}
else -> return noModifications
}
return noModifications
}
override fun after(whenStatement: WhenStatement, parent: Node): Iterable<IAstModification> {

View File

@ -41,10 +41,8 @@ main {
ubyte @zp i
for i in 0 to 7 {
c64.SPRPTR[i] = $0a00 / 64
ubyte twoi = i*2
c64.SPXY[twoi] = 50+25*i
twoi++
c64.SPXY[twoi] = rnd()
c64.SPXY[i*2] = 50+25*i
c64.SPXY[i*2+1] = rnd()
}
c64.SPENA = 255 ; enable all sprites