blob: 7fc4542c15dea7c284496b8bc5121dca6112b681 (
about) (
plain)
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
|
#ifndef EXPRESSION_HPP
#define EXPRESSION_HPP
#include <string>
#include <vector>
#include <sstream>
#include "IdMap.hpp"
#include "Operator.hpp"
template<typename Domain>
struct VariableAssignment;
template<typename Domain>
struct Expression {
virtual ~Expression() { }
virtual Domain eval(const VariableAssignment<Domain>&) const = 0;
virtual void print(std::ostream&) const = 0;
};
template<typename Domain>
struct Constant : public Expression<Domain> {
Constant(const Domain& value)
: _value(value) { }
virtual Domain eval(const VariableAssignment<Domain>&) const {
return _value;
}
void print(std::ostream& cout) const {
cout << _value << "!c";
}
private:
Domain _value;
};
template<typename Domain>
struct Variable : public Expression<Domain> {
Variable(const unsigned int& id, const std::string& name)
: _id(id), _name(name) { }
unsigned int id() const {
return _id;
}
std::string name() const {
return _name;
}
virtual Domain eval(const VariableAssignment<Domain>& rho) const {
return rho[*this];
}
void print(std::ostream& cout) const {
cout << _name << "!v";
}
private:
const unsigned int _id;
const std::string _name;
};
template<typename Domain>
struct OperatorExpression : public Expression<Domain> {
OperatorExpression(const Operator<Domain>& op, const std::vector<Expression<Domain>*>& arguments)
: _operator(op), _arguments(arguments) { }
virtual Domain eval(const VariableAssignment<Domain>& rho) const {
std::vector<Domain> argumentValues;
for (typename std::vector<Expression<Domain>*>::const_iterator it = _arguments.begin();
it != _arguments.end();
++it) {
argumentValues.push_back((*it)->eval(rho));
}
return _operator.eval(argumentValues);
}
const std::vector<Expression<Domain>*> arguments() const {
return _arguments;
}
const Operator<Domain>& op() const {
return _operator;
}
void print(std::ostream& cout) const {
cout << _operator << "(";
for (unsigned int i = 0, length = _arguments.size();
i < length;
++i) {
if (i > 0)
cout << ", ";
cout << *_arguments[i];
}
cout << ")";
}
private:
const Operator<Domain>& _operator;
const std::vector<Expression<Domain>*> _arguments;
};
template<typename Domain>
struct MaxExpression : public OperatorExpression<Domain> {
MaxExpression(const unsigned int& id, const Maximum<Domain>& op, const std::vector<Expression<Domain>*>& arguments)
: OperatorExpression<Domain>(op, arguments), _id(id) { }
unsigned int id() const {
return _id;
}
private:
const unsigned int _id;
};
template<typename T>
std::ostream& operator<<(std::ostream& cout, const Expression<T>& exp) {
exp.print(cout);
return cout;
}
#include "VariableAssignment.hpp"
#endif
|