nicer printing of arrays, fix inc/dec overflow issue in runtimevalue

This commit is contained in:
Irmen de Jong 2019-07-10 01:16:32 +02:00
parent dcab6d00bb
commit c9b16dcbd9
2 changed files with 41 additions and 4 deletions

View File

@ -249,12 +249,21 @@ class AstToSourceCode(val output: (text: String) -> Unit): IAstVisitor {
literalValue.isString -> output("\"${escape(literalValue.strvalue!!)}\"")
literalValue.isArray -> {
if(literalValue.arrayvalue!=null) {
var counter = 0
output("[")
scopelevel++
for (v in literalValue.arrayvalue) {
v.accept(this)
if (v !== literalValue.arrayvalue.last())
output(", ")
counter++
if(counter > 16) {
outputln("")
outputi("")
counter=0
}
}
scopelevel--
output("]")
}
}

View File

@ -427,8 +427,22 @@ open class RuntimeValue(val type: DataType, num: Number?=null, val str: String?=
fun inc(): RuntimeValue {
return when(type) {
in ByteDatatypes -> RuntimeValue(type, byteval!! + 1)
in WordDatatypes -> RuntimeValue(type, wordval!! + 1)
DataType.UBYTE -> RuntimeValue(type, (byteval!! + 1) and 255)
DataType.UWORD -> RuntimeValue(type, (byteval!! + 1) and 65535)
DataType.BYTE -> {
val newval = byteval!! + 1
if(newval == 256)
RuntimeValue(type, 0)
else
RuntimeValue(type, newval)
}
DataType.WORD -> {
val newval = byteval!! + 1
if(newval == 65536)
RuntimeValue(type, 0)
else
RuntimeValue(type, newval)
}
DataType.FLOAT -> RuntimeValue(DataType.FLOAT, floatval!! + 1)
else -> throw ArithmeticException("inc can only work on numeric types")
}
@ -436,8 +450,22 @@ open class RuntimeValue(val type: DataType, num: Number?=null, val str: String?=
fun dec(): RuntimeValue {
return when(type) {
in ByteDatatypes -> RuntimeValue(type, byteval!! - 1)
in WordDatatypes -> RuntimeValue(type, wordval!! - 1)
DataType.UBYTE -> RuntimeValue(type, (byteval!! - 1) and 255)
DataType.UWORD -> RuntimeValue(type, (byteval!! - 1) and 65535)
DataType.BYTE -> {
val newval = byteval!! - 1
if(newval == -129)
RuntimeValue(type, 127)
else
RuntimeValue(type, newval)
}
DataType.WORD -> {
val newval = byteval!! - 1
if(newval == -32769)
RuntimeValue(type, 32767)
else
RuntimeValue(type, newval)
}
DataType.FLOAT -> RuntimeValue(DataType.FLOAT, floatval!! - 1)
else -> throw ArithmeticException("dec can only work on numeric types")
}