-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
73 lines (71 loc) · 2.33 KB
/
main.cpp
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
#include <iostream>
#include <boost/program_options.hpp>
#include "version.h"
#include "Compiler.h"
#include <boost/log/trivial.hpp>
#include <Errors.h>
using namespace std;
using namespace F4;
namespace po = boost::program_options;
int main(int argc, char *argv[]) {
po::options_description description("Available options");
string of;
// Input and output file streams
string ifs, ofs;
// Adding available options
description.add_options()
("help,h", "show this help message")
("out-format", po::value<string>(&of)->default_value("f4o"), "specify output format (f4o, asm, cpp)")
("in-file,i", po::value<string>(&ifs), "specify input file")
("out-file,o", po::value<string>(&ofs), "specify output file")
("link,L", "link the file (only when f4o output selected)")
("version,v", "show version")
;
// Adding positional options for file names
po::positional_options_description pod;
pod.add("in-file", 1);
pod.add("out-file", 1);
// Creating variable map
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(description).positional(pod).run(), vm);
po::notify(vm);
// Show help
if (vm.count("help") || argc == 1) {
cout << description << endl;
return 0;
}
if (vm.count("version")) {
// Write version info
cout << "f4 Programming Language v" << F4_VERSION_STRING << endl;
return 0;
}
// Checking for input file name
if (!vm.count("in-file")) {
message(ECF_UNSPECIFIEDFILE);
return 1;
}
// Checking for output file name
if (!vm.count("out-file")) {
size_t pp = ifs.find_last_of(".", ifs.size());
ofs = (pp != string::npos ? ifs.substr(0, pp + 1) : ifs) + of;
}
//Checking the output format
CompilerOutputFormat cof = COF_UNDEFINED;
if (of == "f4o") cof = COF_OBJECT;
if (of == "asm") cof = COF_ASSEMBLER;
if (of == "cpp") cof = COF_CPLUSPLUS;
if (cof == COF_UNDEFINED) {
message(ECF_INVALIDFORMAT);
return 1;
}
//Creating the compiler
try {
Compiler compiler(ifs, ofs, cof);
compiler.compile();
}
catch (int x) {
BOOST_LOG_TRIVIAL(info) << "Compilation terminated due to fatal error(s)";
return x;
}
return 0;
}