From b3ca30541a2075c52c0d6e1f7842edbd2aa7ea6e Mon Sep 17 00:00:00 2001 From: Will Hawkins Date: Thu, 21 May 2026 22:40:14 -0400 Subject: [PATCH] cli: Add a Preprocess Mode Signed-off-by: Will Hawkins --- Sources/Cli/main.swift | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/Sources/Cli/main.swift b/Sources/Cli/main.swift index 662a29d..2e8509d 100644 --- a/Sources/Cli/main.swift +++ b/Sources/Cli/main.swift @@ -27,7 +27,7 @@ struct Cli: ParsableCommand { static let configuration = CommandConfiguration( abstract: "P4CE compiler, interpreter and debugger.", - subcommands: [Compile.self, CodeGen.self]) + subcommands: [Preprocess.self, Compile.self, CodeGen.self]) } struct CliOptions: ParsableArguments { @@ -38,6 +38,36 @@ struct CliOptions: ParsableArguments { 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 { struct Compile: ParsableCommand { static let configuration = CommandConfiguration(abstract: "Compile P4CE code.")