-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexp.cpp
156 lines (128 loc) · 3.71 KB
/
exp.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* File: exp.cpp
* -------------
* This file implements the Expression class and its subclasses.
*/
#include <string>
#include "../StanfordCPPLib/error.h"
#include "evalstate.h"
#include "exp.h"
#include "../StanfordCPPLib/strlib.h"
using namespace std;
/*
* Implementation notes: the Expression class
* ------------------------------------------
* The Expression class declares no instance variables and needs no code.
*/
Expression::Expression() {
/* Empty */
}
Expression::~Expression() {
/* Empty */
}
/*
* Implementation notes: the ConstantExp subclass
* ----------------------------------------------
* The ConstantExp subclass declares a single instance variable that
* stores the value of the constant. The eval method doesn't use the
* value of state but needs it to match the general prototype for eval.
*/
ConstantExp::ConstantExp(int value) {
this->value = value;
}
int ConstantExp::eval(EvalState & state) {
return value;
}
string ConstantExp::toString() {
return integerToString(value);
}
ExpressionType ConstantExp::getType() {
return CONSTANT;
}
int ConstantExp::getValue() {
return value;
}
/*
* Implementation notes: the IdentifierExp subclass
* ------------------------------------------------
* The IdentifierExp subclass declares a single instance variable that
* stores the name of the variable. The implementation of eval must
* look this variable up in the evaluation state.
*/
IdentifierExp::IdentifierExp(string name) {
//cout << "name = " << name << endl;
if(name == "LET" || name == "REM" || name == "PRINT" || name == "INPUT" || name == "END" || name == "GOTO" || name == "IF" || name == "THEN") error("SYNTAX ERROR");
this->name = name;
}
int IdentifierExp::eval(EvalState & state) {
if (!state.isDefined(name)) error("VARIABLE NOT DEFINED");
return state.getValue(name);
}
string IdentifierExp::toString() {
return name;
}
ExpressionType IdentifierExp::getType() {
return IDENTIFIER;
}
string IdentifierExp::getName() {
return name;
}
/*
* Implementation notes: the CompoundExp subclass
* ----------------------------------------------
* The CompoundExp subclass declares instance variables for the operator
* and the left and right subexpressions. The implementation of eval
* evaluates the subexpressions recursively and then applies the operator.
*/
CompoundExp::CompoundExp(string op, Expression *lhs, Expression *rhs) {
this->op = op;
this->lhs = lhs;
this->rhs = rhs;
}
CompoundExp::~CompoundExp() {
delete lhs;
delete rhs;
}
/*
* Implementation notes: eval
* --------------------------
* The eval method for the compound expression case must check for the
* assignment operator as a special case. Unlike the arithmetic operators
* the assignment operator does not evaluate its left operand.
*/
int CompoundExp::eval(EvalState & state) {
if (op == "=") {
if (lhs->getType() != IDENTIFIER) {
error("Illegal variable in assignment");
}
int val = rhs->eval(state);
state.setValue(((IdentifierExp *) lhs)->getName(), val);
return val;
}
int left = lhs->eval(state);
int right = rhs->eval(state);
if (op == "+") return left + right;
if (op == "-") return left - right;
if (op == "*") return left * right;
if (op == "/") {
if(right == 0) error("DIVIDE BY ZERO");
return left / right;
}
error("Illegal operator in expression");
return 0;
}
string CompoundExp::toString() {
return '(' + lhs->toString() + ' ' + op + ' ' + rhs->toString() + ')';
}
ExpressionType CompoundExp::getType() {
return COMPOUND;
}
string CompoundExp::getOp() {
return op;
}
Expression *CompoundExp::getLHS() {
return lhs;
}
Expression *CompoundExp::getRHS() {
return rhs;
}