File tree Expand file tree Collapse file tree 2 files changed +73
-0
lines changed Expand file tree Collapse file tree 2 files changed +73
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * Ported to C++ from http://www.dofactory.com/Patterns/PatternInterpreter.aspx
3
+ */
4
+
5
+ #include < iostream>
6
+ #include < vector>
7
+ using namespace std ;
8
+
9
+ class Context {
10
+
11
+ };
12
+
13
+
14
+ class AbstractExpression {
15
+ public:
16
+ virtual void interpret ( Context* context ) = 0;
17
+ };
18
+
19
+ class TerminalExpression : public AbstractExpression {
20
+ public:
21
+ virtual void interpret ( Context* context ) {
22
+ cout << " Called Terminal.interpret()" << endl;
23
+ }
24
+ };
25
+
26
+ class NoneterminalExpression : public AbstractExpression {
27
+ public:
28
+ virtual void interpret ( Context* context ) {
29
+ cout << " Called NonTerminal.interpret()" << endl;
30
+ }
31
+ };
32
+
33
+
34
+ int main () {
35
+
36
+ Context* context = new Context ();
37
+
38
+ vector<AbstractExpression*> list;
39
+
40
+ list.push_back ( new TerminalExpression () );
41
+ list.push_back ( new NoneterminalExpression () );
42
+ list.push_back ( new TerminalExpression () );
43
+ list.push_back ( new TerminalExpression () );
44
+
45
+ for ( vector<AbstractExpression*>::iterator it = list.begin ();
46
+ it != list.end ();
47
+ ++it ) {
48
+ (*it)->interpret (context);
49
+ }
50
+
51
+ return 0 ;
52
+ }
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ using namespace std ;
3
+
4
+ class Context {
5
+ public:
6
+ bool lookup ( const char * paramName ) const {
7
+ if ( strcmp ( _xName, paramName ) == 0
8
+ }
9
+
10
+ private:
11
+ char * _xName;
12
+ char * _yName;
13
+ bool _xVlue;
14
+ bool _xVlue;
15
+ }
16
+
17
+ class BooleanExpression {
18
+ public:
19
+ virtual bool evaluate ( Context & ) = 0;
20
+ };
21
+
You can’t perform that action at this time.
0 commit comments