heapId writable

This commit is contained in:
Irmen de Jong 2019-07-01 14:10:52 +02:00
parent 3a1fa9e069
commit 2c69e10489
3 changed files with 12 additions and 15 deletions

View File

@ -1298,7 +1298,7 @@ open class LiteralValue(val type: DataType,
val floatvalue: Double? = null,
strvalue: String? = null,
val arrayvalue: Array<IExpression>? = null,
val heapId: Int? =null,
var heapId: Int? =null,
override val position: Position) : IExpression {
override lateinit var parent: Node
private val initialstrvalue = strvalue

View File

@ -229,9 +229,8 @@ private fun collectionArgOutputNumber(args: List<IExpression>, position: Positio
when(iterable.type) {
DataType.UBYTE, DataType.UWORD, DataType.FLOAT -> throw SyntaxError("function expects an iterable type", position)
else -> {
if(iterable.heapId==null)
throw FatalAstException("iterable value should be on the heap")
val array = program.heap.get(iterable.heapId).array ?: throw SyntaxError("function expects an iterable type", position)
val heapId = iterable.heapId ?: throw FatalAstException("iterable value should be on the heap")
val array = program.heap.get(heapId).array ?: throw SyntaxError("function expects an iterable type", position)
function(array.map {
if(it.integer!=null)
it.integer.toDouble()
@ -293,7 +292,8 @@ private fun builtinAvg(args: List<IExpression>, position: Position, program: Pro
(constants.map { it!!.toDouble() }).average()
}
else {
val integerarray = program.heap.get(iterable.heapId!!).array
val heapId = iterable.heapId!!
val integerarray = program.heap.get(heapId).array
if(integerarray!=null) {
if (integerarray.all { it.integer != null }) {
integerarray.map { it.integer!! }.average()
@ -301,7 +301,7 @@ private fun builtinAvg(args: List<IExpression>, position: Position, program: Pro
throw ExpressionError("cannot avg() over array that does not only contain constant numerical values", position)
}
} else {
val doublearray = program.heap.get(iterable.heapId).doubleArray
val doublearray = program.heap.get(heapId).doubleArray
doublearray?.average() ?: throw SyntaxError("avg requires array argument", position)
}
}

View File

@ -598,14 +598,13 @@ class ConstantFolding(private val program: Program) : IAstProcessor {
if(litval.strvalue(program.heap).length !in 1..255)
addError(ExpressionError("string literal length must be between 1 and 255", litval.position))
else {
val heapId = program.heap.addString(litval.type, litval.strvalue(program.heap)) // TODO: we don't know the actual string type yet, STR != STR_S etc...
val newValue = LiteralValue(litval.type, heapId = heapId, position = litval.position)
return super.process(newValue)
litval.heapId = program.heap.addString(litval.type, litval.strvalue(program.heap)) // TODO: we don't know the actual string type yet, STR != STR_S etc...
}
} else if(litval.arrayvalue!=null) {
// first, adjust the array datatype
val litval2 = adjustArrayValDatatype(litval)
return moveArrayToHeap(litval2)
moveArrayToHeap(litval2)
return litval2
}
return litval
}
@ -650,7 +649,7 @@ class ConstantFolding(private val program: Program) : IAstProcessor {
return litval
}
private fun moveArrayToHeap(arraylit: LiteralValue): LiteralValue {
private fun moveArrayToHeap(arraylit: LiteralValue) {
val array: Array<IExpression> = arraylit.arrayvalue!!.map { it.process(this) }.toTypedArray()
if(array.any {it is AddressOf}) {
val intArrayWithAddressOfs = array.map {
@ -660,12 +659,11 @@ class ConstantFolding(private val program: Program) : IAstProcessor {
else -> throw CompilerException("invalid datatype in array")
}
}
val heapId = program.heap.addIntegerArray(arraylit.type, intArrayWithAddressOfs.toTypedArray())
return LiteralValue(arraylit.type, heapId = heapId, position = arraylit.position)
arraylit.heapId = program.heap.addIntegerArray(arraylit.type, intArrayWithAddressOfs.toTypedArray())
} else {
// array is only constant numerical values
val valuesInArray = array.map { it.constValue(program)!!.asNumericValue!! }
val heapId = when(arraylit.type) {
arraylit.heapId = when(arraylit.type) {
DataType.ARRAY_UB,
DataType.ARRAY_B,
DataType.ARRAY_UW,
@ -679,7 +677,6 @@ class ConstantFolding(private val program: Program) : IAstProcessor {
}
else -> throw CompilerException("invalid arraysize type")
}
return LiteralValue(arraylit.type, heapId = heapId, position = arraylit.position)
}
}