diff --git a/ast/src/main/kotlin/gay/pizza/pork/ast/Break.kt b/ast/src/main/kotlin/gay/pizza/pork/ast/Break.kt index 7a61f95..c9b97d4 100644 --- a/ast/src/main/kotlin/gay/pizza/pork/ast/Break.kt +++ b/ast/src/main/kotlin/gay/pizza/pork/ast/Break.kt @@ -12,10 +12,8 @@ class Break : Expression() { override fun visit(visitor: NodeVisitor): T = visitor.visitBreak(this) - override fun equals(other: Any?): Boolean { - if (other !is Break) return false - return true - } + override fun equals(other: Any?): Boolean = + other is Break override fun hashCode(): Int = 31 * type.hashCode() diff --git a/ast/src/main/kotlin/gay/pizza/pork/ast/Continue.kt b/ast/src/main/kotlin/gay/pizza/pork/ast/Continue.kt index fca1a2c..b9618ca 100644 --- a/ast/src/main/kotlin/gay/pizza/pork/ast/Continue.kt +++ b/ast/src/main/kotlin/gay/pizza/pork/ast/Continue.kt @@ -12,10 +12,8 @@ class Continue : Expression() { override fun visit(visitor: NodeVisitor): T = visitor.visitContinue(this) - override fun equals(other: Any?): Boolean { - if (other !is Continue) return false - return true - } + override fun equals(other: Any?): Boolean = + other is Continue override fun hashCode(): Int = 31 * type.hashCode() diff --git a/ast/src/main/kotlin/gay/pizza/pork/ast/NoneLiteral.kt b/ast/src/main/kotlin/gay/pizza/pork/ast/NoneLiteral.kt index 06357d3..2bba70d 100644 --- a/ast/src/main/kotlin/gay/pizza/pork/ast/NoneLiteral.kt +++ b/ast/src/main/kotlin/gay/pizza/pork/ast/NoneLiteral.kt @@ -12,10 +12,8 @@ class NoneLiteral : Expression() { override fun visit(visitor: NodeVisitor): T = visitor.visitNoneLiteral(this) - override fun equals(other: Any?): Boolean { - if (other !is NoneLiteral) return false - return true - } + override fun equals(other: Any?): Boolean = + other is NoneLiteral override fun hashCode(): Int = 31 * type.hashCode() diff --git a/buildext/src/main/kotlin/gay/pizza/pork/buildext/ast/AstStandardCodegen.kt b/buildext/src/main/kotlin/gay/pizza/pork/buildext/ast/AstStandardCodegen.kt index a0d30d3..075d0a1 100644 --- a/buildext/src/main/kotlin/gay/pizza/pork/buildext/ast/AstStandardCodegen.kt +++ b/buildext/src/main/kotlin/gay/pizza/pork/buildext/ast/AstStandardCodegen.kt @@ -421,9 +421,10 @@ class AstStandardCodegen(pkg: String, outputDirectory: Path, world: AstWorld) : visitSelfFunction.body.add("visitor.visit${type.name}(this)") kotlinClassLike.functions.add(visitSelfFunction) - val equalsAndHashCodeMembers = kotlinClassLike.members.map { + val hashCodeMembers = kotlinClassLike.members.map { it.name }.sortedBy { it == "type" } + val equalsMembers = hashCodeMembers.filterNot { it == "type" } val equalsFunction = KotlinFunction( "equals", returnType = "Boolean", @@ -433,14 +434,19 @@ class AstStandardCodegen(pkg: String, outputDirectory: Path, world: AstWorld) : "other", "Any?" )) - equalsFunction.body.add("if (other !is ${type.name}) return false") - var predicate = equalsAndHashCodeMembers.mapNotNull { - if (it == "type") null else "other.${it} == $it" - }.joinToString(" && ") - if (predicate.isEmpty()) { - predicate = "true" + if (equalsMembers.isNotEmpty()) { + equalsFunction.body.add("if (other !is ${type.name}) return false") + var predicate = equalsMembers.joinToString(" && ") { + "other.${it} == $it" + } + if (predicate.isEmpty()) { + predicate = "true" + } + equalsFunction.body.add("return $predicate") + } else { + equalsFunction.isImmediateExpression = true + equalsFunction.body.add("other is ${type.name}") } - equalsFunction.body.add("return $predicate") kotlinClassLike.functions.add(equalsFunction) val hashCodeFunction = KotlinFunction( @@ -449,12 +455,12 @@ class AstStandardCodegen(pkg: String, outputDirectory: Path, world: AstWorld) : overridden = true ) - if (equalsAndHashCodeMembers.size == 1) { - val member = equalsAndHashCodeMembers.single() + if (hashCodeMembers.size == 1) { + val member = hashCodeMembers.single() hashCodeFunction.isImmediateExpression = true hashCodeFunction.body.add("31 * ${member}.hashCode()") } else { - for ((index, value) in equalsAndHashCodeMembers.withIndex()) { + for ((index, value) in hashCodeMembers.withIndex()) { if (index == 0) { hashCodeFunction.body.add("var result = ${value}.hashCode()") } else { diff --git a/tool/src/main/kotlin/gay/pizza/pork/tool/AttributeCommand.kt b/tool/src/main/kotlin/gay/pizza/pork/tool/AttributeCommand.kt index ff68c98..f145f75 100644 --- a/tool/src/main/kotlin/gay/pizza/pork/tool/AttributeCommand.kt +++ b/tool/src/main/kotlin/gay/pizza/pork/tool/AttributeCommand.kt @@ -3,9 +3,7 @@ package gay.pizza.pork.tool import com.github.ajalt.clikt.core.CliktCommand import com.github.ajalt.clikt.parameters.arguments.argument import gay.pizza.dough.fs.PlatformFsProvider -import gay.pizza.pork.ast.Node import gay.pizza.pork.ast.NodeCoalescer -import gay.pizza.pork.ast.data import gay.pizza.pork.ast.visit import gay.pizza.pork.minimal.FileTool import gay.pizza.pork.parser.ParserAttributes