-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinterpreter.cpp
111 lines (97 loc) · 2.84 KB
/
interpreter.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
/*!
* \brief Interpreter. Given a language, define a representation
* for its grammar along with an Interpreter that uses the
* representation to interpret sentences in the language.
*
* ------> Context
* Client ------> AbstractExpression
* | |
* TerminalExpression NonterminalExpression
* (ValExpression) (AddExpression, SubtractExpression)
*/
#include <iostream>
#include <vector>
#include <map>
// Context.
class Context {
public:
void AddValue(std::string key, int value) {
value_map_.insert(std::pair<std::string, int>(key, value));
}
int GetValue(std::string key) {
return value_map_[key];
}
private:
std::map<std::string, int> value_map_;
};
// AbstractExpression
class Expression {
public:
virtual int Interpreter(Context *context) = 0;
};
// TerminalExpression
class ValExpression : public Expression {
public:
ValExpression(std::string key) {
this->key_ = key;
}
int Interpreter(Context *context) {
return context->GetValue(key_);
}
private:
std::string key_;
};
// NonterminalExpression - Add Function.
class AddExpression : public Expression {
public:
AddExpression(Expression *left, Expression *right) {
this->left_ = left;
this->right_ = right;
}
int Interpreter(Context *context) {
std::cout << "Get into the Interpreter of AddExpression." << std::endl;
int ret = this->left_->Interpreter(context) + this->right_->Interpreter(context);
std::cout << "Finish the interpreting of AddExpression." << std::endl;
return ret;
}
private:
Expression *left_;
Expression *right_;
};
// NonterminalExpression - Subtract Function.
class SubtractExpression : public Expression {
public:
SubtractExpression(Expression *left, Expression *right) {
this->left_ = left;
this->right_ = right;
}
int Interpreter(Context *context) {
std::cout << "Get into the Interpreter of SubtractExpression." << std::endl;
int ret = this->left_->Interpreter(context) - this->right_->Interpreter(context);
std::cout << "Finish the interpreting of SubtractExpression." << std::endl;
return ret;
}
private:
Expression *left_;
Expression *right_;
};
int main() {
// a - b + c
Context *context = new Context();
context->AddValue("a", 3);
context->AddValue("b", 6);
context->AddValue("c", 8);
Expression *a = new ValExpression("a");
Expression *b = new ValExpression("b");
Expression *c = new ValExpression("c");
Expression *value_subtract = new SubtractExpression(a, b);
Expression *value_add = new AddExpression(value_subtract, c);
std::cout << std::endl << "Final Result: " << value_add->Interpreter(context) << std::endl;
delete context;
delete a;
delete b;
delete c;
delete value_subtract;
delete value_add;
return 0;
}