repeat loop count now always rounded to integer

This commit is contained in:
Irmen de Jong 2023-07-06 23:57:44 +02:00
parent 70c9ab9074
commit ffb2027a19
3 changed files with 18 additions and 3 deletions

View File

@ -6,12 +6,14 @@ import prog8.ast.base.FatalAstException
import prog8.ast.expressions.* import prog8.ast.expressions.*
import prog8.ast.maySwapOperandOrder import prog8.ast.maySwapOperandOrder
import prog8.ast.statements.ForLoop import prog8.ast.statements.ForLoop
import prog8.ast.statements.RepeatLoop
import prog8.ast.statements.VarDecl import prog8.ast.statements.VarDecl
import prog8.ast.statements.VarDeclType import prog8.ast.statements.VarDeclType
import prog8.ast.walk.AstWalker import prog8.ast.walk.AstWalker
import prog8.ast.walk.IAstModification import prog8.ast.walk.IAstModification
import prog8.code.core.AssociativeOperators import prog8.code.core.AssociativeOperators
import prog8.code.core.DataType import prog8.code.core.DataType
import kotlin.math.floor
class ConstantFoldingOptimizer(private val program: Program) : AstWalker() { class ConstantFoldingOptimizer(private val program: Program) : AstWalker() {
@ -363,6 +365,16 @@ class ConstantFoldingOptimizer(private val program: Program) : AstWalker() {
return noModifications return noModifications
} }
override fun after(repeatLoop: RepeatLoop, parent: Node): Iterable<IAstModification> {
val count = (repeatLoop.iterations as? NumericLiteral)?.number
if(count!=null && floor(count)!=count) {
val integer = NumericLiteral.optimalInteger(count.toInt(), repeatLoop.position)
repeatLoop.iterations = integer
integer.linkParents(repeatLoop)
}
return noModifications
}
private class ShuffleOperands(val expr: BinaryExpression, private class ShuffleOperands(val expr: BinaryExpression,
val exprOperator: String?, val exprOperator: String?,
val subExpr: BinaryExpression, val subExpr: BinaryExpression,

View File

@ -12,6 +12,7 @@ import prog8.compiler.builtinFunctionReturnType
import java.io.CharConversionException import java.io.CharConversionException
import java.io.File import java.io.File
import kotlin.io.path.Path import kotlin.io.path.Path
import kotlin.math.floor
/** /**
* Semantic analysis. * Semantic analysis.
@ -468,8 +469,10 @@ internal class AstChecker(private val program: Program,
override fun visit(repeatLoop: RepeatLoop) { override fun visit(repeatLoop: RepeatLoop) {
val iterations = repeatLoop.iterations?.constValue(program) val iterations = repeatLoop.iterations?.constValue(program)
if(iterations != null && iterations.number.toInt() > 65535) if (iterations != null) {
errors.err("repeat cannot go over 65535 iterations", iterations.position) require(floor(iterations.number)==iterations.number)
if (iterations.number.toInt() > 65535) errors.err("repeat cannot go over 65535 iterations", iterations.position)
}
val ident = repeatLoop.iterations as? IdentifierReference val ident = repeatLoop.iterations as? IdentifierReference
if(ident!=null) { if(ident!=null) {

View File

@ -1,7 +1,7 @@
TODO TODO
==== ====
- Fix expericodegen errors (examples/cx16/diskspeed, rockrunners etc) - Fix expericodegen errors (examples/cx16/keyboardhandler, rockrunners etc)
... ...