diff options
Diffstat (limited to 'willow/tools/willowc/lib/driver.cpp')
| -rw-r--r-- | willow/tools/willowc/lib/driver.cpp | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/willow/tools/willowc/lib/driver.cpp b/willow/tools/willowc/lib/driver.cpp index e69de29..1962da4 100644 --- a/willow/tools/willowc/lib/driver.cpp +++ b/willow/tools/willowc/lib/driver.cpp @@ -0,0 +1,76 @@ +#include <print> +#include <compiler.hpp> +#include <driver.hpp> + +#include <argparse/argparse.hpp> + +namespace willowc { + +int willowc_main(int argc, char **argv) { + argparse::ArgumentParser cl("willowc"); + cl.set_prefix_chars("-"); + cl.set_assign_chars("="); + + willowc::Compiler compiler; + + auto &ll = cl.add_mutually_exclusive_group(); + ll.add_argument("-Lerror") + .help("only emit diagnostics on error (default)") + .default_value(true) + .implicit_value(true) + .action( + [&](const auto &) { compiler.setLogLevel(willow::Severity::Error); }); + ll.add_argument("-Lwarn") + .help("emit warning messages") + .default_value(false) + .implicit_value(true) + .action([&](const auto &) { + compiler.setLogLevel(willow::Severity::Warning); + }); + ll.add_argument("-Linfo") + .help("emit remarks") + .default_value(false) + .implicit_value(true) + .action([&](const auto &) { + compiler.setLogLevel(willow::Severity::Remark); + }); + ll.add_argument("-Ldebug") + .help("emit all diagnostics") + .default_value(false) + .implicit_value(true) + .action( + [&](const auto &) { compiler.setLogLevel(willow::Severity::Debug); }); + + cl.add_argument("<file>").help("input file path").required().nargs(1); + + if (argc < 2) { + std::cerr << cl; + return 1; + } + + try { + cl.parse_args(argc, argv); + } catch (std::exception &e) { + std::println("error: {}", e.what()); + } + + std::string f; + try { + f = cl.get("<file>"); + } catch (std::exception &e) { + std::println("error: {}", e.what()); + return 1; + } + + if (f == "-") { + if (willow::failed(compiler.addStdIn())) + return 1; + } else if (willow::failed(compiler.addSourceFile(f))) + return 1; + + compiler.run(); + + return 0; +} + +} // namespace willowc |