removed datatype from StringValue classes (is always STR now)

This commit is contained in:
Irmen de Jong 2020-02-08 02:21:18 +01:00
parent 0ff5b79353
commit 875a71c786
12 changed files with 34 additions and 25 deletions

View File

@ -429,7 +429,7 @@ private fun prog8Parser.ExpressionContext.toAst() : Expression {
else -> throw FatalAstException("invalid datatype for numeric literal") else -> throw FatalAstException("invalid datatype for numeric literal")
} }
litval.floatliteral()!=null -> NumericLiteralValue(DataType.FLOAT, litval.floatliteral().toAst(), litval.toPosition()) litval.floatliteral()!=null -> NumericLiteralValue(DataType.FLOAT, litval.floatliteral().toAst(), litval.toPosition())
litval.stringliteral()!=null -> StringLiteralValue(DataType.STR, unescape(litval.stringliteral().text, litval.toPosition()), litval.toPosition()) litval.stringliteral()!=null -> StringLiteralValue(unescape(litval.stringliteral().text, litval.toPosition()), litval.toPosition())
litval.charliteral()!=null -> { litval.charliteral()!=null -> {
try { try {
NumericLiteralValue(DataType.UBYTE, CompilationTarget.encodeString( NumericLiteralValue(DataType.UBYTE, CompilationTarget.encodeString(

View File

@ -407,8 +407,7 @@ class StructLiteralValue(var values: List<Expression>,
} }
} }
class StringLiteralValue(val type: DataType, // only string types class StringLiteralValue(val value: String,
val value: String,
override val position: Position) : Expression() { override val position: Position) : Expression() {
override lateinit var parent: Node override lateinit var parent: Node
@ -420,13 +419,13 @@ class StringLiteralValue(val type: DataType, // only string types
override fun accept(visitor: IAstModifyingVisitor) = visitor.visit(this) override fun accept(visitor: IAstModifyingVisitor) = visitor.visit(this)
override fun accept(visitor: IAstVisitor) = visitor.visit(this) override fun accept(visitor: IAstVisitor) = visitor.visit(this)
override fun toString(): String = "'${escape(value)}'" override fun toString(): String = "'${escape(value)}'"
override fun inferType(program: Program): InferredTypes.InferredType = InferredTypes.knownFor(type) override fun inferType(program: Program): InferredTypes.InferredType = InferredTypes.knownFor(DataType.STR)
operator fun compareTo(other: StringLiteralValue): Int = value.compareTo(other.value) operator fun compareTo(other: StringLiteralValue): Int = value.compareTo(other.value)
override fun hashCode(): Int = Objects.hash(value, type) override fun hashCode(): Int = value.hashCode()
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if(other==null || other !is StringLiteralValue) if(other==null || other !is StringLiteralValue)
return false return false
return value==other.value && type==other.type return value==other.value
} }
var heapId: Int? = null var heapId: Int? = null

View File

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

View File

@ -331,16 +331,12 @@ internal class AstIdentifiersChecker(private val program: Program) : IAstModifyi
if(constvalue!=null) { if(constvalue!=null) {
if (expr.operator == "*") { if (expr.operator == "*") {
// repeat a string a number of times // repeat a string a number of times
val idt = string.inferType(program) return StringLiteralValue(string.value.repeat(constvalue.number.toInt()), expr.position)
return StringLiteralValue(idt.typeOrElse(DataType.STR),
string.value.repeat(constvalue.number.toInt()), expr.position)
} }
} }
if(expr.operator == "+" && operand is StringLiteralValue) { if(expr.operator == "+" && operand is StringLiteralValue) {
// concatenate two strings // concatenate two strings
val idt = string.inferType(program) return StringLiteralValue("${string.value}${operand.value}", expr.position)
return StringLiteralValue(idt.typeOrElse(DataType.STR),
"${string.value}${operand.value}", expr.position)
} }
return expr return expr
} }

View File

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

View File

@ -146,6 +146,19 @@ 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 -> { else -> {
// nothing to do for this type // nothing to do for this type
// this includes strings and structs // this includes strings and structs

View File

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

View File

@ -626,7 +626,7 @@ class AstVm(val program: Program, compilationTarget: String) {
val ident = contextStmt.definingScope().lookup(targetArrayIndexed.identifier.nameInSource, contextStmt) as? VarDecl val ident = contextStmt.definingScope().lookup(targetArrayIndexed.identifier.nameInSource, contextStmt) as? VarDecl
?: throw VmExecutionException("can't find assignment target ${target.identifier}") ?: throw VmExecutionException("can't find assignment target ${target.identifier}")
val identScope = ident.definingScope() val identScope = ident.definingScope()
runtimeVariables.set(identScope, ident.name, RuntimeValueString(array.type, newstr, array.heapId)) runtimeVariables.set(identScope, ident.name, RuntimeValueString(newstr, array.heapId))
} }
} }
else { else {

View File

@ -124,7 +124,7 @@ fun evaluate(expr: Expression, ctx: EvalContext): RuntimeValueBase {
DataType.UWORD -> RuntimeValueNumeric(DataType.UWORD, ctx.mem.getUWord(address)) DataType.UWORD -> RuntimeValueNumeric(DataType.UWORD, ctx.mem.getUWord(address))
DataType.WORD -> RuntimeValueNumeric(DataType.WORD, ctx.mem.getSWord(address)) DataType.WORD -> RuntimeValueNumeric(DataType.WORD, ctx.mem.getSWord(address))
DataType.FLOAT -> RuntimeValueNumeric(DataType.FLOAT, ctx.mem.getFloat(address)) DataType.FLOAT -> RuntimeValueNumeric(DataType.FLOAT, ctx.mem.getFloat(address))
DataType.STR -> RuntimeValueString(DataType.STR, ctx.mem.getString(address), null) DataType.STR -> RuntimeValueString(ctx.mem.getString(address), null)
else -> throw VmExecutionException("unexpected datatype $variable") else -> throw VmExecutionException("unexpected datatype $variable")
} }
} }

View File

@ -83,8 +83,8 @@ class TestParserNumericLiteralValue {
@Test @Test
fun testEqualsRef() { fun testEqualsRef() {
assertEquals(StringLiteralValue(DataType.STR, "hello", dummyPos), StringLiteralValue(DataType.STR, "hello", dummyPos)) assertEquals(StringLiteralValue("hello", dummyPos), StringLiteralValue("hello", dummyPos))
assertNotEquals(StringLiteralValue(DataType.STR, "hello", dummyPos), StringLiteralValue(DataType.STR, "bye", dummyPos)) assertNotEquals(StringLiteralValue("hello", dummyPos), StringLiteralValue("bye", dummyPos))
val lvOne = NumericLiteralValue(DataType.UBYTE, 1, dummyPos) val lvOne = NumericLiteralValue(DataType.UBYTE, 1, dummyPos)
val lvTwo = NumericLiteralValue(DataType.UBYTE, 2, dummyPos) val lvTwo = NumericLiteralValue(DataType.UBYTE, 2, dummyPos)

View File

@ -369,8 +369,8 @@ class TestPetscii {
assertTrue(ten <= ten) assertTrue(ten <= ten)
assertFalse(ten < ten) assertFalse(ten < ten)
val abc = StringLiteralValue(DataType.STR, "abc", Position("", 0, 0, 0)) val abc = StringLiteralValue("abc", Position("", 0, 0, 0))
val abd = StringLiteralValue(DataType.STR, "abd", Position("", 0, 0, 0)) val abd = StringLiteralValue("abd", Position("", 0, 0, 0))
assertEquals(abc, abc) assertEquals(abc, abc)
assertTrue(abc!=abd) assertTrue(abc!=abd)
assertFalse(abc!=abc) assertFalse(abc!=abc)

View File

@ -10,7 +10,7 @@ main {
c64.VMCSB |= 2 ; switch to lowercase charset c64.VMCSB |= 2 ; switch to lowercase charset
str s1 = "HELLO hello 1234 @[/]\n" str s1 = "HELLO hello 1234 @[/]\n"
str s2 = "HELLO hello 1234 @[/]\n" as c64sc str s2 = c64scr("HELLO hello 1234 @[/]\n")
c64scr.print("\n\n\n\nString output via print:\n") c64scr.print("\n\n\n\nString output via print:\n")
c64scr.print(s1) c64scr.print(s1)
@ -18,9 +18,10 @@ main {
c64scr.print("\nThe top two screen lines are set via screencodes.\n") c64scr.print("\nThe top two screen lines are set via screencodes.\n")
ubyte i ubyte i
for i in 0 to len(s1)-1 { for i in 0 to len(s1)-1
@($0400+i) = s1[i] @($0400+i) = s1[i]
for i in 0 to len(s2)-1
@($0400+40+i) = s2[i] @($0400+40+i) = s2[i]
}
} }
} }