diff options
Diffstat (limited to 'impl/Expression.hpp')
-rw-r--r-- | impl/Expression.hpp | 37 |
1 files changed, 34 insertions, 3 deletions
diff --git a/impl/Expression.hpp b/impl/Expression.hpp index 7d2d956..97dda10 100644 --- a/impl/Expression.hpp +++ b/impl/Expression.hpp @@ -1,6 +1,7 @@ #ifndef EXPRESSION_H #define EXPRESSION_H +#include <ostream> #include <map> #include <string> #include <vector> @@ -21,6 +22,9 @@ struct Maximum : public Operator<T> { } return value; } + virtual std::string output() const { + return "max"; + } }; template<typename T> @@ -36,13 +40,16 @@ struct Minimum : public Operator<T> { } return value; } + virtual std::string output() const { + return "min"; + } }; template<typename T> struct Expression { - Expression(Operator<T>* o, const std::vector< Expression<T> >& a) - : _operator(o), _arguments(a) { } + Expression(unsigned int id, Operator<T>* o, const std::vector< Expression<T> >& a) + : _id(id), _operator(o), _arguments(a) { } virtual const T eval(std::map<std::string, T> m) const { return _operator->eval(_arguments, m); } @@ -50,7 +57,7 @@ struct Expression { virtual Expression<T> maxStrategyEager(const std::map<std::string, T> assignment) const { if (dynamic_cast<Maximum<T>*>(_operator) != NULL) { T bestVal; - const Expression<T>* best; + const Expression<T>* best = NULL; for (typename std::vector<Expression<T> >::const_iterator it = _arguments.begin(); it != _arguments.end(); ++it) { @@ -71,10 +78,34 @@ struct Expression { return Expression(_operator, newArgs); } + std::string output() const { + std::string result = _operator->output() + "("; + for (typename std::vector<Expression<T> >::const_iterator it = _arguments.begin(); + it != _arguments.end(); + ++it) { + if (it != _arguments.begin()) { + result += ", "; + } + result += it->output(); + } + return result + ")"; + } + virtual ~Expression() {} private: + unsigned int _id; Operator<T>* _operator; std::vector< Expression<T> > _arguments; + + template<typename E> + friend std::ostream& operator<<(std::ostream&, const Expression<E>&); }; +template<typename T> +std::ostream& operator<<(std::ostream& cout, const Expression<T>& e) { + cout << e._operator->output() << "(" + << ")"; + return cout; +} + #endif |