summaryrefslogtreecommitdiff
path: root/value.cpp
diff options
context:
space:
mode:
authorTom Stellard <tstellar@gmail.com>2010-12-09 00:44:03 -0800
committerTom Stellard <tstellar@gmail.com>2010-12-09 00:44:03 -0800
commit5c16dd6f39da486d31e890d068cb7987a1785c5a (patch)
tree200535f81df43d76f6aea573bde385be8e260962 /value.cpp
parent107f4e5aa5def40ad42db49c0f5b46a7d64700a0 (diff)
Get basic functionality working with a simple test case.
Diffstat (limited to 'value.cpp')
-rw-r--r--value.cpp104
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;
+}