-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.cpp
57 lines (50 loc) · 1.09 KB
/
driver.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
#include "driver.hh"
#include "objects/NilObject.h"
#include "Program.h"
#include <exception>
#include <iostream>
Driver::Driver()
: parse_only(false),
scanner(*this),
parser(scanner, *this) {
variables["IT"] = std::make_shared<const NilObject>();
}
int Driver::parse(const std::string& f) {
file = f;
scan_begin();
int res = parser();
scan_end();
return res;
}
int Driver::evaluate() {
if (!program) return 1;
try {
program->exec(*this);
return 0;
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
return 1;
}
}
int Driver::execute(const std::string& f) {
int res = parse(f);
if (res != 0) {
return res;
}
if (parse_only) {
program->print(std::cout, 0);
std::cout << std::endl;
return 0;
}
return evaluate();
}
void Driver::scan_begin() {
if (!(file.empty() || file == "-")) {
stream.open(file);
// Restart scanner resetting buffer!
scanner.yyrestart(&stream);
}
}
void Driver::scan_end() {
stream.close();
}