language: implement list indexing

This commit is contained in:
2023-09-17 08:38:11 -07:00
parent 3b101bd48a
commit a08526c92c
14 changed files with 90 additions and 0 deletions

View File

@ -292,3 +292,10 @@ types:
type: Symbol
- name: definition
type: StringLiteral
IndexedBy:
parent: Expression
values:
- name: expression
type: Expression
- name: index
type: Expression

View File

@ -36,6 +36,7 @@ digraph A {
type_Continue [shape=box,label="Continue"]
type_NoneLiteral [shape=box,label="NoneLiteral"]
type_Native [shape=box,label="Native"]
type_IndexedBy [shape=box,label="IndexedBy"]
type_Node -> type_Expression
type_Node -> type_Symbol
type_Node -> type_Declaration
@ -64,6 +65,7 @@ digraph A {
type_Expression -> type_Break
type_Expression -> type_Continue
type_Expression -> type_NoneLiteral
type_Expression -> type_IndexedBy
type_Definition -> type_FunctionDefinition
type_Definition -> type_LetDefinition
type_Declaration -> type_ImportDeclaration
@ -108,4 +110,5 @@ digraph A {
type_ForIn -> type_Block [style=dotted]
type_Native -> type_Symbol [style=dotted]
type_Native -> type_StringLiteral [style=dotted]
type_IndexedBy -> type_Expression [style=dotted]
}

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("indexedBy")
class IndexedBy(val expression: Expression, val index: Expression) : Expression() {
override val type: NodeType = NodeType.IndexedBy
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitNodes(expression, index)
override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitIndexedBy(this)
override fun equals(other: Any?): Boolean {
if (other !is IndexedBy) return false
return other.expression == expression && other.index == index
}
override fun hashCode(): Int {
var result = expression.hashCode()
result = 31 * result + index.hashCode()
result = 31 * result + type.hashCode()
return result
}
}

View File

@ -35,6 +35,9 @@ class NodeCoalescer(val handler: (Node) -> Unit) : NodeVisitor<Unit> {
override fun visitImportDeclaration(node: ImportDeclaration): Unit =
handle(node)
override fun visitIndexedBy(node: IndexedBy): Unit =
handle(node)
override fun visitInfixOperation(node: InfixOperation): Unit =
handle(node)

View File

@ -30,6 +30,8 @@ interface NodeParser {
fun parseImportDeclaration(): ImportDeclaration
fun parseIndexedBy(): IndexedBy
fun parseInfixOperation(): InfixOperation
fun parseIntegerLiteral(): IntegerLiteral

View File

@ -34,5 +34,6 @@ fun NodeParser.parse(type: NodeType): Node =
NodeType.Continue -> parseContinue()
NodeType.NoneLiteral -> parseNoneLiteral()
NodeType.Native -> parseNative()
NodeType.IndexedBy -> parseIndexedBy()
else -> throw RuntimeException("Unable to automatically parse type: ${type.name}")
}

View File

@ -17,6 +17,7 @@ enum class NodeType(val parent: NodeType? = null) {
FunctionDefinition(Definition),
If(Expression),
ImportDeclaration(Declaration),
IndexedBy(Expression),
InfixOperation(Expression),
IntegerLiteral(Expression),
LetAssignment(Expression),

View File

@ -24,6 +24,8 @@ interface NodeVisitor<T> {
fun visitImportDeclaration(node: ImportDeclaration): T
fun visitIndexedBy(node: IndexedBy): T
fun visitInfixOperation(node: InfixOperation): T
fun visitIntegerLiteral(node: IntegerLiteral): T

View File

@ -31,6 +31,7 @@ fun <T> NodeVisitor<T>.visit(node: Node): T =
is Continue -> visitContinue(node)
is NoneLiteral -> visitNoneLiteral(node)
is Native -> visitNative(node)
is IndexedBy -> visitIndexedBy(node)
}
fun <T> NodeVisitor<T>.visitNodes(vararg nodes: Node?): List<T> =