summaryrefslogtreecommitdiff
path: root/willow/tools/willowc/lib/driver.cpp
blob: 1962da4fc3325bbdee7cfce3ca2c138b8b4c1a0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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