diff options
Diffstat (limited to 'value.cpp')
-rw-r--r-- | value.cpp | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/value.cpp b/value.cpp new file mode 100644 index 0000000..f210dd1 --- /dev/null +++ b/value.cpp @@ -0,0 +1,104 @@ +#include <sstream> + +#include "value.h" + +#include "evaluator.h" + +tree_value::tree_value( + evaluator * evaluator, + value * lchild, + value * rchild) + : + m_evaluator(evaluator), + m_lchild(lchild), + m_rchild(rchild) + { } + +value * +tree_value::simplify() +{ + if (!m_evaluator) { + return this->clone(); + } + return m_evaluator->evaluate(m_lchild->simplify(), m_rchild->simplify()); +} + +value * +tree_value::clone() +{ + return new tree_value(m_evaluator, + (m_lchild ? m_lchild->clone() : NULL), + (m_rchild ? m_rchild->clone() : NULL)); +} + +std::string +tree_value::to_string() +{ + return "(" + (m_lchild ? m_lchild->to_string() : "?") + ") " + + (m_evaluator ? m_evaluator->to_string() : "?") + " (" + + (m_rchild ? m_rchild->to_string() : "?") + ")"; +} + +const_value::const_value( + register_address address, + float_value * value_ptr) : + value(), + m_reg_address(address), + m_value(value_ptr) + { } + +value * +const_value::simplify() +{ + if (m_value->m_has_value) { + return m_value->clone(); + } else { + return this->clone(); + } +} + +value * +const_value::clone() +{ + return new const_value(m_reg_address, m_value); +} + +std::string +const_value::to_string() +{ + return m_reg_address.to_string(); +} +float_value::float_value() : + m_has_value(false) + { } + +value * +float_value::simplify() +{ + return this; +} + +value * +float_value::clone() +{ + return new float_value(*this); +} + +std::string +float_value::to_string() +{ + std::ostringstream s; + if (!m_has_value) { + s << "?"; + } else { + s << m_value; + } + return s.str(); +} + +void +float_value::set_value(float value) +{ + m_has_value = true; + m_value = value; +} |