-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinterpreter.cpp
More file actions
47 lines (41 loc) · 1.41 KB
/
interpreter.cpp
File metadata and controls
47 lines (41 loc) · 1.41 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
#include "interpreter.hpp"
#include <array>
#include <exception>
#include <iostream>
#include <memory>
#include <sstream>
void EvaluateProgram(char* input) {
parser::Parser parser{std::istringstream{input}};
type_checker::TypeChecker checker;
auto program = parser.ParseStatement();
std::cout << " " << program << ": "
<< checker.TypeOf(runtime::NamedStatementStore(), program)
<< "\n";
interpreter::Interpreter interpreter;
auto res = interpreter.Interpret(program);
std::cout << "=> " << res.first << ": " << res.second << "\n";
}
int main(int argc, char* argv[]) {
if (argc == 1) {
constexpr int line_size = 512;
char line[line_size];
interpreter::Interpreter interpreter;
while (true) {
try {
std::cout << ">> ";
std::cin.getline(&line[0], line_size);
parser::Parser statement_parser(std::istringstream{line});
auto statement_ast = statement_parser.ParseStatement();
auto res = interpreter.Interpret(statement_ast);
std::cout << "=> " << res.first << ": " << res.second << "\n";
} catch (const std::exception& ex) {
std::cerr << "Error: " << ex.what() << "\n";
}
}
} else if (argc == 2) {
EvaluateProgram(argv[1]);
} else {
// Print usage.
}
return 0;
}