diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt index c89cae6b7..c2ee9b101 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt @@ -794,7 +794,7 @@ $repeatLabel lda $counterVar if(stmt.truepart.isEmpty() && stmt.elsepart.isNotEmpty()) throw AssemblyError("only else part contains code, shoud have been switched already") - val jump = stmt.truepart.statements.first() as? Jump + val jump = stmt.truepart.statements.firstOrNull() as? Jump if(jump!=null) { // branch with only a jump (goto) val instruction = branchInstruction(stmt.condition, false) @@ -812,11 +812,13 @@ $repeatLabel lda $counterVar translate(stmt.elsepart) } else { if(stmt.elsepart.isEmpty()) { - val instruction = branchInstruction(stmt.condition, true) - val elseLabel = program.makeLabel("branch_else") - out(" $instruction $elseLabel") - translate(stmt.truepart) - out(elseLabel) + if(stmt.truepart.isNotEmpty()) { + val instruction = branchInstruction(stmt.condition, true) + val elseLabel = program.makeLabel("branch_else") + out(" $instruction $elseLabel") + translate(stmt.truepart) + out(elseLabel) + } } else { val instruction = branchInstruction(stmt.condition, true) val elseLabel = program.makeLabel("branch_else") diff --git a/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt b/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt index 9bfd3af56..90b47f150 100644 --- a/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt +++ b/compiler/src/prog8/compiler/astprocessing/VariousCleanups.kt @@ -8,6 +8,8 @@ import prog8.ast.base.FatalAstException import prog8.ast.expressions.* import prog8.ast.statements.AnonymousScope import prog8.ast.statements.Assignment +import prog8.ast.statements.ConditionalBranch +import prog8.ast.statements.IfElse import prog8.ast.walk.AstWalker import prog8.ast.walk.IAstModification import prog8.code.core.* @@ -219,5 +221,22 @@ internal class VariousCleanups(val program: Program, val errors: IErrorReporter, } return noModifications } + + override fun after(branch: ConditionalBranch, parent: Node): Iterable { + if(branch.truepart.isEmpty() && branch.elsepart.isEmpty()) { + errors.warn("removing empty conditional branch", branch.position) + return listOf(IAstModification.Remove(branch, parent as IStatementContainer)) + } + + return noModifications + } + + override fun after(ifElse: IfElse, parent: Node): Iterable { + if(ifElse.truepart.isEmpty() && ifElse.elsepart.isEmpty()) { + errors.warn("removing empty if-else statement", ifElse.position) + return listOf(IAstModification.Remove(ifElse, parent as IStatementContainer)) + } + return noModifications + } } diff --git a/compiler/test/ast/TestProg8Parser.kt b/compiler/test/ast/TestProg8Parser.kt index 9a5adeae7..fc4c8eea2 100644 --- a/compiler/test/ast/TestProg8Parser.kt +++ b/compiler/test/ast/TestProg8Parser.kt @@ -888,9 +888,11 @@ class TestProg8Parser: FunSpec( { bool bb ubyte cc if cc in [' ', '@', 0] { + cx16.r0L++ } if cc in "email" { + cx16.r0L++ } bb = 99 in array diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 051556c99..213e55299 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -4,6 +4,7 @@ TODO For next release ^^^^^^^^^^^^^^^^ - get rid of diskio.have_first_byte. But detecting invalid filenames should still work. +- check diskio.status(), fix diskio.f_readline - diskio.f_read doesn't signal end of file condition if the requested number of bytes==1 ? - diskio.f_read doesn't work if used after seek with buffer too small? - ir/vm: check weird asm chunks appearing in block?