idea: proper scoping support, completion now works

This commit is contained in:
2023-09-21 23:07:22 -07:00
parent c92a111af7
commit f60715dfb3
25 changed files with 265 additions and 83 deletions

View File

@ -52,7 +52,6 @@ types:
type: Expression
SetAssignment:
parent: Expression
namedElementValue: symbol
values:
- name: symbol
type: Symbol
@ -138,6 +137,8 @@ types:
- name: arguments
type: List<Expression>
ArgumentSpec:
parent: Node
namedElementValue: symbol
values:
- name: symbol
type: Symbol
@ -270,11 +271,17 @@ types:
type: Expression
- name: block
type: Block
ForIn:
parent: Expression
ForInItem:
parent: Node
namedElementValue: symbol
values:
- name: symbol
type: Symbol
ForIn:
parent: Expression
values:
- name: item
type: ForInItem
- name: expression
type: Expression
- name: block

View File

@ -31,6 +31,7 @@ digraph A {
type_StringLiteral [shape=box,label="StringLiteral"]
type_SymbolReference [shape=box,label="SymbolReference"]
type_While [shape=box,label="While"]
type_ForInItem [shape=box,label="ForInItem"]
type_ForIn [shape=box,label="ForIn"]
type_Break [shape=box,label="Break"]
type_Continue [shape=box,label="Continue"]
@ -43,6 +44,8 @@ digraph A {
type_Node -> type_Definition
type_Node -> type_Block
type_Node -> type_CompilationUnit
type_Node -> type_ArgumentSpec
type_Node -> type_ForInItem
type_Node -> type_Native
type_Expression -> type_LetAssignment
type_Expression -> type_VarAssignment
@ -105,7 +108,8 @@ 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_ForInItem -> type_Symbol [style=dotted]
type_ForIn -> type_ForInItem [style=dotted]
type_ForIn -> type_Expression [style=dotted]
type_ForIn -> type_Block [style=dotted]
type_Native -> type_Symbol [style=dotted]

View File

@ -6,4 +6,24 @@ import kotlinx.serialization.Serializable
@Serializable
@SerialName("argumentSpec")
class ArgumentSpec(var symbol: Symbol, var multiple: Boolean = false)
class ArgumentSpec(val symbol: Symbol, val multiple: Boolean = false) : Node() {
override val type: NodeType = NodeType.ArgumentSpec
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitNodes(symbol)
override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitArgumentSpec(this)
override fun equals(other: Any?): Boolean {
if (other !is ArgumentSpec) return false
return other.symbol == symbol && other.multiple == multiple
}
override fun hashCode(): Int {
var result = symbol.hashCode()
result = 31 * result + multiple.hashCode()
result = 31 * result + type.hashCode()
return result
}
}

View File

@ -6,22 +6,22 @@ import kotlinx.serialization.Serializable
@Serializable
@SerialName("forIn")
class ForIn(val symbol: Symbol, val expression: Expression, val block: Block) : Expression() {
class ForIn(val item: ForInItem, 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)
visitor.visitNodes(item, 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
return other.item == item && other.expression == expression && other.block == block
}
override fun hashCode(): Int {
var result = symbol.hashCode()
var result = item.hashCode()
result = 31 * result + expression.hashCode()
result = 31 * result + block.hashCode()
result = 31 * result + type.hashCode()

View File

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

View File

@ -10,7 +10,7 @@ class FunctionDefinition(override val modifiers: DefinitionModifiers, override v
override val type: NodeType = NodeType.FunctionDefinition
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitAll(listOf(symbol), listOf(block), listOf(native))
visitor.visitAll(listOf(symbol), arguments, listOf(block), listOf(native))
override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitFunctionDefinition(this)

View File

@ -2,6 +2,9 @@
package gay.pizza.pork.ast
class NodeCoalescer(val handler: (Node) -> Unit) : NodeVisitor<Unit> {
override fun visitArgumentSpec(node: ArgumentSpec): Unit =
handle(node)
override fun visitBlock(node: Block): Unit =
handle(node)
@ -23,6 +26,9 @@ class NodeCoalescer(val handler: (Node) -> Unit) : NodeVisitor<Unit> {
override fun visitForIn(node: ForIn): Unit =
handle(node)
override fun visitForInItem(node: ForInItem): Unit =
handle(node)
override fun visitFunctionCall(node: FunctionCall): Unit =
handle(node)

View File

@ -2,6 +2,8 @@
package gay.pizza.pork.ast
interface NodeParser {
fun parseArgumentSpec(): ArgumentSpec
fun parseBlock(): Block
fun parseExpression(): Expression
@ -22,6 +24,8 @@ interface NodeParser {
fun parseForIn(): ForIn
fun parseForInItem(): ForInItem
fun parseFunctionCall(): FunctionCall
fun parseFunctionDefinition(): FunctionDefinition

View File

@ -15,6 +15,7 @@ fun NodeParser.parse(type: NodeType): Node =
NodeType.InfixOperation -> parseInfixOperation()
NodeType.BooleanLiteral -> parseBooleanLiteral()
NodeType.FunctionCall -> parseFunctionCall()
NodeType.ArgumentSpec -> parseArgumentSpec()
NodeType.FunctionDefinition -> parseFunctionDefinition()
NodeType.LetDefinition -> parseLetDefinition()
NodeType.If -> parseIf()
@ -29,6 +30,7 @@ fun NodeParser.parse(type: NodeType): Node =
NodeType.StringLiteral -> parseStringLiteral()
NodeType.SymbolReference -> parseSymbolReference()
NodeType.While -> parseWhile()
NodeType.ForInItem -> parseForInItem()
NodeType.ForIn -> parseForIn()
NodeType.Break -> parseBreak()
NodeType.Continue -> parseContinue()

View File

@ -3,6 +3,7 @@ package gay.pizza.pork.ast
enum class NodeType(val parent: NodeType? = null) {
Node,
ArgumentSpec(Node),
Block(Node),
Expression(Node),
BooleanLiteral(Expression),
@ -13,6 +14,7 @@ enum class NodeType(val parent: NodeType? = null) {
Definition(Node),
DoubleLiteral(Expression),
ForIn(Expression),
ForInItem(Node),
FunctionCall(Expression),
FunctionDefinition(Definition),
If(Expression),

View File

@ -2,6 +2,8 @@
package gay.pizza.pork.ast
interface NodeVisitor<T> {
fun visitArgumentSpec(node: ArgumentSpec): T
fun visitBlock(node: Block): T
fun visitBooleanLiteral(node: BooleanLiteral): T
@ -16,6 +18,8 @@ interface NodeVisitor<T> {
fun visitForIn(node: ForIn): T
fun visitForInItem(node: ForInItem): T
fun visitFunctionCall(node: FunctionCall): T
fun visitFunctionDefinition(node: FunctionDefinition): T

View File

@ -12,6 +12,7 @@ fun <T> NodeVisitor<T>.visit(node: Node): T =
is InfixOperation -> visitInfixOperation(node)
is BooleanLiteral -> visitBooleanLiteral(node)
is FunctionCall -> visitFunctionCall(node)
is ArgumentSpec -> visitArgumentSpec(node)
is FunctionDefinition -> visitFunctionDefinition(node)
is LetDefinition -> visitLetDefinition(node)
is If -> visitIf(node)
@ -26,6 +27,7 @@ fun <T> NodeVisitor<T>.visit(node: Node): T =
is StringLiteral -> visitStringLiteral(node)
is SymbolReference -> visitSymbolReference(node)
is While -> visitWhile(node)
is ForInItem -> visitForInItem(node)
is ForIn -> visitForIn(node)
is Break -> visitBreak(node)
is Continue -> visitContinue(node)