-
Notifications
You must be signed in to change notification settings - Fork 0
/
Statement.cpp
82 lines (66 loc) · 1.67 KB
/
Statement.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
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
#include "Statement.hpp"
void Statement::execute(Context& context)
{
}
void StatementSequence::execute(Context& context)
{
for (auto& statement : statements)
{
statement->execute(context);
}
}
void StatementSequence::addStatement(Statement* statement)
{
statements.push_back(statement);
}
void AssignmentStatement::execute(Context& context)
{
context.setVariable(identifier, selector, expression->evaluate(context));
}
void IfStatement::execute(Context& context)
{
//std::cerr << "Executing if statement" << std::endl;
BooleanValue* expressionValue = dynamic_cast<BooleanValue*> (expression->evaluate(context));
if (!expressionValue)
{
throw wrong_type("Wrong expression type in if statement");
}
if (expressionValue->value == true)
{
//std::cerr << "if statement: Expression is true" << std::endl;
ifStatementSequence->execute(context);
}
else
{
//std::cerr << "if statement: Expression is false" << std::endl;
elseStatementSequence->execute(context);
}
}
void WhileStatement::execute(Context& context)
{
while (true)
{
BooleanValue* expressionValue = dynamic_cast<BooleanValue*> (expression->evaluate(context));
if (!expressionValue)
{
throw wrong_type("Wrong expression type in if statement");
}
if (expressionValue->value == false)
{
break;
}
statementSequence->execute(context);
}
}
void ProcedureCallStatement::execute(Context& context)
{
context.getProcedure(identifier)->call(actualParameters, context);
}
void ReadStatement::execute(Context& context)
{
std::cin >> context.getVariableReference(identifier);
}
void WriteStatement::execute(Context& context)
{
std::cout << context.getVariableReference(identifier) << std::endl;
}