From 932bbd0381635ab0d320d867c743a64886b74310 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Fri, 29 Dec 2023 16:23:24 +0100 Subject: [PATCH] allow casting of byte<->ubyte and word<->uword --- .../prog8/ast/expressions/AstExpressions.kt | 8 ++-- docs/source/todo.rst | 1 - examples/test.p8 | 46 +++++++------------ 3 files changed, 21 insertions(+), 34 deletions(-) diff --git a/compilerAst/src/prog8/ast/expressions/AstExpressions.kt b/compilerAst/src/prog8/ast/expressions/AstExpressions.kt index b232e304d..cbd0d745d 100644 --- a/compilerAst/src/prog8/ast/expressions/AstExpressions.kt +++ b/compilerAst/src/prog8/ast/expressions/AstExpressions.kt @@ -577,8 +577,8 @@ class NumericLiteral(val type: DataType, // only numerical types allowed return CastValue(true, null, this) when(type) { DataType.UBYTE -> { - if(targettype==DataType.BYTE && number <= 127) - return CastValue(true, null, NumericLiteral(targettype, number, position)) + if(targettype==DataType.BYTE) + return CastValue(true, null, NumericLiteral(targettype, number.toInt().toByte().toDouble(), position)) if(targettype==DataType.WORD || targettype==DataType.UWORD) return CastValue(true, null, NumericLiteral(targettype, number, position)) if(targettype==DataType.FLOAT) @@ -615,8 +615,8 @@ class NumericLiteral(val type: DataType, // only numerical types allowed return CastValue(true, null, NumericLiteral(targettype, number, position)) if(targettype==DataType.UBYTE && number <= 255) return CastValue(true, null, NumericLiteral(targettype, number, position)) - if(targettype==DataType.WORD && number <= 32767) - return CastValue(true, null, NumericLiteral(targettype, number, position)) + if(targettype==DataType.WORD) + return CastValue(true, null, NumericLiteral(targettype, number.toInt().toShort().toDouble(), position)) if(targettype==DataType.FLOAT) return CastValue(true, null, NumericLiteral(targettype, number, position)) if(targettype==DataType.BOOL) diff --git a/docs/source/todo.rst b/docs/source/todo.rst index b03a69203..5c7c3f52a 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -2,7 +2,6 @@ TODO ==== -- make internalCast() not complain anymore about signed <-> unsigned conversions - fix bitshift.p8 - [on branch: shortcircuit] investigate McCarthy evaluation again? this may also reduce code size perhaps for things like if a>4 or a<2 .... diff --git a/examples/test.p8 b/examples/test.p8 index 72259fc75..0ef1c72b8 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -4,38 +4,26 @@ main { sub start() { - str poem_data = iso:"Once upon a midnight dreary, while I pondered, weak and weary,"+ - iso:"Over many a quaint and curious volume of forgotten lore-"+ - iso:"While I nodded, nearly napping, suddenly there came a tapping,"+ - iso:"As of some one gently rapping, rapping at my chamber door. ..." - uword size = len(poem_data) - - cbm.SETTIM(0,0,0) - repeat 20 { - cx16.r9 = math.crc16(poem_data, size) - } - txt.print_uwhex(cx16.r9, true) - txt.spc() - txt.print_uw(cbm.RDTIM16()) + uword @shared x = 65535 + word @shared y = x as word + txt.print_w(y) + txt.nl() + txt.print_w(x as word) txt.nl() - cbm.SETTIM(0,0,0) - repeat 20 { - cx16.r9 = cx16.memory_crc(poem_data, size) ; faster but I can't figure out the flavour of crc algorithm it uses, it's not any on https://crccalc.com/ - } - txt.print_uwhex(cx16.r9, true) - txt.spc() - txt.print_uw(cbm.RDTIM16()) + word @shared x2 = -1 + uword @shared y2 = x2 as uword + txt.print_uw(y2) + txt.nl() + txt.print_uw(x2 as uword) txt.nl() - cbm.SETTIM(0,0,0) - repeat 20 { - math.crc32(poem_data, size) - } - txt.print_uwhex(cx16.r1, true) - txt.print_uwhex(cx16.r0, false) - txt.spc() - txt.print_uw(cbm.RDTIM16()) - txt.nl() + txt.print_uw(shiftluw1()) } + +; sub shiftluw1() -> uword { +; uword q = $a49f +; return (q << 1) & 65535 as uword +; } + }