mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-02 12:50:55 +00:00
Split out all code into modules.
This commit is contained in:
parent
d46ea1e307
commit
128f40bcf4
@ -23,5 +23,5 @@ fn main() {
|
||||
## Usage
|
||||
|
||||
```
|
||||
./gradlew -q core:run --args 'run ../examples/fib.pork'
|
||||
./gradlew -q tool:run --args 'run ../examples/fib.pork'
|
||||
```
|
||||
|
3
common/build.gradle.kts
Normal file
3
common/build.gradle.kts
Normal file
@ -0,0 +1,3 @@
|
||||
plugins {
|
||||
pork_module
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.util
|
||||
package gay.pizza.pork.common
|
||||
|
||||
class IndentPrinter(
|
||||
val buffer: StringBuilder = StringBuilder(),
|
@ -1,3 +0,0 @@
|
||||
package gay.pizza.pork
|
||||
|
||||
object PorkLanguage
|
@ -1,3 +0,0 @@
|
||||
package gay.pizza.pork.eval
|
||||
|
||||
data object None
|
10
evaluator/build.gradle.kts
Normal file
10
evaluator/build.gradle.kts
Normal file
@ -0,0 +1,10 @@
|
||||
plugins {
|
||||
pork_module
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api(project(":ast"))
|
||||
api(project(":frontend"))
|
||||
|
||||
implementation(project(":common"))
|
||||
}
|
@ -1,3 +1,3 @@
|
||||
package gay.pizza.pork.eval
|
||||
package gay.pizza.pork.evaluator
|
||||
|
||||
class Arguments(val values: List<Any>)
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.eval
|
||||
package gay.pizza.pork.evaluator
|
||||
|
||||
fun interface BlockFunction {
|
||||
fun call(): Any
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.eval
|
||||
package gay.pizza.pork.evaluator
|
||||
|
||||
fun interface CallableFunction {
|
||||
fun call(arguments: Arguments): Any
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.eval
|
||||
package gay.pizza.pork.evaluator
|
||||
|
||||
import gay.pizza.pork.ast.CompilationUnit
|
||||
import gay.pizza.pork.ast.ImportDeclaration
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.eval
|
||||
package gay.pizza.pork.evaluator
|
||||
|
||||
interface EvaluationContextProvider {
|
||||
fun provideEvaluationContext(path: String): EvaluationContext
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.eval
|
||||
package gay.pizza.pork.evaluator
|
||||
|
||||
import gay.pizza.pork.ast.*
|
||||
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.eval
|
||||
package gay.pizza.pork.evaluator
|
||||
|
||||
import gay.pizza.pork.frontend.World
|
||||
|
@ -0,0 +1,3 @@
|
||||
package gay.pizza.pork.evaluator
|
||||
|
||||
data object None
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.eval
|
||||
package gay.pizza.pork.evaluator
|
||||
|
||||
class Scope(val parent: Scope? = null, inherits: List<Scope> = emptyList()) {
|
||||
private val inherited = inherits.toMutableList()
|
10
frontend/build.gradle.kts
Normal file
10
frontend/build.gradle.kts
Normal file
@ -0,0 +1,10 @@
|
||||
plugins {
|
||||
pork_module
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api(project(":ast"))
|
||||
api(project(":parser"))
|
||||
|
||||
implementation(project(":common"))
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
package gay.pizza.pork.frontend
|
||||
|
||||
import gay.pizza.pork.parse.CharSource
|
||||
import gay.pizza.pork.parser.CharSource
|
||||
|
||||
interface ContentSource {
|
||||
fun loadAsCharSource(path: String): CharSource
|
@ -1,13 +1,12 @@
|
||||
package gay.pizza.pork.frontend
|
||||
|
||||
import gay.pizza.pork.parse.CharSource
|
||||
import gay.pizza.pork.parse.StringCharSource
|
||||
import gay.pizza.pork.parser.StringCharSource
|
||||
import java.nio.file.Path
|
||||
import kotlin.io.path.absolutePathString
|
||||
import kotlin.io.path.readText
|
||||
|
||||
class FsContentSource(val root: Path) : ContentSource {
|
||||
override fun loadAsCharSource(path: String): CharSource =
|
||||
override fun loadAsCharSource(path: String): gay.pizza.pork.parser.CharSource =
|
||||
StringCharSource(asFsPath(path).readText())
|
||||
|
||||
override fun stableContentIdentity(path: String): String =
|
@ -2,10 +2,9 @@ package gay.pizza.pork.frontend
|
||||
|
||||
import gay.pizza.pork.ast.CompilationUnit
|
||||
import gay.pizza.pork.ast.ImportDeclaration
|
||||
import gay.pizza.pork.parse.DiscardNodeAttribution
|
||||
import gay.pizza.pork.parse.Parser
|
||||
import gay.pizza.pork.parse.TokenStreamSource
|
||||
import gay.pizza.pork.parse.Tokenizer
|
||||
import gay.pizza.pork.parser.Parser
|
||||
import gay.pizza.pork.parser.TokenStreamSource
|
||||
import gay.pizza.pork.parser.Tokenizer
|
||||
|
||||
class World(val contentSource: ContentSource) {
|
||||
private val units = mutableMapOf<String, CompilationUnit>()
|
||||
@ -19,7 +18,7 @@ class World(val contentSource: ContentSource) {
|
||||
val charSource = contentSource.loadAsCharSource(path)
|
||||
val tokenizer = Tokenizer(charSource)
|
||||
val tokenStream = tokenizer.tokenize()
|
||||
val parser = Parser(TokenStreamSource(tokenStream), DiscardNodeAttribution)
|
||||
val parser = Parser(TokenStreamSource(tokenStream), gay.pizza.pork.parser.DiscardNodeAttribution)
|
||||
return parser.readCompilationUnit()
|
||||
}
|
||||
|
9
parser/build.gradle.kts
Normal file
9
parser/build.gradle.kts
Normal file
@ -0,0 +1,9 @@
|
||||
plugins {
|
||||
pork_module
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api(project(":ast"))
|
||||
|
||||
implementation(project(":common"))
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.parse
|
||||
package gay.pizza.pork.parser
|
||||
|
||||
open class AnsiHighlightScheme : HighlightScheme {
|
||||
override fun highlight(token: Token): Highlight {
|
||||
@ -16,11 +16,16 @@ open class AnsiHighlightScheme : HighlightScheme {
|
||||
} else Highlight(token)
|
||||
}
|
||||
|
||||
open fun string(): AnsiAttributes = AnsiAttributes("32m")
|
||||
open fun symbol(): AnsiAttributes = AnsiAttributes("33m")
|
||||
open fun operator(): AnsiAttributes = AnsiAttributes("34m")
|
||||
open fun keyword(): AnsiAttributes = AnsiAttributes("35m")
|
||||
open fun comment(): AnsiAttributes = AnsiAttributes("37m")
|
||||
open fun string(): AnsiAttributes =
|
||||
AnsiAttributes("32m")
|
||||
open fun symbol(): AnsiAttributes =
|
||||
AnsiAttributes("33m")
|
||||
open fun operator(): AnsiAttributes =
|
||||
AnsiAttributes("34m")
|
||||
open fun keyword(): AnsiAttributes =
|
||||
AnsiAttributes("35m")
|
||||
open fun comment(): AnsiAttributes =
|
||||
AnsiAttributes("37m")
|
||||
|
||||
private fun ansi(attributes: AnsiAttributes, text: String): String =
|
||||
"\u001b[${attributes.color}${text}\u001b[0m"
|
@ -1,7 +1,8 @@
|
||||
package gay.pizza.pork.parse
|
||||
package gay.pizza.pork.parser
|
||||
|
||||
interface CharSource : PeekableSource<Char> {
|
||||
companion object {
|
||||
@Suppress("ConstPropertyName")
|
||||
const val NullChar = 0.toChar()
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.parse
|
||||
package gay.pizza.pork.parser
|
||||
|
||||
import gay.pizza.pork.ast.Node
|
||||
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.parse
|
||||
package gay.pizza.pork.parser
|
||||
|
||||
class Highlight(val token: Token, val text: String? = null) {
|
||||
override fun toString(): String = text ?: token.text
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.parse
|
||||
package gay.pizza.pork.parser
|
||||
|
||||
interface HighlightScheme {
|
||||
fun highlight(token: Token): Highlight
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.parse
|
||||
package gay.pizza.pork.parser
|
||||
|
||||
class Highlighter(val scheme: HighlightScheme) {
|
||||
fun highlight(stream: TokenStream): List<Highlight> =
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.parse
|
||||
package gay.pizza.pork.parser
|
||||
|
||||
import gay.pizza.pork.ast.Node
|
||||
|
@ -1,7 +1,6 @@
|
||||
package gay.pizza.pork.parse
|
||||
package gay.pizza.pork.parser
|
||||
|
||||
import gay.pizza.pork.ast.*
|
||||
import gay.pizza.pork.util.StringEscape
|
||||
|
||||
class Parser(source: PeekableSource<Token>, val attribution: NodeAttribution) {
|
||||
private val unsanitizedSource = source
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.parse
|
||||
package gay.pizza.pork.parser
|
||||
|
||||
interface PeekableSource<T> {
|
||||
val currentIndex: Int
|
@ -1,8 +1,7 @@
|
||||
package gay.pizza.pork.parse
|
||||
package gay.pizza.pork.parser
|
||||
|
||||
import gay.pizza.pork.ast.*
|
||||
import gay.pizza.pork.util.IndentPrinter
|
||||
import gay.pizza.pork.util.StringEscape
|
||||
import gay.pizza.pork.common.IndentPrinter
|
||||
|
||||
class Printer(buffer: StringBuilder) : NodeVisitor<Unit> {
|
||||
private val out = IndentPrinter(buffer)
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.parse
|
||||
package gay.pizza.pork.parser
|
||||
|
||||
class StringCharSource(val input: String) : CharSource {
|
||||
private var index = 0
|
@ -1,8 +1,7 @@
|
||||
package gay.pizza.pork.util
|
||||
package gay.pizza.pork.parser
|
||||
|
||||
object StringEscape {
|
||||
fun escape(input: String): String = input.replace("\n", "\\n")
|
||||
fun unescape(input: String): String = input.replace("\\n", "\n")
|
||||
|
||||
fun unquote(input: String): String = input.substring(1, input.length - 1)
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.parse
|
||||
package gay.pizza.pork.parser
|
||||
|
||||
class Token(val type: TokenType, val start: Int, val text: String) {
|
||||
override fun toString(): String = "$start ${type.name} '${text.replace("\n", "\\n")}'"
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.parse
|
||||
package gay.pizza.pork.parser
|
||||
|
||||
enum class TokenFamily : TokenTypeProperty {
|
||||
OperatorFamily,
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.parse
|
||||
package gay.pizza.pork.parser
|
||||
|
||||
import gay.pizza.pork.ast.NodeCoalescer
|
||||
import gay.pizza.pork.ast.Node
|
@ -1,3 +1,3 @@
|
||||
package gay.pizza.pork.parse
|
||||
package gay.pizza.pork.parser
|
||||
|
||||
interface TokenSource : PeekableSource<Token>
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.parse
|
||||
package gay.pizza.pork.parser
|
||||
|
||||
class TokenStream(val tokens: List<Token>) {
|
||||
override fun toString(): String = tokens.toString()
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.parse
|
||||
package gay.pizza.pork.parser
|
||||
|
||||
class TokenStreamSource(val stream: TokenStream) : TokenSource {
|
||||
private var index = 0
|
@ -1,7 +1,7 @@
|
||||
package gay.pizza.pork.parse
|
||||
package gay.pizza.pork.parser
|
||||
|
||||
import gay.pizza.pork.parse.TokenTypeProperty.*
|
||||
import gay.pizza.pork.parse.TokenFamily.*
|
||||
import gay.pizza.pork.parser.TokenTypeProperty.*
|
||||
import gay.pizza.pork.parser.TokenFamily.*
|
||||
|
||||
enum class TokenType(vararg properties: TokenTypeProperty) {
|
||||
Symbol(SymbolFamily, CharConsumer { (it in 'a'..'z') || (it in 'A'..'Z') || it == '_' }, KeywordUpgrader),
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.parse
|
||||
package gay.pizza.pork.parser
|
||||
|
||||
interface TokenTypeProperty {
|
||||
class SingleChar(val char: Char) : TokenTypeProperty
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.parse
|
||||
package gay.pizza.pork.parser
|
||||
|
||||
class Tokenizer(val source: CharSource) {
|
||||
private var tokenStart: Int = 0
|
@ -1,8 +1,12 @@
|
||||
rootProject.name = "pork"
|
||||
|
||||
include(
|
||||
":common",
|
||||
":ast",
|
||||
":core"
|
||||
":parser",
|
||||
":frontend",
|
||||
":evaluator",
|
||||
":tool"
|
||||
)
|
||||
|
||||
dependencyResolutionManagement {
|
||||
|
@ -7,18 +7,22 @@ plugins {
|
||||
|
||||
dependencies {
|
||||
api(project(":ast"))
|
||||
api(project(":parser"))
|
||||
api(project(":frontend"))
|
||||
api(project(":evaluator"))
|
||||
implementation(libs.clikt)
|
||||
implementation(project(":common"))
|
||||
}
|
||||
|
||||
application {
|
||||
mainClass.set("gay.pizza.pork.cli.MainKt")
|
||||
mainClass.set("gay.pizza.pork.tool.MainKt")
|
||||
}
|
||||
|
||||
graalvmNative {
|
||||
binaries {
|
||||
named("main") {
|
||||
imageName.set("pork")
|
||||
mainClass.set("gay.pizza.pork.cli.MainKt")
|
||||
mainClass.set("gay.pizza.pork.tool.MainKt")
|
||||
sharedLibrary.set(false)
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.cli
|
||||
package gay.pizza.pork.tool
|
||||
|
||||
import com.github.ajalt.clikt.core.CliktCommand
|
||||
import com.github.ajalt.clikt.parameters.arguments.argument
|
@ -1,10 +1,10 @@
|
||||
package gay.pizza.pork.cli
|
||||
package gay.pizza.pork.tool
|
||||
|
||||
import com.github.ajalt.clikt.core.CliktCommand
|
||||
import com.github.ajalt.clikt.parameters.arguments.argument
|
||||
import com.github.ajalt.clikt.parameters.types.path
|
||||
import gay.pizza.pork.ast.NodeCoalescer
|
||||
import gay.pizza.pork.parse.TokenNodeAttribution
|
||||
import gay.pizza.pork.parser.TokenNodeAttribution
|
||||
|
||||
class AttributeCommand : CliktCommand(help = "Attribute AST", name = "attribute") {
|
||||
val path by argument("file").path(mustExist = true, canBeDir = false)
|
@ -1,14 +1,13 @@
|
||||
package gay.pizza.pork.cli
|
||||
package gay.pizza.pork.tool
|
||||
|
||||
import gay.pizza.pork.frontend.ContentSource
|
||||
import gay.pizza.pork.frontend.FsContentSource
|
||||
import gay.pizza.pork.parse.CharSource
|
||||
import gay.pizza.pork.parse.StringCharSource
|
||||
import gay.pizza.pork.parser.StringCharSource
|
||||
import java.nio.file.Path
|
||||
import kotlin.io.path.readText
|
||||
|
||||
class FileTool(val path: Path) : Tool() {
|
||||
override fun createCharSource(): CharSource = StringCharSource(path.readText())
|
||||
override fun createCharSource(): gay.pizza.pork.parser.CharSource = StringCharSource(path.readText())
|
||||
override fun createContentSource(): ContentSource = FsContentSource(path.parent)
|
||||
override fun rootFilePath(): String = path.fileName.toString()
|
||||
}
|
@ -1,15 +1,14 @@
|
||||
package gay.pizza.pork.cli
|
||||
package gay.pizza.pork.tool
|
||||
|
||||
import com.github.ajalt.clikt.core.CliktCommand
|
||||
import com.github.ajalt.clikt.parameters.arguments.argument
|
||||
import com.github.ajalt.clikt.parameters.types.path
|
||||
import gay.pizza.pork.parse.AnsiHighlightScheme
|
||||
|
||||
class HighlightCommand : CliktCommand(help = "Syntax Highlighter", name = "highlight") {
|
||||
val path by argument("file").path(mustExist = true, canBeDir = false)
|
||||
|
||||
override fun run() {
|
||||
val tool = FileTool(path)
|
||||
print(tool.highlight(AnsiHighlightScheme()).joinToString(""))
|
||||
print(tool.highlight(gay.pizza.pork.parser.AnsiHighlightScheme()).joinToString(""))
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.cli
|
||||
package gay.pizza.pork.tool
|
||||
|
||||
import com.github.ajalt.clikt.core.CliktCommand
|
||||
import com.github.ajalt.clikt.parameters.arguments.argument
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.cli
|
||||
package gay.pizza.pork.tool
|
||||
|
||||
import com.github.ajalt.clikt.core.CliktCommand
|
||||
import com.github.ajalt.clikt.core.subcommands
|
@ -1,10 +1,10 @@
|
||||
package gay.pizza.pork.cli
|
||||
package gay.pizza.pork.tool
|
||||
|
||||
import com.github.ajalt.clikt.core.CliktCommand
|
||||
import com.github.ajalt.clikt.parameters.arguments.argument
|
||||
import com.github.ajalt.clikt.parameters.types.path
|
||||
import gay.pizza.pork.eval.CallableFunction
|
||||
import gay.pizza.pork.eval.Scope
|
||||
import gay.pizza.pork.evaluator.CallableFunction
|
||||
import gay.pizza.pork.evaluator.Scope
|
||||
|
||||
class RunCommand : CliktCommand(help = "Run Program", name = "run") {
|
||||
val path by argument("file").path(mustExist = true, canBeDir = false)
|
@ -1,4 +1,4 @@
|
||||
package gay.pizza.pork.cli
|
||||
package gay.pizza.pork.tool
|
||||
|
||||
import com.github.ajalt.clikt.core.CliktCommand
|
||||
import com.github.ajalt.clikt.parameters.arguments.argument
|
@ -1,12 +1,14 @@
|
||||
package gay.pizza.pork.cli
|
||||
package gay.pizza.pork.tool
|
||||
|
||||
import gay.pizza.pork.ast.NodeVisitor
|
||||
import gay.pizza.pork.parse.Printer
|
||||
import gay.pizza.pork.parser.Printer
|
||||
import gay.pizza.pork.ast.CompilationUnit
|
||||
import gay.pizza.pork.eval.*
|
||||
import gay.pizza.pork.evaluator.Arguments
|
||||
import gay.pizza.pork.evaluator.Evaluator
|
||||
import gay.pizza.pork.evaluator.Scope
|
||||
import gay.pizza.pork.frontend.ContentSource
|
||||
import gay.pizza.pork.frontend.World
|
||||
import gay.pizza.pork.parse.*
|
||||
import gay.pizza.pork.parser.*
|
||||
|
||||
abstract class Tool {
|
||||
abstract fun createCharSource(): CharSource
|
@ -1,3 +1,3 @@
|
||||
package gay.pizza.pork.cli
|
||||
package gay.pizza.pork.tool
|
||||
|
||||
fun main(args: Array<String>) = RootCommand().main(args)
|
Loading…
Reference in New Issue
Block a user