This interface class represents "plug-ins" which are attachable to container class Compiler . The compiler uses the plug-ins to "compile" single nodes of abstract syntax trees, which are the intermediate, internal result of parsing expression strings.
In depth information on how this class is used is given in the User Manual And Tutorial of module ALib Expressions .
The overloaded methods TryCompilation are not abstract, but have a default implementation that return constant false
. A plug-in derived from this class needs to override only those methods that represent syntax elements of an expression, that it volunteers to take responsibility for.
The overloaded methods have one input/output parameter which is of one of the following types:
These inner types of this struct are derived from likewise inner struct CompilationInfo , which provides all output members and those input members that are common between the four descendants. In other words, the variants of the struct provide detail information on the node that is actually compiled.
When overriding one of the overloaded methods TryCompilation, it has to be checked if the custom plug-in is responsible for the permutation of given parameter types which are passed. If so, the actual 'compilation' is performed, by filling the result members of the given struct and by returning true
. Each overloaded method provides a suitable return type, which are defined as inner types of this class.
The output information comprises a sample box that determines the result type of the native C++ function that is later invoked when the expression is evaluated against a scope. Together with this type information, the pointer to the callback function itself needs to be set in the given struct.
Alternatively, if a constant result is to be returned, the pointer to the callback function might be kept nulled. In this case, the Box object that represents the result type of the callback function is used to provide the constant value, instead of just a sample value.
There are two possibilities, why a compiler plug-in might return a constant value, instead of a function:
"True"
or "Monday"
do that, while "Today"
returns a callback function.).is to be used for compile-time allocations.
The plug-ins are attached to the Compiler using a dedicated prioritization defined with (arithmetical) enum type CompilePriorities . This means, that if a higher prioritized plug-in already compiles a certain permutation of expression node type and argument types, then a lower prioritized plug-in is not even asked to do so. With this concept, ALib Expressions provides a lot of flexibility: the built-in operators and identifiers can be unplugged completely or just sparsely "superseded" in certain aspects by adding a custom CompilerPlugin with a higher priority than that of the built-in one.
Definition at line 114 of file compilerplugin.hpp.
#include <compilerplugin.hpp>
Inner Type Index: | |
struct | CIAutoCast |
struct | CIBinaryOp |
struct | CIFunction |
struct | CIUnaryOp |
struct | CompilationInfo |
Public Field Index: | |
Compiler & | Cmplr |
const NString | Name |
Public Method Index: | |
CompilerPlugin (const NString &name, Compiler &compiler) | |
virtual | ~CompilerPlugin () |
virtual bool | TryCompilation (CIAutoCast &ciAutoCast) |
virtual bool | TryCompilation (CIBinaryOp &ciBinaryOp) |
virtual bool | TryCompilation (CIFunction &ciFunction) |
virtual bool | TryCompilation (CIUnaryOp &ciUnaryOp) |
Compiler& Cmplr |
The compiler that this plug-in is attached to.
Definition at line 124 of file compilerplugin.hpp.
const NString Name |
The name of the plug-in. This is used with exception information and program listings which are available in debug-compilations of the library.
Definition at line 121 of file compilerplugin.hpp.
|
inline |
Constructor.
name | Assigned to field Name. |
compiler | The compiler we will get attached to. Gets stored in field Cmplr. |
Definition at line 542 of file compilerplugin.hpp.
|
inlinevirtual |
|
inlinevirtual |
Used to provide information to the compiler for casting types.
For details on how this method is overridden, consult the documentation of the input/output parameter type CIAutoCast .
[in,out] | ciAutoCast | The compilation info struct. |
true
if the given info struct was filled, or in other words, if the plug-in chose to provide auto-cast information as requested.false
to indicate that no compilation was done. Reimplemented in Calculus, and AutoCast.
Definition at line 657 of file compilerplugin.hpp.
|
inlinevirtual |
Used to compile binary operators parsed from expressions.
On success, this method has to provide a native C++ callback function together with a sample box that specifies its return type. Both are to be stored in output parameter result .
Alternatively, depending on fields LhsIsConst
and RhsIsConst
of the given compilation info struct, a constant value might be returned or, as a third alternative, field NonConstArgIsResult
may be set to true
, if an "identity" operator is detected For details of the input and output parameters of the function, see struct CIBinaryOp .
The implementation might allow alias operators. If such alias is found, the used original operator should be returned in/out parameter operator .
Such "corrected" operators will appear in the normalized expression strings returned by Expression::GetNormalizedString , in case flag Normalization::ReplaceAliasOperators is set in field Compiler::CfgNormalization .
[in,out] | ciBinaryOp | The compilation info struct. |
true
if the given info struct was filled, or in other words, if the plug-in chose to compile the AST-node.false
to indicate that no compilation was done. Reimplemented in Calculus, ElvisOperator, and Strings.
Definition at line 642 of file compilerplugin.hpp.
|
inlinevirtual |
Used to compile identifiers (parameterless functions ) and functions parsed from expression strings.
The function name is given as an in/out parameter. Implementations might (should) choose to
Such corrected names will appear in the normalized expression strings returned by Expression::GetNormalizedString , in case flag Normalization::ReplaceFunctionNames is set in field Compiler::CfgNormalization .
On success, this method has to provide a (native C++) callback function that accepts start and end iterators of boxed arguments which are of the same time as proposed by the corresponding iterators found in parameter ciFunction , along with the return type of that function.
Alternatively, if a constant identifier is compiled or if all parameters are known to be constant at compile-time, a constant value might be returned. For details of the input and output parameters of the function, see struct CIFunction .
[in,out] | ciFunction | The compilation info struct. |
true
if the given info struct was filled, or in other words, if the plug-in chose to compile the AST-node.false
to indicate that no compilation was done. Reimplemented in Arithmetics, Calculus, and Strings.
Definition at line 584 of file compilerplugin.hpp.
|
inlinevirtual |
Used to compile unary operators parsed from expressions.
On success, this method has to provide a native C++ callback function together with a sample box that specifies its return type. Both are to be stored in output parameter result .
Alternatively, if field ArgIsConst
of the given compilation info struct is true
, a constant value might be returned. For details of the input and output parameters of the function, see struct CIUnaryOp .
The implementation might allow alias operators. If such alias is found, the used original operator should be returned in/out parameter operator .
Such "corrected" operators will appear in the normalized expression strings returned by Expression::GetNormalizedString , in case flag Normalization::ReplaceAliasOperators is set in field Compiler::CfgNormalization .
[in,out] | ciUnaryOp | The compilation info struct. |
true
if the given info struct was filled, or in other words, if the plug-in chose to compile the AST-node.false
to indicate that no compilation was done. Reimplemented in Calculus.
Definition at line 612 of file compilerplugin.hpp.