language: implement for in

This commit is contained in:
2023-09-11 05:34:09 -04:00
parent 9ba150bb69
commit 0aab45094a
12 changed files with 105 additions and 0 deletions

View File

@ -250,6 +250,15 @@ types:
type: Expression
- name: block
type: Block
ForIn:
parent: Expression
values:
- name: symbol
type: Symbol
- name: expression
type: Expression
- name: block
type: Block
Break:
parent: Expression
values: []

View File

@ -30,6 +30,7 @@ digraph A {
type_StringLiteral [shape=box,label="StringLiteral"]
type_SymbolReference [shape=box,label="SymbolReference"]
type_While [shape=box,label="While"]
type_ForIn [shape=box,label="ForIn"]
type_Break [shape=box,label="Break"]
type_Continue [shape=box,label="Continue"]
type_Native [shape=box,label="Native"]
@ -56,6 +57,7 @@ digraph A {
type_Expression -> type_StringLiteral
type_Expression -> type_SymbolReference
type_Expression -> type_While
type_Expression -> type_ForIn
type_Expression -> type_Break
type_Expression -> type_Continue
type_Definition -> type_FunctionDefinition
@ -97,6 +99,9 @@ digraph A {
type_SymbolReference -> type_Symbol [style=dotted]
type_While -> type_Expression [style=dotted]
type_While -> type_Block [style=dotted]
type_ForIn -> type_Symbol [style=dotted]
type_ForIn -> type_Expression [style=dotted]
type_ForIn -> type_Block [style=dotted]
type_Native -> type_Symbol [style=dotted]
type_Native -> type_StringLiteral [style=dotted]
}

View File

@ -0,0 +1,30 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.ast
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
@SerialName("forIn")
class ForIn(val symbol: Symbol, val expression: Expression, val block: Block) : Expression() {
override val type: NodeType = NodeType.ForIn
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitNodes(symbol, expression, block)
override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitForIn(this)
override fun equals(other: Any?): Boolean {
if (other !is ForIn) return false
return other.symbol == symbol && other.expression == expression && other.block == block
}
override fun hashCode(): Int {
var result = symbol.hashCode()
result = 31 * result + expression.hashCode()
result = 31 * result + block.hashCode()
result = 31 * result + type.hashCode()
return result
}
}

View File

@ -20,6 +20,9 @@ class NodeCoalescer(val handler: (Node) -> Unit) : NodeVisitor<Unit> {
override fun visitDoubleLiteral(node: DoubleLiteral): Unit =
handle(node)
override fun visitForIn(node: ForIn): Unit =
handle(node)
override fun visitFunctionCall(node: FunctionCall): Unit =
handle(node)

View File

@ -12,6 +12,7 @@ enum class NodeType(val parent: NodeType? = null) {
Declaration(Node),
Definition(Node),
DoubleLiteral(Expression),
ForIn(Expression),
FunctionCall(Expression),
FunctionDefinition(Definition),
If(Expression),

View File

@ -14,6 +14,8 @@ interface NodeVisitor<T> {
fun visitDoubleLiteral(node: DoubleLiteral): T
fun visitForIn(node: ForIn): T
fun visitFunctionCall(node: FunctionCall): T
fun visitFunctionDefinition(node: FunctionDefinition): T

View File

@ -25,6 +25,7 @@ fun <T> NodeVisitor<T>.visit(node: Node): T =
is StringLiteral -> visitStringLiteral(node)
is SymbolReference -> visitSymbolReference(node)
is While -> visitWhile(node)
is ForIn -> visitForIn(node)
is Break -> visitBreak(node)
is Continue -> visitContinue(node)
is Native -> visitNative(node)