Skip to content

Commit

Permalink
while loop support, and native functions (including ffi!)
Browse files Browse the repository at this point in the history
  • Loading branch information
azenla committed Sep 7, 2023
1 parent ddff6cb commit 236f812
Show file tree
Hide file tree
Showing 34 changed files with 465 additions and 113 deletions.
32 changes: 27 additions & 5 deletions ast/src/main/ast/pork.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,18 @@ types:
- name: arguments
type: List<Symbol>
- name: block
type: Block
type: Block?
- name: native
type: Native?
If:
parent: Expression
values:
- name: condition
type: Expression
- name: thenExpression
type: Expression
- name: elseExpression
type: Expression?
- name: thenBlock
type: Block
- name: elseBlock
type: Block?
ImportDeclaration:
parent: Declaration
values:
Expand Down Expand Up @@ -150,3 +152,23 @@ types:
values:
- name: symbol
type: Symbol
While:
parent: Expression
values:
- name: condition
type: Expression
- name: block
type: Block
Break:
parent: Expression
values: []
Continue:
parent: Expression
values: []
Native:
parent: Node
values:
- name: form
type: Symbol
- name: definition
type: StringLiteral
22 changes: 22 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/Break.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
@SerialName("break")
class Break : Expression() {
override val type: NodeType = NodeType.Break

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

override fun equals(other: Any?): Boolean {
if (other !is Break) return false
return true
}

override fun hashCode(): Int =
31 * type.hashCode()
}
22 changes: 22 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/Continue.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
@SerialName("continue")
class Continue : Expression() {
override val type: NodeType = NodeType.Continue

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

override fun equals(other: Any?): Boolean {
if (other !is Continue) return false
return true
}

override fun hashCode(): Int =
31 * type.hashCode()
}
7 changes: 4 additions & 3 deletions ast/src/main/kotlin/gay/pizza/pork/ast/FunctionDefinition.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@ import kotlinx.serialization.Serializable

