ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
program.inl
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of module \alib_expressions of the \aliblong.
4///
5/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8ALIB_EXPORT namespace alib { namespace expressions {
9
10/// This is the detail namespace of #alib::expressions implementing the abstract syntax tree,
11/// the expression parser, the expression program and the virtual machine to execute the program
12/// with expression evaluation.
13namespace detail {
14
15
16//==================================================================================================
17/// This class represents a program that is "run on" the \alib{expressions;detail::VirtualMachine}
18/// to evaluate an expression.
19///
20/// ## Friends ##
21/// class \alib{expressions;detail::VirtualMachine}
22//==================================================================================================
23class Program : public ProgramBase
24{
25 //################################################################################################
26 // Public fields
27 //################################################################################################
28 public:
29 /// The compiler that created this object.
31
32 /// The expression that this program evaluates.
34
35
36 //################################################################################################
37 // Internal fields
38 //################################################################################################
39 protected:
40 /// Shortcut.
42
43 /// The array of commands.
45
46 /// The number of commands.
48
49 /// List of compile-time identified nested expressions. Using the shared pointers, it is
50 /// ensured that the expressions do not get deleted until this program is.
52
53 /// Counter of the number of optimization made during program assembly.
55
56 /// Data needed during compilation. An instance of this object is allocated
57 /// in the temporary compile-time monotonic allocator.
59 {
60 /// The intermediate program listing. Commands are collected here during
61 /// compilation. Only when finalized, the result is copied into the vector that
62 /// the outer class program inherits from, which uses the non-temporary monotonic
63 /// allocator.
65
66 /// Used with compilation. Stores the positions of current result types while adding new
67 /// commands.
69
70 /// Compile-time information on conditional operator jump positions.
72 {
73 #if !DOXYGEN
74 ConditionalInfo( VM::PC q, VM::PC t, int f)
75 : QJumpPos (q)
76 , TJumpPos (t)
77 , ConstFlags(f) {}
78 #endif
79
80 VM::PC QJumpPos; ///< The position of the lhs result.
81 VM::PC TJumpPos; ///< The position of the jump command between T and F.
82 int ConstFlags; ///< Two bits: Bit 1 determines whether Q was constant and
83 ///< bit 0 stores the value of Q (if constant)
84 };
85
86 /// Stores the positions of current results while adding new commands.
87 /// The third value is used for optimizing constant conditionals out.
89
90 /// Needed during compilation. Collects information from plug-ins to create meaningful
91 /// messages.
93
94 /// Constructor.<br>
95 /// The given allocator is used exclusively during compilation.
96 /// Its memory is cleared (respectively reset to a previous state) after the
97 /// compilation completed. The program is created in this allocator. Only when
98 /// compilation is finished (and after all optimizations have been performed)
99 /// it is copied to the compile-time scope's allocator.<br>
100 /// (Refers to object \alib{expressions;Compiler::allocator}.)
101 ///
102 /// @param compileTimeAllocator The allocator to use temporarily during compilation.
103 CompileStorage( MonoAllocator& compileTimeAllocator )
104 : Assembly (compileTimeAllocator)
105 , ResultStack (compileTimeAllocator)
106 , ConditionalStack (compileTimeAllocator)
107 , FunctionsWithNonMatchingArguments(compileTimeAllocator) {
108 Assembly .reserve(30);
109 ResultStack .reserve(20);
110 ConditionalStack.reserve(5);
111 }
112
113 };
114
115 /// Set of data needed during compilation and deleted afterwards. Also, this field indicates
116 /// that compilation is "suppressed", which is used when normalized optimized expression
117 /// strings are generated from de-compiled programs.
119
120
121 //################################################################################################
122 // Constructor/destructor
123 //############################################### P ##############################################
124 public:
125 /// Constructor.
126 ///
127 /// Prepares the assembly if \p{compileTimeAlloc} is given. If it is \c nullptr, then no
128 /// program is assembled. This option is used for creating normalized expression strings from
129 /// de-compiled, optimized programs.
130 ///
131 /// @param pCompiler Stored in field #compiler.
132 /// @param pExpression Stored in field #expression.
133 /// @param ctAlloc A temporary allocator valid until assembly of the program
134 /// finished.<br>
135 /// If \c nullptr, no assembly is performed in later invocations
136 /// of assemble methods.
137 /// This option is used for the generation of normalized, optimized
138 /// expression strings. from reversely created ASTs.
140 Program( Compiler& pCompiler, ExpressionVal& pExpression, MonoAllocator* ctAlloc );
141
142 /// Destructor.
144
145 //################################################################################################
146 // Overrides
147 //################################################################################################
148 /// Returns the result type of the program.
149 /// @return The program's result type.
151 const Box& ResultType() const { return commands[commandsCount-1].ResultType; }
152
153
154 /// Returns the number of \alib{expressions::detail;VirtualMachine::Command} that the
155 /// program encompasses.
156 /// @return The program's length.
157 integer Length() const { return commandsCount; }
158
159 /// Returns the command at the given program counter \p{pc}.
160 /// @param pc The program counter of the command to get.
161 /// @return A reference to the requested command.
162 VM::Command& At( VM::PC pc ) { return commands[pc]; }
163
164 /// @return The number of optimizations or \c -1 if optimizations were not activated during
165 /// program assembly.
166 int CtdOptimizations() const { return qtyOptimizations; }
167
168 /// Runs the program using the virtual machine.
169 ///
170 /// @param scope The evaluation scope.
171 /// @return The result value.
172 Box Run(Scope& scope) { return VirtualMachine::Run(*this, scope); }
173
174
175 //################################################################################################
176 // Assemble methods
177 //################################################################################################
178
179 /// Has to be invoked to finalizes the program after.
180 /// No further invocations of assemble methods must be invoked after a call to this method.
182 void AssembleFinalize();
183
184
185 /// Add a command that produces a constant value. Used with literals.
186 /// @param value The value to produce.
187 /// @param idxInOriginal The index of the operator in the expression string.
188 /// @param idxInNormalized The index of the operator in the normalized expression
189 /// string.
191 void AssembleConstant( Box& value,
192 integer idxInOriginal, integer idxInNormalized );
193
194 /// Add a command that invokes a native function.
195 /// @param functionName The name of the function.
196 /// Used to query the corresponding native C++ callback
197 /// function from the compiler plug-ins.
198 /// @param idxInOriginal The index of the operator in the expression string.
199 /// @param idxInNormalized The index of the operator in the normalized expression
200 /// string.
201 /// @param isIdentifier To be \c true, if no parentheses were given. In this case,
202 /// was given as an 'identifier', what
203 /// means that no brackets '()' had been added.
204 /// @param qtyArgs The number of function args. If negative, this indicates
205 /// that the function name was given as an 'identifier', what
206 /// means that no brackets '()' had been added.
208 void AssembleFunction( AString& functionName, bool isIdentifier, int qtyArgs,
209 integer idxInOriginal, integer idxInNormalized );
210
211 /// Add a command that invokes a native function that implements an unary operator.
212 /// @param op The operator to add a command for.
213 /// @param idxInOriginal The index of the operator in the expression string.
214 /// @param idxInNormalized The index of the operator in the normalized expression string.
216 void AssembleUnaryOp( String& op, integer idxInOriginal, integer idxInNormalized );
217
218 /// Add a command that invokes a native function that implements a binary operator.
219 /// @param op The operator to add a command for.
220 /// @param idxInOriginal The index of the operator in the expression string.
221 /// @param idxInNormalized The index of the operator in the normalized expression string.
223 void AssembleBinaryOp( String& op, integer idxInOriginal, integer idxInNormalized );
224
225 /// To be called after the AST for \c Q was assembled. Adds an "jump on false" statement,
226 /// unless it is detected that \c Q is constant. In this case, only one of the subsequent
227 /// sub-expressions is assembled.
228 ///
229 /// Has to be followed by assembly of \c T, followed by
230 /// #AssembleCondFinalize_T and furthermore assembly of \c F, followed by
231 /// #AssembleCondFinalize_F.
232 ///
233 /// @param idxInOriginal The index of the operator in the expression string.
234 /// @param idxInNormalized The index of the operator in the normalized expression string.
236 void AssembleCondFinalize_Q( integer idxInOriginal, integer idxInNormalized );
237
238 /// End of ternary \c T expression. Jumps to end of \c f.
239 /// @param idxInOriginal The index of the operator in the expression string.
240 /// @param idxInNormalized The index of the operator in the normalized expression string.
242 void AssembleCondFinalize_T( integer idxInOriginal, integer idxInNormalized );
243
244 /// Finalizes a previously started conditional expression.
245 /// @param idxInOriginal The index of the operator in the expression string.
246 /// @param idxInNormalized The index of the operator in the normalized expression string.
248 void AssembleCondFinalize_F( integer idxInOriginal, integer idxInNormalized );
249
250 //################################################################################################
251 // Internals used during compilation
252 //################################################################################################
253 protected:
254 /// Collects \p{qty} types from the result stack and stores them, respectively the constant
255 /// values in field \doxlinkproblem{structalib_1_1expressions_1_1Scope.html;af1f402e9cac81ef3ad0a982590344472;Scope::Stack;expressions::Scope::Stack}
256 /// stack of field \alib{expressions;ExpressionVal::ctScope}.
257 /// @param qty The number of types to collect.
258 /// @return \c true if all arguments collected were constants.
260 bool collectArgs( integer qty );
261
262};
263
264}}} // namespace [alib::expressions::detail]
Base class exported by the main module ALib.Expressions.H for technical reasons.
Definition compiler.inl:506
ExpressionVal & expression
The expression that this program evaluates.
Definition program.inl:33
ALIB_DLL void AssembleUnaryOp(String &op, integer idxInOriginal, integer idxInNormalized)
Definition program.cpp:381
int qtyOptimizations
Counter of the number of optimization made during program assembly.
Definition program.inl:54
VirtualMachine VM
Shortcut.
Definition program.inl:41
ALIB_DLL const Box & ResultType() const
Definition program.inl:151
ALIB_DLL void AssembleFunction(AString &functionName, bool isIdentifier, int qtyArgs, integer idxInOriginal, integer idxInNormalized)
Definition program.cpp:205
ALIB_DLL void AssembleBinaryOp(String &op, integer idxInOriginal, integer idxInNormalized)
Definition program.cpp:552
ALIB_DLL void AssembleCondFinalize_Q(integer idxInOriginal, integer idxInNormalized)
Definition program.cpp:742
ALIB_DLL Program(Compiler &pCompiler, ExpressionVal &pExpression, MonoAllocator *ctAlloc)
Definition program.cpp:47
VM::Command & At(VM::PC pc)
Definition program.inl:162
ALIB_DLL void AssembleConstant(Box &value, integer idxInOriginal, integer idxInNormalized)
Definition program.cpp:191
Compiler & compiler
The compiler that created this object.
Definition program.inl:30
StdVectorMA< Expression > ctNestedExpressions
Definition program.inl:51
ALIB_DLL void AssembleCondFinalize_F(integer idxInOriginal, integer idxInNormalized)
Definition program.cpp:792
ALIB_DLL ~Program()
Destructor.
Definition program.cpp:55
ALIB_DLL bool collectArgs(integer qty)
Definition program.cpp:170
integer commandsCount
The number of commands.
Definition program.inl:47
VM::Command * commands
The array of commands.
Definition program.inl:44
ALIB_DLL void AssembleCondFinalize_T(integer idxInOriginal, integer idxInNormalized)
Definition program.cpp:771
#define ALIB_DLL
Definition alib.inl:503
#define ALIB_EXPORT
Definition alib.inl:497
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
std::vector< T, StdMA< T > > StdVectorMA
Type alias in namespace alib.
containers::List< T, MonoAllocator, TRecycling > ListMA
Type alias in namespace alib.
Definition list.inl:693
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
boxing::Box Box
Type alias in namespace alib.
Definition box.inl:1149
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2189
Compile-time information on conditional operator jump positions.
Definition program.inl:72
VM::PC TJumpPos
The position of the jump command between T and F.
Definition program.inl:81
StdVectorMA< ConditionalInfo > ConditionalStack
Definition program.inl:88
CompileStorage(MonoAllocator &compileTimeAllocator)
Definition program.inl:103
StdVectorMA< VirtualMachine::Command * > Assembly
Definition program.inl:64
static ALIB_DLL alib::Box Run(Program &program, Scope &scope)
integer PC
Type definition for a program counter.