-
Notifications
You must be signed in to change notification settings - Fork 11
/
Variant1.h
116 lines (99 loc) · 2.7 KB
/
Variant1.h
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
#pragma once
#include <cstring>
namespace InterpreterPattern::Variant1
{
class IContext {
public:
virtual bool Lookup(const char*) const = 0;
};
class BooleanExp {
public:
BooleanExp() {}
virtual ~BooleanExp() {}
virtual bool Evaluate(IContext&) = 0;
virtual BooleanExp* Replace(const char*, BooleanExp&) = 0;
virtual BooleanExp* Copy() const = 0;
};
class VariableExp : public BooleanExp {
public:
VariableExp(const char*);
virtual ~VariableExp() {}
virtual bool Evaluate(IContext&);
virtual BooleanExp* Replace(const char*, BooleanExp&);
virtual BooleanExp* Copy() const;
private:
char* _name;
};
VariableExp::VariableExp(const char* name) {
// _name = strdup(name);
}
bool VariableExp::Evaluate(IContext& aContext) {
return aContext.Lookup(_name);
}
BooleanExp* VariableExp::Copy() const {
return new VariableExp(_name);
}
BooleanExp* VariableExp::Replace(const char* name, BooleanExp& exp) {
if (strcmp(name, _name) == 0) {
return exp.Copy();
}
else {
return new VariableExp(_name);
}
}
class AndExp : public BooleanExp {
public:
AndExp(BooleanExp*, BooleanExp*);
virtual ~AndExp() {}
virtual bool Evaluate(IContext&);
virtual BooleanExp* Replace(const char*, BooleanExp&);
virtual BooleanExp* Copy() const;
private:
BooleanExp* _operand1;
BooleanExp* _operand2;
};
AndExp::AndExp(BooleanExp* op1, BooleanExp* op2)
{
_operand1 = op1;
_operand2 = op2;
}
bool AndExp::Evaluate(IContext& aContext) {
return _operand1->Evaluate(aContext) && _operand2->Evaluate(aContext);
}
BooleanExp* AndExp::Copy() const
{
return new AndExp(_operand1->Copy(), _operand2->Copy());
}
BooleanExp* AndExp::Replace(const char* name, BooleanExp& exp) {
return new AndExp(
_operand1->Replace(name, exp),
_operand2->Replace(name, exp)
);
}
class Constant : public BooleanExp {
public:
Constant(bool) {}
virtual bool Evaluate(IContext&) { return true; }
virtual BooleanExp* Replace(const char*, BooleanExp&) { return nullptr; }
virtual BooleanExp* Copy() const { return nullptr; }
};
class OrExp : public BooleanExp {
public:
OrExp(BooleanExp*, BooleanExp*) {}
virtual bool Evaluate(IContext&) { return true; }
virtual BooleanExp* Replace(const char*, BooleanExp&) { return nullptr; }
virtual BooleanExp* Copy() const { return nullptr; }
};
class NotExp : public BooleanExp {
public:
NotExp(BooleanExp*) {}
virtual bool Evaluate(IContext&) { return true; }
virtual BooleanExp* Replace(const char*, BooleanExp&) { return nullptr; }
virtual BooleanExp* Copy() const { return nullptr; }
};
class Context : public IContext {
public:
bool Lookup(const char*) const { return true; }
void Assign(VariableExp*, bool) {}
};
}