while loop support, and native functions (including ffi!)

This commit is contained in:
2023-09-06 19:07:28 -07:00
parent ddff6cb365
commit 236f812caf
34 changed files with 467 additions and 115 deletions

View File

@ -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:
@ -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

View File

@ -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()
}

View File

@ -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()
}

View File

@ -6,18 +6,18 @@ 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 {
@ -25,6 +25,7 @@ class FunctionDefinition(override val modifiers: DefinitionModifiers, override v
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
}

View File

@ -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
}

View File

@ -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
}
}

View File

@ -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)
@ -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)
@ -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)

View File

@ -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),
@ -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)
}

View File

@ -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
@ -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
@ -33,4 +39,6 @@ interface NodeVisitor<T> {
fun visitSymbol(node: Symbol): T
fun visitSymbolReference(node: SymbolReference): T
fun visitWhile(node: While): T
}

View File

@ -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()

View File

@ -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
}
}