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 ExpressionVal
Definition at line 68 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) , IF_ALIB_CAMP } |
using | Plugins = std::vector<lang::PluginContainer<CompilerPlugin, CompilePriorities>::Slot> |
Alias shortcut to the type of the plug-in vector inherited from PluginContainer. | |
Public Type Index: inherited from PluginContainer< CompilerPlugin, CompilePriorities > | |
using | PluginType |
This exposes the template parameter pTPlugin to the outer world. | |
using | PrioritiesType |
This exposes the template parameter pTPlugin to the outer world. | |
Public Field Index: | |
HashMap< MonoAllocator, String, String, alib::hash_string_ignore_case< character >, alib::equal_to_string_ignore_case< character > > | AlphabeticBinaryOperatorAliases |
HashMap< MonoAllocator, String, String, alib::hash_string_ignore_case< character >, alib::equal_to_string_ignore_case< character > > | AlphabeticUnaryOperatorAliases |
HashMap< MonoAllocator, String, int > | BinaryOperators |
BuiltInPlugins | CfgBuiltInPlugins = BuiltInPlugins::ALL |
Compilation | CfgCompilation = Compilation::DEFAULT |
Compilation flags. | |
SPFormatter | CfgFormatter |
strings::util::Token | CfgNestedExpressionFunction |
String | CfgNestedExpressionOperator = A_CHAR("*") |
String | CfgNestedExpressionThrowIdentifier |
Normalization | CfgNormalization = Normalization::DEFAULT |
StringVectorMA | CfgNormalizationDisallowed |
ExpressionRepository * | Repository = nullptr |
List< MonoAllocator, String > | UnaryOperators |
Protected Field Index: | |
MonoAllocator | allocator |
HashMap< MonoAllocator, AString, Expression, std::hash< String >, std::equal_to< String > > | namedExpressions |
The map of 'named' expressions. | |
detail::Parser * | parser = nullptr |
The expression parser. | |
HashMap< MonoAllocator, TypeFunctors::Key, NAString, TypeFunctors::Hash, TypeFunctors::EqualTo > | typeMap |
The map of Type names and bit flag values. | |
Protected Field Index: inherited from PluginContainer< CompilerPlugin, CompilePriorities > | |
std::vector< Slot > | plugins |
The plug-ins we have attached in descending priority order. | |
Protected Method Index: | |
virtual ALIB_API Scope * | createCompileTimeScope (MonoAllocator &expressionAllocator) |
ALIB_API void | getOptimizedExpressionString (ExpressionVal &expression) |
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 78 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. |
IF_ALIB_CAMP | Installs plugins::DateAndTime. ) |
Definition at line 213 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 87 of file compiler.hpp.
HashMap<MonoAllocator, String, String, alib::hash_string_ignore_case<character>, alib::equal_to_string_ignore_case<character> > AlphabeticBinaryOperatorAliases |
This public map allows defining 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 142 of file compiler.hpp.
HashMap<MonoAllocator, String, String, alib::hash_string_ignore_case<character>, alib::equal_to_string_ignore_case<character> > AlphabeticUnaryOperatorAliases |
This public map allows defining 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 132 of file compiler.hpp.
HashMap<MonoAllocator, String, int> BinaryOperators |
The list of binary operators. The map stores the precedences.
To define a new binary operator, entry may be added before invoking SetupDefaults using method AddBinaryOperator.
Flag Compilation::DefaultUnaryOperators controls if method SetupDefaults adds operators resourced with enumeration DefaultBinaryOperators.
Definition at line 169 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 239 of file compiler.hpp.
Compilation CfgCompilation = Compilation::DEFAULT |
Compilation flags.
Definition at line 273 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 {lang::format::Formatter;Default;default formatter} is copied here.
Definition at line 316 of file compiler.hpp.
strings::util::Token CfgNestedExpressionFunction |
Name descriptor for nested expression function. Defaults to "Expression" with a minimum abbreviation of 4
characters, ignoring letter case.
Resourced with the key "EF"
Definition at line 264 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 256 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 270 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 277 of file compiler.hpp.
StringVectorMA 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 298 of file compiler.hpp.
|
protected |
The map of 'named' expressions.
Definition at line 102 of file compiler.hpp.
|
protected |
The expression parser.
Definition at line 90 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 322 of file compiler.hpp.
|
protected |
The map of Type names and bit flag values.
Definition at line 96 of file compiler.hpp.
List<MonoAllocator, String> UnaryOperators |
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 121 of file compiler.hpp.
Compiler | ( | ) |
Constructor. Construction usually is three steps:
Cfg
).Definition at line 108 of file compiler.cpp.
|
virtual |
Destructor.
Definition at line 151 of file compiler.cpp.
|
inline |
Adds a binary operator to this expression compiler. This method should be invoked before invoking SetupDefaults.
Operator symbols must be added only once.
symbol | The operator symbol. |
precedence | The precedence of the operator. |
Definition at line 178 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 358 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 426 of file compiler.cpp.
|
inline |
Adds an unary operator to this expression compiler. This method should be invoked before invoking SetupDefaults.
Operator symbols must be added only once.
symbol | The operator symbol. |
Definition at line 150 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 ExpressionVal::GetOriginalString, the normalized version can be retrieved after the compilation with method ExpressionVal::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 createCompileTimeScope. 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 createCompileTimeScope accordingly.
expressionString | The string to parse. |
nullptr
on failure. Definition at line 242 of file compiler.cpp.
|
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 a custom scope object.
expressionAllocator | The allocator of the expression used to allocate the compile-time scope. |
Definition at line 157 of file compiler.cpp.
|
inline |
Provides access to the internal MonoAllocator.
Definition at line 512 of file compiler.hpp.
|
inline |
Returns the precedence of given operator symbol.
symbol | The operator symbol. |
Definition at line 190 of file compiler.hpp.
|
virtual |
Returns a named expression previously defined using AddNamed. In the case that no expression with the given name 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 392 of file compiler.cpp.
|
protected |
Implements ExpressionVal::GetOptimizedString.
expression | the expression to generate an optimized string for. |
Definition at line 330 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 488 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 163 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 434 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 476 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 450 of file compiler.cpp.