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")
}
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 -> {
try {
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
val value: String,
class StringLiteralValue(val value: String,
override val position: Position) : Expression() {
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: IAstVisitor) = visitor.visit(this)
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)
override fun hashCode(): Int = Objects.hash(value, type)
override fun hashCode(): Int = value.hashCode()
override fun equals(other: Any?): Boolean {
if(other==null || other !is StringLiteralValue)
return false
return value==other.value && type==other.type
return value==other.value
}
var heapId: Int? = null

View File

@ -700,7 +700,7 @@ internal class AstChecker(private val program: Program,
}
override fun visit(string: StringLiteralValue) {
checkValueTypeAndRangeString(string.type, string)
checkValueTypeAndRangeString(DataType.STR, string)
super.visit(string)
if(string.heapId==null)
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 (expr.operator == "*") {
// repeat a string a number of times
val idt = string.inferType(program)
return StringLiteralValue(idt.typeOrElse(DataType.STR),
string.value.repeat(constvalue.number.toInt()), expr.position)
return StringLiteralValue(string.value.repeat(constvalue.number.toInt()), expr.position)
}
}
if(expr.operator == "+" && operand is StringLiteralValue) {
// concatenate two strings
val idt = string.inferType(program)
return StringLiteralValue(idt.typeOrElse(DataType.STR),
"${string.value}${operand.value}", expr.position)
return StringLiteralValue("${string.value}${operand.value}", expr.position)
}
return expr
}

View File

@ -195,7 +195,7 @@ class VarDecl(val type: VarDeclType,
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, 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)
}

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 -> {
// nothing to do for this type
// 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 {
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
?: throw VmExecutionException("can't find assignment target ${target.identifier}")
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 {

View File

@ -124,7 +124,7 @@ fun evaluate(expr: Expression, ctx: EvalContext): RuntimeValueBase {
DataType.UWORD -> RuntimeValueNumeric(DataType.UWORD, ctx.mem.getUWord(address))
DataType.WORD -> RuntimeValueNumeric(DataType.WORD, ctx.mem.getSWord(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")
}
}

View File

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

View File

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

View File

@ -10,7 +10,7 @@ main {
c64.VMCSB |= 2 ; switch to lowercase charset
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(s1)
@ -18,9 +18,10 @@ main {
c64scr.print("\nThe top two screen lines are set via screencodes.\n")
ubyte i
for i in 0 to len(s1)-1 {
for i in 0 to len(s1)-1
@($0400+i) = s1[i]
for i in 0 to len(s2)-1
@($0400+40+i) = s2[i]
}
}
}