summaryrefslogtreecommitdiff
path: root/willow/tools/willowc/lib/compiler.cpp
blob: b933f65a5aefd471cc6675393de87985a2bb6bab (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
#include <compiler.hpp>

#include <willow/IR/Diagnostic.h>
#include <willow/IR/Location.h>
#include <willow/Util/Color.h>

#include <parser.hpp>

#include <iostream>
#include <print>

namespace willowc {

Compiler::Compiler()
    : sourcemanager_(), log_level_(willow::Severity::Error),
      diagnostic_engine_([this](const willow::Diagnostic &d) { emitDiagnostic(d); }) {}

void Compiler::run() {
  assert(sourcemanager_.numFiles() == 1);
  Compiler::compile(0);
}

void Compiler::compile(FileID file) {
  Parser parser{sourcemanager_.getFile(file), diagnostic_engine_};

  auto x = parser.run();
}

void Compiler::emitDiagnostic(const willow::Diagnostic &d) {
  using namespace willow::termcolor;
  if (log_level_ > d.severity)
    return;

  if (d.location) {
    std::print(std::cerr, "{}{}: ", TextStyle{AnsiColor::None, Emphasis::Bold},
               d.location.value());
  }

  std::print(std::cerr, "{}{}: {}{}{}\n", willow::getSeverityColor(d.severity),
             willow::getSeverityName(d.severity),
             TextStyle{AnsiColor::Default, Emphasis::Bold}, d.message, TextStyle{});
  // TODO: trace
}

willow::LogicalResult Compiler::addSourceFile(const std::string &path) {
  std::optional<FileID> maybe_source_file = sourcemanager_.addFile(path);
  if (!maybe_source_file) {
    std::println(std::cerr, "error: failed to open input file '{}'", path);
    return willow::failure();
  }

  return willow::success();
}

willow::LogicalResult Compiler::addStdIn() {
  std::optional<FileID> maybestdin = sourcemanager_.addStdIn();

  if (!maybestdin) {
    std::println(std::cerr, "error: failed to read from stdin");
    return willow::failure();
  }

  return willow::success();
}

}; // namespace willowc