-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
46 lines (35 loc) · 1.14 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
#include <iostream>
#include <string>
#include "tokens.h"
#include "lexer.h"
#include "parser.h"
#include "interpreter.h"
int main() {
std::string text;
while (true) {
try {
std::cout << "calc > " << std::flush;
std::getline(std::cin, text);
if (std::cin.bad()) {
std::cerr << "IO error\n";
break;
} else if (std::cin.eof()) {
break;
}
Math_Interpreter::Lexer lexer(text);
std::vector<Math_Interpreter::Token> tokens = lexer.generate_tokens();
//Math_Interpreter::print_tokens(tokens);
Math_Interpreter::Parser parser(tokens);
std::unique_ptr<Math_Interpreter::Node> tree = parser.parse();
if (!tree) {
continue;
}
//std::cout << tree->str() << '\n';
Math_Interpreter::Interpreter interpreter;
Math_Interpreter::Number value = interpreter.visit(tree);
std::cout << value.str() << '\n';
} catch (const std::string& e) {
std::cerr << e << '\n';
}
}
}