Prepare for CFE.

This commit is contained in:
Alex Zenla 2023-09-03 01:11:27 -07:00
parent d572a5c732
commit 7a771980e0
Signed by: alex
GPG Key ID: C0780728420EBFE5
9 changed files with 17 additions and 30 deletions

View File

@ -1,7 +0,0 @@
package gay.pizza.pork.ast
enum class NodeTypeTrait {
Intermediate,
Operation,
Literal
}

View File

@ -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.types.path
import gay.pizza.pork.ast.nodes.Node
import gay.pizza.pork.frontend.FileFrontend
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
@ -19,7 +18,7 @@ class AstCommand : CliktCommand(help = "Print AST", name = "ast") {
}
override fun run() {
val frontend = FileFrontend(path)
println(json.encodeToString(Node.serializer(), frontend.parse()))
val tool = FileTool(path)
println(json.encodeToString(Node.serializer(), tool.parse()))
}
}

View File

@ -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.types.path
import gay.pizza.pork.ast.NodeCoalescer
import gay.pizza.pork.frontend.FileFrontend
import gay.pizza.pork.parse.TokenNodeAttribution
class AttributeCommand : CliktCommand(help = "Attribute AST", name = "attribute") {
val path by argument("file").path(mustExist = true, canBeDir = false)
override fun run() {
val frontend = FileFrontend(path)
val tool = FileTool(path)
val attribution = TokenNodeAttribution()
val compilationUnit = frontend.parse(attribution)
val compilationUnit = tool.parse(attribution)
val coalescer = NodeCoalescer { node ->
val tokens = attribution.assembleTokens(node)

View File

@ -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.StringCharSource
import java.nio.file.Path
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 resolveImportSource(path: String): CharSource =
StringCharSource(this.path.parent.resolve(path).readText())

View File

@ -3,14 +3,13 @@ package gay.pizza.pork.cli
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.frontend.FileFrontend
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 frontend = FileFrontend(path)
print(frontend.highlight(AnsiHighlightScheme()).joinToString(""))
val tool = FileTool(path)
print(tool.highlight(AnsiHighlightScheme()).joinToString(""))
}
}

View File

@ -3,13 +3,12 @@ package gay.pizza.pork.cli
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.frontend.FileFrontend
class ReprintCommand : CliktCommand(help = "Reprint Parsed Compilation Unit", name = "reprint") {
val path by argument("file").path(mustExist = true, canBeDir = false)
override fun run() {
val frontend = FileFrontend(path)
print(frontend.reprint())
val tool = FileTool(path)
print(tool.reprint())
}
}

View File

@ -6,20 +6,19 @@ import com.github.ajalt.clikt.parameters.types.path
import gay.pizza.pork.eval.Arguments
import gay.pizza.pork.eval.CallableFunction
import gay.pizza.pork.eval.Scope
import gay.pizza.pork.frontend.FileFrontend
class RunCommand : CliktCommand(help = "Run Program", name = "run") {
val path by argument("file").path(mustExist = true, canBeDir = false)
override fun run() {
val frontend = FileFrontend(path)
val tool = FileTool(path)
val scope = Scope()
scope.define("println", CallableFunction { arguments ->
for (argument in arguments.values) {
println(argument)
}
})
frontend.evaluate(scope)
tool.evaluate(scope)
scope.call("main", Arguments(emptyList()))
}
}

View File

@ -3,14 +3,13 @@ package gay.pizza.pork.cli
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.frontend.FileFrontend
class TokenizeCommand : CliktCommand(help = "Tokenize Compilation Unit", name = "tokenize") {
val path by argument("file").path(mustExist = true, canBeDir = false)
override fun run() {
val frontend = FileFrontend(path)
val tokenStream = frontend.tokenize()
val tool = FileTool(path)
val tokenStream = tool.tokenize()
for (token in tokenStream.tokens) {
println("${token.start} ${token.type.name} '${sanitize(token.text)}'")
}

View File

@ -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.Printer
@ -8,7 +8,7 @@ import gay.pizza.pork.eval.ImportLoader
import gay.pizza.pork.eval.Scope
import gay.pizza.pork.parse.*
abstract class Frontend {
abstract class Tool {
abstract fun createCharSource(): CharSource
abstract fun resolveImportSource(path: String): CharSource
@ -28,7 +28,7 @@ abstract class Frontend {
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 {
val tokenStream = Tokenizer(frontend.resolveImportSource(path)).tokenize()
return Parser(TokenStreamSource(tokenStream), DiscardNodeAttribution).readCompilationUnit()