ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
CompilerPlugin Struct Reference

Description:


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:

  1. Identifiers (parameterless functions) might just represent constant values. (For example identifiers "True" or "Monday" do that, while "Today" returns a callback function.).
  2. The compiler provides information about whether the given parameters are constant values. In this case, a compiler plug-in may decide to evaluate the result of the function at compile-time. This is often, but not always possible and depends largely on the fact if scope information is used with the function as well.
    For more details on this topic see manual section 11.5 Optimizations.
Attention
If constant data is returned, it has to be assured, that the contents of the returned Box remains valid during the life-cycle of the expression. This is assured for all C++ fundamental types. For custom types it depends on where the constant value is received from and how boxing is performed. (By default, custom types bigger as two "words" (2 x 64/32 bits) are boxed as pointers to the assigned object.)
Field CompilationInfo::CompileTimeScope

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.

Note
Instead of deriving from this struct, it is recommended to consider convenience struct plugins::Calculus instead.
More information on this topic is given in manual section 5.4 Class Calculus

Definition at line 114 of file compilerplugin.hpp.

#include <compilerplugin.hpp>

Inheritance diagram for CompilerPlugin:
[legend]
Collaboration diagram for CompilerPlugin:
[legend]

Inner Type Index:

struct  CIAutoCast
 
struct  CIBinaryOp
 
struct  CIFunction
 
struct  CIUnaryOp
 
struct  CompilationInfo
 

Public Field Index:

CompilerCmplr
 
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)
 

Field Details:

◆ Cmplr

Compiler& Cmplr

The compiler that this plug-in is attached to.

Definition at line 124 of file compilerplugin.hpp.

◆ Name

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.

Constructor(s) / Destructor Details::

◆ CompilerPlugin()

CompilerPlugin ( const NString & name,
Compiler & compiler )
inline

Constructor.

Parameters
nameAssigned to field Name.
compilerThe compiler we will get attached to. Gets stored in field Cmplr.

Definition at line 542 of file compilerplugin.hpp.

◆ ~CompilerPlugin()

virtual ~CompilerPlugin ( )
inlinevirtual

Virtual destructor

Definition at line 550 of file compilerplugin.hpp.

Method Details:

◆ TryCompilation() [1/4]

virtual bool TryCompilation ( CIAutoCast & ciAutoCast)
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 .

Parameters
[in,out]ciAutoCastThe compilation info struct.
Returns
Implementations have to return true if the given info struct was filled, or in other words, if the plug-in chose to provide auto-cast information as requested.
This default implementation returns false to indicate that no compilation was done.

Reimplemented in Calculus, and AutoCast.

Definition at line 657 of file compilerplugin.hpp.

◆ TryCompilation() [2/4]

virtual bool TryCompilation ( CIBinaryOp & ciBinaryOp)
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 .

Parameters
[in,out]ciBinaryOpThe compilation info struct.
Returns
Implementations have to return true if the given info struct was filled, or in other words, if the plug-in chose to compile the AST-node.
This default implementation returns false to indicate that no compilation was done.

Reimplemented in Calculus, ElvisOperator, and Strings.

Definition at line 642 of file compilerplugin.hpp.

◆ TryCompilation() [3/4]

virtual bool TryCompilation ( CIFunction & ciFunction)
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

  • on the one hand allow abbreviations and letter case insensitive identifier recognitions, and
  • consequently return 'corrected' identifier names if an identifier name matched and the element got compiled.

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 .

Parameters
[in,out]ciFunctionThe compilation info struct.
Returns
Implementations have to return true if the given info struct was filled, or in other words, if the plug-in chose to compile the AST-node.
This default implementation returns false to indicate that no compilation was done.

Reimplemented in Arithmetics, Calculus, and Strings.

Definition at line 584 of file compilerplugin.hpp.

◆ TryCompilation() [4/4]

virtual bool TryCompilation ( CIUnaryOp & ciUnaryOp)
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 .

Parameters
[in,out]ciUnaryOpThe compilation info struct.
Returns
Implementations have to return true if the given info struct was filled, or in other words, if the plug-in chose to compile the AST-node.
This default implementation returns false to indicate that no compilation was done.

Reimplemented in Calculus.

Definition at line 612 of file compilerplugin.hpp.


The documentation for this struct was generated from the following file: