mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-03 05:10:55 +00:00
Prepare for CFE.
This commit is contained in:
parent
d572a5c732
commit
7a771980e0
@ -1,7 +0,0 @@
|
|||||||
package gay.pizza.pork.ast
|
|
||||||
|
|
||||||
enum class NodeTypeTrait {
|
|
||||||
Intermediate,
|
|
||||||
Operation,
|
|
||||||
Literal
|
|
||||||
}
|
|
@ -4,7 +4,6 @@ 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.nodes.Node
|
||||||
import gay.pizza.pork.frontend.FileFrontend
|
|
||||||
import kotlinx.serialization.ExperimentalSerializationApi
|
import kotlinx.serialization.ExperimentalSerializationApi
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
|
|
||||||
@ -19,7 +18,7 @@ class AstCommand : CliktCommand(help = "Print AST", name = "ast") {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun run() {
|
override fun run() {
|
||||||
val frontend = FileFrontend(path)
|
val tool = FileTool(path)
|
||||||
println(json.encodeToString(Node.serializer(), frontend.parse()))
|
println(json.encodeToString(Node.serializer(), tool.parse()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,16 +4,15 @@ 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.NodeCoalescer
|
import gay.pizza.pork.ast.NodeCoalescer
|
||||||
import gay.pizza.pork.frontend.FileFrontend
|
|
||||||
import gay.pizza.pork.parse.TokenNodeAttribution
|
import gay.pizza.pork.parse.TokenNodeAttribution
|
||||||
|
|
||||||
class AttributeCommand : CliktCommand(help = "Attribute AST", name = "attribute") {
|
class AttributeCommand : CliktCommand(help = "Attribute AST", name = "attribute") {
|
||||||
val path by argument("file").path(mustExist = true, canBeDir = false)
|
val path by argument("file").path(mustExist = true, canBeDir = false)
|
||||||
|
|
||||||
override fun run() {
|
override fun run() {
|
||||||
val frontend = FileFrontend(path)
|
val tool = FileTool(path)
|
||||||
val attribution = TokenNodeAttribution()
|
val attribution = TokenNodeAttribution()
|
||||||
val compilationUnit = frontend.parse(attribution)
|
val compilationUnit = tool.parse(attribution)
|
||||||
|
|
||||||
val coalescer = NodeCoalescer { node ->
|
val coalescer = NodeCoalescer { node ->
|
||||||
val tokens = attribution.assembleTokens(node)
|
val tokens = attribution.assembleTokens(node)
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
package gay.pizza.pork.frontend
|
package gay.pizza.pork.cli
|
||||||
|
|
||||||
import gay.pizza.pork.parse.CharSource
|
import gay.pizza.pork.parse.CharSource
|
||||||
import gay.pizza.pork.parse.StringCharSource
|
import gay.pizza.pork.parse.StringCharSource
|
||||||
import java.nio.file.Path
|
import java.nio.file.Path
|
||||||
import kotlin.io.path.readText
|
import kotlin.io.path.readText
|
||||||
|
|
||||||
class FileFrontend(val path: Path) : Frontend() {
|
class FileTool(val path: Path) : Tool() {
|
||||||
override fun createCharSource(): CharSource = StringCharSource(path.readText())
|
override fun createCharSource(): CharSource = StringCharSource(path.readText())
|
||||||
override fun resolveImportSource(path: String): CharSource =
|
override fun resolveImportSource(path: String): CharSource =
|
||||||
StringCharSource(this.path.parent.resolve(path).readText())
|
StringCharSource(this.path.parent.resolve(path).readText())
|
@ -3,14 +3,13 @@ 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.frontend.FileFrontend
|
|
||||||
import gay.pizza.pork.parse.AnsiHighlightScheme
|
import gay.pizza.pork.parse.AnsiHighlightScheme
|
||||||
|
|
||||||
class HighlightCommand : CliktCommand(help = "Syntax Highlighter", name = "highlight") {
|
class HighlightCommand : CliktCommand(help = "Syntax Highlighter", name = "highlight") {
|
||||||
val path by argument("file").path(mustExist = true, canBeDir = false)
|
val path by argument("file").path(mustExist = true, canBeDir = false)
|
||||||
|
|
||||||
override fun run() {
|
override fun run() {
|
||||||
val frontend = FileFrontend(path)
|
val tool = FileTool(path)
|
||||||
print(frontend.highlight(AnsiHighlightScheme()).joinToString(""))
|
print(tool.highlight(AnsiHighlightScheme()).joinToString(""))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,13 +3,12 @@ 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.frontend.FileFrontend
|
|
||||||
|
|
||||||
class ReprintCommand : CliktCommand(help = "Reprint Parsed Compilation Unit", name = "reprint") {
|
class ReprintCommand : CliktCommand(help = "Reprint Parsed Compilation Unit", name = "reprint") {
|
||||||
val path by argument("file").path(mustExist = true, canBeDir = false)
|
val path by argument("file").path(mustExist = true, canBeDir = false)
|
||||||
|
|
||||||
override fun run() {
|
override fun run() {
|
||||||
val frontend = FileFrontend(path)
|
val tool = FileTool(path)
|
||||||
print(frontend.reprint())
|
print(tool.reprint())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,20 +6,19 @@ import com.github.ajalt.clikt.parameters.types.path
|
|||||||
import gay.pizza.pork.eval.Arguments
|
import gay.pizza.pork.eval.Arguments
|
||||||
import gay.pizza.pork.eval.CallableFunction
|
import gay.pizza.pork.eval.CallableFunction
|
||||||
import gay.pizza.pork.eval.Scope
|
import gay.pizza.pork.eval.Scope
|
||||||
import gay.pizza.pork.frontend.FileFrontend
|
|
||||||
|
|
||||||
class RunCommand : CliktCommand(help = "Run Program", name = "run") {
|
class RunCommand : CliktCommand(help = "Run Program", name = "run") {
|
||||||
val path by argument("file").path(mustExist = true, canBeDir = false)
|
val path by argument("file").path(mustExist = true, canBeDir = false)
|
||||||
|
|
||||||
override fun run() {
|
override fun run() {
|
||||||
val frontend = FileFrontend(path)
|
val tool = FileTool(path)
|
||||||
val scope = Scope()
|
val scope = Scope()
|
||||||
scope.define("println", CallableFunction { arguments ->
|
scope.define("println", CallableFunction { arguments ->
|
||||||
for (argument in arguments.values) {
|
for (argument in arguments.values) {
|
||||||
println(argument)
|
println(argument)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
frontend.evaluate(scope)
|
tool.evaluate(scope)
|
||||||
scope.call("main", Arguments(emptyList()))
|
scope.call("main", Arguments(emptyList()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,14 +3,13 @@ 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.frontend.FileFrontend
|
|
||||||
|
|
||||||
class TokenizeCommand : CliktCommand(help = "Tokenize Compilation Unit", name = "tokenize") {
|
class TokenizeCommand : CliktCommand(help = "Tokenize Compilation Unit", name = "tokenize") {
|
||||||
val path by argument("file").path(mustExist = true, canBeDir = false)
|
val path by argument("file").path(mustExist = true, canBeDir = false)
|
||||||
|
|
||||||
override fun run() {
|
override fun run() {
|
||||||
val frontend = FileFrontend(path)
|
val tool = FileTool(path)
|
||||||
val tokenStream = frontend.tokenize()
|
val tokenStream = tool.tokenize()
|
||||||
for (token in tokenStream.tokens) {
|
for (token in tokenStream.tokens) {
|
||||||
println("${token.start} ${token.type.name} '${sanitize(token.text)}'")
|
println("${token.start} ${token.type.name} '${sanitize(token.text)}'")
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package gay.pizza.pork.frontend
|
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.ast.Printer
|
||||||
@ -8,7 +8,7 @@ import gay.pizza.pork.eval.ImportLoader
|
|||||||
import gay.pizza.pork.eval.Scope
|
import gay.pizza.pork.eval.Scope
|
||||||
import gay.pizza.pork.parse.*
|
import gay.pizza.pork.parse.*
|
||||||
|
|
||||||
abstract class Frontend {
|
abstract class Tool {
|
||||||
abstract fun createCharSource(): CharSource
|
abstract fun createCharSource(): CharSource
|
||||||
abstract fun resolveImportSource(path: String): CharSource
|
abstract fun resolveImportSource(path: String): CharSource
|
||||||
|
|
||||||
@ -28,7 +28,7 @@ abstract class Frontend {
|
|||||||
|
|
||||||
fun <T> visit(visitor: NodeVisitor<T>): T = visitor.visit(parse())
|
fun <T> visit(visitor: NodeVisitor<T>): T = visitor.visit(parse())
|
||||||
|
|
||||||
private class FrontendImportLoader(val frontend: Frontend) : ImportLoader {
|
private class FrontendImportLoader(val frontend: Tool) : ImportLoader {
|
||||||
override fun load(path: String): CompilationUnit {
|
override fun load(path: String): CompilationUnit {
|
||||||
val tokenStream = Tokenizer(frontend.resolveImportSource(path)).tokenize()
|
val tokenStream = Tokenizer(frontend.resolveImportSource(path)).tokenize()
|
||||||
return Parser(TokenStreamSource(tokenStream), DiscardNodeAttribution).readCompilationUnit()
|
return Parser(TokenStreamSource(tokenStream), DiscardNodeAttribution).readCompilationUnit()
|
Loading…
Reference in New Issue
Block a user