mirror of
https://github.com/irmen/prog8.git
synced 2025-01-12 04:30:03 +00:00
removed complexity restriction on array indexing expressions
This commit is contained in:
parent
70a78e74f6
commit
0adce9b9c6
@ -27,7 +27,7 @@ internal fun Program.processAstBeforeAsmGeneration(compilerOptions: CompilationO
|
|||||||
boolRemover.visit(this)
|
boolRemover.visit(this)
|
||||||
boolRemover.applyModifications()
|
boolRemover.applyModifications()
|
||||||
|
|
||||||
val fixer = BeforeAsmAstChanger(this, compilerOptions, errors)
|
val fixer = BeforeAsmAstChanger(this, compilerOptions)
|
||||||
fixer.visit(this)
|
fixer.visit(this)
|
||||||
while (errors.noErrors() && fixer.applyModifications() > 0) {
|
while (errors.noErrors() && fixer.applyModifications() > 0) {
|
||||||
fixer.visit(this)
|
fixer.visit(this)
|
||||||
|
@ -9,14 +9,10 @@ import prog8.ast.getTempVar
|
|||||||
import prog8.ast.statements.*
|
import prog8.ast.statements.*
|
||||||
import prog8.ast.walk.AstWalker
|
import prog8.ast.walk.AstWalker
|
||||||
import prog8.ast.walk.IAstModification
|
import prog8.ast.walk.IAstModification
|
||||||
import prog8.ast.walk.IAstVisitor
|
|
||||||
import prog8.code.core.*
|
import prog8.code.core.*
|
||||||
import prog8.code.target.VMTarget
|
import prog8.code.target.VMTarget
|
||||||
|
|
||||||
internal class BeforeAsmAstChanger(val program: Program,
|
internal class BeforeAsmAstChanger(val program: Program, private val options: CompilationOptions) : AstWalker() {
|
||||||
private val options: CompilationOptions,
|
|
||||||
private val errors: IErrorReporter
|
|
||||||
) : AstWalker() {
|
|
||||||
|
|
||||||
override fun before(breakStmt: Break, parent: Node): Iterable<IAstModification> {
|
override fun before(breakStmt: Break, parent: Node): Iterable<IAstModification> {
|
||||||
throw InternalCompilerException("break should have been replaced by goto $breakStmt")
|
throw InternalCompilerException("break should have been replaced by goto $breakStmt")
|
||||||
@ -219,12 +215,6 @@ internal class BeforeAsmAstChanger(val program: Program,
|
|||||||
override fun after(arrayIndexedExpression: ArrayIndexedExpression, parent: Node): Iterable<IAstModification> {
|
override fun after(arrayIndexedExpression: ArrayIndexedExpression, parent: Node): Iterable<IAstModification> {
|
||||||
|
|
||||||
if(options.compTarget.name!=VMTarget.NAME) { // don't apply this optimization/check for Vm target
|
if(options.compTarget.name!=VMTarget.NAME) { // don't apply this optimization/check for Vm target
|
||||||
val containingStatement = getContainingStatement(arrayIndexedExpression)
|
|
||||||
if(getComplexArrayIndexedExpressions(containingStatement).size > 1) {
|
|
||||||
errors.err("it's not possible to use more than one complex array indexing expression in a single statement; break it up via a temporary variable for instance", containingStatement.position)
|
|
||||||
return noModifications
|
|
||||||
}
|
|
||||||
|
|
||||||
val index = arrayIndexedExpression.indexer.indexExpr
|
val index = arrayIndexedExpression.indexer.indexExpr
|
||||||
if (index !is NumericLiteral && index !is IdentifierReference) {
|
if (index !is NumericLiteral && index !is IdentifierReference) {
|
||||||
// replace complex indexing expression with a temp variable to hold the computed index first
|
// replace complex indexing expression with a temp variable to hold the computed index first
|
||||||
@ -235,42 +225,6 @@ internal class BeforeAsmAstChanger(val program: Program,
|
|||||||
return noModifications
|
return noModifications
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getComplexArrayIndexedExpressions(stmt: Statement): List<ArrayIndexedExpression> {
|
|
||||||
|
|
||||||
class Searcher : IAstVisitor {
|
|
||||||
val complexArrayIndexedExpressions = mutableListOf<ArrayIndexedExpression>()
|
|
||||||
override fun visit(arrayIndexedExpression: ArrayIndexedExpression) {
|
|
||||||
val ix = arrayIndexedExpression.indexer.indexExpr
|
|
||||||
if(ix !is NumericLiteral && ix !is IdentifierReference)
|
|
||||||
complexArrayIndexedExpressions.add(arrayIndexedExpression)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun visit(branch: ConditionalBranch) {}
|
|
||||||
|
|
||||||
override fun visit(forLoop: ForLoop) {}
|
|
||||||
|
|
||||||
override fun visit(ifElse: IfElse) {
|
|
||||||
ifElse.condition.accept(this)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun visit(untilLoop: UntilLoop) {
|
|
||||||
untilLoop.condition.accept(this)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val searcher = Searcher()
|
|
||||||
stmt.accept(searcher)
|
|
||||||
return searcher.complexArrayIndexedExpressions
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getContainingStatement(expression: Expression): Statement {
|
|
||||||
var node: Node = expression
|
|
||||||
while(node !is Statement)
|
|
||||||
node = node.parent
|
|
||||||
|
|
||||||
return node
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getAutoIndexerVarFor(expr: ArrayIndexedExpression): MutableList<IAstModification> {
|
private fun getAutoIndexerVarFor(expr: ArrayIndexedExpression): MutableList<IAstModification> {
|
||||||
val modifications = mutableListOf<IAstModification>()
|
val modifications = mutableListOf<IAstModification>()
|
||||||
val statement = expr.containingStatement
|
val statement = expr.containingStatement
|
||||||
|
Loading…
x
Reference in New Issue
Block a user