aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/server/parser/operator.hpp
blob: b29ae9258834fd60a834bb9a1d85ec30674cbea4 (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
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
// 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.

#pragma once

#include "error.hpp"

#include <lexer/token.hpp>
#include <lexer/token_type.hpp>

#include <cmath>

namespace math::server::parser {

class BinaryOp {
public:
    using Token = lexer::Token;
    using Type = Token::Type;

    static bool is(const Token& token) {
        switch (token.get_type()) {
            case Type::PLUS:
            case Type::MINUS:
            case Type::ASTERISK:
            case Type::SLASH:
            case Type::CARET:
                return true;

            default:
                return false;
        }
    }

    static BinaryOp from_token(const Token& token) {
        if (!is(token)) {
            throw ParserError{"internal: token is not a binary operator"};
        }
        return BinaryOp{token};
    }

    static constexpr unsigned min_precedence() { return 0; }

    static unsigned get_precedence(Type type) {
        switch (type) {
            case Type::PLUS:
            case Type::MINUS:
                return min_precedence();

            case Type::ASTERISK:
            case Type::SLASH:
                return min_precedence() + 1;

            case Type::CARET:
                return min_precedence() + 3;

            default:
                throw ParserError{"internal: undefined operator precedence"};
        }
    }

    unsigned get_precedence() const { return get_precedence(m_type); }

    bool is_right_associative() const {
        switch (m_type) {
            case Type::CARET:
                return true;

            default:
                return false;
        }
    }

    bool is_left_associative() const { return !is_right_associative(); }

    double exec(double lhs, double rhs) const {
        switch (m_type) {
            case Type::PLUS:
                return lhs + rhs;

            case Type::MINUS:
                return lhs - rhs;

            case Type::ASTERISK:
                return lhs * rhs;

            case Type::SLASH:
                // Trapping the CPU would be better?
                if (rhs == 0.) {
                    throw ParserError{"division by zero"};
                }
                return lhs / rhs;

            case Type::CARET:
                return std::pow(lhs, rhs);

            default:
                throw ParserError{"internal: unsupported operator"};
        }
    }

private:
    explicit BinaryOp(const Token& token) : m_type{token.get_type()} {}

    Type m_type;
};

} // namespace math::server::parser