10#if !defined (HPP_ALIB_EXPRESSIONS_PLUGINS_AUTOCAST)
15#if !defined (HPP_ALIB_EXPRESSIONS_PLUGINS_STRINGS)
20#if !defined (HPP_ALIB_EXPRESSIONS_PLUGINS_ARITHMETICS)
28#define BOL(box) (box).Unbox<bool >()
29#define INT(box) (box).Unbox<integer>()
31#define FUNC( name,...) Box name( Scope& scope, \
34 { (void) scope; (void) args; (void) end; __VA_ARGS__ }
37namespace alib {
namespace expressions {
namespace plugins {
54SortedTypes SortedType( Type type )
56 if( type.IsType<
bool >() )
return SortedTypes::Bool;
57 if( type.IsType<integer>() )
return SortedTypes::Integer;
58 if( type.IsType<
double >() )
return SortedTypes::Float;
59 if( type.IsType<String >() )
return SortedTypes::String;
60 return SortedTypes::UNKNOWN;
64FUNC( castI2F ,
return static_cast<double >( INT(ARG0) ); )
65FUNC( castB2F ,
return static_cast<double >( BOL(ARG0) ); )
66FUNC( castB2I ,
return static_cast<integer>( BOL(ARG0)); )
74bool AutoCast::TryCompilation( CIAutoCast& ciAutoCast )
77 if ( ciAutoCast.ArgsBegin + 1 >= ciAutoCast.ArgsEnd )
81 SortedTypes t1= SortedType( * ciAutoCast.ArgsBegin );
82 SortedTypes t2= SortedType( *(ciAutoCast.ArgsBegin+1) );
87 if( t1 > t2 ) { major= t1; minor= t2; }
88 else { major= t2; minor= t1; }
92 String decompileFunctionCall;
93 ALIB_DBG(
const char* upgradeCastDBG; )
96 if( major == SortedTypes::String )
99 decompileFunctionCall =
A_CHAR(
"String");
100ALIB_DBG(upgradeCastDBG =
"CBToString"; )
104 else if( major == SortedTypes::Float && minor == SortedTypes::Integer )
106 upgradeCast = castI2F;
107 decompileFunctionCall =
A_CHAR(
"Float");
108ALIB_DBG(upgradeCastDBG =
"castI2F"; )
113 else if( major == SortedTypes::Float && minor == SortedTypes::Bool )
115 upgradeCast = castB2F;
116 decompileFunctionCall =
A_CHAR(
"Float");
117ALIB_DBG(upgradeCastDBG =
"castB2F"; )
122 else if( major == SortedTypes::Integer && minor == SortedTypes::Bool )
124 upgradeCast = castB2I;
125 decompileFunctionCall =
A_CHAR(
"Integer");
126ALIB_DBG(upgradeCastDBG =
"castB2I"; )
130 else if( major == SortedTypes::Bool )
133 decompileFunctionCall =
A_CHAR(
"Boolean");
134ALIB_DBG(upgradeCastDBG =
"ToBoolean"; )
143 ciAutoCast.Callback = upgradeCast;
144 ciAutoCast.TypeOrValue = *(ciAutoCast.ArgsBegin + 1);
145 ciAutoCast.ReverseCastFunctionName = decompileFunctionCall;
146ALIB_DBG(ciAutoCast.DbgCallbackName = upgradeCastDBG; )
150 ciAutoCast.CallbackRhs = upgradeCast;
151 ciAutoCast.TypeOrValueRhs = *(ciAutoCast.ArgsBegin );
152 ciAutoCast.ReverseCastFunctionNameRhs = decompileFunctionCall;
153ALIB_DBG(ciAutoCast.DbgCallbackNameRhs = upgradeCastDBG; )
157 if( ciAutoCast.IsConst && ciAutoCast.Callback !=
nullptr )
159 ciAutoCast.TypeOrValue = ciAutoCast.Callback( ciAutoCast.CompileTimeScope,
160 ciAutoCast.ArgsBegin ,
161 ciAutoCast.ArgsEnd - 1 );
162 ciAutoCast.Callback =
nullptr;
164 else if( ciAutoCast.RhsIsConst && ciAutoCast.CallbackRhs !=
nullptr )
166 ciAutoCast.TypeOrValueRhs= ciAutoCast.CallbackRhs( ciAutoCast.CompileTimeScope,
167 ciAutoCast.ArgsBegin + 1 ,
168 ciAutoCast.ArgsEnd );
169 ciAutoCast.CallbackRhs =
nullptr;
ALIB_API Box CBToString(Scope &scope, ArgIterator args, ArgIterator)
ALIB_API Box ToBoolean(Scope &scope, ArgIterator args, ArgIterator)
Box(*)(Scope &scope, ArgIterator argsBegin, ArgIterator argsEnd) CallbackDecl
platform_specific integer
expressions::CompilerPlugin CompilerPlugin
Type alias in namespace alib.
strings::TString< character > String
Type alias in namespace alib.
expressions::Compiler Compiler
Type alias in namespace alib.
ALIB_API AutoCast(Compiler &compiler)