-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterpreter.cpp
More file actions
55 lines (41 loc) · 1.15 KB
/
Interpreter.cpp
File metadata and controls
55 lines (41 loc) · 1.15 KB
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
//
// Interpreter.cpp
// Exam
//
// Created by Max Reshetey on 23/11/2016.
// Copyright © 2016 Max Reshetey. All rights reserved.
//
#include "Interpreter.h"
bool Context::lookup(string name)
{
return _map[name];
}
void Context::assign(VariableExpression* expression, bool value)
{
_map[expression->name()] = value;
};
namespace Interpreter
{
void test()
{
cout << "=== Interpreter pattern ===\n\n";
VariableExpression * x = new VariableExpression("X");
VariableExpression * y = new VariableExpression("Y");
// Simple (true && false) expression
Context context;
context.assign(x, true);
context.assign(y, false);
BooleanExpression * expression = new AndExpression(x, y);
bool result = expression->evaluate(context);
cout << "See interpretation result: " << result << "\n";
// Now replace false with true
VariableExpression * z = new VariableExpression("Z");
context.assign(z, true);
BooleanExpression * changedExpression = expression->replace("Y", *z);
result = changedExpression->evaluate(context);
cout << "See interpretation result: " << result << "\n";
delete expression;
delete changedExpression;
cout << endl;
}
}