-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathinterpreter_implementation.cc
More file actions
115 lines (102 loc) · 2.94 KB
/
interpreter_implementation.cc
File metadata and controls
115 lines (102 loc) · 2.94 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
106
107
108
109
110
111
112
113
114
115
#include "implementation.hh"
#include <iostream>
#include <sstream>
std::map<std::string, unsigned> value_table;
unsigned number_expression::get_value() const {
return value;
}
unsigned boolean_expression::get_value() const {
return (unsigned)value;
}
unsigned id_expression::get_value() const {
if(value_table.count(name) == 0) {
error(line, std::string("Variable has not been initialized: ") + name);
}
return value_table[name];
}
unsigned binop_expression::get_value() const {
int left_value = left->get_value();
int right_value = right->get_value();
if(op == "+") {
return left_value + right_value;
} else if(op == "-") {
return left_value - right_value;
} else if(op == "*") {
return left_value * right_value;
} else if(op == "/") {
return left_value / right_value;
} else if(op == "%") {
return left_value % right_value;
} else if(op == "<") {
return left_value < right_value;
} else if(op == ">") {
return left_value > right_value;
} else if(op == "<=") {
return left_value <= right_value;
} else if(op == ">=") {
return left_value >= right_value;
} else if(op == "and") {
return left_value && right_value;
} else if(op == "or") {
return left_value || right_value;
} else if(op == "=") {
return left_value == right_value;
} else {
error(line, std::string("Unknown operator: ") + op);
}
}
unsigned not_expression::get_value() const {
return !(bool)(operand->get_value());
}
void assign_instruction::execute() {
value_table[left] = right->get_value();
}
void read_instruction::execute() {
std::string input_line;
getline(std::cin, input_line);
if(symbol_table[id].symbol_type == natural) {
std::stringstream ss(input_line);
unsigned input;
ss >> input;
value_table[id] = input;
} else if(symbol_table[id].symbol_type == boolean) {
if(input_line == "true") {
value_table[id] = 1;
} else {
value_table[id] = 0;
}
}
}
void write_instruction::execute() {
if(exp_type == natural) {
std::cout << exp->get_value() << std::endl;
} else if(exp_type == boolean) {
if(exp->get_value()) {
std::cout << "true" << std::endl;
} else {
std::cout << "false" << std::endl;
}
}
}
void if_instruction::execute() {
if(condition->get_value()) {
execute_commands(true_branch);
} else {
execute_commands(false_branch);
}
}
void while_instruction::execute() {
std::list<instruction*>::iterator it;
while(condition->get_value()) {
execute_commands(body);
}
}
void execute_commands(std::list<instruction*>* commands) {
if(!commands) {
return;
}
std::list<instruction*>::iterator it;
for(it = commands->begin(); it != commands->end(); ++it) {
(*it)->execute();
}
}