Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Compiler_Operator.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
42#ifndef COMPILER_OPERATOR_H
43#define COMPILER_OPERATOR_H
44
45#include <cassert>
46#include <limits>
47
48#include <Compiler_Token.H>
49
50namespace Aleph {
53{
54 Invalid,
55 Assign,
56 Plus,
57 Minus,
58 Star,
59 Slash,
60 Percent,
61 EqEq,
62 NotEq,
63 Less,
64 LessEq,
65 Greater,
67 AndAnd,
68 OrOr,
69 Bang,
70 Amp,
71 Pipe,
72 Caret,
73 Tilde,
74 PlusEq,
75 MinusEq,
76 StarEq,
77 SlashEq,
81};
82
84inline const char *compiler_operator_name(const Compiler_Operator_Kind kind) noexcept
85{
86 switch (kind)
87 {
89 return "Invalid";
91 return "Assign";
93 return "Plus";
95 return "Minus";
97 return "Star";
99 return "Slash";
101 return "Percent";
103 return "EqEq";
105 return "NotEq";
107 return "Less";
109 return "LessEq";
111 return "Greater";
113 return "GreaterEq";
115 return "AndAnd";
117 return "OrOr";
119 return "Bang";
121 return "Amp";
123 return "Pipe";
125 return "Caret";
127 return "Tilde";
129 return "PlusEq";
131 return "MinusEq";
133 return "StarEq";
135 return "SlashEq";
137 return "PercentEq";
139 return "PlusPlus";
141 return "MinusMinus";
142 }
143
144 return "Invalid";
145}
146
149{
150 switch (kind)
151 {
204 default:
205 break;
206 }
207
209}
210
213{
214 switch (kind)
215 {
268 default:
269 break;
270 }
271
273}
274
277{
278 switch (kind)
279 {
285 return true;
286 default:
287 break;
288 }
289
290 return false;
291}
292
314
317{
318 Ok,
319 Overflow,
322};
323
330
344[[nodiscard]] inline Compiler_Integer_Result
346 const long long lhs,
347 const long long rhs) noexcept
348{
349 using Status = Compiler_Integer_Status;
350 constexpr long long max = std::numeric_limits<long long>::max();
351 constexpr long long min = std::numeric_limits<long long>::min();
352
353 switch (op)
354 {
356 if ((rhs > 0 and lhs > max - rhs) or (rhs < 0 and lhs < min - rhs))
357 return {Status::Overflow, 0};
358 return {Status::Ok, lhs + rhs};
359
361 if ((rhs < 0 and lhs > max + rhs) or (rhs > 0 and lhs < min + rhs))
362 return {Status::Overflow, 0};
363 return {Status::Ok, lhs - rhs};
364
366 if (lhs > 0 ? (rhs > 0 ? lhs > max / rhs : rhs < min / lhs)
367 : (rhs > 0 ? lhs < min / rhs : lhs != 0 and rhs < max / lhs))
368 return {Status::Overflow, 0};
369 return {Status::Ok, lhs * rhs};
370
372 if (rhs == 0)
373 return {Status::Division_By_Zero, 0};
374 if (lhs == min and rhs == -1)
375 return {Status::Overflow, 0};
376 return {Status::Ok, lhs / rhs};
377
379 if (rhs == 0)
380 return {Status::Modulo_By_Zero, 0};
381 if (rhs == -1) // LLONG_MIN % -1 is undefined in C++
382 return {Status::Ok, 0};
383 return {Status::Ok, lhs % rhs};
384
386 return {Status::Ok, lhs & rhs};
387
389 return {Status::Ok, lhs | rhs};
390
392 return {Status::Ok, lhs ^ rhs};
393
394 default:
395 break;
396 }
397
398 assert(false and "compiler_integer_binary(): not an integer operator");
399 return {Status::Overflow, 0};
400}
401
407[[nodiscard]] inline Compiler_Integer_Result
408compiler_integer_negate(const long long operand) noexcept
409{
410 if (operand == std::numeric_limits<long long>::min())
412 return {Compiler_Integer_Status::Ok, -operand};
413}
414} // namespace Aleph
415
416#endif
Token model and parser-facing metadata for compiler front-ends.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4188
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_min_function > > min(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4189
Main namespace for Aleph-w library functions.
@ Invalid
Uninitialized register/slot or explicit invalid placeholder.
const char * compiler_operator_name(const Compiler_Operator_Kind kind) noexcept
Returns a stable debug name for one operator kind.
Compiler_Integer_Result compiler_integer_negate(const long long operand) noexcept
Negates one integer of the language.
Compiler_Integer_Status
Outcome of one integer operation of the language.
@ Overflow
The result does not fit in a signed 64-bit integer.
@ Division_By_Zero
/ with a zero divisor.
@ Modulo_By_Zero
% with a zero divisor.
@ Ok
The result is in value.
and
Check uniqueness with explicit hash + equality functors.
Compiler_Token_Kind compiler_operator_to_token_kind(const Compiler_Operator_Kind kind) noexcept
Maps one reusable operator kind back into the MVP token kind.
Compiler_Token_Kind
Token kinds supported by the compiler MVP.
@ Invalid
Sentinel for lexer errors or uninitialized tokens.
Compiler_Integer_Result compiler_integer_binary(const Compiler_Operator_Kind op, const long long lhs, const long long rhs) noexcept
Applies one integer operator of the language to two values.
Compiler_Operator_Kind compiler_operator_compound_base(const Compiler_Operator_Kind kind) noexcept
Returns the arithmetic operator underlying one compound assignment.
bool compiler_operator_is_compound_assignment(const Compiler_Operator_Kind kind) noexcept
Returns whether kind is one compound-assignment operator.
Compiler_Operator_Kind compiler_operator_from_token_kind(const Compiler_Token_Kind kind) noexcept
Maps one reference-frontend token kind into a reusable operator kind.
Compiler_Operator_Kind
Stable operator kinds shared by reusable compiler layers.
Result of compiler_integer_binary() and compiler_integer_negate().
long long value
The result, when status is Ok.
Compiler_Integer_Status status
What happened.