Skip to content

Commit

Permalink
ast: utilize extension functions to prevent larger stack frames from …
Browse files Browse the repository at this point in the history
…default interface methods
  • Loading branch information
azenla committed Sep 6, 2023
1 parent 290d8d0 commit 9f90e05
Show file tree
Hide file tree
Showing 53 changed files with 480 additions and 271 deletions.
4 changes: 4 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/Block.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
Expand All @@ -11,6 +12,9 @@ class Block(val expressions: List<Expression>) : Node() {
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitAll(expressions)

override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitBlock(this)

override fun equals(other: Any?): Boolean {
if (other !is Block) return false
return other.expressions == expressions
Expand Down
4 changes: 4 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/BooleanLiteral.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
Expand All @@ -8,6 +9,9 @@ import kotlinx.serialization.Serializable
class BooleanLiteral(val value: Boolean) : Expression() {
override val type: NodeType = NodeType.BooleanLiteral

override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitBooleanLiteral(this)

override fun equals(other: Any?): Boolean {
if (other !is BooleanLiteral) return false
return other.value == value
Expand Down
4 changes: 4 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/CompilationUnit.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
Expand All @@ -11,6 +12,9 @@ class CompilationUnit(val declarations: List<Declaration>, val definitions: List
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitAll(declarations, definitions)

override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitCompilationUnit(this)

override fun equals(other: Any?): Boolean {
if (other !is CompilationUnit) return false
return other.declarations == declarations && other.definitions == definitions
Expand Down
1 change: 1 addition & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/Declaration.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
Expand Down
1 change: 1 addition & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/Definition.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
Expand Down
1 change: 1 addition & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/Expression.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
Expand Down
4 changes: 4 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/FunctionCall.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
Expand All @@ -11,6 +12,9 @@ class FunctionCall(val symbol: Symbol, val arguments: List<Expression>) : Expres
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitAll(listOf(symbol), arguments)

override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitFunctionCall(this)

override fun equals(other: Any?): Boolean {
if (other !is FunctionCall) return false
return other.symbol == symbol && other.arguments == arguments
Expand Down
4 changes: 4 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/FunctionDefinition.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
Expand All @@ -11,6 +12,9 @@ class FunctionDefinition(override val modifiers: DefinitionModifiers, override v
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitAll(listOf(symbol), arguments, listOf(block))

override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitFunctionDefinition(this)

override fun equals(other: Any?): Boolean {
if (other !is FunctionDefinition) return false
return other.modifiers == modifiers && other.symbol == symbol && other.arguments == arguments && other.block == block
Expand Down
4 changes: 4 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/If.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
Expand All @@ -11,6 +12,9 @@ class If(val condition: Expression, val thenExpression: Expression, val elseExpr
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitNodes(condition, thenExpression, elseExpression)

override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitIf(this)

override fun equals(other: Any?): Boolean {
if (other !is If) return false
return other.condition == condition && other.thenExpression == thenExpression && other.elseExpression == elseExpression
Expand Down
4 changes: 4 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/ImportDeclaration.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
Expand All @@ -11,6 +12,9 @@ class ImportDeclaration(val path: StringLiteral) : Declaration() {
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitNodes(path)

override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitImportDeclaration(this)

override fun equals(other: Any?): Boolean {
if (other !is ImportDeclaration) return false
return other.path == path
Expand Down
4 changes: 4 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/InfixOperation.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
Expand All @@ -11,6 +12,9 @@ class InfixOperation(val left: Expression, val op: InfixOperator, val right: Exp
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitNodes(left, right)

override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitInfixOperation(this)

override fun equals(other: Any?): Boolean {
if (other !is InfixOperation) return false
return other.left == left && other.op == op && other.right == right
Expand Down
1 change: 1 addition & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/InfixOperator.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
Expand Down
4 changes: 4 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/IntLiteral.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
Expand All @@ -8,6 +9,9 @@ import kotlinx.serialization.Serializable
class IntLiteral(val value: Int) : Expression() {
override val type: NodeType = NodeType.IntLiteral

override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitIntLiteral(this)

override fun equals(other: Any?): Boolean {
if (other !is IntLiteral) return false
return other.value == value
Expand Down
4 changes: 4 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/Lambda.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
Expand All @@ -11,6 +12,9 @@ class Lambda(val arguments: List<Symbol>, val expressions: List<Expression>) : E
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitAll(arguments, expressions)

override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitLambda(this)

override fun equals(other: Any?): Boolean {
if (other !is Lambda) return false
return other.arguments == arguments && other.expressions == expressions
Expand Down
4 changes: 4 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/LetAssignment.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
Expand All @@ -11,6 +12,9 @@ class LetAssignment(val symbol: Symbol, val value: Expression) : Expression() {
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitNodes(symbol, value)

override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitLetAssignment(this)

override fun equals(other: Any?): Boolean {
if (other !is LetAssignment) return false
return other.symbol == symbol && other.value == value
Expand Down
4 changes: 4 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/ListLiteral.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
Expand All @@ -11,6 +12,9 @@ class ListLiteral(val items: List<Expression>) : Expression() {
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitAll(items)

override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitListLiteral(this)

override fun equals(other: Any?): Boolean {
if (other !is ListLiteral) return false
return other.items == items
Expand Down
4 changes: 4 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/Node.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
Expand All @@ -10,4 +11,7 @@ sealed class Node {

open fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
emptyList()

open fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visit(this)
}
1 change: 1 addition & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/NodeCoalescer.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

class NodeCoalescer(val handler: (Node) -> Unit) : NodeVisitor<Unit> {
Expand Down
1 change: 1 addition & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/NodeType.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

enum class NodeType(val parent: NodeType? = null) {
Expand Down
28 changes: 1 addition & 27 deletions ast/src/main/kotlin/gay/pizza/pork/ast/NodeVisitor.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

interface NodeVisitor<T> {
Expand Down Expand Up @@ -34,31 +35,4 @@ interface NodeVisitor<T> {
fun visitSymbol(node: Symbol): T

fun visitSymbolReference(node: SymbolReference): T

fun visitNodes(vararg nodes: Node?): List<T> =
nodes.asSequence().filterNotNull().map { visit(it) }.toList()

fun visitAll(vararg nodeLists: List<Node>): List<T> =
nodeLists.asSequence().flatten().map { visit(it) }.toList()

fun visit(node: Node): T =
when (node) {
is Symbol -> visitSymbol(node)
is Block -> visitBlock(node)
is CompilationUnit -> visitCompilationUnit(node)
is LetAssignment -> visitLetAssignment(node)
is InfixOperation -> visitInfixOperation(node)
is BooleanLiteral -> visitBooleanLiteral(node)
is FunctionCall -> visitFunctionCall(node)
is FunctionDefinition -> visitFunctionDefinition(node)
is If -> visitIf(node)
is ImportDeclaration -> visitImportDeclaration(node)
is IntLiteral -> visitIntLiteral(node)
is Lambda -> visitLambda(node)
is ListLiteral -> visitListLiteral(node)
is Parentheses -> visitParentheses(node)
is PrefixOperation -> visitPrefixOperation(node)
is StringLiteral -> visitStringLiteral(node)
is SymbolReference -> visitSymbolReference(node)
}
}
29 changes: 29 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/NodeVisitorExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

fun <T> NodeVisitor<T>.visit(node: Node): T =
when (node) {
is Symbol -> visitSymbol(node)
is Block -> visitBlock(node)
is CompilationUnit -> visitCompilationUnit(node)
is LetAssignment -> visitLetAssignment(node)
is InfixOperation -> visitInfixOperation(node)
is BooleanLiteral -> visitBooleanLiteral(node)
is FunctionCall -> visitFunctionCall(node)
is FunctionDefinition -> visitFunctionDefinition(node)
is If -> visitIf(node)
is ImportDeclaration -> visitImportDeclaration(node)
is IntLiteral -> visitIntLiteral(node)
is Lambda -> visitLambda(node)
is ListLiteral -> visitListLiteral(node)
is Parentheses -> visitParentheses(node)
is PrefixOperation -> visitPrefixOperation(node)
is StringLiteral -> visitStringLiteral(node)
is SymbolReference -> visitSymbolReference(node)
}

fun <T> NodeVisitor<T>.visitNodes(vararg nodes: Node?): List<T> =
nodes.asSequence().filterNotNull().map { visit(it) }.toList()

fun <T> NodeVisitor<T>.visitAll(vararg nodeLists: List<Node>): List<T> =
nodeLists.asSequence().flatten().map { visit(it) }.toList()
4 changes: 4 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/Parentheses.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
Expand All @@ -11,6 +12,9 @@ class Parentheses(val expression: Expression) : Expression() {
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitNodes(expression)

override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitParentheses(this)

override fun equals(other: Any?): Boolean {
if (other !is Parentheses) return false
return other.expression == expression
Expand Down
4 changes: 4 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/PrefixOperation.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
Expand All @@ -11,6 +12,9 @@ class PrefixOperation(val op: PrefixOperator, val expression: Expression) : Expr
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitNodes(expression)

override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitPrefixOperation(this)

override fun equals(other: Any?): Boolean {
if (other !is PrefixOperation) return false
return other.op == op && other.expression == expression
Expand Down
1 change: 1 addition & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/PrefixOperator.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
Expand Down
4 changes: 4 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/StringLiteral.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
Expand All @@ -8,6 +9,9 @@ import kotlinx.serialization.Serializable
class StringLiteral(val text: String) : Expression() {
override val type: NodeType = NodeType.StringLiteral

override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitStringLiteral(this)

override fun equals(other: Any?): Boolean {
if (other !is StringLiteral) return false
return other.text == text
Expand Down
4 changes: 4 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/Symbol.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
Expand All @@ -8,6 +9,9 @@ import kotlinx.serialization.Serializable
class Symbol(val id: String) : Node() {
override val type: NodeType = NodeType.Symbol

override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitSymbol(this)

override fun equals(other: Any?): Boolean {
if (other !is Symbol) return false
return other.id == id
Expand Down
4 changes: 4 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/SymbolReference.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
Expand All @@ -11,6 +12,9 @@ class SymbolReference(val symbol: Symbol) : Expression() {
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitNodes(symbol)

override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitSymbolReference(this)

override fun equals(other: Any?): Boolean {
if (other !is SymbolReference) return false
return other.symbol == symbol
Expand Down
Loading

0 comments on commit 9f90e05

Please sign in to comment.