summaryrefslogtreecommitdiff
path: root/value.cpp
blob: f210dd198d34db0bb9d7ccb1b5b049bbf0601401 (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
#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;
}