mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-03 21:21:33 +00:00
language: implement list indexing
This commit is contained in:
@ -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
|
||||
}
|
||||
|
||||
@ -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)
|
||||
|
@ -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)
|
||||
|
Reference in New Issue
Block a user