diff --git a/compiler/src/prog8/ast/expressions/AstExpressions.kt b/compiler/src/prog8/ast/expressions/AstExpressions.kt index 470165321..24f4d85ed 100644 --- a/compiler/src/prog8/ast/expressions/AstExpressions.kt +++ b/compiler/src/prog8/ast/expressions/AstExpressions.kt @@ -497,7 +497,11 @@ class ReferenceLiteralValue(val type: DataType, // only reference types allo throw FatalAstException("weird array element $it") it } else { - num.cast(elementType) // TODO this can throw an exception + try { + num.cast(elementType) + } catch(x: ExpressionError) { + return null + } } }.toTypedArray() return ReferenceLiteralValue(targettype, null, array=castArray, position = position) @@ -668,7 +672,7 @@ data class IdentifierReference(val nameInSource: List, override val posi override fun accept(visitor: IAstModifyingVisitor) = visitor.visit(this) override fun accept(visitor: IAstVisitor) = visitor.visit(this) - override fun referencesIdentifiers(vararg name: String): Boolean = nameInSource.last() in name // @todo is this correct all the time? + override fun referencesIdentifiers(vararg name: String): Boolean = nameInSource.last() in name override fun inferType(program: Program): DataType? { val targetStmt = targetStatement(program.namespace) diff --git a/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt b/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt index 984f21f4f..b23b64d78 100644 --- a/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt +++ b/compiler/src/prog8/ast/processing/AstIdentifiersChecker.kt @@ -390,7 +390,7 @@ internal fun fixupArrayDatatype(array: ReferenceLiteralValue, vardecl: VarDecl, } vardecl.value = litval2 litval2.linkParents(vardecl) - litval2.addToHeap(heap) // TODO is the previous array discarded from the resulting asm code? + litval2.addToHeap(heap) return litval2 } } else { diff --git a/compiler/src/prog8/compiler/Zeropage.kt b/compiler/src/prog8/compiler/Zeropage.kt index 3aa5859cf..bb490799a 100644 --- a/compiler/src/prog8/compiler/Zeropage.kt +++ b/compiler/src/prog8/compiler/Zeropage.kt @@ -13,7 +13,7 @@ abstract class Zeropage(protected val options: CompilationOptions) { val allowedDatatypes = NumericDatatypes - fun available() = free.size + fun available() = if(options.zeropage==ZeropageType.DONTUSE) 0 else free.size fun allocate(scopedname: String, datatype: DataType, position: Position?): Int { assert(scopedname.isEmpty() || !allocations.values.any { it.first==scopedname } ) {"isSameAs scopedname can't be allocated twice"} diff --git a/compiler/src/prog8/optimizer/ConstantFolding.kt b/compiler/src/prog8/optimizer/ConstantFolding.kt index ed4016785..6be1bb7e3 100644 --- a/compiler/src/prog8/optimizer/ConstantFolding.kt +++ b/compiler/src/prog8/optimizer/ConstantFolding.kt @@ -210,7 +210,7 @@ class ConstantFolding(private val program: Program) : IAstModifyingVisitor { val possibleDts = arg.second.possibleDatatypes val argConst = arg.first.value.constValue(program) if(argConst!=null && argConst.type !in possibleDts) { - val convertedValue = argConst.cast(possibleDts.first()) // TODO can throw exception + val convertedValue = argConst.cast(possibleDts.first()) functionCall.arglist[arg.first.index] = convertedValue optimizationsDone++ } @@ -226,7 +226,7 @@ class ConstantFolding(private val program: Program) : IAstModifyingVisitor { val expectedDt = arg.second.type val argConst = arg.first.value.constValue(program) if(argConst!=null && argConst.type!=expectedDt) { - val convertedValue = argConst.cast(expectedDt) // TODO can throw exception + val convertedValue = argConst.cast(expectedDt) functionCall.arglist[arg.first.index] = convertedValue optimizationsDone++ } @@ -356,7 +356,7 @@ class ConstantFolding(private val program: Program) : IAstModifyingVisitor { subleftIsConst: Boolean, subrightIsConst: Boolean): Expression { - // @todo this implements only a small set of possible reorderings for now + // todo: this implements only a small set of possible reorderings at this time if(expr.operator==subExpr.operator) { // both operators are the isSameAs. // If + or *, we can simply swap the const of expr and Var in subexpr. @@ -544,11 +544,15 @@ class ConstantFolding(private val program: Program) : IAstModifyingVisitor { override fun visit(forLoop: ForLoop): Statement { fun adjustRangeDt(rangeFrom: NumericLiteralValue, targetDt: DataType, rangeTo: NumericLiteralValue, stepLiteral: NumericLiteralValue?, range: RangeExpr): RangeExpr { - // TODO casts can throw exception - val newFrom = rangeFrom.cast(targetDt) - val newTo = rangeTo.cast(targetDt) - val newStep: Expression = - stepLiteral?.cast(targetDt) ?: range.step + val newFrom: NumericLiteralValue + val newTo: NumericLiteralValue + try { + newFrom = rangeFrom.cast(targetDt) + newTo = rangeTo.cast(targetDt) + } catch (x: ExpressionError) { + return range + } + val newStep: Expression = stepLiteral?.cast(targetDt) ?: range.step return RangeExpr(newFrom, newTo, newStep, range.position) } diff --git a/compiler/test/UnitTests.kt b/compiler/test/UnitTests.kt index 55f83b76a..b764f60a9 100644 --- a/compiler/test/UnitTests.kt +++ b/compiler/test/UnitTests.kt @@ -169,7 +169,15 @@ class TestZeropage { } } - // TODO test dontuse option + @Test + fun testZpDontuse() { + val zp = C64Zeropage(CompilationOptions(OutputType.RAW, LauncherType.NONE, ZeropageType.DONTUSE, emptyList(), false)) + println(zp.free) + assertEquals(0, zp.available()) + assertFailsWith { + zp.allocate("", DataType.BYTE, null) + } + } @Test fun testFreeSpaces() {