ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
elvisoperator.cpp
1//##################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2025 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6//##################################################################################################
7#include "alib_precompile.hpp"
8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
10#endif
11#if ALIB_C20_MODULES
12 module;
13#endif
14//========================================= Global Fragment ========================================
16
17//============================================== Module ============================================
18#if ALIB_C20_MODULES
19 module ALib.Expressions.Impl;
20 import ALib.Characters.Functions;
21 import ALib.Strings;
22#else
24# include "ALib.Strings.H"
26#endif
27//========================================== Implementation ========================================
28//! @cond NO_DOX
29namespace alib { namespace expressions { namespace plugins {
30
32: CompilerPlugin("ALib Elvis", compiler, CompilePriorities::ElvisOperator ) {}
33
34namespace {
35
36Box elvis( Scope&, ArgIterator arg, ArgIterator )
37{ return (*arg).Call<FIsTrue>() ? *arg : *(arg + 1); }
38
39} // anonymous namespace
40
41bool ElvisOperator::TryCompilation( CIBinaryOp& ciBinaryOp ) {
42 Box& lhs= * ciBinaryOp.ArgsBegin;
43 Box& rhs= *(ciBinaryOp.ArgsBegin + 1);
44
45 // not Elvis operator A_CHAR("A ?: B") ?
46 if( ciBinaryOp.Operator != A_CHAR("?:") || !lhs.IsSameType(rhs) )
47 return false;
48
49 ALIB_DBG( ciBinaryOp.DbgCallbackName = "elvis"; )
50
51 // constant A?
52 if( ciBinaryOp.LhsIsConst ) {
53 if( lhs.Call<FIsTrue>() )
54 ciBinaryOp.TypeOrValue= lhs;
55 else {
56 // constant B?
57 if( ciBinaryOp.RhsIsConst )
58 ciBinaryOp.TypeOrValue= rhs;
59 else
60 ciBinaryOp.NonConstArgIsResult= true;
61 }
62 return true;
63 }
64
65 // non constant A
66 ciBinaryOp.Callback = elvis;
67 ciBinaryOp.TypeOrValue= rhs;
68
69 return true;
70}
71
72}}} // namespace [alib::expressions::detail]
73//! @endcond
decltype(std::declval< typename TFDecl::Signature >()(std::declval< Box & >(), std::declval< TArgs >()...)) Call(TArgs &&... args) const
Definition box.inl:985
#define A_CHAR(STR)
#define ALIB_DBG(...)
Definition alib.inl:853
expressions::CompilerPlugin CompilerPlugin
Type alias in namespace alib.
boxing::Box Box
Type alias in namespace alib.
Definition box.inl:1149
expressions::Compiler Compiler
Type alias in namespace alib.
Definition compiler.inl:540
ALIB_DLL ElvisOperator(Compiler &compiler)