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"
20 module ALib.Expressions.Impl;
21 import ALib.Characters.Functions;
41#define SPACE(flag) ( HasBits(format, Normalization::flag ) ? normSpace : EMPTY_STRING )
42#define COND_SPACE(flag, force) if( HasBits(format, Normalization::flag ) || force ) normalized << ' '
46 for(
auto& it : compiler.CfgNormalizationDisallowed )
47 if( it.Length() > spaceInsertionPos
48 && normalized.ContainsAt( it, positionToCheck ) )
50 normalized.InsertAt(
A_CHAR(
" "), positionToCheck + spaceInsertionPos );
63 arg= arg->Optimize( normalization );
68 Lhs=
Lhs->Optimize( normalization );
69 Rhs=
Rhs->Optimize( normalization );
74 Q=
Q->Optimize( normalization );
75 T=
T->Optimize( normalization );
76 F=
F->Optimize( normalization );
126 else if(
Value.IsType<
double>() ) {
128 auto oldFlags= nf->
Flags;
131 nf->
Flags+= NumberFormatFlags::ForceScientific;
150 else normalized <<
Dec(value, 0, nf);
177 functionName <<
Name;
180 normalized << functionName;
187 bool replacedNestedExpressionIdentifierByLiteral=
false;
188 bool thirdArgumentIsThrowIdentifier=
false;
190 if( qtyArgs < 1 || qtyArgs > 3 ) {
203 replacedNestedExpressionIdentifierByLiteral=
true;
218 thirdArgumentIsThrowIdentifier=
true;
229 if( replacedNestedExpressionIdentifierByLiteral
234 argAst->Assemble( program, allocator, normalized );
236 normalized << dynamic_cast<ASTLiteral*>(argAst)->Value.Unbox<
String>();
242 if( no != 2 || !thirdArgumentIsThrowIdentifier )
243 argAst->Assemble( program, allocator, normalized );
255 if( completeIdentifierNames )
270 bool replacedNestedExpressionIdentifierByLiteral=
false;
278 nonVerbalOp= it.Mapped();
289 replacedNestedExpressionIdentifierByLiteral=
true;
293 auto opIdx= normalized.
Length();
295 auto opLen= normalized.
Length() - opIdx;
304 bool opSpaceIfNotVerbal= !brackets
307 normalized << ( brackets
315 Argument->Assemble( program, allocator, normalized );
318 if( replacedNestedExpressionIdentifierByLiteral
321 normalized.
ShortenTo( lenBeforeArgument );
322 normalized << dynamic_cast<ASTLiteral*>(
Argument )->Value.Unbox<
String>();
332 if( op !=
Operator || isVerbalOp ) {
343 for(
integer i= opIdx ; i < opIdx + opLen ; ++i )
349 if( !opSpaceIfNotVerbal && !isalpha(op.CharAtStart() ) )
350 normalized.
Delete( opIdx + opLen, 1 );
362 checkForbiddenStrings( program.
compiler, normalized, opIdx, opLen );
371 if( op ==
A_CHAR(
"[]") ) {
373 Lhs->Assemble( program, allocator, normalized );
381 auto opIdx= normalized.
Length();
382 Rhs->Assemble( program, allocator, normalized );
407 || ( lhsBinaryPrecedence
409 || precedence > lhsBinaryPrecedence
411 && rhsBinaryPrecedence )
419 if( lhsBrackets ) normalized << normBracketOpen[bracketStringIdx];
420 Lhs->Assemble( program, allocator, normalized );
421 if( lhsBrackets ) normalized << normBracketClose[bracketStringIdx];
426 auto opIdx= normalized.
Length();
428 auto opLen= normalized.
Length() - opIdx;
443 || ( rhsBinaryPrecedence
445 || ( precedence >= rhsBinaryPrecedence )
447 && precedence < rhsBinaryPrecedence )
449 && lhsBinaryPrecedence )
455 if( rhsBrackets ) normalized << normBracketOpen[bracketStringIdx];
456 Rhs->Assemble( program, allocator, normalized );
457 if( rhsBrackets ) normalized << normBracketClose[bracketStringIdx];
463 if( op !=
Operator || isVerbalOp ) {
474 for(
integer i= opIdx ; i < opIdx + opLen ; ++i )
481 normalized.
Delete( opIdx + opLen, 1 );
482 normalized.
Delete( opIdx - 1 , 1 );
494 checkForbiddenStrings( program.
compiler, normalized, opIdx, opLen );
506 Q->Assemble( program, allocator, normalized );
517 if( brackets ) normalized << normBracketOpen[bracketStringIdx];
518 T->Assemble( program, allocator, normalized );
519 if( brackets ) normalized << normBracketClose[bracketStringIdx];
533 if( brackets ) normalized << normBracketOpen[bracketStringIdx];
534 F->Assemble( program, allocator, normalized );
535 if( brackets ) normalized << normBracketClose[bracketStringIdx];
Normalization CfgNormalization
Compilation CfgCompilation
Compilation flags.
strings::util::Token CfgNestedExpressionFunction
String CfgNestedExpressionThrowIdentifier
HashMap< MonoAllocator, String, String, alib::hash_string_ignore_case< character >, alib::equal_to_string_ignore_case< character > > AlphabeticUnaryOperatorAliases
int GetBinaryOperatorPrecedence(const String &symbol)
String CfgNestedExpressionOperator
ALIB_DLL void AssembleUnaryOp(String &op, integer idxInOriginal, integer idxInNormalized)
ALIB_DLL void AssembleFunction(AString &functionName, bool isIdentifier, int qtyArgs, integer idxInOriginal, integer idxInNormalized)
ALIB_DLL void AssembleBinaryOp(String &op, integer idxInOriginal, integer idxInNormalized)
ALIB_DLL void AssembleCondFinalize_Q(integer idxInOriginal, integer idxInNormalized)
ALIB_DLL void AssembleConstant(Box &value, integer idxInOriginal, integer idxInNormalized)
Compiler & compiler
The compiler that created this object.
ALIB_DLL void AssembleCondFinalize_F(integer idxInOriginal, integer idxInNormalized)
ALIB_DLL void AssembleCondFinalize_T(integer idxInOriginal, integer idxInNormalized)
TAString & Delete(integer regionStart, integer regionLength=MAX_LEN)
TAString & ReplaceSubstring(const TString< TChar > &src, integer regionStart, integer regionLength)
TAString & ShortenTo(integer newLength)
void DbgDisableBufferReplacementWarning()
constexpr integer Length() const
TChar CharAtStart() const
bool Equals(const TString< TChar > &rhs) const
ALIB_DLL bool Match(const String &needle)
#define ALIB_CALLER_NULLED
#define ALIB_STRING_RESETTER(astring)
@ FunctionInnerBracketSpace
@ ConditionalOpSpaceBeforeQM
@ RedundantUnaryOpBrackets
@ QuoteUnaryNestedExpressionOperatorArgument
@ ReplaceVerbalOperatorsToLowerCase
See sibling flag ReplaceVerbalOperatorsToSymbolic.
@ FunctionSpaceBeforeOpeningBracket
@ FunctionSpaceBeforeComma
@ UnaryOpSpaceIfUnaryFollows
@ RedundantRhsBracketsIfRhsIsStrongerBinaryOp
@ ConditionalOpSpaceAfterQM
@ UnaryOpSpaceIfBracketFollows
@ FunctionSpaceAfterComma
@ UnaryOpInnerBracketSpace
@ ConditionalOpSpaceBeforeColon
@ SubscriptSpaceBeforeBrackets
@ RedundantBracketsBetweenTwoUnaryOps
@ RedundantBracketsIfLhsAndRhsAreBinaryOps
@ SubscriptInnerBracketSpace
@ FunctionInnerBracketSpaceIfNoArguments
@ RedundantConditionalOpBrackets
@ NONE
All flags are cleared, may be used for testing bits.
@ RedundantBinaryOpBrackets
@ RemoveRedundantUnaryOpsOnNumberLiterals
@ ReplaceVerbalOperatorsToUpperCase
See sibling flag ReplaceVerbalOperatorsToSymbolic.
@ ConditionalOpSpaceAfterColon
@ AllowIdentifiersForNestedExpressions
@ NestedExpressionCallArgumentMismatch
@ Local
Denotes local reach.
@ On
Switch it on, switched on, etc.
strings::TEscape< character > Escape
Type alias in namespace alib.
strings::TDec< character > Dec
Type alias in namespace alib.
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
LocalString< 64 > String64
Type alias name for TLocalString<character,64>.
constexpr const String EMPTY_STRING
An empty string of the default character type.
lang::integer integer
Type alias in namespace alib.
strings::TNumberFormat< character > NumberFormat
Type alias in namespace alib.
strings::TOct< character > Oct
Type alias in namespace alib.
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
strings::THex< character > Hex
Type alias in namespace alib.
exceptions::Exception Exception
Type alias in namespace alib.
strings::TString< character > String
Type alias in namespace alib.
expressions::Compiler Compiler
Type alias in namespace alib.
strings::TBin< character > Bin
Type alias in namespace alib.
@ Identifier
An identifier.
@ UnaryOp
A unary operator.
@ BinaryOp
A binary operator.
@ TernaryOp
A ternary operator.
virtual void Assemble(Program &program, MonoAllocator &allocator, AString &normalized) override
virtual AST * Optimize(Normalization normalization) override
ASTBinaryOp(const String &op, AST *lhs, AST *rhs, integer position)
AST * Lhs
The left-hand-side expression node.
String Operator
The operator symbol.
virtual void Assemble(Program &program, MonoAllocator &allocator, AString &normalized) override
integer ColonPosition
The index of the colon in the expression string.
virtual AST * Optimize(Normalization normalization) override
String Name
The function name as parsed.
virtual void Assemble(Program &program, MonoAllocator &allocator, AString &normalized) override
ListMA< AST * > Arguments
The argument nodes.
Abstract syntax tree node representing identifiers.
String Name
The name of the identifier as parsed from the expression string.
virtual AST * Optimize(Normalization normalization) override
virtual void Assemble(Program &program, MonoAllocator &allocator, AString &normalized) override
Abstract syntax tree node representing identifiers.
@ Scientific
Float was given in scientific format.
@ Hexadecimal
Integral value was given in hexadecimal format.
@ Binary
Integral value was given in binary format.
@ Octal
Integral value was given in octal format.
Box Value
The value of the literal.
virtual void Assemble(Program &program, MonoAllocator &allocator, AString &normalized) override
NFHint Format
Format hints.
virtual AST * Optimize(Normalization normalization) override
AST * Argument
The argument node.
String Operator
The operator symbol.
virtual void Assemble(Program &program, MonoAllocator &allocator, AString &normalized) override
integer Position
Position in the original expression string.
AST()=delete
Deleted default constructor.