implement basic type annotations (not yet used or declarable)

This commit is contained in:
Alex Zenla
2025-07-20 00:52:24 -07:00
parent a48fac4581
commit 5ac70d800e
25 changed files with 161 additions and 28 deletions

View File

@ -18,6 +18,11 @@ types:
- name: modifiers
type: DefinitionModifiers
required: true
TypeSpec:
parent: Node
values:
- name: symbol
type: Symbol
DefinitionModifiers:
values:
- name: export
@ -42,6 +47,8 @@ types:
type: Symbol
- name: value
type: Expression
- name: typeSpec
type: TypeSpec?
VarAssignment:
parent: Expression
namedElementValue: symbol
@ -50,6 +57,8 @@ types:
type: Symbol
- name: value
type: Expression
- name: typeSpec
type: TypeSpec?
SetAssignment:
parent: Expression
values:
@ -142,6 +151,8 @@ types:
values:
- name: symbol
type: Symbol
- name: typeSpec
type: TypeSpec?
- name: multiple
type: Boolean
defaultValue: "false"
@ -155,6 +166,8 @@ types:
type: Symbol
- name: arguments
type: List<ArgumentSpec>
- name: returnType
type: TypeSpec?
- name: block
type: Block?
- name: nativeFunctionDescriptor
@ -167,6 +180,8 @@ types:
type: DefinitionModifiers
- name: symbol
type: Symbol
- name: typeSpec
type: TypeSpec?
- name: value
type: Expression
If:

View File

