mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-02 21:00:56 +00:00
implement support for type definitions
This commit is contained in:
parent
5ac70d800e
commit
f7ff896f81
@ -330,6 +330,13 @@ types:
|
||||
type: Symbol
|
||||
- name: definitions
|
||||
type: List<StringLiteral>
|
||||
NativeTypeDescriptor:
|
||||
parent: Node
|
||||
values:
|
||||
- name: form
|
||||
type: Symbol
|
||||
- name: definitions
|
||||
type: List<StringLiteral>
|
||||
IndexedBy:
|
||||
parent: Expression
|
||||
values:
|
||||
@ -337,3 +344,13 @@ types:
|
||||
type: Expression
|
||||
- name: index
|
||||
type: Expression
|
||||
TypeDefinition:
|
||||
parent: Definition
|
||||
namedElementValue: symbol
|
||||
values:
|
||||
- name: modifiers
|
||||
type: DefinitionModifiers
|
||||
- name: symbol
|
||||
type: Symbol
|
||||
- name: nativeTypeDescriptor
|
||||
type: NativeTypeDescriptor?
|
||||
|
@ -40,7 +40,9 @@ digraph A {
|
||||
type_Return [shape=box,label="Return"]
|
||||
type_NoneLiteral [shape=box,label="NoneLiteral"]
|
||||
type_NativeFunctionDescriptor [shape=box,label="NativeFunctionDescriptor"]
|
||||
type_NativeTypeDescriptor [shape=box,label="NativeTypeDescriptor"]
|
||||
type_IndexedBy [shape=box,label="IndexedBy"]
|
||||
type_TypeDefinition [shape=box,label="TypeDefinition"]
|
||||
type_Node -> type_Expression
|
||||
type_Node -> type_Symbol
|
||||
type_Node -> type_Declaration
|
||||
@ -52,6 +54,7 @@ digraph A {
|
||||
type_Node -> type_ImportPath
|
||||
type_Node -> type_ForInItem
|
||||
type_Node -> type_NativeFunctionDescriptor
|
||||
type_Node -> type_NativeTypeDescriptor
|
||||
type_Expression -> type_LetAssignment
|
||||
type_Expression -> type_VarAssignment
|
||||
type_Expression -> type_SetAssignment
|
||||
@ -77,6 +80,7 @@ digraph A {
|
||||
type_Expression -> type_IndexedBy
|
||||
type_Definition -> type_FunctionDefinition
|
||||
type_Definition -> type_LetDefinition
|
||||
type_Definition -> type_TypeDefinition
|
||||
type_Declaration -> type_ImportDeclaration
|
||||
type_Definition -> type_Symbol [style=dotted]
|
||||
type_Definition -> type_DefinitionModifiers [style=dotted]
|
||||
@ -129,5 +133,10 @@ digraph A {
|
||||
type_Return -> type_Expression [style=dotted]
|
||||
type_NativeFunctionDescriptor -> type_Symbol [style=dotted]
|
||||
type_NativeFunctionDescriptor -> type_StringLiteral [style=dotted]
|
||||
type_NativeTypeDescriptor -> type_Symbol [style=dotted]
|
||||
type_NativeTypeDescriptor -> type_StringLiteral [style=dotted]
|
||||
type_IndexedBy -> type_Expression [style=dotted]
|
||||
type_TypeDefinition -> type_DefinitionModifiers [style=dotted]
|
||||
type_TypeDefinition -> type_Symbol [style=dotted]
|
||||
type_TypeDefinition -> type_NativeTypeDescriptor [style=dotted]
|
||||
}
|
||||
|
@ -23,6 +23,10 @@ abstract class FunctionLevelVisitor<T> : NodeVisitor<T> {
|
||||
topLevelUsedError("FunctionDefinition")
|
||||
}
|
||||
|
||||
override fun visitTypeDefinition(node: TypeDefinition): T {
|
||||
topLevelUsedError("TypeDefinition")
|
||||
}
|
||||
|
||||
override fun visitImportDeclaration(node: ImportDeclaration): T {
|
||||
topLevelUsedError("ImportDeclaration")
|
||||
}
|
||||
@ -39,6 +43,10 @@ abstract class FunctionLevelVisitor<T> : NodeVisitor<T> {
|
||||
topLevelUsedError("NativeFunctionDescriptor")
|
||||
}
|
||||
|
||||
override fun visitNativeTypeDescriptor(node: NativeTypeDescriptor): T {
|
||||
topLevelUsedError("NativeTypeDescriptor")
|
||||
}
|
||||
|
||||
private fun topLevelUsedError(name: String): Nothing {
|
||||
throw RuntimeException("$name cannot be visited in a FunctionVisitor.")
|
||||
}
|
||||
|
@ -0,0 +1,29 @@
|
||||
// GENERATED CODE FROM PORK AST CODEGEN
|
||||
package gay.pizza.pork.ast.gen
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
@SerialName("nativeTypeDescriptor")
|
||||
class NativeTypeDescriptor(val form: Symbol, val definitions: List<StringLiteral>) : Node() {
|
||||
override val type: NodeType = NodeType.NativeTypeDescriptor
|
||||
|
||||
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
|
||||
visitor.visitAll(listOf(form), definitions)
|
||||
|
||||
override fun <T> visit(visitor: NodeVisitor<T>): T =
|
||||
visitor.visitNativeTypeDescriptor(this)
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (other !is NativeTypeDescriptor) return false
|
||||
return other.form == form && other.definitions == definitions
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = form.hashCode()
|
||||
result = 31 * result + definitions.hashCode()
|
||||
result = 31 * result + type.hashCode()
|
||||
return result
|
||||
}
|
||||
}
|
@ -68,6 +68,9 @@ class NodeCoalescer(val followChildren: Boolean = true, val handler: (Node) -> U
|
||||
override fun visitNativeFunctionDescriptor(node: NativeFunctionDescriptor): Unit =
|
||||
handle(node)
|
||||
|
||||
override fun visitNativeTypeDescriptor(node: NativeTypeDescriptor): Unit =
|
||||
handle(node)
|
||||
|
||||
override fun visitNoneLiteral(node: NoneLiteral): Unit =
|
||||
handle(node)
|
||||
|
||||
@ -95,6 +98,9 @@ class NodeCoalescer(val followChildren: Boolean = true, val handler: (Node) -> U
|
||||
override fun visitSymbolReference(node: SymbolReference): Unit =
|
||||
handle(node)
|
||||
|
||||
override fun visitTypeDefinition(node: TypeDefinition): Unit =
|
||||
handle(node)
|
||||
|
||||
override fun visitTypeSpec(node: TypeSpec): Unit =
|
||||
handle(node)
|
||||
|
||||
|
@ -52,6 +52,8 @@ interface NodeParser {
|
||||
|
||||
fun parseNativeFunctionDescriptor(): NativeFunctionDescriptor
|
||||
|
||||
fun parseNativeTypeDescriptor(): NativeTypeDescriptor
|
||||
|
||||
fun parseNoneLiteral(): NoneLiteral
|
||||
|
||||
fun parseParentheses(): Parentheses
|
||||
@ -70,6 +72,8 @@ interface NodeParser {
|
||||
|
||||
fun parseSymbolReference(): SymbolReference
|
||||
|
||||
fun parseTypeDefinition(): TypeDefinition
|
||||
|
||||
fun parseTypeSpec(): TypeSpec
|
||||
|
||||
fun parseVarAssignment(): VarAssignment
|
||||
|
@ -39,6 +39,8 @@ fun NodeParser.parse(type: NodeType): Node =
|
||||
NodeType.Return -> parseReturn()
|
||||
NodeType.NoneLiteral -> parseNoneLiteral()
|
||||
NodeType.NativeFunctionDescriptor -> parseNativeFunctionDescriptor()
|
||||
NodeType.NativeTypeDescriptor -> parseNativeTypeDescriptor()
|
||||
NodeType.IndexedBy -> parseIndexedBy()
|
||||
NodeType.TypeDefinition -> parseTypeDefinition()
|
||||
else -> throw RuntimeException("Unable to automatically parse type: ${type.name}")
|
||||
}
|
||||
|
@ -28,6 +28,7 @@ enum class NodeType(val parent: NodeType? = null) {
|
||||
ListLiteral(Expression),
|
||||
LongLiteral(Expression),
|
||||
NativeFunctionDescriptor(Node),
|
||||
NativeTypeDescriptor(Node),
|
||||
NoneLiteral(Expression),
|
||||
Parentheses(Expression),
|
||||
PrefixOperation(Expression),
|
||||
@ -37,6 +38,7 @@ enum class NodeType(val parent: NodeType? = null) {
|
||||
SuffixOperation(Expression),
|
||||
Symbol(Node),
|
||||
SymbolReference(Expression),
|
||||
TypeDefinition(Definition),
|
||||
TypeSpec(Node),
|
||||
VarAssignment(Expression),
|
||||
While(Expression)
|
||||
|
@ -46,6 +46,8 @@ interface NodeVisitor<T> {
|
||||
|
||||
fun visitNativeFunctionDescriptor(node: NativeFunctionDescriptor): T
|
||||
|
||||
fun visitNativeTypeDescriptor(node: NativeTypeDescriptor): T
|
||||
|
||||
fun visitNoneLiteral(node: NoneLiteral): T
|
||||
|
||||
fun visitParentheses(node: Parentheses): T
|
||||
@ -64,6 +66,8 @@ interface NodeVisitor<T> {
|
||||
|
||||
fun visitSymbolReference(node: SymbolReference): T
|
||||
|
||||
fun visitTypeDefinition(node: TypeDefinition): T
|
||||
|
||||
fun visitTypeSpec(node: TypeSpec): T
|
||||
|
||||
fun visitVarAssignment(node: VarAssignment): T
|
||||
|
@ -36,7 +36,9 @@ fun <T> NodeVisitor<T>.visit(node: Node): T =
|
||||
is Return -> visitReturn(node)
|
||||
is NoneLiteral -> visitNoneLiteral(node)
|
||||
is NativeFunctionDescriptor -> visitNativeFunctionDescriptor(node)
|
||||
is NativeTypeDescriptor -> visitNativeTypeDescriptor(node)
|
||||
is IndexedBy -> visitIndexedBy(node)
|
||||
is TypeDefinition -> visitTypeDefinition(node)
|
||||
}
|
||||
|
||||
fun <T> NodeVisitor<T>.visitNodes(vararg nodes: Node?): List<T> =
|
||||
|
30
ast/src/main/kotlin/gay/pizza/pork/ast/gen/TypeDefinition.kt
Normal file
30
ast/src/main/kotlin/gay/pizza/pork/ast/gen/TypeDefinition.kt
Normal file
@ -0,0 +1,30 @@
|
||||
// GENERATED CODE FROM PORK AST CODEGEN
|
||||
package gay.pizza.pork.ast.gen
|
||||
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
@SerialName("typeDefinition")
|
||||
class TypeDefinition(override val modifiers: DefinitionModifiers, override val symbol: Symbol, val nativeTypeDescriptor: NativeTypeDescriptor?) : Definition() {
|
||||
override val type: NodeType = NodeType.TypeDefinition
|
||||
|
||||
override fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> =
|
||||
visitor.visitNodes(symbol, nativeTypeDescriptor)
|
||||
|
||||
override fun <T> visit(visitor: NodeVisitor<T>): T =
|
||||
visitor.visitTypeDefinition(this)
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (other !is TypeDefinition) return false
|
||||
return other.modifiers == modifiers && other.symbol == symbol && other.nativeTypeDescriptor == nativeTypeDescriptor
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = modifiers.hashCode()
|
||||
result = 31 * result + symbol.hashCode()
|
||||
result = 31 * result + nativeTypeDescriptor.hashCode()
|
||||
result = 31 * result + type.hashCode()
|
||||
return result
|
||||
}
|
||||
}
|
@ -3,7 +3,9 @@ package gay.pizza.pork.evaluator
|
||||
import gay.pizza.pork.ast.gen.Definition
|
||||
import gay.pizza.pork.ast.gen.FunctionDefinition
|
||||
import gay.pizza.pork.ast.gen.LetDefinition
|
||||
import gay.pizza.pork.ast.gen.TypeDefinition
|
||||
import gay.pizza.pork.ast.gen.visit
|
||||
import gay.pizza.pork.execution.None
|
||||
import gay.pizza.pork.frontend.Slab
|
||||
|
||||
class SlabContext(val slab: Slab, val evaluator: Evaluator, rootScope: Scope) {
|
||||
@ -50,6 +52,7 @@ class SlabContext(val slab: Slab, val evaluator: Evaluator, rootScope: Scope) {
|
||||
EvaluationVisitor(internalScope.fork("let ${definition.symbol.id}"), CallStack())
|
||||
.visit(definition.value)
|
||||
}
|
||||
is TypeDefinition -> None
|
||||
}
|
||||
|
||||
private fun processFinalImportScopes() {
|
||||
|
@ -8,11 +8,12 @@ class Parser(source: TokenSource, attribution: NodeAttribution) :
|
||||
ParserBase(source, attribution) {
|
||||
override fun parseArgumentSpec(): ArgumentSpec = produce(NodeType.ArgumentSpec) {
|
||||
val symbol = parseSymbol()
|
||||
val multiple = next(TokenType.DotDotDot)
|
||||
var typeSpec: TypeSpec? = null
|
||||
if (next(TokenType.Colon)) {
|
||||
typeSpec = parseTypeSpec()
|
||||
}
|
||||
ArgumentSpec(symbol, typeSpec = typeSpec, next(TokenType.DotDotDot))
|
||||
ArgumentSpec(symbol, typeSpec = typeSpec, multiple = multiple)
|
||||
}
|
||||
|
||||
override fun parseBlock(): Block = expect(NodeType.Block, TokenType.LeftCurly) {
|
||||
@ -175,6 +176,7 @@ class Parser(source: TokenSource, attribution: NodeAttribution) :
|
||||
override fun parseDefinition(): Definition =
|
||||
when (val type = peekAheadUntilNotIn(*TokenType.DeclarationModifiers)) {
|
||||
TokenType.Func -> parseFunctionDefinition()
|
||||
TokenType.Type -> parseTypeDefinition()
|
||||
TokenType.Let -> parseLetDefinition()
|
||||
else -> throw ParseError(
|
||||
"Failed to parse token: ${type.name} as" +
|
||||
@ -330,6 +332,15 @@ class Parser(source: TokenSource, attribution: NodeAttribution) :
|
||||
NativeFunctionDescriptor(form, definitions)
|
||||
}
|
||||
|
||||
override fun parseNativeTypeDescriptor(): NativeTypeDescriptor = expect(NodeType.NativeTypeDescriptor, TokenType.Native) {
|
||||
val form = parseSymbol()
|
||||
val definitions = mutableListOf<StringLiteral>()
|
||||
while (peek(TokenType.Quote)) {
|
||||
definitions.add(parseStringLiteral())
|
||||
}
|
||||
NativeTypeDescriptor(form = form, definitions = definitions)
|
||||
}
|
||||
|
||||
override fun parseNoneLiteral(): NoneLiteral = expect(NodeType.NoneLiteral, TokenType.None) {
|
||||
NoneLiteral()
|
||||
}
|
||||
@ -390,6 +401,15 @@ class Parser(source: TokenSource, attribution: NodeAttribution) :
|
||||
SymbolReference(parseSymbol())
|
||||
}
|
||||
|
||||
override fun parseTypeDefinition(): TypeDefinition = produce(NodeType.TypeDefinition) {
|
||||
val definitionModifiers = parseDefinitionModifiers()
|
||||
expect(TokenType.Type)
|
||||
val name = parseSymbol()
|
||||
expect(TokenType.Equals)
|
||||
val nativeTypeDescriptor = parseNativeTypeDescriptor()
|
||||
TypeDefinition(modifiers = definitionModifiers, symbol = name, nativeTypeDescriptor = nativeTypeDescriptor)
|
||||
}
|
||||
|
||||
override fun parseTypeSpec(): TypeSpec = produce(NodeType.TypeSpec) {
|
||||
TypeSpec(parseSymbol())
|
||||
}
|
||||
|
@ -90,6 +90,18 @@ class Printer(buffer: StringBuilder) : NodeVisitor<Unit> {
|
||||
}
|
||||
}
|
||||
|
||||
override fun visitNativeTypeDescriptor(node: NativeTypeDescriptor) {
|
||||
append("native ")
|
||||
visit(node.form)
|
||||
append(" ")
|
||||
for ((index, argument) in node.definitions.withIndex()) {
|
||||
visit(argument)
|
||||
if (index + 1 != node.definitions.size) {
|
||||
append(" ")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun visitNoneLiteral(node: NoneLiteral) {
|
||||
append("none")
|
||||
}
|
||||
@ -137,6 +149,16 @@ class Printer(buffer: StringBuilder) : NodeVisitor<Unit> {
|
||||
visit(node.symbol)
|
||||
}
|
||||
|
||||
override fun visitTypeDefinition(node: TypeDefinition) {
|
||||
visitDefinitionModifiers(node.modifiers)
|
||||
append("type ")
|
||||
visit(node.symbol)
|
||||
append(" = ")
|
||||
if (node.nativeTypeDescriptor != null) {
|
||||
visit(node.nativeTypeDescriptor!!)
|
||||
}
|
||||
}
|
||||
|
||||
override fun visitTypeSpec(node: TypeSpec) {
|
||||
visit(node.symbol)
|
||||
}
|
||||
|
@ -1,11 +1,17 @@
|
||||
export func print(values...)
|
||||
export type int32 = native internal "int32"
|
||||
export type int64 = native internal "int64"
|
||||
export type string = native internal "string"
|
||||
export type list = native internal "list"
|
||||
export type any = native internal "any"
|
||||
|
||||
export func print(values...: string)
|
||||
native internal "print"
|
||||
|
||||
export func println(values...)
|
||||
export func println(values...: string)
|
||||
native internal "println"
|
||||
|
||||
export func listSet(list, index, value)
|
||||
export func listSet(list: list, index: int32, value: any)
|
||||
native internal "listSet"
|
||||
|
||||
export func listInitWith(size, value)
|
||||
export func listInitWith(size: int32, value: any)
|
||||
native internal "listInitWith"
|
||||
|
@ -0,0 +1,15 @@
|
||||
// GENERATED CODE FROM PORK AST CODEGEN
|
||||
package gay.pizza.pork.idea.psi.gen
|
||||
|
||||
import com.intellij.lang.ASTNode
|
||||
import com.intellij.navigation.ItemPresentation
|
||||
import gay.pizza.pork.idea.psi.PorkElementHelpers
|
||||
import javax.swing.Icon
|
||||
|
||||
class NativeTypeDescriptorElement(node: ASTNode) : PorkElement(node) {
|
||||
override fun getIcon(flags: Int): Icon? =
|
||||
PorkElementHelpers.iconOf(this)
|
||||
|
||||
override fun getPresentation(): ItemPresentation? =
|
||||
PorkElementHelpers.presentationOf(this)
|
||||
}
|
@ -43,7 +43,9 @@ object PorkElementFactory {
|
||||
NodeType.Return -> ReturnElement(node)
|
||||
NodeType.NoneLiteral -> NoneLiteralElement(node)
|
||||
NodeType.NativeFunctionDescriptor -> NativeFunctionDescriptorElement(node)
|
||||
NodeType.NativeTypeDescriptor -> NativeTypeDescriptorElement(node)
|
||||
NodeType.IndexedBy -> IndexedByElement(node)
|
||||
NodeType.TypeDefinition -> TypeDefinitionElement(node)
|
||||
else -> ASTWrapperPsiElement(node)
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,25 @@
|
||||
// GENERATED CODE FROM PORK AST CODEGEN
|
||||
package gay.pizza.pork.idea.psi.gen
|
||||
|
||||
import com.intellij.lang.ASTNode
|
||||
import com.intellij.navigation.ItemPresentation
|
||||
import com.intellij.psi.PsiElement
|
||||
import gay.pizza.pork.idea.psi.PorkElementHelpers
|
||||
import javax.swing.Icon
|
||||
|
||||
class TypeDefinitionElement(node: ASTNode) : PorkNamedElement(node) {
|
||||
override fun getName(): String? =
|
||||
PorkElementHelpers.nameOfNamedElement(this)
|
||||
|
||||
override fun setName(name: String): PsiElement =
|
||||
PorkElementHelpers.setNameOfNamedElement(this, name)
|
||||
|
||||
override fun getNameIdentifier(): PsiElement? =
|
||||
PorkElementHelpers.nameIdentifierOfNamedElement(this)
|
||||
|
||||
override fun getIcon(flags: Int): Icon? =
|
||||
PorkElementHelpers.iconOf(this)
|
||||
|
||||
override fun getPresentation(): ItemPresentation? =
|
||||
PorkElementHelpers.presentationOf(this)
|
||||
}
|
@ -67,6 +67,7 @@ enum class TokenType(vararg val properties: TokenTypeProperty) {
|
||||
Import(AnyOf("import", "impork", "porkload"), KeywordFamily),
|
||||
Export(ManyChars("export"), KeywordFamily),
|
||||
Func(ManyChars("func"), KeywordFamily),
|
||||
Type(ManyChars("type"), KeywordFamily),
|
||||
Native(ManyChars("native"), KeywordFamily),
|
||||
Let(ManyChars("let"), KeywordFamily),
|
||||
Var(ManyChars("var"), KeywordFamily),
|
||||
|
Loading…
Reference in New Issue
Block a user