-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatement.cpp
115 lines (92 loc) · 2.42 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
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 <iostream>
#include <cstdlib>
#include "exception.h"
#include "number.h"
#include "symbolTable.h"
#include "functionTable.h"
#include "expression.h"
#include "function.h"
#include "exception.h"
#include "statement.h"
using std::cout;
using std::cerr;
using std::endl;
int Statement::getLineNumber() const {
return lineNumber;
}
Assignment::~Assignment() {
delete exp;
}
void Assignment::eval(SymbolTable& st, FunctionTable& ft) const {
st[name] = exp->eval(st, ft);
}
FunctionCall::~FunctionCall() {
delete expList;
}
void FunctionCall::eval(SymbolTable& st, FunctionTable& ft) const {
// semi-hack: create const FunctionTable to ensure only accessor
// operator[] is used - don't want to get a NULL function*
const FunctionTable cft(ft);
cft[name]->apply(st, ft, expList);
}
FunctionDef::~FunctionDef() {
delete function;
}
void FunctionDef::eval(SymbolTable& st, FunctionTable& ft) const {
ft[name] = function;
}
IfElse::~IfElse() {
delete condition;
delete trueList;
delete falseList;
}
void IfElse::eval(SymbolTable& st, FunctionTable& ft) const {
if (condition->eval(st, ft) > 0) {
trueList->eval(st, ft);
} else {
falseList->eval(st, ft);
}
}
While::~While() {
delete condition;
delete stmtList;
}
void While::eval(SymbolTable& st, FunctionTable& ft) const {
while (condition->eval(st, ft) > 0) {
stmtList->eval(st, ft);
}
}
Print::~Print() {
delete exp;
}
void Print::eval(SymbolTable& st, FunctionTable& ft) const {
Number n(exp->eval(st, ft));
cout << ">> " << n << endl;
}
Return::~Return() {
delete exp;
}
void Return::eval(SymbolTable& st, FunctionTable& ft) const {
throw ReturnValue(exp->eval(st, ft), lineNumber);
}
StatementList::~StatementList() {
for (list<Statement*>::const_iterator it = stmtList.begin(); it != stmtList.end(); ++it) {
delete *it;
}
}
void StatementList::add(Statement* stmt) {
stmtList.push_back(stmt);
}
void StatementList::eval(SymbolTable& st, FunctionTable& ft) const {
// eval() each statement in statement list
list<Statement*>::const_iterator it;
try {
for (it = stmtList.begin(); it != stmtList.end(); ++it) {
(*it)->eval(st, ft);
}
} catch (ParseException e) {
// if we catch an exception indicating a semantic error, print it and bail out
cerr << "error (line " << (*it)->getLineNumber() << "): " << e.what() << endl;
exit(2);
}
}