cli: Add a Preprocess Mode

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-05-21 22:40:14 -04:00
parent 022dc94fde
commit b3ca30541a
+31 -1
View File
@@ -27,7 +27,7 @@ struct Cli: ParsableCommand {
static let configuration = CommandConfiguration( static let configuration = CommandConfiguration(
abstract: "P4CE compiler, interpreter and debugger.", abstract: "P4CE compiler, interpreter and debugger.",
subcommands: [Compile.self, CodeGen.self]) subcommands: [Preprocess.self, Compile.self, CodeGen.self])
} }
struct CliOptions: ParsableArguments { struct CliOptions: ParsableArguments {
@@ -38,6 +38,36 @@ struct CliOptions: ParsableArguments {
var search: [String] = [] var search: [String] = []
} }
extension Cli {
struct Preprocess: ParsableCommand {
static let configuration = CommandConfiguration(abstract: "Compile P4CE code.")
@ParentCommand var parent: Cli
@OptionGroup var options: CliOptions
mutating func run() {
let sm = SourceManager(options.search.map { FilePath($0) })
let prep = SourceCodePreprocessor(sm)
let file = FilePath(options.path)
let formatter: any Formattable =
if parent.plain != 0 {
FormatterPlain()
} else {
FormatterAnsi()
}
let maybe_source = prep.preprocess(file)
guard case .Ok(let source) = maybe_source else {
print(ErrorWithLabel("Preprocessor Error", maybe_source.error()!).format(formatter))
return
}
print(source.getSource())
}
}
}
extension Cli { extension Cli {
struct Compile: ParsableCommand { struct Compile: ParsableCommand {
static let configuration = CommandConfiguration(abstract: "Compile P4CE code.") static let configuration = CommandConfiguration(abstract: "Compile P4CE code.")