simplified heapId for stringvalue

This commit is contained in:
Irmen de Jong 2020-02-08 15:54:03 +01:00
parent 875a71c786
commit 14a13da7ec
6 changed files with 6 additions and 34 deletions

View File

@ -407,10 +407,14 @@ class StructLiteralValue(var values: List<Expression>,
}
}
private var heapIdSequence = 0 // unique ids for strings and arrays "on the heap"
class StringLiteralValue(val value: String,
override val position: Position) : Expression() {
override lateinit var parent: Node
val heapId = ++heapIdSequence
override fun linkParents(parent: Node) {
this.parent = parent
}
@ -427,19 +431,8 @@ class StringLiteralValue(val value: String,
return false
return value==other.value
}
var heapId: Int? = null
private set
fun addToHeap() {
if(heapId==null)
heapId = ++heapIdSequence
}
}
private var heapIdSequence = 0
class ArrayLiteralValue(val type: DataType, // only array types
val value: Array<Expression>,
initHeapId: Int? =null,
@ -648,7 +641,7 @@ data class IdentifierReference(val nameInSource: List<String>, override val posi
val value = (node as? VarDecl)?.value ?: throw FatalAstException("requires a reference value")
return when (value) {
is IdentifierReference -> value.heapId(namespace)
is StringLiteralValue -> value.heapId ?: throw FatalAstException("string is not on the heap: $value")
is StringLiteralValue -> value.heapId
is ArrayLiteralValue -> value.heapId ?: throw FatalAstException("array is not on the heap: $value")
else -> throw FatalAstException("requires a reference value")
}

View File

@ -702,8 +702,6 @@ internal class AstChecker(private val program: Program,
override fun visit(string: StringLiteralValue) {
checkValueTypeAndRangeString(DataType.STR, string)
super.visit(string)
if(string.heapId==null)
throw FatalAstException("string should have been moved to heap at ${string.position}")
}
override fun visit(expr: PrefixExpression) {

View File

@ -253,9 +253,6 @@ internal class AstIdentifiersChecker(private val program: Program) : IAstModifyi
// intern the string; move it into the heap
if (string.value.length !in 1..255)
checkResult.add(ExpressionError("string literal length must be between 1 and 255", string.position))
else {
string.addToHeap()
}
return if (vardecl != null)
string
else
@ -292,7 +289,6 @@ internal class AstIdentifiersChecker(private val program: Program) : IAstModifyi
// a referencetype literal value that's not declared as a variable
// we need to introduce an auto-generated variable for this to be able to refer to the value
// note: if the var references the same literal value, it is not yet de-duplicated here.
string.addToHeap()
val scope = string.definingScope()
val variable = VarDecl.createAuto(string)
return replaceWithIdentifier(variable, scope, string.parent)

View File

@ -192,8 +192,6 @@ class VarDecl(val type: VarDeclType,
private var autoHeapValueSequenceNumber = 0
fun createAuto(string: StringLiteralValue): VarDecl {
if(string.heapId==null)
throw FatalAstException("can only create autovar for a string that has a heapid $string")
val autoVarName = "auto_heap_value_${++autoHeapValueSequenceNumber}"
return VarDecl(VarDeclType.VAR, DataType.STR, ZeropageWish.NOT_IN_ZEROPAGE, null, autoVarName, null, string,
isArray = false, autogeneratedDontRemove = true, position = string.position)

View File

@ -146,19 +146,6 @@ class ConstantFolding(private val program: Program) : IAstModifyingVisitor {
}
}
}
DataType.STR -> {
TODO("$decl")
// if(functionCall.target.nameInSource == listOf("c64scr")) {
// val parent = functionCall.parent
// if(parent is VarDecl && parent.datatype==DataType.STR && functionCall.args.size == 1) {
// val str = functionCall.args[0] as IdentifierReference
// if(str!=null) {
// val decl = str.targetVarDecl(functionCall.definingScope())
// TODO("$decl")
// }
// }
// }
}
else -> {
// nothing to do for this type
// this includes strings and structs

View File

@ -573,7 +573,7 @@ class RuntimeValueNumeric(type: DataType, num: Number): RuntimeValueBase(type) {
class RuntimeValueString(val str: String, val heapId: Int?): RuntimeValueBase(DataType.STR) {
companion object {
fun fromLv(string: StringLiteralValue): RuntimeValueString {
return RuntimeValueString(string.value, string.heapId!!)
return RuntimeValueString(string.value, string.heapId)
}
}