From 05fc6fa47a7fe679d8c34bc9d240803be5abb28f Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Tue, 30 Oct 2018 09:22:32 +0100 Subject: [PATCH] tweaks --- compiler/src/prog8/ast/AstChecker.kt | 26 ++++++++++--------- .../src/prog8/compiler/target/c64/AsmGen.kt | 6 ++--- .../src/prog8/functions/BuiltinFunctions.kt | 2 +- .../src/prog8/optimizing/ConstantFolding.kt | 12 ++++----- compiler/src/prog8/stackvm/ScreenDialog.kt | 2 +- 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/compiler/src/prog8/ast/AstChecker.kt b/compiler/src/prog8/ast/AstChecker.kt index 8f049151d..25d2331fb 100644 --- a/compiler/src/prog8/ast/AstChecker.kt +++ b/compiler/src/prog8/ast/AstChecker.kt @@ -413,18 +413,20 @@ class AstChecker(private val namespace: INameScope, when(decl.type) { VarDeclType.VAR, VarDeclType.CONST -> { if (decl.value == null) { - if (decl.datatype in NumericDatatypes) { - // initialize numeric var with value zero by default. - val litVal = LiteralValue(DataType.UBYTE, 0, position = decl.position) - litVal.parent = decl - decl.value = litVal - } - else if(decl.type==VarDeclType.VAR) { - val litVal = LiteralValue(decl.datatype, heapId = heapStringSentinel, position=decl.position) // point to the sentinel heap value instead - litVal.parent=decl - decl.value = litVal - } else { - err("var/const declaration needs a compile-time constant initializer value for type ${decl.datatype}") // const fold should have provided it! + when { + decl.datatype in NumericDatatypes -> { + // initialize numeric var with value zero by default. + val litVal = LiteralValue(DataType.UBYTE, 0, position = decl.position) + litVal.parent = decl + decl.value = litVal + } + decl.type==VarDeclType.VAR -> { + val litVal = LiteralValue(decl.datatype, heapId = heapStringSentinel, position=decl.position) // point to the sentinel heap value instead + litVal.parent=decl + decl.value = litVal + } + else -> err("var/const declaration needs a compile-time constant initializer value for type ${decl.datatype}") + // const fold should have provided it! } return super.process(decl) } diff --git a/compiler/src/prog8/compiler/target/c64/AsmGen.kt b/compiler/src/prog8/compiler/target/c64/AsmGen.kt index b95e449e6..47da6bf3f 100644 --- a/compiler/src/prog8/compiler/target/c64/AsmGen.kt +++ b/compiler/src/prog8/compiler/target/c64/AsmGen.kt @@ -1055,9 +1055,9 @@ class AsmGen(val options: CompilationOptions, val program: IntermediateProgram, // store A in target val storeByte = when(segment[2].callLabel) { "A" -> "" - "X" -> " tax" - "Y" -> " tya" - else -> " sta ${segment[2].callLabel}" + "X" -> "tax" + "Y" -> "tya" + else -> "sta ${segment[2].callLabel}" } " $setupPtr | $loadByte | $storeByte" diff --git a/compiler/src/prog8/functions/BuiltinFunctions.kt b/compiler/src/prog8/functions/BuiltinFunctions.kt index 8cf8323bb..6e5ab5517 100644 --- a/compiler/src/prog8/functions/BuiltinFunctions.kt +++ b/compiler/src/prog8/functions/BuiltinFunctions.kt @@ -363,7 +363,7 @@ private fun builtinLen(args: List, position: Position, namespace:IN private fun numericLiteral(value: Number, position: Position): LiteralValue { val floatNum=value.toDouble() val tweakedValue: Number = - if(floatNum==Math.floor(floatNum) && floatNum in -32768..65535) + if(floatNum==Math.floor(floatNum) && (floatNum>=-32768 && floatNum<=65535)) floatNum.toInt() // we have an integer disguised as a float. else floatNum diff --git a/compiler/src/prog8/optimizing/ConstantFolding.kt b/compiler/src/prog8/optimizing/ConstantFolding.kt index 1b44f8dc9..b1fba1eed 100644 --- a/compiler/src/prog8/optimizing/ConstantFolding.kt +++ b/compiler/src/prog8/optimizing/ConstantFolding.kt @@ -100,7 +100,7 @@ class ConstantFolding(private val namespace: INameScope, private val heap: HeapV } else -> {} } - val fillArray = IntArray(size) { _ -> fillvalue } + val fillArray = IntArray(size) { fillvalue } val heapId = heap.add(decl.datatype, fillArray) decl.value = LiteralValue(decl.datatype, heapId = heapId, position = litval?.position ?: decl.position) } @@ -124,7 +124,7 @@ class ConstantFolding(private val namespace: INameScope, private val heap: HeapV if(fillvalue< FLOAT_MAX_NEGATIVE || fillvalue> FLOAT_MAX_POSITIVE) errors.add(ExpressionError("float value overflow", litval?.position ?: decl.position)) else { - val fillArray = DoubleArray(size) { _ -> fillvalue } + val fillArray = DoubleArray(size) { fillvalue } val heapId = heap.add(decl.datatype, fillArray) decl.value = LiteralValue(decl.datatype, heapId = heapId, position = litval?.position ?: decl.position) } @@ -575,7 +575,7 @@ class ConstantFolding(private val namespace: INameScope, private val heap: HeapV assignment.value = LiteralValue(DataType.UWORD, wordvalue = lv.asIntegerValue, position=lv.position) else if(lv.type==DataType.FLOAT) { val d = lv.floatvalue!! - if(floor(d)==d && d in 0..65535) + if(floor(d)==d && d>=0 && d<=65535) assignment.value = LiteralValue(DataType.UWORD, wordvalue=floor(d).toInt(), position=lv.position) } } @@ -587,7 +587,7 @@ class ConstantFolding(private val namespace: INameScope, private val heap: HeapV assignment.value = LiteralValue(DataType.UBYTE, lv.bytevalue.toShort(), position=lv.position) else if(lv.type==DataType.FLOAT) { val d = lv.floatvalue!! - if(floor(d)==d && d in 0..255) + if(floor(d)==d && d >=0 && d<=255) assignment.value = LiteralValue(DataType.UBYTE, floor(d).toShort(), position=lv.position) } } @@ -599,7 +599,7 @@ class ConstantFolding(private val namespace: INameScope, private val heap: HeapV assignment.value = LiteralValue(DataType.BYTE, lv.bytevalue, position=lv.position) else if(lv.type==DataType.FLOAT) { val d = lv.floatvalue!! - if(floor(d)==d && d in 0..127) + if(floor(d)==d && d>=0 && d<=127) assignment.value = LiteralValue(DataType.BYTE, floor(d).toShort(), position=lv.position) } } @@ -611,7 +611,7 @@ class ConstantFolding(private val namespace: INameScope, private val heap: HeapV assignment.value = LiteralValue(DataType.WORD, wordvalue=lv.wordvalue, position=lv.position) else if(lv.type==DataType.FLOAT) { val d = lv.floatvalue!! - if(floor(d)==d && d in -32768..32767) + if(floor(d)==d && d>=-32768 && d<=32767) assignment.value = LiteralValue(DataType.BYTE, floor(d).toShort(), position=lv.position) } } diff --git a/compiler/src/prog8/stackvm/ScreenDialog.kt b/compiler/src/prog8/stackvm/ScreenDialog.kt index eaa27ed39..9c4bac2a2 100644 --- a/compiler/src/prog8/stackvm/ScreenDialog.kt +++ b/compiler/src/prog8/stackvm/ScreenDialog.kt @@ -150,7 +150,7 @@ class ScreenDialog : JFrame() { } fun start() { - val repaintTimer = Timer(1000 / 60) { _ -> repaint() } + val repaintTimer = Timer(1000 / 60) { repaint() } repaintTimer.start() } }