@Serializable
@SerialName("functionDefinition")
class FunctionDefinition(override val modifiers: DefinitionModifiers, override val symbol: Symbol, val arguments: List<Symbol>, val block: Block) : Definition() {
class FunctionDefinition(override val modifiers: DefinitionModifiers, override val symbol: Symbol, val arguments: List<Symbol>, val block: Block?, val native: Native?) : Definition() {
override val type: NodeType = NodeType.FunctionDefinition

override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitAll(listOf(symbol), arguments, listOf(block))
visitor.visitAll(listOf(symbol), arguments, listOf(block), listOf(native))

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
return other.modifiers == modifiers && other.symbol == symbol && other.arguments == arguments && other.block == block && other.native == native
}

override fun hashCode(): Int {
var result = modifiers.hashCode()
result = 31 * result + symbol.hashCode()
result = 31 * result + arguments.hashCode()
result = 31 * result + block.hashCode()
result = 31 * result + native.hashCode()
result = 31 * result + type.hashCode()
return result
}
Expand Down
10 changes: 5 additions & 5 deletions ast/src/main/kotlin/gay/pizza/pork/ast/If.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ import kotlinx.serialization.Serializable

@Serializable
@SerialName("if")
class If(val condition: Expression, val thenExpression: Expression, val elseExpression: Expression?) : Expression() {
class If(val condition: Expression, val thenBlock: Block, val elseBlock: Block?) : Expression() {
override val type: NodeType = NodeType.If

override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitNodes(condition, thenExpression, elseExpression)
visitor.visitNodes(condition, thenBlock, elseBlock)

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
return other.condition == condition && other.thenBlock == thenBlock && other.elseBlock == elseBlock
}

override fun hashCode(): Int {
var result = condition.hashCode()
result = 31 * result + thenExpression.hashCode()
result = 31 * result + elseExpression.hashCode()
result = 31 * result + thenBlock.hashCode()
result = 31 * result + elseBlock.hashCode()
result = 31 * result + type.hashCode()
return result
}
Expand Down
29 changes: 29 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/Native.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

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
@SerialName("native")
class Native(val form: Symbol, val definition: StringLiteral) : Node() {
override val type: NodeType = NodeType.Native

override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitNodes(form, definition)

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

override fun equals(other: Any?): Boolean {
if (other !is Native) return false
return other.form == form && other.definition == definition
}

override fun hashCode(): Int {
var result = form.hashCode()
result = 31 * result + definition.hashCode()
result = 31 * result + type.hashCode()
return result
}
}
12 changes: 12 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/NodeCoalescer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ class NodeCoalescer(val handler: (Node) -> Unit) : NodeVisitor<Unit> {
override fun visitBooleanLiteral(node: BooleanLiteral): Unit =
handle(node)

override fun visitBreak(node: Break): Unit =
handle(node)

override fun visitCompilationUnit(node: CompilationUnit): Unit =
handle(node)

override fun visitContinue(node: Continue): Unit =
handle(node)

override fun visitFunctionCall(node: FunctionCall): Unit =
handle(node)

Expand All @@ -35,6 +41,9 @@ class NodeCoalescer(val handler: (Node) -> Unit) : NodeVisitor<Unit> {
override fun visitListLiteral(node: ListLiteral): Unit =
handle(node)

override fun visitNative(node: Native): Unit =
handle(node)

override fun visitParentheses(node: Parentheses): Unit =
handle(node)

Expand All @@ -50,6 +59,9 @@ class NodeCoalescer(val handler: (Node) -> Unit) : NodeVisitor<Unit> {
override fun visitSymbolReference(node: SymbolReference): Unit =
handle(node)

override fun visitWhile(node: While): Unit =
handle(node)

fun handle(node: Node) {
handler(node)
node.visitChildren(this)
Expand Down
6 changes: 5 additions & 1 deletion ast/src/main/kotlin/gay/pizza/pork/ast/NodeType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ enum class NodeType(val parent: NodeType? = null) {
Block(Node),
Expression(Node),
BooleanLiteral(Expression),
Break(Expression),
CompilationUnit(Node),
Continue(Expression),
Declaration(Node),
Definition(Node),
FunctionCall(Expression),
Expand All @@ -17,9 +19,11 @@ enum class NodeType(val parent: NodeType? = null) {
IntLiteral(Expression),
LetAssignment(Expression),
ListLiteral(Expression),
Native(Node),
Parentheses(Expression),
PrefixOperation(Expression),
StringLiteral(Expression),
Symbol(Node),
SymbolReference(Expression)
SymbolReference(Expression),
While(Expression)
}
8 changes: 8 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/NodeVisitor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ interface NodeVisitor<T> {

fun visitBooleanLiteral(node: BooleanLiteral): T

fun visitBreak(node: Break): T

fun visitCompilationUnit(node: CompilationUnit): T

fun visitContinue(node: Continue): T

fun visitFunctionCall(node: FunctionCall): T

fun visitFunctionDefinition(node: FunctionDefinition): T
Expand All @@ -24,6 +28,8 @@ interface NodeVisitor<T> {

fun visitListLiteral(node: ListLiteral): T

fun visitNative(node: Native): T

fun visitParentheses(node: Parentheses): T

fun visitPrefixOperation(node: PrefixOperation): T
Expand All @@ -33,4 +39,6 @@ interface NodeVisitor<T> {
fun visitSymbol(node: Symbol): T

fun visitSymbolReference(node: SymbolReference): T

fun visitWhile(node: While): T
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ fun <T> NodeVisitor<T>.visit(node: Node): T =
is PrefixOperation -> visitPrefixOperation(node)
is StringLiteral -> visitStringLiteral(node)
is SymbolReference -> visitSymbolReference(node)
is While -> visitWhile(node)
is Break -> visitBreak(node)
is Continue -> visitContinue(node)
is Native -> visitNative(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()
fun <T> NodeVisitor<T>.visitAll(vararg nodeLists: List<Node?>): List<T> =
nodeLists.asSequence().flatten().filterNotNull().map { visit(it) }.toList()
29 changes: 29 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/While.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

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
@SerialName("while")
class While(val condition: Expression, val block: Block) : Expression() {
override val type: NodeType = NodeType.While

override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitNodes(condition, block)

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

override fun equals(other: Any?): Boolean {
if (other !is While) return false
return other.condition == condition && other.block == block
}

override fun hashCode(): Int {
var result = condition.hashCode()
result = 31 * result + block.hashCode()
result = 31 * result + type.hashCode()
return result
}
}
Loading

0 comments on commit 236f812

Please sign in to comment.