-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatom.cpp
230 lines (182 loc) · 4.71 KB
/
atom.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "atom.hpp"
#include <sstream>
#include <cctype>
#include <cmath>
#include <limits>
Atom::Atom(): m_type(NoneKind) {}
Atom::Atom(double value) {
setNumber(value);
}
Atom::Atom(complex value) {
setComplex(value);
}
Atom::Atom(const Token& token): Atom() {
// is token a number?
double temp;
std::istringstream iss(token.asString());
if (iss >> temp) {
// check for trailing characters if >> succeeds, else assume symbol
if (iss.rdbuf()->in_avail() == 0) {
setNumber(temp);
}
} else {
// make sure does not start with number
if (!std::isdigit(token.asString()[0])) {
setSymbol(token.asString());
}
}
}
Atom::Atom(const std::string& value): Atom() {
setSymbol(value);
}
void Atom::copy(const Atom& x) {
switch (x.m_type) {
case NoneKind:
m_type = NoneKind;
break;
case NumberKind:
setNumber(x.numberValue);
break;
case ComplexKind:
setComplex(x.complexValue);
break;
case SymbolKind:
setSymbol(x.stringValue);
break;
case StringLiteralKind:
setStringLiteral(x.stringValue);
break;
}
}
Atom::Atom(const Atom& x): Atom() {
copy(x);
}
Atom& Atom::operator=(const Atom& x) {
if (this != &x) {
copy(x);
}
return *this;
}
Atom::~Atom() {
// we need to ensure the destructor of the symbol string is called
if (m_type == SymbolKind || m_type == StringLiteralKind) {
stringValue.~basic_string();
}
}
bool Atom::isNone() const noexcept {
return m_type == NoneKind;
}
bool Atom::isNumber() const noexcept {
return m_type == NumberKind;
}
bool Atom::isComplex() const noexcept {
return m_type == ComplexKind;
}
bool Atom::isSymbol() const noexcept {
return m_type == SymbolKind;
}
bool Atom::isStringLiteral() const noexcept {
return m_type == StringLiteralKind;
}
double Atom::truncateToZero(double value) {
// if the value is smaller than or equal to epsilon, just make it zero
if (fabs(value) <= std::numeric_limits<double>::epsilon()) {
return 0;
}
return value;
}
void Atom::setNumber(double value) {
m_type = NumberKind;
numberValue = truncateToZero(value);
}
void Atom::setComplex(complex value) {
m_type = ComplexKind;
complexValue = complex(truncateToZero(value.real()), truncateToZero(value.imag()));
}
void Atom::setSymbol(const std::string& value) {
// we need to ensure the destructor of the symbol string is called
if (m_type == SymbolKind){
stringValue.~basic_string();
}
// Check if there are quotes (only) surrounding the value - this makes it a string literal
if (value.front() == '"' && value.find('"', 1) == value.length() - 1) {
// set as a string literal without the quotes
setStringLiteral(value.substr(1, value.find_last_of('"') - 1));
} else {
m_type = SymbolKind;
new (&stringValue) std::string(value);
}
}
void Atom::setStringLiteral(const std::string& value) {
if (m_type == StringLiteralKind){
stringValue.~basic_string();
} else {
m_type = StringLiteralKind;
}
new (&stringValue) std::string(value);
}
double Atom::asNumber() const noexcept {
return (m_type == NumberKind) ? numberValue : 0.0;
}
complex Atom::asComplex() const noexcept {
switch(m_type) {
case(ComplexKind):
return complexValue;
case(NumberKind):
return complex(numberValue, 0);
default:
return complex(0, 0);
}
}
std::string Atom::asSymbol(bool noQuotes) const noexcept {
std::string result;
// Just get the string value for symbol kinds and string literals with no quotes
if (m_type == SymbolKind || (m_type == StringLiteralKind && noQuotes)){
return stringValue;
} else if (m_type == StringLiteralKind) {
// Add quotes around the result if it is a string literal and if noQuotes is false
result = '"' + stringValue + '"';
}
return result;
}
bool Atom::operator==(const Atom& right) const noexcept {
if (m_type != right.m_type) {
return false;
}
switch(m_type) {
case NoneKind:
// Avoid the default case for NoneKind - if both are NoneKind, then it will return
// true once outside this switch statement
break;
case NumberKind:
{
double dleft = numberValue;
double dright = right.numberValue;
double diff = fabs(dleft - dright);
if(std::isnan(diff) || (diff > std::numeric_limits<double>::epsilon())) {
return false;
}
}
break;
case ComplexKind:
return complexValue == right.complexValue;
case SymbolKind:
return stringValue == right.stringValue;
case StringLiteralKind:
return stringValue == right.stringValue;
}
return true;
}
bool operator!=(const Atom& left, const Atom& right) noexcept {
return !(left == right);
}
std::ostream& operator<<(std::ostream& out, const Atom& a) {
if (a.isNumber()) {
out << a.asNumber();
} else if (a.isComplex()) {
out << a.asComplex().real() << "," << a.asComplex().imag();
} else if (a.isSymbol() || a.isStringLiteral()) {
out << a.asSymbol();
}
return out;
}