Skip to content

Commit

Permalink
language: implement list indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
azenla committed Sep 17, 2023
1 parent 3b101bd commit a08526c
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ast/src/main/ast/pork.yml
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,10 @@ types:
type: Symbol
- name: definition
type: StringLiteral
IndexedBy:
parent: Expression
values:
- name: expression
type: Expression
- name: index
type: Expression
3 changes: 3 additions & 0 deletions ast/src/main/graph/types.dot
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
}
29 changes: 29 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/IndexedBy.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("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
}
}
3 changes: 3 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 @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions ast/src/main/kotlin/gay/pizza/pork/ast/NodeParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ interface NodeParser {

fun parseImportDeclaration(): ImportDeclaration

fun parseIndexedBy(): IndexedBy

fun parseInfixOperation(): InfixOperation

fun parseIntegerLiteral(): IntegerLiteral
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
}
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
Expand Up @@ -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),
Expand Down
2 changes: 2 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 @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,21 @@ class EvaluationVisitor(root: Scope) : NodeVisitor<Any> {
topLevelUsedError("ImportDeclaration", "CompilationUnitContext")
}

override fun visitIndexedBy(node: IndexedBy): Any {
val value = node.expression.visit(this)
val index = node.index.visit(this)

if (value is List<*> && index is Number) {
return value[index.toInt()] ?: None
}

if (value is Array<*> && index is Number) {
return value[index.toInt()] ?: None
}

throw RuntimeException("Failed to index '${value}' by '${index}': Unsupported types used.")
}

override fun visitCompilationUnit(node: CompilationUnit): Any {
topLevelUsedError("CompilationUnit", "CompilationUnitContext")
}
Expand Down
12 changes: 12 additions & 0 deletions parser/src/main/kotlin/gay/pizza/pork/parser/Parser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ class Parser(source: TokenSource, attribution: NodeAttribution) :
val infixOperator = ParserHelpers.convertInfixOperator(infixToken)
InfixOperation(expression, infixOperator, parseExpression())
}
} else if (next(TokenType.LeftBracket)) {
val index = parseExpression()
expect(TokenType.RightBracket)
IndexedBy(expression, index)
} else expression
}

Expand Down Expand Up @@ -237,6 +241,14 @@ class Parser(source: TokenSource, attribution: NodeAttribution) :
ImportDeclaration(form, components)
}

override fun parseIndexedBy(): IndexedBy = guarded(NodeType.IndexedBy) {
val expression = parseExpression()
expect(TokenType.LeftBracket)
val index = parseExpression()
expect(TokenType.RightBracket)
IndexedBy(expression, index)
}

override fun parseInfixOperation(): InfixOperation = guarded(NodeType.InfixOperation) {
val infixToken = next()
val infixOperator = ParserHelpers.convertInfixOperator(infixToken)
Expand Down
7 changes: 7 additions & 0 deletions parser/src/main/kotlin/gay/pizza/pork/parser/Printer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,13 @@ class Printer(buffer: StringBuilder) : NodeVisitor<Unit> {
}
}

override fun visitIndexedBy(node: IndexedBy) {
visit(node.expression)
append("[")
visit(node.index)
append("]")
}

override fun visitCompilationUnit(node: CompilationUnit) {
for (declaration in node.declarations) {
visit(declaration)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// GENERATED CODE FROM PORK AST CODEGEN
package gay.pizza.pork.idea.psi.gen

import com.intellij.lang.ASTNode

class IndexedByElement(node: ASTNode) : PorkElement(node)
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ object PorkElementFactory {
NodeType.Continue -> ContinueElement(node)
NodeType.NoneLiteral -> NoneLiteralElement(node)
NodeType.Native -> NativeElement(node)
NodeType.IndexedBy -> IndexedByElement(node)
else -> ASTWrapperPsiElement(node)
}
}

0 comments on commit a08526c

Please sign in to comment.