This is a central class of library module ALib Expressions used to compile expression strings.
For information about general use and features of this class consult the ALib Expressions User Manual.
class Expression
Definition at line 89 of file compiler.hpp.
#include <compiler.hpp>
Public Type Index: | |
enum class | BuiltInPlugins { NONE = 0 , ALL = -1 , ElvisOperator = (1 << 1) , AutoCast = (1 << 2) , Arithmetics = (1 << 3) , Math = (1 << 4) , Strings = (1 << 5) , ALIB_IF_CAMP } |
using | Plugins = std::vector<lang::PluginContainer<CompilerPlugin, CompilePriorities>::Slot> |
Public Type Index: inherited from PluginContainer< CompilerPlugin, CompilePriorities > | |
using | TPlugin |
using | TPriorities |
Public Field Index: | |
HashMap< String, String, alib::hash_string_ignore_case< character >, alib::equal_to_string_ignore_case< character > > | AlphabeticBinaryOperatorAliases |
HashMap< String, String, alib::hash_string_ignore_case< character >, alib::equal_to_string_ignore_case< character > > | AlphabeticUnaryOperatorAliases |
HashMap< String, int > | BinaryOperators |
BuiltInPlugins | CfgBuiltInPlugins = BuiltInPlugins::ALL |
Compilation | CfgCompilation = Compilation::DEFAULT |
SPFormatter | CfgFormatter |
strings::util::Token | CfgNestedExpressionFunction |
String | CfgNestedExpressionOperator = A_CHAR("*") |
String | CfgNestedExpressionThrowIdentifier |
Normalization | CfgNormalization = Normalization::DEFAULT |
std::vector< AString > | CfgNormalizationDisallowed |
ExpressionRepository * | Repository = nullptr |
List< String > | UnaryOperators |
using Plugins = std::vector<lang::PluginContainer<CompilerPlugin, CompilePriorities>::Slot> |
Alias shortcut to the type of the plug-in vector inherited from PluginContainer.
Definition at line 99 of file compiler.hpp.
|
strong |
Bitwise enum class that lists the plug-ins built-into ALib expression library. This bitfield is used with method SetupDefaults, which reads the flags set in CfgBuiltInPlugins and installs the plug-ins accordingly.
Enumerator | |
---|---|
NONE | Installs no plug-in. |
ALL | Installs all plug-ins. |
ElvisOperator | Installs plugins::ElvisOperator . |
AutoCast | Installs plugins::AutoCast . |
Arithmetics | Installs plugins::Arithmetics . |
Math | Installs plugins::Math . |
Strings | Installs plugins::Strings . |
ALIB_IF_CAMP | Installs plugins::DateAndTime . ) |
Definition at line 247 of file compiler.hpp.
|
protected |
Memory for used for permanent allocations during the set-up phase. Later it is also used for temporary allocations during compilation and reset to its state after setup.
Definition at line 108 of file compiler.hpp.
HashMap<String, String, alib::hash_string_ignore_case<character>, alib::equal_to_string_ignore_case<character> > AlphabeticBinaryOperatorAliases |
This public map allows to define alias names for binary operators. Such names must consist only of alphabetic characters.
Flag Compilation::DefaultAlphabeticOperatorAliases controls if method SetupDefaults adds the aliases defined with resourced data records of enumeration DefaultAlphabeticBinaryOperatorAliases to this map.
Definition at line 166 of file compiler.hpp.
HashMap<String, String, alib::hash_string_ignore_case<character>, alib::equal_to_string_ignore_case<character> > AlphabeticUnaryOperatorAliases |
This public map allows to define alias names for unary operators. Such names must consist only of alphabetic characters.
Flag Compilation::DefaultAlphabeticOperatorAliases controls if method SetupDefaults adds the aliases defined with resourced data records of enumeration DefaultAlphabeticUnaryOperatorAliases to this map.
Definition at line 154 of file compiler.hpp.
The list of binary operators. The map stores the precedences.
To define a new binary operator, entry may be added prior to invoking SetupDefaults using method AddBinaryOperator.
Flag Compilation::DefaultUnaryOperators controls if method SetupDefaults adds operators resourced with enumeration DefaultBinaryOperators .
Definition at line 197 of file compiler.hpp.
BuiltInPlugins CfgBuiltInPlugins = BuiltInPlugins::ALL |
Bitfield that defines the built-in compiler plug-ins that are created and inserted by method SetupDefaults.
Defaults to BuiltInPlugins::ALL
Responsibility::KeepWithSender
. Definition at line 275 of file compiler.hpp.
Compilation CfgCompilation = Compilation::DEFAULT |
Compilation flags.
Definition at line 317 of file compiler.hpp.
SPFormatter CfgFormatter |
Formatter used throughout all phases of the life-cycle of an expression:
For the latter two, a copy of the pointer is provided through field Scope::Formatter . It is important not to change the formatting parameters in between the creation of the compile-time scope and the creation of the evaluation-time scope. If done, the expression results could differ when optimizations during compilation are performed.
In the constructor of this class, a clone of the ALib default formatter is set here.
Definition at line 366 of file compiler.hpp.
strings::util::Token CfgNestedExpressionFunction |
Name descriptor for nested expression function. Defaults to "Expression" with minimum abbreviation of 4
characters, ignoring letter case.
Resourced with key "EF"
Definition at line 304 of file compiler.hpp.
The operator defined in this public field is used to address nested expressions. The operator used can be customized to any defined (!) unary operator.
To disable nested expression parsing, change to empty or nulled string.
Defaults to "*"
.
Definition at line 294 of file compiler.hpp.
String CfgNestedExpressionThrowIdentifier |
Keyword used with optional third parameter of expression function. Defaults to "throw"
. Resourced with key "EF"
Definition at line 312 of file compiler.hpp.
Normalization CfgNormalization = Normalization::DEFAULT |
Flags that allow to tweak the result of the normalization of the originally parsed expression string.
Definition at line 323 of file compiler.hpp.
std::vector<AString> CfgNormalizationDisallowed |
This list is used with the normalization of the expression string. It contains strings, which are not allowed to appear in normalized expression strings.
In the current library version, such strings might occur only if flags Normalization::UnaryOpSpace , Normalization::UnaryOpSpaceIfUnaryFollows or Normalization::BinaryOpSpaces are not set. As a sample see expression:
1 - -2
If no space is written after binary operators, this expression would be normalized to: 1–2
While this just looks irritating to a homo sapiens, it would be even illegal if well known C++ operator –
was supported. Therefore, string "--"
is added to this map in method SetupDefaults, which forces the normalization logic to insert a space, even if spaces are otherwise configured to be omitted.
Definition at line 346 of file compiler.hpp.
|
protected |
The map of 'named' expressions.
Definition at line 121 of file compiler.hpp.
|
protected |
The expression parser.
Definition at line 111 of file compiler.hpp.
ExpressionRepository* Repository = nullptr |
Optional pointer to a default or custom implementation of a repository that provides expression strings for named nested expressions.
Definition at line 374 of file compiler.hpp.
|
protected |
The map of Type names and bit flag values.
Definition at line 116 of file compiler.hpp.
The list of unary operators. To define a new unary operator, method AddUnaryOperator should be used. In debug-builds, the methods asserts that no double insertions are performed.
Custom insertions may be performed before or after invoking SetupDefaults, but before a first expression is compiled.
Flag Compilation::DefaultUnaryOperators controls if method SetupDefaults adds operators resourced with enumeration DefaultUnaryOperators .
Definition at line 142 of file compiler.hpp.
Compiler | ( | ) |
Constructor. Construction usually is three steps:
Cfg
).Definition at line 97 of file compiler.cpp.
|
virtual |
|
inline |
Adds a binary operator to this expression compiler. This method should be invoked prior to prior to invoking SetupDefaults.
Operator symbols must be added only once.
symbol | The operator symbol. |
precedence | The precedence of the operator. |
Definition at line 208 of file compiler.hpp.
Compiles the given expressionString and adds a compiled expression to the map of named expressions.
name | The name used to map the expression. |
expressionString | The string to parse. If nulled, entry name is removed (if existing). |
true
if an expression with the same named existed and was replaced. Otherwise returns false
. Definition at line 357 of file compiler.cpp.
Registers a (custom) type name with this compiler. The name will be used to display a type name to end-users, for example, when malformed expressions throw an exception.
The built-in types get registered in the constructor of the compiler, where the type names are read from the ResourcePool of static library object EXPRESSIONS .
It is recommended that custom plug-ins invoke this method in their constructor.
sample | A sample value of the type, which is implicitly boxed when passed. |
name | The name of the type. |
Definition at line 424 of file compiler.cpp.
|
inline |
Adds an unary operator to this expression compiler. This method should be invoked prior to prior to invoking SetupDefaults.
Operator symbols must be added only once.
symbol | The operator symbol. |
Definition at line 176 of file compiler.hpp.
|
virtual |
Firstly, this method parses the given expression string thereby builds an intermediate internal abstract syntax tree . This tree is then translated into a detail::Program which evaluates the expression in respect to a Scope when executed.
For building the program, attached CompilerPlugin instances are queried.
During this process, a normalized version of the input string is always created. While the original input string can be accessed with Expression::GetOriginalString , the normalized version can be retrieved after the compilation with method Expression::GetNormalizedString . During compilation, the normalization rules (flags) stored in field CfgNormalization are applied.
For compilation, a compile-time scope object is created by calling virtual method getCompileTimeScope. If not overwritten, a standard scope object is used. If custom callbacks exists that are invokable at compile-time (to perform program optimization) and that at the same time rely on custom mechanics provided with a custom scope class, then a custom, derived version of this class has to be used that overrides method getCompileTimeScope accordingly.
expressionString | The string to parse. |
nullptr
on failure. Definition at line 251 of file compiler.cpp.
|
inline |
Returns the precedence of given operator symbol.
symbol | The operator symbol. |
Definition at line 222 of file compiler.hpp.
|
protectedvirtual |
This virtual method is called by Compile to create the scope object used for allocations that are made for intermediate results at compile-time.
Compile-time allocations occur when expression terms that use purely constant arguments are evaluated at compile-time. Such optimization consequently leads to the invocation of callback functions at compile-time.
If now, custom callback functions rely on custom allocation mechanics, provided with custom scope types, then this method has to be overridden to return such custom scope object.
Definition at line 151 of file compiler.cpp.
|
virtual |
Returns a named expression previously defined using AddNamed. In the case that no expression with the given named was defined and optional interface Repository is set, then this interface is used to retrieve a corresponding expression string and compile the named expression 'on the fly'.
name | The name of the expression. |
Definition at line 389 of file compiler.cpp.
|
protected |
Implements Expression::GetOptimizedString .
expression | the expression to generate an optimized string for. |
Definition at line 332 of file compiler.cpp.
|
inline |
Removes a named expression. This is just a shortcut to AddNamed, providing a nulled string for parameter expressionString .
name | The name of the expression to be removed. |
true
if an expression with the given name existed and was removed. Otherwise returns false
. Definition at line 540 of file compiler.hpp.
void SetupDefaults | ( | ) |
Completes construction according to configuration options provided with fields prefixed Cfg
. In detail, the following actions are performed:
Definition at line 160 of file compiler.cpp.
Returns the name of the type of the boxed value. Note that custom types need to be registered with method AddType.
If given argument box is a void box, the returned string is "NONE".
box | The box to receive the type name for. |
Definition at line 432 of file compiler.cpp.
ALIB_WARNINGS_RESTORE void WriteFunctionSignature | ( | ArgIterator | begin, |
ArgIterator | end, | ||
AString & | target ) |
Writes the signature of an argument list to a string buffer.
begin | Iterator to the first argument. |
end | End-iterator. |
target | The string to write into. |
Definition at line 474 of file compiler.cpp.
ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE void WriteFunctionSignature | ( | Box ** | boxArray, |
size_t | qty, | ||
AString & | target ) |
Writes the signature of a function (as for example found in Calculus::FunctionEntry ) to a string buffer.
boxArray | The start of the array of pointers. |
qty | The array's length. |
target | The string to write into. |
Definition at line 448 of file compiler.cpp.