Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
105 lines (96 loc) · 2.69 KB
/
Copy pathmain.cpp
File metadata and controls
105 lines (96 loc) · 2.69 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "types.hpp"
#include "TokenType.hpp"
#include "Token.h"
#include "Scanner.h"
#include "Parser.h"
#include "Interpreter.h"
#include "Resolver.h"
#ifdef NUMBER_USE_INT64_VARIANT
constexpr const char* NUMBER_IMPL = "int64_t + double variant";
#else
constexpr const char* NUMBER_IMPL = "double-only";
#endif
#ifdef NDEBUG
constexpr const char* BUILD_TYPE = "Release";
#else
constexpr const char* BUILD_TYPE = "Debug";
#endif
void showVersion() {
std::cout << "Lox Interpreter v1.0\n"
<< "Number implementation: " << NUMBER_IMPL << "\n"
<< "Build type: " << BUILD_TYPE << "\n"
<< "Compiled: " << __DATE__ << " " << __TIME__ << std::endl;
}
std::string read_file_contents(const std::string& file_name) {
std::ifstream file(file_name);
if (!file.is_open()) {
std::cerr << "File " << file_name << " not found" << std::endl;
std::exit(EXIT_FAILURE);
}
std::ostringstream buffer;
buffer << file.rdbuf();
file.close();
return buffer.str();
}
void run(const std::string& contents) {
Scanner scanner(contents);
static Interpreter interpreter{};
static Resolver resolver{&interpreter};
const std::vector<Token> tokens = scanner.scanTokens();
Parser parser(tokens);
const auto stmts = parser.parse();
if (parser.hadError) {
std::cerr << "Parse error" << std::endl;
return;
}
resolver.resolve(stmts);
if (resolver.has_error) {
std::cerr << "Resolver error" << std::endl;
return;
}
auto start = std::chrono::high_resolution_clock::now();
interpreter.interpret(stmts);
const auto end = std::chrono::high_resolution_clock::now();
const auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "Run in: " << duration.count() << " ms\n" << std::endl;
if (interpreter.has_error) {
std::cerr << "Interpret error" << std::endl;
return;
}
// for (Token t : tokens) {
// std::cout << t << "\n";
// }
// std::cout << std::endl;
}
void tokenizeFile(const std::string& file_name) {
const std::string file_contents = read_file_contents(file_name);
run(file_contents);
}
void tokenizePrompt() {
while (true) {
std::cout << "> " << std::flush;
std::string input;
std::getline(std::cin, input);
if (input.empty()) break;
run(input);
}
}
int main(const int argc, const char* argv[]) {
if (argc > 2) {
std::cerr << "Usage: ./ltwi [input_file]" << std::endl;
return 64;
}
if (argc == 2 && std::strcmp(argv[1], "--version") == 0) {
showVersion();
return 0;
}
if (argc == 2 && std::strcmp(argv[1], "--help") == 0) {
std::cerr << "Usage: ./ltwi [input_file]" << std::endl;
}
if (argc == 1) {
tokenizePrompt();
} else {
tokenizeFile(argv[1]);
}
return 0;
}