@ -4,6 +4,7 @@ digraph A {
type_Symbol [shape=box,label="Symbol"]
type_Declaration [shape=box,label="Declaration"]
type_Definition [shape=box,label="Definition"]
type_TypeSpec [shape=box,label="TypeSpec"]
type_DefinitionModifiers [shape=box,label="DefinitionModifiers"]
type_Block [shape=box,label="Block"]
type_CompilationUnit [shape=box,label="CompilationUnit"]
@ -44,6 +45,7 @@ digraph A {
type_Node -> type_Symbol
type_Node -> type_Declaration
type_Node -> type_Definition
type_Node -> type_TypeSpec
type_Node -> type_Block
type_Node -> type_CompilationUnit
type_Node -> type_ArgumentSpec
@ -78,13 +80,16 @@ digraph A {
type_Declaration -> type_ImportDeclaration
type_Definition -> type_Symbol [style=dotted]
type_Definition -> type_DefinitionModifiers [style=dotted]
type_TypeSpec -> type_Symbol [style=dotted]
type_Block -> type_Expression [style=dotted]
type_CompilationUnit -> type_Declaration [style=dotted]
type_CompilationUnit -> type_Definition [style=dotted]
type_LetAssignment -> type_Symbol [style=dotted]
type_LetAssignment -> type_Expression [style=dotted]
type_LetAssignment -> type_TypeSpec [style=dotted]
type_VarAssignment -> type_Symbol [style=dotted]
type_VarAssignment -> type_Expression [style=dotted]
type_VarAssignment -> type_TypeSpec [style=dotted]
type_SetAssignment -> type_Symbol [style=dotted]
type_SetAssignment -> type_Expression [style=dotted]
type_InfixOperation -> type_Expression [style=dotted]
@ -92,13 +97,16 @@ digraph A {
type_FunctionCall -> type_Symbol [style=dotted]
type_FunctionCall -> type_Expression [style=dotted]
type_ArgumentSpec -> type_Symbol [style=dotted]
type_ArgumentSpec -> type_TypeSpec [style=dotted]
type_FunctionDefinition -> type_DefinitionModifiers [style=dotted]
type_FunctionDefinition -> type_Symbol [style=dotted]
type_FunctionDefinition -> type_ArgumentSpec [style=dotted]
type_FunctionDefinition -> type_TypeSpec [style=dotted]
type_FunctionDefinition -> type_Block [style=dotted]
type_FunctionDefinition -> type_NativeFunctionDescriptor [style=dotted]
type_LetDefinition -> type_DefinitionModifiers [style=dotted]
type_LetDefinition -> type_Symbol [style=dotted]
type_LetDefinition -> type_TypeSpec [style=dotted]
type_LetDefinition -> type_Expression [style=dotted]
type_If -> type_Expression [style=dotted]
type_If -> type_Block [style=dotted]

View File

@ -9,6 +9,9 @@ abstract class FunctionLevelVisitor<T> : NodeVisitor<T> {
override fun visitSymbol(node: Symbol): T =
throw RuntimeException("Visiting Symbol is not supported.")
override fun visitTypeSpec(node: TypeSpec): T =
throw RuntimeException("Visiting TypeSpec is not supported.")
override fun visitLetDefinition(node: LetDefinition): T {
topLevelUsedError("LetDefinition")
}

View File

@ -6,22 +6,23 @@ import kotlinx.serialization.Serializable
@Serializable
@SerialName("argumentSpec")
class ArgumentSpec(val symbol: Symbol, val multiple: Boolean = false) : Node() {
class ArgumentSpec(val symbol: Symbol, val typeSpec: TypeSpec?, val multiple: Boolean = false) : Node() {
override val type: NodeType = NodeType.ArgumentSpec
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitNodes(symbol)
visitor.visitNodes(symbol, typeSpec)
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
return other.symbol == symbol && other.typeSpec == typeSpec && other.multiple == multiple
}
override fun hashCode(): Int {
var result = symbol.hashCode()
result = 31 * result + typeSpec.hashCode()
result = 31 * result + multiple.hashCode()
result = 31 * result + type.hashCode()
return result

View File

@ -6,24 +6,25 @@ import kotlinx.serialization.Serializable
@Serializable
@SerialName("functionDefinition")
class FunctionDefinition(override val modifiers: DefinitionModifiers, override val symbol: Symbol, val arguments: List<ArgumentSpec>, val block: Block?, val nativeFunctionDescriptor: NativeFunctionDescriptor?) : Definition() {
class FunctionDefinition(override val modifiers: DefinitionModifiers, override val symbol: Symbol, val arguments: List<ArgumentSpec>, val returnType: TypeSpec?, val block: Block?, val nativeFunctionDescriptor: NativeFunctionDescriptor?) : Definition() {
override val type: NodeType = NodeType.FunctionDefinition
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitAll(listOf(symbol), arguments, listOf(block), listOf(nativeFunctionDescriptor))
visitor.visitAll(listOf(symbol), arguments, listOf(returnType), listOf(block), listOf(nativeFunctionDescriptor))
override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitFunctionDefinition(this)
override fun equals(other: Any?): Boolean {
if (other !is FunctionDefinition) return false
return other.modifiers == modifiers && other.symbol == symbol && other.arguments == arguments && other.block == block && other.nativeFunctionDescriptor == nativeFunctionDescriptor
return other.modifiers == modifiers && other.symbol == symbol && other.arguments == arguments && other.returnType == returnType && other.block == block && other.nativeFunctionDescriptor == nativeFunctionDescriptor
}
override fun hashCode(): Int {
var result = modifiers.hashCode()
result = 31 * result + symbol.hashCode()
result = 31 * result + arguments.hashCode()
result = 31 * result + returnType.hashCode()
result = 31 * result + block.hashCode()
result = 31 * result + nativeFunctionDescriptor.hashCode()
result = 31 * result + type.hashCode()

View File

@ -6,23 +6,24 @@ import kotlinx.serialization.Serializable
@Serializable
@SerialName("letAssignment")
class LetAssignment(val symbol: Symbol, val value: Expression) : Expression() {
class LetAssignment(val symbol: Symbol, val value: Expression, val typeSpec: TypeSpec?) : Expression() {
override val type: NodeType = NodeType.LetAssignment
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitNodes(symbol, value)
visitor.visitNodes(symbol, value, typeSpec)
override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitLetAssignment(this)
override fun equals(other: Any?): Boolean {
if (other !is LetAssignment) return false
return other.symbol == symbol && other.value == value
return other.symbol == symbol && other.value == value && other.typeSpec == typeSpec
}
override fun hashCode(): Int {
var result = symbol.hashCode()
result = 31 * result + value.hashCode()
result = 31 * result + typeSpec.hashCode()
result = 31 * result + type.hashCode()
return result
}

View File

@ -6,23 +6,24 @@ import kotlinx.serialization.Serializable
@Serializable
@SerialName("letDefinition")
class LetDefinition(override val modifiers: DefinitionModifiers, override val symbol: Symbol, val value: Expression) : Definition() {
class LetDefinition(override val modifiers: DefinitionModifiers, override val symbol: Symbol, val typeSpec: TypeSpec?, val value: Expression) : Definition() {
override val type: NodeType = NodeType.LetDefinition
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitNodes(symbol, value)
visitor.visitNodes(symbol, typeSpec, value)
override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitLetDefinition(this)
override fun equals(other: Any?): Boolean {
if (other !is LetDefinition) return false
return other.modifiers == modifiers && other.symbol == symbol && other.value == value
return other.modifiers == modifiers && other.symbol == symbol && other.typeSpec == typeSpec && other.value == value
}
override fun hashCode(): Int {
var result = modifiers.hashCode()
result = 31 * result + symbol.hashCode()
result = 31 * result + typeSpec.hashCode()
result = 31 * result + value.hashCode()
result = 31 * result + type.hashCode()
return result

View File

@ -95,6 +95,9 @@ class NodeCoalescer(val followChildren: Boolean = true, val handler: (Node) -> U
override fun visitSymbolReference(node: SymbolReference): Unit =
handle(node)
override fun visitTypeSpec(node: TypeSpec): Unit =
handle(node)
override fun visitVarAssignment(node: VarAssignment): Unit =
handle(node)

View File

@ -70,6 +70,8 @@ interface NodeParser {
fun parseSymbolReference(): SymbolReference
fun parseTypeSpec(): TypeSpec
fun parseVarAssignment(): VarAssignment
fun parseWhile(): While

View File

@ -7,6 +7,7 @@ fun NodeParser.parse(type: NodeType): Node =
NodeType.Symbol -> parseSymbol()
NodeType.Declaration -> parseDeclaration()
NodeType.Definition -> parseDefinition()
NodeType.TypeSpec -> parseTypeSpec()
NodeType.Block -> parseBlock()
NodeType.CompilationUnit -> parseCompilationUnit()
NodeType.LetAssignment -> parseLetAssignment()

View File

@ -37,6 +37,7 @@ enum class NodeType(val parent: NodeType? = null) {
SuffixOperation(Expression),
Symbol(Node),
SymbolReference(Expression),
TypeSpec(Node),
VarAssignment(Expression),
While(Expression)
}

View File

@ -64,6 +64,8 @@ interface NodeVisitor<T> {
fun visitSymbolReference(node: SymbolReference): T
fun visitTypeSpec(node: TypeSpec): T
fun visitVarAssignment(node: VarAssignment): T
fun visitWhile(node: While): T

View File

@ -4,6 +4,7 @@ package gay.pizza.pork.ast.gen
fun <T> NodeVisitor<T>.visit(node: Node): T =
when (node) {
is Symbol -> visitSymbol(node)
is TypeSpec -> visitTypeSpec(node)
is Block -> visitBlock(node)
is CompilationUnit -> visitCompilationUnit(node)
is LetAssignment -> visitLetAssignment(node)

View File

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

View File

@ -6,23 +6,24 @@ import kotlinx.serialization.Serializable
@Serializable
@SerialName("varAssignment")
class VarAssignment(val symbol: Symbol, val value: Expression) : Expression() {
class VarAssignment(val symbol: Symbol, val value: Expression, val typeSpec: TypeSpec?) : Expression() {
override val type: NodeType = NodeType.VarAssignment
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
visitor.visitNodes(symbol, value)
visitor.visitNodes(symbol, value, typeSpec)
override fun <T> visit(visitor: NodeVisitor<T>): T =
visitor.visitVarAssignment(this)
override fun equals(other: Any?): Boolean {
if (other !is VarAssignment) return false
return other.symbol == symbol && other.value == value
return other.symbol == symbol && other.value == value && other.typeSpec == typeSpec
}
override fun hashCode(): Int {
var result = symbol.hashCode()
result = 31 * result + value.hashCode()
result = 31 * result + typeSpec.hashCode()
result = 31 * result + type.hashCode()
return result
}