-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatements.cpp
More file actions
88 lines (75 loc) · 2.31 KB
/
statements.cpp
File metadata and controls
88 lines (75 loc) · 2.31 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
#include "statements.h"
void ListOfStatements::Run() {
for (auto&& element : list_) {
element->Run();
}
}
void ListOfStatements::Add(Statement* state) { list_.emplace_back(state); }
void PrintStatement::Run() {
if (!expression_) {
exit(NULL_POINTER_ERROR);
}
if (expression_->GetType() == Expression::T_BOOL) {
std::cout << (static_cast<bool>(expression_->Count()) ? "true" : "false");
} else if (expression_->GetType() == Expression::T_STRING) {
std::cout << expression_->CountString();
} else {
std::cout << expression_->Count();
}
std::cout << std::endl;
}
void IfStatement::Run() {
if (!expression_) {
exit(NULL_POINTER_ERROR);
}
if (static_cast<bool>(expression_->Count())) {
first_statement_->Run();
} else if (second_statement_) {
second_statement_->Run();
}
}
void WhileStatement::Run() {
if (!expression_) {
exit(NULL_POINTER_ERROR);
}
while (static_cast<bool>(expression_->Count())) {
statements_->Run();
}
}
void DoStatement::Run() {
if (!expression_) {
exit(NULL_POINTER_ERROR);
}
do {
statements_->Run();
} while (static_cast<bool>(expression_->Count()));
}
void AssignStatement::Run() {
if (expression_) {
if (expression_->GetType() == Expression::Type::T_STRING) {
std::string result = expression_->CountString();
if (!variables_container.ExistsString(variable_name_)) {
std::cerr << variable_name_
<< " isn't declared in this scope or has incompatible type\n";
exit(NOT_DECLARED_VARIABLE);
}
variables_container.ChangeString(variable_name_, result);
return;
}
double value = expression_->Count();
if (variables_container.ExistsInt(variable_name_)) {
variables_container.ChangeInt(variable_name_, static_cast<int>(value));
} else if (variables_container.ExistsDouble(variable_name_)) {
variables_container.ChangeDouble(variable_name_, value);
} else if (variables_container.ExistsBool(variable_name_)) {
variables_container.ChangeBool(variable_name_, static_cast<bool>(value));
} else {
std::cerr << variable_name_
<< " isn't declared in this scope or has incompatible type\n";
exit(NOT_DECLARED_VARIABLE);
}
} else {
std::cerr << "Null pointer error\n";
exit(NULL_POINTER_ERROR);
}
}