fix some more issues with array vardecls without array size specifier

This commit is contained in:
Irmen de Jong 2019-04-21 03:04:13 +02:00
parent 0298cf8b90
commit 755af6010e
5 changed files with 33 additions and 9 deletions

View File

@ -664,7 +664,7 @@ enum class VarDeclType {
class VarDecl(val type: VarDeclType,
private val declaredDatatype: DataType,
val zeropage: Boolean,
val arraysize: ArrayIndex?,
var arraysize: ArrayIndex?,
val isUnsizedArray: Boolean,
val name: String,
var value: IExpression?,

View File

@ -551,7 +551,7 @@ private class AstChecker(private val namespace: INameScope,
}
VarDeclType.MEMORY -> {
if(decl.arraysize!=null) {
val arraySize = decl.arraysize.size() ?: 1
val arraySize = decl.arraysize!!.size() ?: 1
when(decl.datatype) {
DataType.ARRAY_B, DataType.ARRAY_UB ->
if(arraySize > 256)

View File

@ -155,6 +155,17 @@ private class StatementReorderer(private val namespace: INameScope, private val
return scope
}
override fun process(decl: VarDecl): IStatement {
if(decl.arraysize==null) {
val array = decl.value as? LiteralValue
if(array!=null && array.isArray) {
val size = heap.get(array.heapId!!).arraysize
decl.arraysize = ArrayIndex(LiteralValue.optimalInteger(size, decl.position), decl.position)
}
}
return super.process(decl)
}
private fun sortConstantAssignments(statements: MutableList<IStatement>) {
// sort assignments by datatype and value, so multiple initializations with the same value can be optimized (to load the value just once)
val result = mutableListOf<IStatement>()

View File

@ -70,7 +70,7 @@ class ConstantFolding(private val namespace: INameScope, private val heap: HeapV
errors.add(ExpressionError("arraysize requires only integers here", litval.position))
if(decl.arraysize==null)
return decl
val size = decl.arraysize.size()
val size = decl.arraysize!!.size()
if ((litval==null || !litval.isArray) && size != null && rangeExpr==null) {
// arraysize initializer is empty or a single int, and we know the size; create the arraysize.
val fillvalue = if (litval == null) 0 else litval.asIntegerValue ?: 0
@ -100,7 +100,7 @@ class ConstantFolding(private val namespace: INameScope, private val heap: HeapV
DataType.ARRAY_F -> {
if(decl.arraysize==null)
return decl
val size = decl.arraysize.size()
val size = decl.arraysize!!.size()
if ((litval==null || !litval.isArray) && size != null) {
// arraysize initializer is empty or a single int, and we know the size; create the arraysize.
val fillvalue = if (litval == null) 0.0 else litval.asNumericValue?.toDouble() ?: 0.0

View File

@ -1,15 +1,28 @@
%zeropage basicsafe
%option enable_floats
%import c64flt
~ main {
&uword COLORS = $d020
float[] fa = [1.1,2.2,3.3]
ubyte[] uba = [10,2,3,4]
byte[] ba = [-10,2,3,4]
uword[] uwa = [100,20,30,40]
word[] wa = [-100,20,30,40]
sub start() {
COLORS=12345
COLORS=12346
@(COLORS) = 54
float a
a=avg([1,2,3,4])
c64flt.print_f(a)
c64.CHROUT('\n')
a=avg([100,200,300,400])
c64flt.print_f(a)
c64.CHROUT('\n')
a=avg([1.1,2.2,3.3,4.4])
c64flt.print_f(a)
c64.CHROUT('\n')
return
}
}