summaryrefslogtreecommitdiff
path: root/value.cpp
blob: 12013441e85ba0029234fe78bd0c6fc5162a3e89 (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
126
#include <sstream>

#include "value.h"

#include "evaluator.h"

value::value(enum value_type type) :
	m_type(type)
	{ }

enum value_type
value::get_type()
{
	return m_type;
}

tree_value::tree_value(
	evaluator * evaluator,
	value * lchild,
	value * rchild)
	:
	value(VALUE_TYPE_TREE),
	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 ? m_lchild->simplify() : NULL,
			m_rchild ? m_rchild->simplify() : NULL);
}

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() : "?") + ")";
}

float_value::float_value() :
	value(VALUE_TYPE_FLOAT),
	m_has_value(false)
	{ }

float_value::float_value(float init_value) :
	/* XXX: Can we call the other constructor here? */
	value(VALUE_TYPE_FLOAT),
	m_has_value(true),
	m_value(init_value)
	{ }

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;
}

const_value::const_value(
	register_address address,
	float_value * value_ptr) :
	value(VALUE_TYPE_CONST),
	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();
}