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.")