better names

This commit is contained in:
Irmen de Jong 2020-08-23 16:08:31 +02:00
parent d0674ad688
commit 1be139759c
12 changed files with 59 additions and 65 deletions

View File

@ -545,7 +545,7 @@ internal class AstChecker(private val program: Program,
} }
VarDeclType.MEMORY -> { VarDeclType.MEMORY -> {
if(decl.arraysize!=null) { if(decl.arraysize!=null) {
val arraySize = decl.arraysize!!.size() ?: 1 val arraySize = decl.arraysize!!.constIndex() ?: 1
when(decl.datatype) { when(decl.datatype) {
DataType.ARRAY_B, DataType.ARRAY_UB -> DataType.ARRAY_B, DataType.ARRAY_UB ->
if(arraySize > 256) if(arraySize > 256)
@ -587,7 +587,7 @@ internal class AstChecker(private val program: Program,
// array length limits // array length limits
if(decl.isArray) { if(decl.isArray) {
val length = decl.arraysize!!.size() ?: 1 val length = decl.arraysize!!.constIndex() ?: 1
when (decl.datatype) { when (decl.datatype) {
DataType.STR, DataType.ARRAY_UB, DataType.ARRAY_B -> { DataType.STR, DataType.ARRAY_UB, DataType.ARRAY_B -> {
if(length==0 || length>256) if(length==0 || length>256)
@ -957,7 +957,7 @@ internal class AstChecker(private val program: Program,
if(target is VarDecl) { if(target is VarDecl) {
if(target.datatype !in IterableDatatypes) if(target.datatype !in IterableDatatypes)
errors.err("indexing requires an iterable variable", arrayIndexedExpression.position) errors.err("indexing requires an iterable variable", arrayIndexedExpression.position)
val arraysize = target.arraysize?.size() val arraysize = target.arraysize?.constIndex()
if(arraysize!=null) { if(arraysize!=null) {
// check out of bounds // check out of bounds
val index = (arrayIndexedExpression.arrayspec.index as? NumericLiteralValue)?.number?.toInt() val index = (arrayIndexedExpression.arrayspec.index as? NumericLiteralValue)?.number?.toInt()
@ -1078,7 +1078,7 @@ internal class AstChecker(private val program: Program,
if(value.type.istype(targetDt)) { if(value.type.istype(targetDt)) {
if(!checkArrayValues(value, targetDt)) if(!checkArrayValues(value, targetDt))
return false return false
val arraySpecSize = arrayspec.size() val arraySpecSize = arrayspec.constIndex()
val arraySize = value.value.size val arraySize = value.value.size
if(arraySpecSize!=null && arraySpecSize>0) { if(arraySpecSize!=null && arraySpecSize>0) {
if(arraySpecSize<1 || arraySpecSize>256) if(arraySpecSize<1 || arraySpecSize>256)
@ -1100,7 +1100,7 @@ internal class AstChecker(private val program: Program,
if(value.type.istype(targetDt)) { if(value.type.istype(targetDt)) {
if(!checkArrayValues(value, targetDt)) if(!checkArrayValues(value, targetDt))
return false return false
val arraySpecSize = arrayspec.size() val arraySpecSize = arrayspec.constIndex()
val arraySize = value.value.size val arraySize = value.value.size
if(arraySpecSize!=null && arraySpecSize>0) { if(arraySpecSize!=null && arraySpecSize>0) {
if(arraySpecSize<1 || arraySpecSize>128) if(arraySpecSize<1 || arraySpecSize>128)
@ -1123,7 +1123,7 @@ internal class AstChecker(private val program: Program,
if(!checkArrayValues(value, targetDt)) if(!checkArrayValues(value, targetDt))
return false return false
val arraySize = value.value.size val arraySize = value.value.size
val arraySpecSize = arrayspec.size() val arraySpecSize = arrayspec.constIndex()
if(arraySpecSize!=null && arraySpecSize>0) { if(arraySpecSize!=null && arraySpecSize>0) {
if(arraySpecSize < 1 || arraySpecSize>51) if(arraySpecSize < 1 || arraySpecSize>51)
return err("float array length must be 1-51") return err("float array length must be 1-51")

View File

@ -261,8 +261,8 @@ open class VarDecl(val type: VarDeclType,
member.isArray, member.isArray,
true, true,
member.position member.position
) as Statement )
}.toMutableList() }.toMutableList<Statement>()
structHasBeenFlattened = true structHasBeenFlattened = true
return result return result
} }
@ -300,7 +300,7 @@ class ArrayIndex(var index: Expression, override val position: Position) : Node
return("ArrayIndex($index, pos=$position)") return("ArrayIndex($index, pos=$position)")
} }
fun size() = (index as? NumericLiteralValue)?.number?.toInt() fun constIndex() = (index as? NumericLiteralValue)?.number?.toInt()
} }
open class Assignment(var target: AssignTarget, var value: Expression, override val position: Position) : Statement() { open class Assignment(var target: AssignTarget, var value: Expression, override val position: Position) : Statement() {
@ -449,9 +449,9 @@ data class AssignTarget(var identifier: IdentifierReference?,
this.identifier!=null -> value is IdentifierReference && value.nameInSource==identifier!!.nameInSource this.identifier!=null -> value is IdentifierReference && value.nameInSource==identifier!!.nameInSource
this.arrayindexed!=null -> value is ArrayIndexedExpression && this.arrayindexed!=null -> value is ArrayIndexedExpression &&
value.identifier.nameInSource==arrayindexed!!.identifier.nameInSource && value.identifier.nameInSource==arrayindexed!!.identifier.nameInSource &&
value.arrayspec.size()!=null && value.arrayspec.constIndex()!=null &&
arrayindexed!!.arrayspec.size()!=null && arrayindexed!!.arrayspec.constIndex()!=null &&
value.arrayspec.size()==arrayindexed!!.arrayspec.size() value.arrayspec.constIndex()==arrayindexed!!.arrayspec.constIndex()
else -> false else -> false
} }
} }

View File

@ -15,6 +15,7 @@ import prog8.compiler.target.c64.C64MachineDefinition
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_HI_HEX import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_HI_HEX
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_LO_HEX import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_LO_HEX
import prog8.compiler.target.c64.Petscii import prog8.compiler.target.c64.Petscii
import prog8.compiler.target.c64.codegen.assignment.AssignmentAsmGen
import prog8.compiler.target.generatedLabelPrefix import prog8.compiler.target.generatedLabelPrefix
import prog8.functions.BuiltinFunctions import prog8.functions.BuiltinFunctions
import prog8.functions.FSignature import prog8.functions.FSignature
@ -317,7 +318,7 @@ internal class AsmGen(private val program: Program,
else { else {
// no init value, use zeros // no init value, use zeros
val zero = decl.zeroElementValue() val zero = decl.zeroElementValue()
Array(decl.arraysize!!.size()!!) { zero } Array(decl.arraysize!!.constIndex()!!) { zero }
} }
val floatFills = array.map { val floatFills = array.map {
val number = (it as NumericLiteralValue).number val number = (it as NumericLiteralValue).number
@ -392,7 +393,7 @@ internal class AsmGen(private val program: Program,
else { else {
// no array init value specified, use a list of zeros // no array init value specified, use a list of zeros
val zero = decl.zeroElementValue() val zero = decl.zeroElementValue()
Array(decl.arraysize!!.size()!!) { zero } Array(decl.arraysize!!.constIndex()!!) { zero }
} }
return when (decl.datatype) { return when (decl.datatype) {
DataType.ARRAY_UB -> DataType.ARRAY_UB ->
@ -419,7 +420,7 @@ internal class AsmGen(private val program: Program,
else { else {
// no array init value specified, use a list of zeros // no array init value specified, use a list of zeros
val zero = decl.zeroElementValue() val zero = decl.zeroElementValue()
Array(decl.arraysize!!.size()!!) { zero } Array(decl.arraysize!!.constIndex()!!) { zero }
} }
return when (decl.datatype) { return when (decl.datatype) {
DataType.ARRAY_UB -> DataType.ARRAY_UB ->

View File

@ -78,7 +78,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
if (variable is IdentifierReference) { if (variable is IdentifierReference) {
val decl = variable.targetVarDecl(program.namespace)!! val decl = variable.targetVarDecl(program.namespace)!!
val varName = asmgen.asmIdentifierName(variable) val varName = asmgen.asmIdentifierName(variable)
val numElements = decl.arraysize!!.size() val numElements = decl.arraysize!!.constIndex()
when (decl.datatype) { when (decl.datatype) {
DataType.ARRAY_UB, DataType.ARRAY_B -> { DataType.ARRAY_UB, DataType.ARRAY_B -> {
asmgen.out(""" asmgen.out("""
@ -120,7 +120,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
if (variable is IdentifierReference) { if (variable is IdentifierReference) {
val decl = variable.targetVarDecl(program.namespace)!! val decl = variable.targetVarDecl(program.namespace)!!
val varName = asmgen.asmIdentifierName(variable) val varName = asmgen.asmIdentifierName(variable)
val numElements = decl.arraysize!!.size() val numElements = decl.arraysize!!.constIndex()
when (decl.datatype) { when (decl.datatype) {
DataType.ARRAY_UB, DataType.ARRAY_B -> { DataType.ARRAY_UB, DataType.ARRAY_B -> {
asmgen.out(""" asmgen.out("""
@ -487,7 +487,7 @@ internal class BuiltinFunctionsAsmGen(private val program: Program, private val
private fun outputPushAddressAndLenghtOfArray(arg: Expression) { private fun outputPushAddressAndLenghtOfArray(arg: Expression) {
arg as IdentifierReference arg as IdentifierReference
val identifierName = asmgen.asmIdentifierName(arg) val identifierName = asmgen.asmIdentifierName(arg)
val size = arg.targetVarDecl(program.namespace)!!.arraysize!!.size()!! val size = arg.targetVarDecl(program.namespace)!!.arraysize!!.constIndex()!!
asmgen.out(""" asmgen.out("""
lda #<$identifierName lda #<$identifierName
sta $ESTACK_LO_HEX,x sta $ESTACK_LO_HEX,x

View File

@ -290,7 +290,7 @@ $loopLabel lda ${65535.toHex()} ; modified
$endLabel""") $endLabel""")
} }
DataType.ARRAY_UB, DataType.ARRAY_B -> { DataType.ARRAY_UB, DataType.ARRAY_B -> {
val length = decl.arraysize!!.size()!! val length = decl.arraysize!!.constIndex()!!
val indexVar = asmgen.makeLabel("for_index") val indexVar = asmgen.makeLabel("for_index")
asmgen.out(""" asmgen.out("""
ldy #0 ldy #0
@ -324,7 +324,7 @@ $indexVar .byte 0""")
asmgen.out(endLabel) asmgen.out(endLabel)
} }
DataType.ARRAY_W, DataType.ARRAY_UW -> { DataType.ARRAY_W, DataType.ARRAY_UW -> {
val length = decl.arraysize!!.size()!! * 2 val length = decl.arraysize!!.constIndex()!! * 2
val indexVar = asmgen.makeLabel("for_index") val indexVar = asmgen.makeLabel("for_index")
val loopvarName = asmgen.asmIdentifierName(stmt.loopVar) val loopvarName = asmgen.asmIdentifierName(stmt.loopVar)
asmgen.out(""" asmgen.out("""

View File

@ -1,4 +1,4 @@
package prog8.compiler.target.c64.codegen package prog8.compiler.target.c64.codegen.assignment
import prog8.ast.Program import prog8.ast.Program
import prog8.ast.base.* import prog8.ast.base.*
@ -7,9 +7,10 @@ import prog8.ast.statements.AssignTarget
import prog8.ast.statements.Assignment import prog8.ast.statements.Assignment
import prog8.ast.statements.DirectMemoryWrite import prog8.ast.statements.DirectMemoryWrite
import prog8.compiler.AssemblyError import prog8.compiler.AssemblyError
import prog8.compiler.target.c64.codegen.AsmGen
enum class TargetStorageKind { internal enum class TargetStorageKind {
VARIABLE, VARIABLE,
ARRAY, ARRAY,
MEMORY, MEMORY,
@ -17,7 +18,7 @@ enum class TargetStorageKind {
STACK STACK
} }
enum class SourceStorageKind { internal enum class SourceStorageKind {
LITERALNUMBER, LITERALNUMBER,
VARIABLE, VARIABLE,
ARRAY, ARRAY,
@ -39,7 +40,7 @@ internal class AsmAssignTarget(val kind: TargetStorageKind,
) )
{ {
val constMemoryAddress by lazy { memory?.addressExpression?.constValue(program)?.number?.toInt() ?: 0} val constMemoryAddress by lazy { memory?.addressExpression?.constValue(program)?.number?.toInt() ?: 0}
val constArrayIndexValue by lazy { array?.arrayspec?.size() ?: 0 } val constArrayIndexValue by lazy { array?.arrayspec?.constIndex() }
val vardecl by lazy { variable?.targetVarDecl(program.namespace)!! } val vardecl by lazy { variable?.targetVarDecl(program.namespace)!! }
val asmName by lazy { val asmName by lazy {
if(variable!=null) if(variable!=null)
@ -82,7 +83,7 @@ internal class AsmAssignSource(val kind: SourceStorageKind,
) )
{ {
val constMemoryAddress by lazy { memory?.addressExpression?.constValue(program)?.number?.toInt() ?: 0} val constMemoryAddress by lazy { memory?.addressExpression?.constValue(program)?.number?.toInt() ?: 0}
val constArrayIndexValue by lazy { array?.arrayspec?.size() ?: 0 } val constArrayIndexValue by lazy { array?.arrayspec?.constIndex() }
val vardecl by lazy { variable?.targetVarDecl(program.namespace)!! } val vardecl by lazy { variable?.targetVarDecl(program.namespace)!! }
companion object { companion object {

View File

@ -1,4 +1,4 @@
package prog8.compiler.target.c64.codegen package prog8.compiler.target.c64.codegen.assignment
import prog8.ast.Program import prog8.ast.Program
import prog8.ast.base.* import prog8.ast.base.*
@ -9,6 +9,7 @@ import prog8.compiler.target.c64.C64MachineDefinition
import prog8.compiler.target.c64.C64MachineDefinition.C64Zeropage import prog8.compiler.target.c64.C64MachineDefinition.C64Zeropage
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_HI_HEX import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_HI_HEX
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_LO_HEX import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_LO_HEX
import prog8.compiler.target.c64.codegen.AsmGen
import prog8.compiler.toHex import prog8.compiler.toHex
// TODO optimize the array indexes where the index is a constant // TODO optimize the array indexes where the index is a constant
@ -18,7 +19,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
private val augmentableAsmGen = AugmentableAssignmentAsmGen(program, this, asmgen) private val augmentableAsmGen = AugmentableAssignmentAsmGen(program, this, asmgen)
internal fun translate(assignment: Assignment) { fun translate(assignment: Assignment) {
var source = AsmAssignSource.fromAstSource(assignment.value, program) var source = AsmAssignSource.fromAstSource(assignment.value, program)
val target = AsmAssignTarget.fromAstAssignment(assignment, program, asmgen) val target = AsmAssignTarget.fromAstAssignment(assignment, program, asmgen)
@ -33,8 +34,15 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
val assign = AsmAssignment(source, target, assignment.isAugmentable, assignment.position) val assign = AsmAssignment(source, target, assignment.isAugmentable, assignment.position)
when { if(assign.isAugmentable)
source.kind==SourceStorageKind.LITERALNUMBER -> { augmentableAsmGen.translate(assign)
else
translateNormalAssignment(assign)
}
fun translateNormalAssignment(assign: AsmAssignment) {
when(assign.source.kind) {
SourceStorageKind.LITERALNUMBER -> {
// simple case: assign a constant number // simple case: assign a constant number
val num = assign.source.number!!.number val num = assign.source.number!!.number
when (assign.source.datatype) { when (assign.source.datatype) {
@ -44,7 +52,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
else -> throw AssemblyError("weird numval type") else -> throw AssemblyError("weird numval type")
} }
} }
source.kind==SourceStorageKind.VARIABLE -> { SourceStorageKind.VARIABLE -> {
// simple case: assign from another variable // simple case: assign from another variable
val variable = assign.source.variable!! val variable = assign.source.variable!!
when (assign.source.datatype) { when (assign.source.datatype) {
@ -55,22 +63,6 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
else -> throw AssemblyError("unsupported assignment target type ${assign.target.datatype}") else -> throw AssemblyError("unsupported assignment target type ${assign.target.datatype}")
} }
} }
assign.isAugmentable -> {
// special case: the in-place assignment / modification
augmentableAsmGen.translate(assign)
}
else -> translateOtherAssignment(assign)
}
}
internal fun translateOtherAssignment(assign: AsmAssignment) {
// source kind: expression, register, stack (only expression implemented for now)
when(assign.source.kind) {
SourceStorageKind.LITERALNUMBER,
SourceStorageKind.VARIABLE -> {
throw AssemblyError("assignment value type ${assign.source.kind} should have been handled elsewhere")
}
SourceStorageKind.ARRAY -> { SourceStorageKind.ARRAY -> {
val value = assign.source.array!! val value = assign.source.array!!
val elementDt = assign.source.datatype val elementDt = assign.source.datatype
@ -131,7 +123,7 @@ internal class AssignmentAsmGen(private val program: Program, private val asmgen
} }
} }
internal fun assignToRegister(reg: CpuRegister, value: Int?, identifier: IdentifierReference?) { fun assignToRegister(reg: CpuRegister, value: Int?, identifier: IdentifierReference?) {
if(value!=null) { if(value!=null) {
asmgen.out(" ld${reg.toString().toLowerCase()} #${value.toHex()}") asmgen.out(" ld${reg.toString().toLowerCase()} #${value.toHex()}")
} else if(identifier!=null) { } else if(identifier!=null) {

View File

@ -1,4 +1,4 @@
package prog8.compiler.target.c64.codegen package prog8.compiler.target.c64.codegen.assignment
import prog8.ast.Program import prog8.ast.Program
import prog8.ast.base.* import prog8.ast.base.*
@ -7,6 +7,7 @@ import prog8.compiler.AssemblyError
import prog8.compiler.target.c64.C64MachineDefinition.C64Zeropage import prog8.compiler.target.c64.C64MachineDefinition.C64Zeropage
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_HI_PLUS1_HEX import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_HI_PLUS1_HEX
import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_LO_PLUS1_HEX import prog8.compiler.target.c64.C64MachineDefinition.ESTACK_LO_PLUS1_HEX
import prog8.compiler.target.c64.codegen.AsmGen
import prog8.compiler.toHex import prog8.compiler.toHex
internal class AugmentableAssignmentAsmGen(private val program: Program, internal class AugmentableAssignmentAsmGen(private val program: Program,
@ -14,7 +15,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
private val asmgen: AsmGen) { private val asmgen: AsmGen) {
fun translate(assign: AsmAssignment) { fun translate(assign: AsmAssignment) {
require(assign.isAugmentable) require(assign.isAugmentable)
require(assign.source.kind==SourceStorageKind.EXPRESSION) require(assign.source.kind== SourceStorageKind.EXPRESSION)
val value = assign.source.expression!! val value = assign.source.expression!!
when (value) { when (value) {
@ -210,7 +211,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
} }
TargetStorageKind.ARRAY -> { TargetStorageKind.ARRAY -> {
println("*** TODO optimize simple inplace array assignment ${target.array} $operator= $value") println("*** TODO optimize simple inplace array assignment ${target.array} $operator= $value")
assignmentAsmGen.translateOtherAssignment(target.origAssign) // TODO get rid of this fallback for the most common cases here assignmentAsmGen.translateNormalAssignment(target.origAssign) // TODO get rid of this fallback for the most common cases here
} }
TargetStorageKind.REGISTER -> TODO() TargetStorageKind.REGISTER -> TODO()
TargetStorageKind.STACK -> TODO() TargetStorageKind.STACK -> TODO()
@ -1018,7 +1019,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
DataType.UWORD, DataType.WORD -> { DataType.UWORD, DataType.WORD -> {
when (outerCastDt) { when (outerCastDt) {
DataType.UBYTE, DataType.BYTE -> { DataType.UBYTE, DataType.BYTE -> {
if(target.kind==TargetStorageKind.VARIABLE) { if(target.kind== TargetStorageKind.VARIABLE) {
asmgen.out(" lda #0 | sta ${target.asmName}+1") asmgen.out(" lda #0 | sta ${target.asmName}+1")
} else } else
throw AssemblyError("weird value") throw AssemblyError("weird value")
@ -1057,7 +1058,7 @@ internal class AugmentableAssignmentAsmGen(private val program: Program,
+ eor #1 + eor #1
sta ${target.asmName}""") sta ${target.asmName}""")
} }
TargetStorageKind.MEMORY-> { TargetStorageKind.MEMORY -> {
val mem = target.memory!! val mem = target.memory!!
when (mem.addressExpression) { when (mem.addressExpression) {
is NumericLiteralValue -> { is NumericLiteralValue -> {

View File

@ -258,7 +258,7 @@ private fun builtinSizeof(args: List<Expression>, position: Position, program: P
return when { return when {
dt.typeOrElse(DataType.STRUCT) in ArrayDatatypes -> { dt.typeOrElse(DataType.STRUCT) in ArrayDatatypes -> {
val length = (target as VarDecl).arraysize!!.size() ?: throw CannotEvaluateException("sizeof", "unknown array size") val length = (target as VarDecl).arraysize!!.constIndex() ?: throw CannotEvaluateException("sizeof", "unknown array size")
val elementDt = ArrayElementTypes.getValue(dt.typeOrElse(DataType.STRUCT)) val elementDt = ArrayElementTypes.getValue(dt.typeOrElse(DataType.STRUCT))
numericLiteral(elementDt.memorySize() * length, position) numericLiteral(elementDt.memorySize() * length, position)
} }
@ -293,7 +293,7 @@ private fun builtinLen(args: List<Expression>, position: Position, program: Prog
throw SyntaxError("len requires one argument", position) throw SyntaxError("len requires one argument", position)
val directMemVar = ((args[0] as? DirectMemoryRead)?.addressExpression as? IdentifierReference)?.targetVarDecl(program.namespace) val directMemVar = ((args[0] as? DirectMemoryRead)?.addressExpression as? IdentifierReference)?.targetVarDecl(program.namespace)
var arraySize = directMemVar?.arraysize?.size() var arraySize = directMemVar?.arraysize?.constIndex()
if(arraySize != null) if(arraySize != null)
return NumericLiteralValue.optimalInteger(arraySize, position) return NumericLiteralValue.optimalInteger(arraySize, position)
if(args[0] is ArrayLiteralValue) if(args[0] is ArrayLiteralValue)
@ -305,7 +305,7 @@ private fun builtinLen(args: List<Expression>, position: Position, program: Prog
return when(target.datatype) { return when(target.datatype) {
DataType.ARRAY_UB, DataType.ARRAY_B, DataType.ARRAY_UW, DataType.ARRAY_W, DataType.ARRAY_F -> { DataType.ARRAY_UB, DataType.ARRAY_B, DataType.ARRAY_UW, DataType.ARRAY_W, DataType.ARRAY_F -> {
arraySize = target.arraysize?.size() arraySize = target.arraysize?.constIndex()
if(arraySize==null) if(arraySize==null)
throw CannotEvaluateException("len", "arraysize unknown") throw CannotEvaluateException("len", "arraysize unknown")
NumericLiteralValue.optimalInteger(arraySize, args[0].position) NumericLiteralValue.optimalInteger(arraySize, args[0].position)

View File

@ -56,7 +56,7 @@ internal class ConstantIdentifierReplacer(private val program: Program, private
)) ))
} }
} }
else if(decl.arraysize?.size()==null) { else if(decl.arraysize?.constIndex()==null) {
val size = decl.arraysize!!.index.constValue(program) val size = decl.arraysize!!.index.constValue(program)
if(size!=null) { if(size!=null) {
return listOf(IAstModification.SetExpression( return listOf(IAstModification.SetExpression(
@ -81,7 +81,7 @@ internal class ConstantIdentifierReplacer(private val program: Program, private
val rangeExpr = decl.value as? RangeExpr val rangeExpr = decl.value as? RangeExpr
if(rangeExpr!=null) { if(rangeExpr!=null) {
// convert the initializer range expression to an actual array // convert the initializer range expression to an actual array
val declArraySize = decl.arraysize?.size() val declArraySize = decl.arraysize?.constIndex()
if(declArraySize!=null && declArraySize!=rangeExpr.size()) if(declArraySize!=null && declArraySize!=rangeExpr.size())
errors.err("range expression size doesn't match declared array size", decl.value?.position!!) errors.err("range expression size doesn't match declared array size", decl.value?.position!!)
val constRange = rangeExpr.toConstantIntegerRange() val constRange = rangeExpr.toConstantIntegerRange()
@ -101,7 +101,7 @@ internal class ConstantIdentifierReplacer(private val program: Program, private
} }
if(numericLv!=null && numericLv.type==DataType.FLOAT) if(numericLv!=null && numericLv.type==DataType.FLOAT)
errors.err("arraysize requires only integers here", numericLv.position) errors.err("arraysize requires only integers here", numericLv.position)
val size = decl.arraysize?.size() ?: return noModifications val size = decl.arraysize?.constIndex() ?: return noModifications
if (rangeExpr==null && numericLv!=null) { if (rangeExpr==null && numericLv!=null) {
// arraysize initializer is empty or a single int, and we know the size; create the arraysize. // arraysize initializer is empty or a single int, and we know the size; create the arraysize.
val fillvalue = numericLv.number.toInt() val fillvalue = numericLv.number.toInt()
@ -125,18 +125,18 @@ internal class ConstantIdentifierReplacer(private val program: Program, private
else -> {} else -> {}
} }
// create the array itself, filled with the fillvalue. // create the array itself, filled with the fillvalue.
val array = Array(size) {fillvalue}.map { NumericLiteralValue(ArrayElementTypes.getValue(decl.datatype), it, numericLv.position) as Expression}.toTypedArray() val array = Array(size) {fillvalue}.map { NumericLiteralValue(ArrayElementTypes.getValue(decl.datatype), it, numericLv.position) }.toTypedArray<Expression>()
val refValue = ArrayLiteralValue(InferredTypes.InferredType.known(decl.datatype), array, position = numericLv.position) val refValue = ArrayLiteralValue(InferredTypes.InferredType.known(decl.datatype), array, position = numericLv.position)
return listOf(IAstModification.ReplaceNode(decl.value!!, refValue, decl)) return listOf(IAstModification.ReplaceNode(decl.value!!, refValue, decl))
} }
} }
DataType.ARRAY_F -> { DataType.ARRAY_F -> {
val size = decl.arraysize?.size() ?: return noModifications val size = decl.arraysize?.constIndex() ?: return noModifications
val litval = decl.value as? NumericLiteralValue val litval = decl.value as? NumericLiteralValue
val rangeExpr = decl.value as? RangeExpr val rangeExpr = decl.value as? RangeExpr
if(rangeExpr!=null) { if(rangeExpr!=null) {
// convert the initializer range expression to an actual array of floats // convert the initializer range expression to an actual array of floats
val declArraySize = decl.arraysize?.size() val declArraySize = decl.arraysize?.constIndex()
if(declArraySize!=null && declArraySize!=rangeExpr.size()) if(declArraySize!=null && declArraySize!=rangeExpr.size())
errors.err("range expression size doesn't match declared array size", decl.value?.position!!) errors.err("range expression size doesn't match declared array size", decl.value?.position!!)
val constRange = rangeExpr.toConstantIntegerRange() val constRange = rangeExpr.toConstantIntegerRange()
@ -154,7 +154,7 @@ internal class ConstantIdentifierReplacer(private val program: Program, private
errors.err("float value overflow", litval.position) errors.err("float value overflow", litval.position)
else { else {
// create the array itself, filled with the fillvalue. // create the array itself, filled with the fillvalue.
val array = Array(size) {fillvalue}.map { NumericLiteralValue(DataType.FLOAT, it, litval.position) as Expression}.toTypedArray() val array = Array(size) {fillvalue}.map { NumericLiteralValue(DataType.FLOAT, it, litval.position) }.toTypedArray<Expression>()
val refValue = ArrayLiteralValue(InferredTypes.InferredType.known(DataType.ARRAY_F), array, position = litval.position) val refValue = ArrayLiteralValue(InferredTypes.InferredType.known(DataType.ARRAY_F), array, position = litval.position)
return listOf(IAstModification.ReplaceNode(decl.value!!, refValue, decl)) return listOf(IAstModification.ReplaceNode(decl.value!!, refValue, decl))
} }

View File

@ -229,7 +229,7 @@ internal class StatementOptimizer(private val program: Program,
} }
} }
else if(iterable.datatype in ArrayDatatypes) { else if(iterable.datatype in ArrayDatatypes) {
val size = iterable.arraysize!!.size() val size = iterable.arraysize!!.constIndex()
if(size==1) { if(size==1) {
// loop over array of length 1 -> just assign the single value // loop over array of length 1 -> just assign the single value
val av = (iterable.value as ArrayLiteralValue).value[0].constValue(program)?.number val av = (iterable.value as ArrayLiteralValue).value[0].constValue(program)?.number

View File

@ -4,7 +4,6 @@ import org.antlr.v4.runtime.*
import prog8.ast.Module import prog8.ast.Module
import prog8.ast.Program import prog8.ast.Program
import prog8.ast.antlr.toAst import prog8.ast.antlr.toAst
import prog8.ast.base.ErrorReporter
import prog8.ast.base.Position import prog8.ast.base.Position
import prog8.ast.base.SyntaxError import prog8.ast.base.SyntaxError
import prog8.ast.base.checkImportedValid import prog8.ast.base.checkImportedValid
@ -34,7 +33,7 @@ internal class CustomLexer(val modulePath: Path, input: CharStream?) : prog8Lexe
internal fun moduleName(fileName: Path) = fileName.toString().substringBeforeLast('.') internal fun moduleName(fileName: Path) = fileName.toString().substringBeforeLast('.')
internal class ModuleImporter() { internal class ModuleImporter {
internal fun importModule(program: Program, filePath: Path): Module { internal fun importModule(program: Program, filePath: Path): Module {
print("importing '${moduleName(filePath.fileName)}'") print("importing '${moduleName(filePath.fileName)}'")