Skip to content

Commit

Permalink
Prepare for CFE.
Browse files Browse the repository at this point in the history
  • Loading branch information
azenla committed Sep 3, 2023
1 parent d572a5c commit 7a77198
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 30 deletions.
7 changes: 0 additions & 7 deletions src/main/kotlin/gay/pizza/pork/ast/NodeTypeTrait.kt

This file was deleted.

5 changes: 2 additions & 3 deletions src/main/kotlin/gay/pizza/pork/cli/AstCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()))
}
}
5 changes: 2 additions & 3 deletions src/main/kotlin/gay/pizza/pork/cli/AttributeCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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())
Expand Down
5 changes: 2 additions & 3 deletions src/main/kotlin/gay/pizza/pork/cli/HighlightCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(""))
}
}
5 changes: 2 additions & 3 deletions src/main/kotlin/gay/pizza/pork/cli/ReprintCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
5 changes: 2 additions & 3 deletions src/main/kotlin/gay/pizza/pork/cli/RunCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
}
5 changes: 2 additions & 3 deletions src/main/kotlin/gay/pizza/pork/cli/TokenizeCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)}'")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand All @@ -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()
Expand Down

0 comments on commit 7a77198

Please sign in to comment.