From eacf8b896a70b8a5610e4d4c4b46340445832f5a Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 20 Nov 2021 21:48:30 +0100 Subject: [PATCH] fix augmentable check to align with what the asmgen understands --- .../src/prog8/ast/statements/AstStatements.kt | 14 ++-- compilerAst/src/prog8/ast/walk/AstWalker.kt | 4 +- examples/test.p8 | 79 +++++++++++-------- 3 files changed, 56 insertions(+), 41 deletions(-) diff --git a/compilerAst/src/prog8/ast/statements/AstStatements.kt b/compilerAst/src/prog8/ast/statements/AstStatements.kt index 6c720d56a..02f5d826b 100644 --- a/compilerAst/src/prog8/ast/statements/AstStatements.kt +++ b/compilerAst/src/prog8/ast/statements/AstStatements.kt @@ -302,7 +302,7 @@ class ArrayIndex(var indexExpr: Expression, } fun accept(visitor: IAstVisitor) = indexExpr.accept(visitor) - fun accept(visitor: AstWalker, parent: Node) = indexExpr.accept(visitor, this) + fun accept(visitor: AstWalker) = indexExpr.accept(visitor, this) override fun toString(): String { return("ArrayIndex($indexExpr, pos=$position)") @@ -353,13 +353,13 @@ open class Assignment(var target: AssignTarget, var value: Expression, final ove if(binExpr.operator in "+-") { val leftBinExpr = binExpr.left as? BinaryExpression - if(leftBinExpr!=null && leftBinExpr.operator in "+-") { + val rightBinExpr = binExpr.right as? BinaryExpression + if(rightBinExpr==null && leftBinExpr!=null && leftBinExpr.operator in "+-") { // A = (A +- x) +- y if(leftBinExpr.left isSameAs target || leftBinExpr.right isSameAs target || binExpr.right isSameAs target) return true } - val rightBinExpr = binExpr.right as? BinaryExpression - if(rightBinExpr!=null && rightBinExpr.operator in "+-") { + if(leftBinExpr==null && rightBinExpr!=null && rightBinExpr.operator in "+-") { // A = y +- (A +- x) if(rightBinExpr.left isSameAs target || rightBinExpr.right isSameAs target || binExpr.left isSameAs target) return true @@ -371,15 +371,15 @@ open class Assignment(var target: AssignTarget, var value: Expression, final ove return true // A = v A val leftBinExpr = binExpr.left as? BinaryExpression - if(leftBinExpr?.operator == binExpr.operator) { + val rightBinExpr = binExpr.right as? BinaryExpression + if(leftBinExpr?.operator == binExpr.operator && rightBinExpr==null) { // one of these? // A = (A x) y // A = (x A) y // A = (x y) A return leftBinExpr.left isSameAs target || leftBinExpr.right isSameAs target || binExpr.right isSameAs target } - val rightBinExpr = binExpr.right as? BinaryExpression - if(rightBinExpr?.operator == binExpr.operator) { + if(rightBinExpr?.operator == binExpr.operator && leftBinExpr==null) { // one of these? // A = y (A x) // A = y (x y) diff --git a/compilerAst/src/prog8/ast/walk/AstWalker.kt b/compilerAst/src/prog8/ast/walk/AstWalker.kt index 21c66c77e..87ab13d11 100644 --- a/compilerAst/src/prog8/ast/walk/AstWalker.kt +++ b/compilerAst/src/prog8/ast/walk/AstWalker.kt @@ -235,7 +235,7 @@ abstract class AstWalker { fun visit(decl: VarDecl, parent: Node) { track(before(decl, parent), decl, parent) decl.value?.accept(this, decl) - decl.arraysize?.accept(this, decl) + decl.arraysize?.accept(this) track(after(decl, parent), decl, parent) } @@ -375,7 +375,7 @@ abstract class AstWalker { fun visit(arrayIndexedExpression: ArrayIndexedExpression, parent: Node) { track(before(arrayIndexedExpression, parent), arrayIndexedExpression, parent) arrayIndexedExpression.arrayvar.accept(this, arrayIndexedExpression) - arrayIndexedExpression.indexer.accept(this, arrayIndexedExpression) + arrayIndexedExpression.indexer.accept(this) track(after(arrayIndexedExpression, parent), arrayIndexedExpression, parent) } diff --git a/examples/test.p8 b/examples/test.p8 index 2247c11b4..dd764f0d2 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,5 +1,4 @@ %import textio -%import floats main { @@ -7,46 +6,62 @@ main { ubyte xx = 1 ubyte yy = 2 - uword aw - byte bb - float fl ;; TODO add these constant folders: ; ;; (X + C1) + (Y + C2) => (X + Y) + (C1 + C2) ;; (X + C1) - (Y + C2) => (X - Y) + (C1 - C2) ;; ---> together: (X + C1) (Y + C2) => (X Y) + (C1 C2) -; + +;; (X * C) + (Y * C) => (X + Y) * C +;; (X * C) - (Y * C) => (X - Y) * C +;; ---> together: (X * C) (Y * C) => (X Y) * C + ; ;; (X - C1) + (Y - C2) => (X + Y) - (C1 + C2) ;; (X - C1) - (Y - C2) => (X - Y) - (C1 - C2) ; -; xx=6 -; yy=8 -; -; yy = (xx+5)+(yy+10) -; txt.print_ub(yy) ; 29 -; txt.nl() -; -; xx=100 -; yy=8 -; yy = (xx+5)-(yy+10) -; txt.print_ub(yy) ; 87 -; txt.nl() -; -; xx=50 -; yy=40 -; yy = (xx-5)+(yy-10) -; txt.print_ub(yy) ; 75 -; txt.nl() -; -; xx=50 -; yy=20 -; yy = (xx-5)-(yy-10) -; txt.print_ub(yy) ; 35 -; txt.nl() -; -; repeat { -; } + + + ; result should be: 29 42 40 87 75 35 + + xx=6 + yy=8 + yy = (xx+5)+(yy+10) + txt.print_ub(yy) ; 29 + txt.nl() + + xx=6 + yy=8 + yy = (xx*3)+(yy*3) + txt.print_ub(yy) ; 42 + txt.nl() + + xx=13 + yy=5 + yy = (xx*5)-(yy*5) + txt.print_ub(yy) ; 40 + txt.nl() + + xx=100 + yy=8 + yy = (xx+5)-(yy+10) + txt.print_ub(yy) ; 87 + txt.nl() + + xx=50 + yy=40 + yy = (xx-5)+(yy-10) + txt.print_ub(yy) ; 75 + txt.nl() + + xx=50 + yy=20 + yy = (xx-5)-(yy-10) + txt.print_ub(yy) ; 35 + txt.nl() + + repeat { + } } }