mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2026-11-04 16:02:20 +00:00
Multi-module arrangement and the start of AST generation.
This commit is contained in:
94 files changed
+377
-138
No files matched your search
@@ -23,5 +23,5 @@ fn main() {
|
|||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```
|
```
|
||||||
./gradlew -q run --args 'run examples/fib.pork'
|
./gradlew -q core:run --args 'run ../examples/fib.pork'
|
||||||
```
|
```
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
plugins {
|
||||||
|
pork_module
|
||||||
|
|
||||||
|
id("gay.pizza.pork.ast")
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
root: Node
|
||||||
|
types:
|
||||||
|
Expression:
|
||||||
|
parent: Node
|
||||||
|
Symbol:
|
||||||
|
parent: Node
|
||||||
|
Declaration:
|
||||||
|
parent: Node
|
||||||
|
Definition:
|
||||||
|
parent: Node
|
||||||
|
values:
|
||||||
|
- name: symbol
|
||||||
|
type: Symbol
|
||||||
|
- name: modifiers
|
||||||
|
type: DefinitionModifiers
|
||||||
|
DefinitionModifiers:
|
||||||
|
values:
|
||||||
|
- name: export
|
||||||
|
type: Boolean
|
||||||
|
Block:
|
||||||
|
parent: Node
|
||||||
|
values:
|
||||||
|
- name: expressions
|
||||||
|
type: List<Expression>
|
||||||
|
CompilationUnit:
|
||||||
|
parent: Node
|
||||||
|
values:
|
||||||
|
- name: declarations
|
||||||
|
type: List<Declaration>
|
||||||
|
- name: definitions
|
||||||
|
type: List<Declaration>
|
||||||
|
Assignment:
|
||||||
|
parent: Expression
|
||||||
|
values:
|
||||||
|
- name: symbol
|
||||||
|
type: Symbol
|
||||||
|
- name: value
|
||||||
|
type: Expression
|
||||||
+1
-3
@@ -1,7 +1,5 @@
|
|||||||
package gay.pizza.pork.ast.nodes
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
import gay.pizza.pork.ast.NodeType
|
|
||||||
import gay.pizza.pork.ast.NodeVisitor
|
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
+1
-3
@@ -1,7 +1,5 @@
|
|||||||
package gay.pizza.pork.ast.nodes
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
import gay.pizza.pork.ast.NodeType
|
|
||||||
import gay.pizza.pork.ast.NodeVisitor
|
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
+1
-2
@@ -1,6 +1,5 @@
|
|||||||
package gay.pizza.pork.ast.nodes
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
import gay.pizza.pork.ast.NodeType
|
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
+1
-3
@@ -1,7 +1,5 @@
|
|||||||
package gay.pizza.pork.ast.nodes
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
import gay.pizza.pork.ast.NodeType
|
|
||||||
import gay.pizza.pork.ast.NodeVisitor
|
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package gay.pizza.pork.ast.nodes
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package gay.pizza.pork.ast.nodes
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package gay.pizza.pork.ast.nodes
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package gay.pizza.pork.ast.nodes
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
+1
-3
@@ -1,7 +1,5 @@
|
|||||||
package gay.pizza.pork.ast.nodes
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
import gay.pizza.pork.ast.NodeType
|
|
||||||
import gay.pizza.pork.ast.NodeVisitor
|
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
+1
-3
@@ -1,7 +1,5 @@
|
|||||||
package gay.pizza.pork.ast.nodes
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
import gay.pizza.pork.ast.NodeType
|
|
||||||
import gay.pizza.pork.ast.NodeVisitor
|
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
+1
-3
@@ -1,7 +1,5 @@
|
|||||||
package gay.pizza.pork.ast.nodes
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
import gay.pizza.pork.ast.NodeType
|
|
||||||
import gay.pizza.pork.ast.NodeVisitor
|
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
+1
-3
@@ -1,7 +1,5 @@
|
|||||||
package gay.pizza.pork.ast.nodes
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
import gay.pizza.pork.ast.NodeType
|
|
||||||
import gay.pizza.pork.ast.NodeVisitor
|
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
+1
-3
@@ -1,7 +1,5 @@
|
|||||||
package gay.pizza.pork.ast.nodes
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
import gay.pizza.pork.ast.NodeType
|
|
||||||
import gay.pizza.pork.ast.NodeVisitor
|
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package gay.pizza.pork.ast.nodes
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
+1
-2
@@ -1,6 +1,5 @@
|
|||||||
package gay.pizza.pork.ast.nodes
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
import gay.pizza.pork.ast.NodeType
|
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
+1
-3
@@ -1,7 +1,5 @@
|
|||||||
package gay.pizza.pork.ast.nodes
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
import gay.pizza.pork.ast.NodeType
|
|
||||||
import gay.pizza.pork.ast.NodeVisitor
|
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
+1
-3
@@ -1,7 +1,5 @@
|
|||||||
package gay.pizza.pork.ast.nodes
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
import gay.pizza.pork.ast.NodeType
|
|
||||||
import gay.pizza.pork.ast.NodeVisitor
|
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
sealed class Node {
|
||||||
|
abstract val type: NodeType
|
||||||
|
open fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> = emptyList()
|
||||||
|
}
|
||||||
-2
@@ -1,7 +1,5 @@
|
|||||||
package gay.pizza.pork.ast
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
import gay.pizza.pork.ast.nodes.*
|
|
||||||
|
|
||||||
class NodeCoalescer(val handler: (Node) -> Unit) : NodeVisitor<Unit> {
|
class NodeCoalescer(val handler: (Node) -> Unit) : NodeVisitor<Unit> {
|
||||||
override fun visitIntLiteral(node: IntLiteral): Unit = handle(node)
|
override fun visitIntLiteral(node: IntLiteral): Unit = handle(node)
|
||||||
override fun visitStringLiteral(node: StringLiteral): Unit = handle(node)
|
override fun visitStringLiteral(node: StringLiteral): Unit = handle(node)
|
||||||
File renamed without changes.
-2
@@ -1,7 +1,5 @@
|
|||||||
package gay.pizza.pork.ast
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
import gay.pizza.pork.ast.nodes.*
|
|
||||||
|
|
||||||
interface NodeVisitor<T> {
|
interface NodeVisitor<T> {
|
||||||
fun visitIntLiteral(node: IntLiteral): T
|
fun visitIntLiteral(node: IntLiteral): T
|
||||||
fun visitStringLiteral(node: StringLiteral): T
|
fun visitStringLiteral(node: StringLiteral): T
|
||||||
+1
-3
@@ -1,7 +1,5 @@
|
|||||||
package gay.pizza.pork.ast.nodes
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
import gay.pizza.pork.ast.NodeType
|
|
||||||
import gay.pizza.pork.ast.NodeVisitor
|
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
+1
-3
@@ -1,7 +1,5 @@
|
|||||||
package gay.pizza.pork.ast.nodes
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
import gay.pizza.pork.ast.NodeType
|
|
||||||
import gay.pizza.pork.ast.NodeVisitor
|
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package gay.pizza.pork.ast.nodes
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
+1
-2
@@ -1,6 +1,5 @@
|
|||||||
package gay.pizza.pork.ast.nodes
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
import gay.pizza.pork.ast.NodeType
|
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
+1
-2
@@ -1,6 +1,5 @@
|
|||||||
package gay.pizza.pork.ast.nodes
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
import gay.pizza.pork.ast.NodeType
|
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
+1
-3
@@ -1,7 +1,5 @@
|
|||||||
package gay.pizza.pork.ast.nodes
|
package gay.pizza.pork.ast
|
||||||
|
|
||||||
import gay.pizza.pork.ast.NodeType
|
|
||||||
import gay.pizza.pork.ast.NodeVisitor
|
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
@@ -1,51 +1,3 @@
|
|||||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
|
||||||
|
|
||||||
plugins {
|
|
||||||
application
|
|
||||||
|
|
||||||
kotlin("jvm") version "1.9.10"
|
|
||||||
kotlin("plugin.serialization") version "1.9.10"
|
|
||||||
|
|
||||||
id("com.github.johnrengelman.shadow") version "8.1.1"
|
|
||||||
id("org.graalvm.buildtools.native") version "0.9.25"
|
|
||||||
}
|
|
||||||
|
|
||||||
repositories {
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
java {
|
|
||||||
val javaVersion = JavaVersion.toVersion(17)
|
|
||||||
sourceCompatibility = javaVersion
|
|
||||||
targetCompatibility = javaVersion
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
implementation("org.jetbrains.kotlin:kotlin-bom")
|
|
||||||
implementation("com.github.ajalt.clikt:clikt:4.2.0")
|
|
||||||
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks.withType<KotlinCompile> {
|
|
||||||
kotlinOptions.jvmTarget = "17"
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks.withType<Wrapper> {
|
tasks.withType<Wrapper> {
|
||||||
gradleVersion = "8.3"
|
gradleVersion = "8.3"
|
||||||
}
|
}
|
||||||
|
|
||||||
application {
|
|
||||||
mainClass.set("gay.pizza.pork.cli.MainKt")
|
|
||||||
}
|
|
||||||
|
|
||||||
graalvmNative {
|
|
||||||
binaries {
|
|
||||||
named("main") {
|
|
||||||
imageName.set("pork")
|
|
||||||
mainClass.set("gay.pizza.pork.cli.MainKt")
|
|
||||||
sharedLibrary.set(false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks.run.get().outputs.upToDateWhen { false }
|
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
plugins {
|
||||||
|
`kotlin-dsl`
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
gradlePluginPortal()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.10")
|
||||||
|
implementation("org.jetbrains.kotlin:kotlin-serialization:1.9.10")
|
||||||
|
|
||||||
|
implementation("com.fasterxml.jackson.core:jackson-databind:2.15.2")
|
||||||
|
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.15.2")
|
||||||
|
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.15.2")
|
||||||
|
}
|
||||||
|
|
||||||
|
gradlePlugin {
|
||||||
|
plugins {
|
||||||
|
create("pork_ast") {
|
||||||
|
id = "gay.pizza.pork.ast"
|
||||||
|
implementationClass = "gay.pizza.pork.gradle.PorkAstPlugin"
|
||||||
|
|
||||||
|
displayName = "Pork AST"
|
||||||
|
description = "AST generation code for pork"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package gay.pizza.pork.gradle
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper
|
||||||
|
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
|
||||||
|
import com.fasterxml.jackson.module.kotlin.KotlinModule
|
||||||
|
import gay.pizza.pork.gradle.ast.AstDescription
|
||||||
|
import org.gradle.api.DefaultTask
|
||||||
|
import org.gradle.api.tasks.InputFile
|
||||||
|
import org.gradle.api.tasks.TaskAction
|
||||||
|
import gay.pizza.pork.gradle.ast.AstWorld
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
open class GenerateAstCode : DefaultTask() {
|
||||||
|
init {
|
||||||
|
outputs.upToDateWhen { false }
|
||||||
|
}
|
||||||
|
|
||||||
|
@get:InputFile
|
||||||
|
val astDescriptionFile: File = project.file("src/main/ast/pork.yml")
|
||||||
|
|
||||||
|
@TaskAction
|
||||||
|
fun generate() {
|
||||||
|
val astYamlText = astDescriptionFile.readText()
|
||||||
|
val mapper = ObjectMapper(YAMLFactory())
|
||||||
|
mapper.registerModules(KotlinModule.Builder().build())
|
||||||
|
val astDescription = mapper.readValue(astYamlText, AstDescription::class.java)
|
||||||
|
val world = AstWorld()
|
||||||
|
world.build(astDescription)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package gay.pizza.pork.gradle
|
||||||
|
|
||||||
|
import org.gradle.api.Plugin
|
||||||
|
import org.gradle.api.Project
|
||||||
|
|
||||||
|
class PorkAstPlugin : Plugin<Project> {
|
||||||
|
override fun apply(target: Project) {
|
||||||
|
target.tasks.create("generateAstCode", GenerateAstCode::class.java)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package gay.pizza.pork.gradle.ast
|
||||||
|
|
||||||
|
data class AstDescription(
|
||||||
|
val root: String,
|
||||||
|
val types: Map<String, AstTypeDescription>
|
||||||
|
)
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package gay.pizza.pork.gradle.ast
|
||||||
|
|
||||||
|
enum class AstPrimitive(val id: kotlin.String) {
|
||||||
|
Boolean("Boolean"),
|
||||||
|
String("String")
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package gay.pizza.pork.gradle.ast
|
||||||
|
|
||||||
|
class AstType(val name: String, var parent: AstType? = null) {
|
||||||
|
private val internalValues = mutableListOf<AstValue>()
|
||||||
|
|
||||||
|
val values: List<AstValue>
|
||||||
|
get() = internalValues
|
||||||
|
|
||||||
|
internal fun addValue(value: AstValue) {
|
||||||
|
internalValues.add(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package gay.pizza.pork.gradle.ast
|
||||||
|
|
||||||
|
data class AstTypeDescription(
|
||||||
|
val parent: String? = null,
|
||||||
|
val values: List<AstValueDescription> = emptyList()
|
||||||
|
)
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package gay.pizza.pork.gradle.ast
|
||||||
|
|
||||||
|
class AstTypeRef(
|
||||||
|
val type: AstType? = null,
|
||||||
|
val primitive: AstPrimitive? = null,
|
||||||
|
val form: AstTypeRefForm
|
||||||
|
) {
|
||||||
|
companion object {
|
||||||
|
fun parse(input: String, registry: AstTypeRegistry): AstTypeRef {
|
||||||
|
if (input.startsWith("List<")) {
|
||||||
|
val underlyingType = input.substring(5, input.length - 1)
|
||||||
|
val underlyingRef = parse(underlyingType, registry)
|
||||||
|
return AstTypeRef(
|
||||||
|
type = underlyingRef.type,
|
||||||
|
primitive = underlyingRef.primitive,
|
||||||
|
form = AstTypeRefForm.List
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
val primitive = AstPrimitive.values().firstOrNull { it.name == input }
|
||||||
|
if (primitive != null) {
|
||||||
|
return AstTypeRef(
|
||||||
|
primitive = primitive,
|
||||||
|
form = AstTypeRefForm.Single
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return AstTypeRef(type = registry.lookup(input), form = AstTypeRefForm.Single)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package gay.pizza.pork.gradle.ast
|
||||||
|
|
||||||
|
enum class AstTypeRefForm {
|
||||||
|
Single,
|
||||||
|
List
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package gay.pizza.pork.gradle.ast
|
||||||
|
|
||||||
|
class AstTypeRegistry {
|
||||||
|
private val internalTypes = mutableSetOf<AstType>()
|
||||||
|
|
||||||
|
val types: Set<AstType>
|
||||||
|
get() = internalTypes
|
||||||
|
|
||||||
|
fun add(type: AstType): AstType {
|
||||||
|
internalTypes.add(type)
|
||||||
|
return type
|
||||||
|
}
|
||||||
|
|
||||||
|
fun lookupOrNull(name: String): AstType? =
|
||||||
|
internalTypes.singleOrNull { it.name == name }
|
||||||
|
|
||||||
|
fun lookup(name: String): AstType = lookupOrNull(name)
|
||||||
|
?: throw RuntimeException("Unknown AstType: $name")
|
||||||
|
|
||||||
|
fun roleOfType(type: AstType): AstTypeRole =
|
||||||
|
when {
|
||||||
|
type.parent == null && type.values.isNotEmpty() ->
|
||||||
|
AstTypeRole.RootNode
|
||||||
|
type.parent != null && type.values.all { it.abstract } ->
|
||||||
|
AstTypeRole.HierarchyNode
|
||||||
|
type.parent != null && type.values.none { it.abstract } ->
|
||||||
|
AstTypeRole.AstNode
|
||||||
|
type.parent == null && type.values.isNotEmpty() ->
|
||||||
|
AstTypeRole.ValueHolder
|
||||||
|
else -> throw RuntimeException("Unable to determine role of type ${type.name}")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package gay.pizza.pork.gradle.ast
|
||||||
|
|
||||||
|
enum class AstTypeRole {
|
||||||
|
RootNode,
|
||||||
|
HierarchyNode,
|
||||||
|
AstNode,
|
||||||
|
ValueHolder
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package gay.pizza.pork.gradle.ast
|
||||||
|
|
||||||
|
class AstValue(
|
||||||
|
val name: String,
|
||||||
|
val typeRef: AstTypeRef,
|
||||||
|
val abstract: Boolean = false
|
||||||
|
)
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package gay.pizza.pork.gradle.ast
|
||||||
|
|
||||||
|
data class AstValueDescription(
|
||||||
|
val name: String,
|
||||||
|
val type: String
|
||||||
|
)
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package gay.pizza.pork.gradle.ast
|
||||||
|
|
||||||
|
class AstWorld {
|
||||||
|
val typeRegistry: AstTypeRegistry = AstTypeRegistry()
|
||||||
|
|
||||||
|
fun build(description: AstDescription) {
|
||||||
|
val rootType = typeRegistry.add(AstType(description.root))
|
||||||
|
for (typeName in description.types.keys) {
|
||||||
|
if (typeName == rootType.name) {
|
||||||
|
throw RuntimeException("Cannot have type with the same name as the root type.")
|
||||||
|
}
|
||||||
|
|
||||||
|
typeRegistry.add(AstType(typeName))
|
||||||
|
}
|
||||||
|
|
||||||
|
for ((typeName, typeDescription) in description.types) {
|
||||||
|
val type = typeRegistry.lookup(typeName)
|
||||||
|
if (typeDescription.parent != null) {
|
||||||
|
type.parent = typeRegistry.lookup(typeDescription.parent)
|
||||||
|
}
|
||||||
|
for (value in typeDescription.values) {
|
||||||
|
val typeRef = AstTypeRef.parse(value.type, typeRegistry)
|
||||||
|
val typeValue = AstValue(value.name, typeRef)
|
||||||
|
type.addValue(typeValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
kotlin("jvm")
|
||||||
|
kotlin("plugin.serialization")
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
java {
|
||||||
|
val javaVersion = JavaVersion.toVersion(17)
|
||||||
|
sourceCompatibility = javaVersion
|
||||||
|
targetCompatibility = javaVersion
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.withType<KotlinCompile> {
|
||||||
|
kotlinOptions.jvmTarget = "17"
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation("org.jetbrains.kotlin:kotlin-bom")
|
||||||
|
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.0")
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
plugins {
|
||||||
|
application
|
||||||
|
pork_module
|
||||||
|
id("com.github.johnrengelman.shadow") version "8.1.1"
|
||||||
|
id("org.graalvm.buildtools.native") version "0.9.25"
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
api(project(":ast"))
|
||||||
|
implementation(libs.clikt)
|
||||||
|
}
|
||||||
|
|
||||||
|
application {
|
||||||
|
mainClass.set("gay.pizza.pork.cli.MainKt")
|
||||||
|
}
|
||||||
|
|
||||||
|
graalvmNative {
|
||||||
|
binaries {
|
||||||
|
named("main") {
|
||||||
|
imageName.set("pork")
|
||||||
|
mainClass.set("gay.pizza.pork.cli.MainKt")
|
||||||
|
sharedLibrary.set(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.run.get().outputs.upToDateWhen { false }
|
||||||
File renamed without changes.
+1
-1
@@ -3,7 +3,7 @@ package gay.pizza.pork.cli
|
|||||||
import com.github.ajalt.clikt.core.CliktCommand
|
import com.github.ajalt.clikt.core.CliktCommand
|
||||||
import com.github.ajalt.clikt.parameters.arguments.argument
|
import com.github.ajalt.clikt.parameters.arguments.argument
|
||||||
import com.github.ajalt.clikt.parameters.types.path
|
import com.github.ajalt.clikt.parameters.types.path
|
||||||
import gay.pizza.pork.ast.nodes.Node
|
import gay.pizza.pork.ast.Node
|
||||||
import kotlinx.serialization.ExperimentalSerializationApi
|
import kotlinx.serialization.ExperimentalSerializationApi
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
|
|
||||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+2
-2
@@ -1,8 +1,8 @@
|
|||||||
package gay.pizza.pork.cli
|
package gay.pizza.pork.cli
|
||||||
|
|
||||||
import gay.pizza.pork.ast.NodeVisitor
|
import gay.pizza.pork.ast.NodeVisitor
|
||||||
import gay.pizza.pork.ast.Printer
|
import gay.pizza.pork.parse.Printer
|
||||||
import gay.pizza.pork.ast.nodes.CompilationUnit
|
import gay.pizza.pork.ast.CompilationUnit
|
||||||
import gay.pizza.pork.eval.*
|
import gay.pizza.pork.eval.*
|
||||||
import gay.pizza.pork.frontend.ContentSource
|
import gay.pizza.pork.frontend.ContentSource
|
||||||
import gay.pizza.pork.frontend.World
|
import gay.pizza.pork.frontend.World
|
||||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+2
-2
@@ -1,7 +1,7 @@
|
|||||||
package gay.pizza.pork.eval
|
package gay.pizza.pork.eval
|
||||||
|
|
||||||
import gay.pizza.pork.ast.nodes.CompilationUnit
|
import gay.pizza.pork.ast.CompilationUnit
|
||||||
import gay.pizza.pork.ast.nodes.ImportDeclaration
|
import gay.pizza.pork.ast.ImportDeclaration
|
||||||
|
|
||||||
class EvaluationContext(
|
class EvaluationContext(
|
||||||
val compilationUnit: CompilationUnit,
|
val compilationUnit: CompilationUnit,
|
||||||
File renamed without changes.
+2
-3
@@ -1,7 +1,6 @@
|
|||||||
package gay.pizza.pork.eval
|
package gay.pizza.pork.eval
|
||||||
|
|
||||||
import gay.pizza.pork.ast.NodeVisitor
|
import gay.pizza.pork.ast.*
|
||||||
import gay.pizza.pork.ast.nodes.*
|
|
||||||
|
|
||||||
class EvaluationVisitor(root: Scope) : NodeVisitor<Any> {
|
class EvaluationVisitor(root: Scope) : NodeVisitor<Any> {
|
||||||
private var currentScope: Scope = root
|
private var currentScope: Scope = root
|
||||||
@@ -65,7 +64,7 @@ class EvaluationVisitor(root: Scope) : NodeVisitor<Any> {
|
|||||||
visit(node.thenExpression)
|
visit(node.thenExpression)
|
||||||
} else {
|
} else {
|
||||||
if (node.elseExpression != null) {
|
if (node.elseExpression != null) {
|
||||||
visit(node.elseExpression)
|
visit(node.elseExpression!!)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+2
-2
@@ -1,7 +1,7 @@
|
|||||||
package gay.pizza.pork.frontend
|
package gay.pizza.pork.frontend
|
||||||
|
|
||||||
import gay.pizza.pork.ast.nodes.CompilationUnit
|
import gay.pizza.pork.ast.CompilationUnit
|
||||||
import gay.pizza.pork.ast.nodes.ImportDeclaration
|
import gay.pizza.pork.ast.ImportDeclaration
|
||||||
import gay.pizza.pork.parse.DiscardNodeAttribution
|
import gay.pizza.pork.parse.DiscardNodeAttribution
|
||||||
import gay.pizza.pork.parse.Parser
|
import gay.pizza.pork.parse.Parser
|
||||||
import gay.pizza.pork.parse.TokenStreamSource
|
import gay.pizza.pork.parse.TokenStreamSource
|
||||||
File renamed without changes.
File renamed without changes.
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
package gay.pizza.pork.parse
|
package gay.pizza.pork.parse
|
||||||
|
|
||||||
import gay.pizza.pork.ast.nodes.Node
|
import gay.pizza.pork.ast.Node
|
||||||
|
|
||||||
object DiscardNodeAttribution : NodeAttribution {
|
object DiscardNodeAttribution : NodeAttribution {
|
||||||
override fun enter() {}
|
override fun enter() {}
|
||||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
package gay.pizza.pork.parse
|
package gay.pizza.pork.parse
|
||||||
|
|
||||||
import gay.pizza.pork.ast.nodes.Node
|
import gay.pizza.pork.ast.Node
|
||||||
|
|
||||||
interface NodeAttribution {
|
interface NodeAttribution {
|
||||||
fun enter()
|
fun enter()
|
||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
package gay.pizza.pork.parse
|
package gay.pizza.pork.parse
|
||||||
|
|
||||||
import gay.pizza.pork.ast.nodes.*
|
import gay.pizza.pork.ast.*
|
||||||
import gay.pizza.pork.util.StringEscape
|
import gay.pizza.pork.util.StringEscape
|
||||||
|
|
||||||
class Parser(source: PeekableSource<Token>, val attribution: NodeAttribution) {
|
class Parser(source: PeekableSource<Token>, val attribution: NodeAttribution) {
|
||||||
File renamed without changes.
+3
-3
@@ -1,6 +1,6 @@
|
|||||||
package gay.pizza.pork.ast
|
package gay.pizza.pork.parse
|
||||||
|
|
||||||
import gay.pizza.pork.ast.nodes.*
|
import gay.pizza.pork.ast.*
|
||||||
import gay.pizza.pork.util.IndentPrinter
|
import gay.pizza.pork.util.IndentPrinter
|
||||||
import gay.pizza.pork.util.StringEscape
|
import gay.pizza.pork.util.StringEscape
|
||||||
|
|
||||||
@@ -134,7 +134,7 @@ class Printer(buffer: StringBuilder) : NodeVisitor<Unit> {
|
|||||||
append("else")
|
append("else")
|
||||||
out.increaseIndent()
|
out.increaseIndent()
|
||||||
appendLine()
|
appendLine()
|
||||||
visit(node.elseExpression)
|
visit(node.elseExpression!!)
|
||||||
out.decreaseIndent()
|
out.decreaseIndent()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
package gay.pizza.pork.parse
|
package gay.pizza.pork.parse
|
||||||
|
|
||||||
import gay.pizza.pork.ast.NodeCoalescer
|
import gay.pizza.pork.ast.NodeCoalescer
|
||||||
import gay.pizza.pork.ast.nodes.Node
|
import gay.pizza.pork.ast.Node
|
||||||
import java.util.IdentityHashMap
|
import java.util.IdentityHashMap
|
||||||
|
|
||||||
class TokenNodeAttribution : NodeAttribution {
|
class TokenNodeAttribution : NodeAttribution {
|
||||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -1 +1,17 @@
|
|||||||
rootProject.name = "pork"
|
rootProject.name = "pork"
|
||||||
|
|
||||||
|
include(
|
||||||
|
":ast",
|
||||||
|
":core"
|
||||||
|
)
|
||||||
|
|
||||||
|
dependencyResolutionManagement {
|
||||||
|
versionCatalogs {
|
||||||
|
create("libs") {
|
||||||
|
version("clikt", "4.2.0")
|
||||||
|
|
||||||
|
library("clikt", "com.github.ajalt.clikt", "clikt")
|
||||||
|
.versionRef("clikt")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
package gay.pizza.pork.ast.nodes
|
|
||||||
|
|
||||||
import gay.pizza.pork.ast.NodeType
|
|
||||||
import gay.pizza.pork.ast.Printer
|
|
||||||
import gay.pizza.pork.ast.NodeVisitor
|
|
||||||
import kotlinx.serialization.Serializable
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
sealed class Node {
|
|
||||||
abstract val type: NodeType
|
|
||||||
open fun <T> visitChildren(visitor: NodeVisitor<T>): List<T> = emptyList()
|
|
||||||
|
|
||||||
override fun toString(): String =
|
|
||||||
let { node -> buildString { Printer(this).visit(node) } }
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user