-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.cpp
58 lines (48 loc) · 1.11 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
#define DOCTEST_CONFIG_IMPLEMENT
#include <doctest/doctest.h>
#include <fstream>
#include <iostream>
#include "compiler.h"
#include "scanner.h"
#include "vm.h"
namespace lox {
inline void repl() noexcept {
while (true) {
std::cout << "> ";
std::string source;
std::getline(std::cin, source);
VM{std::cout}.interpret(std::move(source));
}
}
inline void run_file(const std::string &filepath) {
std::ifstream ifs(filepath);
VM{std::cout}.interpret(std::string{std::istreambuf_iterator<char>{ifs},
std::istreambuf_iterator<char>{}});
}
inline int main(int argc, char *argv[]) noexcept {
if (argc == 1) {
repl();
} else if (argc == 2) {
run_file(argv[1]);
} else {
fprintf(stderr, "Usage: lox [path]\n");
}
return 0;
}
} // namespace lox
int main(int argc, char *argv[]) {
#ifdef NDEBUG
lox::main(argc, argv);
#else
doctest::Context context;
context.applyCommandLine(argc, argv);
int res = context.run();
if (context.shouldExit()) {
return res;
}
if (argc > 1) {
lox::run_file(argv[argc - 1]);
}
return res;
#endif
}