diff --git a/compiler/test/ast/TestVariousCompilerAst.kt b/compiler/test/ast/TestVariousCompilerAst.kt index cafbdb714..dfdd370c3 100644 --- a/compiler/test/ast/TestVariousCompilerAst.kt +++ b/compiler/test/ast/TestVariousCompilerAst.kt @@ -549,5 +549,46 @@ main { tc.type shouldBe DataType.UBYTE tc.expression shouldBe instanceOf() } + + test("multi vardecls smart desugaring") { + val src=""" +main { + sub start() { + ubyte @shared x,y,z + ubyte @shared k,l,m = 42 + uword @shared r,s,t = sys.progend() + } +}""" + val result = compileText(Cx16Target(), optimize=true, src, writeAssembly=false)!! + val st = result.compilerAst.entrypoint.statements + st.size shouldBe 18 + st[0] shouldBe instanceOf() // x + st[2] shouldBe instanceOf() // y + st[4] shouldBe instanceOf() // z + st[6] shouldBe instanceOf() // k + st[8] shouldBe instanceOf() // l + st[10] shouldBe instanceOf() // m + st[12] shouldBe instanceOf() // r + st[14] shouldBe instanceOf() // s + st[16] shouldBe instanceOf() // t + val valX = (st[1] as Assignment).value + (valX as NumericLiteral).number shouldBe 0.0 + val valY = (st[3] as Assignment).value + (valY as NumericLiteral).number shouldBe 0.0 + val valZ = (st[5] as Assignment).value + (valZ as NumericLiteral).number shouldBe 0.0 + val valK = (st[7] as Assignment).value + (valK as NumericLiteral).number shouldBe 42.0 + val valL = (st[9] as Assignment).value + (valL as NumericLiteral).number shouldBe 42.0 + val valM = (st[11] as Assignment).value + (valM as NumericLiteral).number shouldBe 42.0 + val valR = (st[13] as Assignment).value + (valR as FunctionCallExpression).target.nameInSource shouldBe listOf("sys", "progend") + val valS = (st[15] as Assignment).value + (valS as IdentifierReference).nameInSource shouldBe listOf("r") + val valT = (st[17] as Assignment).value + (valT as IdentifierReference).nameInSource shouldBe listOf("r") + } }) diff --git a/compilerAst/src/prog8/ast/statements/AstStatements.kt b/compilerAst/src/prog8/ast/statements/AstStatements.kt index b2ce0269a..d6f12fc13 100644 --- a/compilerAst/src/prog8/ast/statements/AstStatements.kt +++ b/compilerAst/src/prog8/ast/statements/AstStatements.kt @@ -307,7 +307,7 @@ class VarDecl(val type: VarDeclType, this.arraysize?.referencesIdentifier(nameInSource)==true fun desugarMultiDecl(): List { - if(value?.isSimple==true) { + if(value==null || value?.isSimple==true) { // just copy the initialization value to a separata vardecl for each component return names.map { val copy = VarDecl(type, origin, datatype, zeropage, arraysize?.copy(), it, emptyList(), value?.copy(), diff --git a/docs/source/todo.rst b/docs/source/todo.rst index c9f91c54f..82b309640 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,8 +1,6 @@ TODO ==== -ubyte x,y compiles to more code than ubyte x + ubyte y - can we make ubyte x,y = cbm.SCREEN() work? (sugar for ubyte x,y // x,y=cbm.SCREEN() ?) ... diff --git a/examples/test.p8 b/examples/test.p8 index 25bed855b..14c91279c 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,28 +1,11 @@ %import textio -%import verafx %zeropage basicsafe %option no_sysinit main { sub start() { - uword result, resulthi - result, resulthi = verafx.mult(9344, 6522) - txt.print_uwhex(resulthi, true) - txt.spc() - txt.print_uwhex(result, false) - txt.nl() - - word sresult, sresulthi - sresult, sresulthi = verafx.muls(9344, -6522) - txt.print_w(sresulthi) - txt.spc() - txt.print_w(sresult) - txt.nl() - - sresult, sresulthi = verafx.muls(144, -22) - txt.print_w(sresulthi) - txt.spc() - txt.print_w(sresult) - txt.nl() + ubyte @shared x,y,z + ubyte @shared k,l,m = 42 + uword @shared r,s,t = sys.progend() } }