blob: 466012f2eb8a8751fe2a127446e6dd27ef6f0b70 (
plain) (
blame)
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
|
// Copyright (c) 2019 Egor Tensin <Egor.Tensin@gmail.com>
// This file is part of the "math-server" project.
// For details, see https://github.com/egor-tensin/math-server.
// Distributed under the MIT License.
#include <lexer/error.hpp>
#include <lexer/token.hpp>
#include <lexer/token_type.hpp>
#include <cmath>
#include <limits>
#include <variant>
namespace math::server::lexer {
namespace {
constexpr double nan() {
return std::numeric_limits<double>::quiet_NaN();
}
bool is_nan(double x) {
return std::isnan(x);
}
bool numbers_equal(double x, double y) {
if (is_nan(x) && is_nan(y)) {
return true;
}
return x == y;
}
} // namespace
Token::Token(Type type) : m_type{type} {
if (token::token_has_value(type)) {
throw LexerError{"internal: must have a value: " + token::type_to_int_string(type)};
}
}
Token::Token(double value) : m_type{Type::NUMBER}, m_value{value} {}
bool Token::operator==(const Token& other) const {
if (m_type != other.m_type) {
return false;
}
if (token::is_const_token(m_type)) {
return true;
}
if (m_type == Type::NUMBER) {
return numbers_equal(as_number(), other.as_number());
}
throw LexerError{"internal: can't compare tokens of type: " +
token::type_to_int_string(m_type)};
}
double Token::as_number() const {
const auto type = get_type();
if (type != Type::NUMBER) {
throw LexerError{"internal: not a number: " + token::type_to_int_string(type)};
}
return std::get<double>(m_value);
}
std::ostream& operator<<(std::ostream& os, const Token& token) {
switch (token.m_type) {
case token::Type::NUMBER:
os << token.as_number();
break;
default:
os << token::type_to_string(token.m_type);
break;
}
return os;
}
} // namespace math::server::lexer
|