summaryrefslogtreecommitdiffhomepage
path: root/mcc.cpp
blob: bb89d299c0569a39c10db933e44939a5e7b92300 (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
//
// CLI
//

#include "cpp.h"
#include "elf.h"
#include "file.h"
#include "programopts.h"

#include <iostream>

using namespace std::string_literals;

namespace {

 void usage() {
  std::cout << "Usage: mcc <translation_unit>" << std::endl;
 }

}

int main(int argc, char* argv[])
{
 // Processing of options in lambdas: each do return true iff parameter was consumed
 std::map<std::string, std::function<bool(const std::string&)>> option_prefixes{
  {"-h", [&](const std::string& parameter) -> bool { usage(); return false; }},
 };

 try {
  CPP cpp;

  ProgramOpts options(argc, argv, option_prefixes);
  options.process();

  std::vector<std::string> parameters {options.nonOptionArguments()};

  if (parameters.size() != 1) {
   usage();
   return 1;
  }

  fs::path in_filename{parameters[0]};
  fs::path out_filename{in_filename.parent_path() / in_filename.stem()};

  if (in_filename == out_filename)
   throw std::runtime_error("Bad output filename: "s + out_filename.generic_string());

  auto unit {File::getFile(in_filename)};

  std::string unit_string(reinterpret_cast<char*>(unit.data()), unit.size());

  cpp.compile(unit_string);

  Elf::Write(out_filename, cpp.getCode(), cpp.getData());
 } catch (const std::exception& ex) {
  std::cout << "Error: " << ex.what() << std::endl;
  return 1;
 }

 return 0;
}