HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
operator.hpp
1// Copyright Take Vos 2019, 2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "../macros.hpp"
8#include <cstdint>
9#include <limits>
10#include <tuple>
11#include <string_view>
12
13hi_export_module(hikogui.parser.operator);
14
15hi_export namespace hi::inline v1 {
16
17enum class graphic_character_t {
18 none = 0x00,
19 exclamation_mark = 0x01,
20 double_quote = 0x02,
21 hash = 0x03,
22 dollar = 0x04,
23 percent = 0x05,
24 ampersand = 0x06,
25 single_quote = 0x07,
26 open_paren = 0x08,
27 close_paren = 0x09,
28 star = 0x0a,
29 plus = 0x0b,
30 comma = 0x0c,
31 minus = 0x0d,
32 dot = 0x0e,
33 slash = 0x0f,
34 colon = 0x10,
35 semi_colon = 0x11,
36 less_than = 0x12,
37 equal = 0x13,
38 greater_than = 0x14,
39 question_mark = 0x15,
40 open_bracket = 0x16,
41 back_slash = 0x17,
42 close_bracket = 0x18,
43 carret = 0x19,
44 underscore = 0x1a,
45 back_quote = 0x1b,
46 open_brace = 0x1c,
47 pipe = 0x1d,
48 close_brace = 0x1e,
49 tilde = 0x1f
50};
51
52[[nodiscard]] constexpr graphic_character_t char_to_graphic_character(char x) noexcept
53{
54 switch (x) {
55 case '!': return graphic_character_t::exclamation_mark;
56 case '"': return graphic_character_t::double_quote;
57 case '#': return graphic_character_t::hash;
58 case '$': return graphic_character_t::dollar;
59 case '%': return graphic_character_t::percent;
60 case '&': return graphic_character_t::ampersand;
61 case '\'': return graphic_character_t::single_quote;
62 case '(': return graphic_character_t::open_paren;
63 case ')': return graphic_character_t::close_paren;
64 case '*': return graphic_character_t::star;
65 case '+': return graphic_character_t::plus;
66 case ',': return graphic_character_t::comma;
67 case '-': return graphic_character_t::minus;
68 case '.': return graphic_character_t::dot;
69 case '/': return graphic_character_t::slash;
70 case ':': return graphic_character_t::colon;
71 case ';': return graphic_character_t::semi_colon;
72 case '<': return graphic_character_t::less_than;
73 case '=': return graphic_character_t::equal;
74 case '>': return graphic_character_t::greater_than;
75 case '?': return graphic_character_t::question_mark;
76 case '[': return graphic_character_t::open_bracket;
77 case '\\': return graphic_character_t::back_slash;
78 case ']': return graphic_character_t::close_bracket;
79 case '^': return graphic_character_t::carret;
80 case '_': return graphic_character_t::underscore;
81 case '`': return graphic_character_t::back_quote;
82 case '{': return graphic_character_t::open_brace;
83 case '|': return graphic_character_t::pipe;
84 case '}': return graphic_character_t::close_brace;
85 case '~': return graphic_character_t::tilde;
86 default: return graphic_character_t::none;
87 }
88}
89
90[[nodiscard]] constexpr uint64_t operator_to_int(std::string_view str) noexcept
91{
92 uint64_t r = 0;
93 for (auto const c: str) {
94 r <<= 5;
95 r |= static_cast<uint64_t>(char_to_graphic_character(c));
96 }
97 return r;
98}
99
103[[nodiscard]] constexpr std::pair<uint8_t, bool> binary_operator_precedence(std::string_view str) noexcept
104{
105 switch (operator_to_int(str)) {
106 case operator_to_int("::"): return {uint8_t{1}, true};
107 case operator_to_int("("): return {uint8_t{2}, true};
108 case operator_to_int("["): return {uint8_t{2}, true};
109 case operator_to_int("."): return {uint8_t{2}, true};
110 case operator_to_int("->"): return {uint8_t{2}, true};
111 case operator_to_int(".*"): return {uint8_t{4}, true};
112 case operator_to_int("->*"): return {uint8_t{4}, true};
113 case operator_to_int("**"): return {uint8_t{4}, true};
114 case operator_to_int("*"): return {uint8_t{5}, true};
115 case operator_to_int("/"): return {uint8_t{5}, true};
116 case operator_to_int("%"): return {uint8_t{5}, true};
117 case operator_to_int("+"): return {uint8_t{6}, true};
118 case operator_to_int("-"): return {uint8_t{6}, true};
119 case operator_to_int("<<"): return {uint8_t{7}, true};
120 case operator_to_int(">>"): return {uint8_t{7}, true};
121 case operator_to_int("<=>"): return {uint8_t{8}, true};
122 case operator_to_int("<"): return {uint8_t{9}, true};
123 case operator_to_int(">"): return {uint8_t{9}, true};
124 case operator_to_int("<="): return {uint8_t{9}, true};
125 case operator_to_int(">="): return {uint8_t{9}, true};
126 case operator_to_int("=="): return {uint8_t{10}, true};
127 case operator_to_int("!="): return {uint8_t{10}, true};
128 case operator_to_int("&"): return {uint8_t{11}, true};
129 case operator_to_int("^"): return {uint8_t{12}, true};
130 case operator_to_int("|"): return {uint8_t{13}, true};
131 case operator_to_int("&&"): return {uint8_t{14}, true};
132 case operator_to_int("||"): return {uint8_t{15}, true};
133 case operator_to_int("?"): return {uint8_t{16}, false};
134 case operator_to_int("="): return {uint8_t{16}, false};
135 case operator_to_int("+="): return {uint8_t{16}, false};
136 case operator_to_int("-="): return {uint8_t{16}, false};
137 case operator_to_int("*="): return {uint8_t{16}, false};
138 case operator_to_int("/="): return {uint8_t{16}, false};
139 case operator_to_int("%="): return {uint8_t{16}, false};
140 case operator_to_int("<<="): return {uint8_t{16}, false};
141 case operator_to_int(">>="): return {uint8_t{16}, false};
142 case operator_to_int("&="): return {uint8_t{16}, false};
143 case operator_to_int("^="): return {uint8_t{16}, false};
144 case operator_to_int("|="): return {uint8_t{16}, false};
145 case operator_to_int(","): return {uint8_t{17}, true};
146 case operator_to_int("]"): return {uint8_t{17}, true};
147 case operator_to_int(")"): return {uint8_t{17}, true};
148 case operator_to_int("!"): return {uint8_t{18}, true};
149 default: return {std::numeric_limits<uint8_t>::max(), true};
150 }
151}
152
156[[nodiscard]] constexpr std::pair<uint8_t, bool> operator_precedence(std::string_view str, bool binary) noexcept
157{
158 return binary ? binary_operator_precedence(str) : std::pair<uint8_t, bool>{uint8_t{3}, false};
159}
160
161} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
constexpr std::pair< uint8_t, bool > binary_operator_precedence(std::string_view str) noexcept
Binary Operator Precedence according to C++.
Definition operator.hpp:103
constexpr std::pair< uint8_t, bool > operator_precedence(std::string_view str, bool binary) noexcept
Operator Precedence according to C++.
Definition operator.hpp:156
T max(T... args)