cli: Add a Preprocess Mode
Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
+31
-1
@@ -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.")
|
||||||
|
|||||||
Reference in New Issue
Block a user