mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-02 21:00:56 +00:00
language: implement list indexing
This commit is contained in:
parent
3b101bd48a
commit
a08526c92c
@ -292,3 +292,10 @@ types:
|
|||||||
type: Symbol
|
type: Symbol
|
||||||
- name: definition
|
- name: definition
|
||||||
type: StringLiteral
|
type: StringLiteral
|
||||||
|
IndexedBy:
|
||||||
|
parent: Expression
|
||||||
|
values:
|
||||||
|
- name: expression
|
||||||
|
type: Expression
|
||||||
|
- name: index
|
||||||
|
type: Expression
|
||||||
|
@ -36,6 +36,7 @@ digraph A {
|
|||||||
type_Continue [shape=box,label="Continue"]
|
type_Continue [shape=box,label="Continue"]
|
||||||
type_NoneLiteral [shape=box,label="NoneLiteral"]
|
type_NoneLiteral [shape=box,label="NoneLiteral"]
|
||||||
type_Native [shape=box,label="Native"]
|
type_Native [shape=box,label="Native"]
|
||||||
|
type_IndexedBy [shape=box,label="IndexedBy"]
|
||||||
type_Node -> type_Expression
|
type_Node -> type_Expression
|
||||||
type_Node -> type_Symbol
|
type_Node -> type_Symbol
|
||||||
type_Node -> type_Declaration
|
type_Node -> type_Declaration
|
||||||
@ -64,6 +65,7 @@ digraph A {
|
|||||||
type_Expression -> type_Break
|
type_Expression -> type_Break
|
||||||
type_Expression -> type_Continue
|
type_Expression -> type_Continue
|
||||||
type_Expression -> type_NoneLiteral
|
type_Expression -> type_NoneLiteral
|
||||||
|
type_Expression -> type_IndexedBy
|
||||||
type_Definition -> type_FunctionDefinition
|
type_Definition -> type_FunctionDefinition
|
||||||
type_Definition -> type_LetDefinition
|
type_Definition -> type_LetDefinition
|
||||||
type_Declaration -> type_ImportDeclaration
|
type_Declaration -> type_ImportDeclaration
|
||||||
@ -108,4 +110,5 @@ digraph A {
|
|||||||
type_ForIn -> type_Block [style=dotted]
|
type_ForIn -> type_Block [style=dotted]
|
||||||
type_Native -> type_Symbol [style=dotted]
|
type_Native -> type_Symbol [style=dotted]
|
||||||
type_Native -> type_StringLiteral [style=dotted]
|
type_Native -> type_StringLiteral [style=dotted]
|
||||||
|
type_IndexedBy -> type_Expression [style=dotted]
|
||||||
}
|
}
|
||||||
|
29
ast/src/main/kotlin/gay/pizza/pork/ast/IndexedBy.kt
Normal file
29
ast/src/main/kotlin/gay/pizza/pork/ast/IndexedBy.kt
Normal 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
|
||||||
|
}
|
||||||
|
}
|
@ -35,6 +35,9 @@ class NodeCoalescer(val handler: (Node) -> Unit) : NodeVisitor<Unit> {
|
|||||||
override fun visitImportDeclaration(node: ImportDeclaration): Unit =
|
override fun visitImportDeclaration(node: ImportDeclaration): Unit =
|
||||||
handle(node)
|
handle(node)
|
||||||
|
|
||||||
|
override fun visitIndexedBy(node: IndexedBy): Unit =
|
||||||
|
handle(node)
|
||||||
|
|
||||||
override fun visitInfixOperation(node: InfixOperation): Unit =
|
override fun visitInfixOperation(node: InfixOperation): Unit =
|
||||||
handle(node)
|
handle(node)
|
||||||
|
|
||||||
|
@ -30,6 +30,8 @@ interface NodeParser {
|
|||||||
|
|
||||||
fun parseImportDeclaration(): ImportDeclaration
|
fun parseImportDeclaration(): ImportDeclaration
|
||||||
|
|
||||||
|
fun parseIndexedBy(): IndexedBy
|
||||||
|
|
||||||
fun parseInfixOperation(): InfixOperation
|
fun parseInfixOperation(): InfixOperation
|
||||||
|
|
||||||
fun parseIntegerLiteral(): IntegerLiteral
|
fun parseIntegerLiteral(): IntegerLiteral
|
||||||
|
@ -34,5 +34,6 @@ fun NodeParser.parse(type: NodeType): Node =
|
|||||||
NodeType.Continue -> parseContinue()
|
NodeType.Continue -> parseContinue()
|
||||||
NodeType.NoneLiteral -> parseNoneLiteral()
|
NodeType.NoneLiteral -> parseNoneLiteral()
|
||||||
NodeType.Native -> parseNative()
|
NodeType.Native -> parseNative()
|
||||||
|
NodeType.IndexedBy -> parseIndexedBy()
|
||||||
else -> throw RuntimeException("Unable to automatically parse type: ${type.name}")
|
else -> throw RuntimeException("Unable to automatically parse type: ${type.name}")
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@ enum class NodeType(val parent: NodeType? = null) {
|
|||||||
FunctionDefinition(Definition),
|
FunctionDefinition(Definition),
|
||||||
If(Expression),
|
If(Expression),
|
||||||
ImportDeclaration(Declaration),
|
ImportDeclaration(Declaration),
|
||||||
|
IndexedBy(Expression),
|
||||||
InfixOperation(Expression),
|
InfixOperation(Expression),
|
||||||
IntegerLiteral(Expression),
|
IntegerLiteral(Expression),
|
||||||
LetAssignment(Expression),
|
LetAssignment(Expression),
|
||||||
|
@ -24,6 +24,8 @@ interface NodeVisitor<T> {
|
|||||||
|
|
||||||
fun visitImportDeclaration(node: ImportDeclaration): T
|
fun visitImportDeclaration(node: ImportDeclaration): T
|
||||||
|
|
||||||
|
fun visitIndexedBy(node: IndexedBy): T
|
||||||
|
|
||||||
fun visitInfixOperation(node: InfixOperation): T
|
fun visitInfixOperation(node: InfixOperation): T
|
||||||
|
|
||||||
fun visitIntegerLiteral(node: IntegerLiteral): T
|
fun visitIntegerLiteral(node: IntegerLiteral): T
|
||||||
|
@ -31,6 +31,7 @@ fun <T> NodeVisitor<T>.visit(node: Node): T =
|
|||||||
is Continue -> visitContinue(node)
|
is Continue -> visitContinue(node)
|
||||||
is NoneLiteral -> visitNoneLiteral(node)
|
is NoneLiteral -> visitNoneLiteral(node)
|
||||||
is Native -> visitNative(node)
|
is Native -> visitNative(node)
|
||||||
|
is IndexedBy -> visitIndexedBy(node)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> NodeVisitor<T>.visitNodes(vararg nodes: Node?): List<T> =
|
fun <T> NodeVisitor<T>.visitNodes(vararg nodes: Node?): List<T> =
|
||||||
|
@ -366,6 +366,21 @@ class EvaluationVisitor(root: Scope) : NodeVisitor<Any> {
|
|||||||
topLevelUsedError("ImportDeclaration", "CompilationUnitContext")
|
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 {
|
override fun visitCompilationUnit(node: CompilationUnit): Any {
|
||||||
topLevelUsedError("CompilationUnit", "CompilationUnitContext")
|
topLevelUsedError("CompilationUnit", "CompilationUnitContext")
|
||||||
}
|
}
|
||||||
|
@ -78,6 +78,10 @@ class Parser(source: TokenSource, attribution: NodeAttribution) :
|
|||||||
val infixOperator = ParserHelpers.convertInfixOperator(infixToken)
|
val infixOperator = ParserHelpers.convertInfixOperator(infixToken)
|
||||||
InfixOperation(expression, infixOperator, parseExpression())
|
InfixOperation(expression, infixOperator, parseExpression())
|
||||||
}
|
}
|
||||||
|
} else if (next(TokenType.LeftBracket)) {
|
||||||
|
val index = parseExpression()
|
||||||
|
expect(TokenType.RightBracket)
|
||||||
|
IndexedBy(expression, index)
|
||||||
} else expression
|
} else expression
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -237,6 +241,14 @@ class Parser(source: TokenSource, attribution: NodeAttribution) :
|
|||||||
ImportDeclaration(form, components)
|
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) {
|
override fun parseInfixOperation(): InfixOperation = guarded(NodeType.InfixOperation) {
|
||||||
val infixToken = next()
|
val infixToken = next()
|
||||||
val infixOperator = ParserHelpers.convertInfixOperator(infixToken)
|
val infixOperator = ParserHelpers.convertInfixOperator(infixToken)
|
||||||
|
@ -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) {
|
override fun visitCompilationUnit(node: CompilationUnit) {
|
||||||
for (declaration in node.declarations) {
|
for (declaration in node.declarations) {
|
||||||
visit(declaration)
|
visit(declaration)
|
||||||
|
@ -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)
|
@ -38,6 +38,7 @@ object PorkElementFactory {
|
|||||||
NodeType.Continue -> ContinueElement(node)
|
NodeType.Continue -> ContinueElement(node)
|
||||||
NodeType.NoneLiteral -> NoneLiteralElement(node)
|
NodeType.NoneLiteral -> NoneLiteralElement(node)
|
||||||
NodeType.Native -> NativeElement(node)
|
NodeType.Native -> NativeElement(node)
|
||||||
|
NodeType.IndexedBy -> IndexedByElement(node)
|
||||||
else -> ASTWrapperPsiElement(node)
|
else -> ASTWrapperPsiElement(node)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user