ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
program.hpp
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-2024 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8#ifndef HPP_ALIB_EXPRESSIONS_DETAIL_PROGRAM
9#define HPP_ALIB_EXPRESSIONS_DETAIL_PROGRAM
10#pragma once
16
17namespace alib { namespace expressions {
18
19/// This is the detail namespace of #alib::expressions implementing the abstract syntax tree,
20/// the expression parser, the expression program and the virtual machine to execute the program
21/// with expression evaluation.
22namespace detail {
23
24
25//==================================================================================================
26/// This class represents a program that is "run on" the \alib{expressions;detail::VirtualMachine}
27/// to evaluate an expression.
28///
29/// ## Friends ##
30/// class \alib{expressions;detail::VirtualMachine}
31//==================================================================================================
33{
34 // #############################################################################################
35 // Public fields
36 // #############################################################################################
37 public:
38 /// The compiler that created this object.
40
41 /// The expression that this program evaluates.
43
44
45 // #############################################################################################
46 // Internal fields
47 // #############################################################################################
48 protected:
49 /// Shortcut.
51
52 /// The array of commands.
54
55 /// The number of commands.
57
58 /// List of compile-time identified nested expressions. Using the shared pointers, it is
59 /// ensured that the expressions do not get deleted until this program is.
61
62 /// Counter of the number of optimization made during program assembly.
64
65 /// Data needed during compilation. An instance of this object is allocated
66 /// in the temporary compile-time monotonic allocator.
68 {
69 /// The intermediate program listing. Commands are collected here during
70 /// compilation. Only when finalized, the result is copied into the vector that
71 /// the outer class program inherits from, which uses the non-temporary monotonic
72 /// allocator.
74
75 /// Used with compilation. Stores the positions of current result types while adding new
76 /// commands.
78
79 /// Compile-time information on conditional operator jump positions.
81 {
82 #if !DOXYGEN
83 ConditionalInfo( VM::PC q, VM::PC t, int f)
84 : QJumpPos (q)
85 , TJumpPos (t)
86 , ConstFlags(f)
87 {}
88 #endif
89
90 VM::PC QJumpPos; ///< The position of the lhs result.
91 VM::PC TJumpPos; ///< The position of the jump command between T and F.
92 int ConstFlags; ///< Two bits: Bit 1 determines whether Q was constant and
93 ///< bit 0 stores the value of Q (if constant)
94 };
95
96 /// Stores the positions of current results while adding new commands.
97 /// The third value is used for optimizing constant conditionals out.
99
100 /// Needed during compilation. Collects information from plug-ins to create meaningful
101 /// messages.
103
104 /// Constructor.<br>
105 /// The given allocator is used exclusively during compilation.
106 /// Its memory is cleared (respectively reset to a previous state) after the
107 /// compilation completed. The program is created in this allocator. Only when
108 /// compilation is finished (and after all optimizations have been performed)
109 /// it is copied to the compile-time scope's allocator.<br>
110 /// (Refers to object \alib{expressions;Compiler::allocator}.)
111 ///
112 /// @param compileTimeAllocator The allocator to use temporarily during compilation.
113 CompileStorage( MonoAllocator& compileTimeAllocator )
114 : Assembly (compileTimeAllocator)
115 , ResultStack (compileTimeAllocator)
116 , ConditionalStack (compileTimeAllocator)
117 , FunctionsWithNonMatchingArguments(compileTimeAllocator)
118 {
119 Assembly .reserve(30);
120 ResultStack .reserve(20);
121 ConditionalStack.reserve(5);
122 }
123
124 };
125
126 /// Set of data needed during compilation and deleted afterwards. Also, this field indicates
127 /// that compilation is "suppressed", which is used when normalized optimized expression
128 /// strings are generated from de-compiled programs.
130
131
132 // #############################################################################################
133 // Constructor/destructor
134 // ##################################################P###########################################
135 public:
136 //==========================================================================================
137 /// Constructor.
138 ///
139 /// Prepares the assembly if \p{compileTimeAlloc} is given. If it is \c nullptr, then no
140 /// program is assembled. This option is used for creating normalized expression strings from
141 /// de-compiled, optimized programs.
142 ///
143 /// @param pCompiler Stored in field #compiler.
144 /// @param pExpression Stored in field #expression.
145 /// @param ctAlloc A temporary allocator valid until assembly of the program
146 /// finished.<br>
147 /// If \c nullptr, no assembly is performed in later invocations
148 /// of assemble methods.
149 /// This option is used for the generation of normalized, optimized
150 /// expression strings. from reversely created ASTs.
151 //==========================================================================================
153 Program( Compiler& pCompiler, ExpressionVal& pExpression, MonoAllocator* ctAlloc );
154
155 //==========================================================================================
156 /// Destructor.
157 //==========================================================================================
159
160 // #############################################################################################
161 // Overrides
162 // #############################################################################################
163 //==========================================================================================
164 /// Returns the result type of the program.
165 /// @return The program's result type.
166 //==========================================================================================
174
175
176 //==========================================================================================
177 /// Returns the number of \alib{expressions::detail;VirtualMachine::Command} that the
178 /// program encompasses.
179 /// @return The program's length.
180 //==========================================================================================
182 {
183 return commandsCount;
184 }
185
186 //==========================================================================================
187 /// Returns the command at the given program counter \p{pc}.
188 /// @param pc The program counter of the command to get.
189 /// @return A reference to the requested command.
190 //==========================================================================================
197
198 //==========================================================================================
199 /// Implementation of abstract interface method.
200 ///
201 /// @return The number of optimizations or \c -1 if optimizations were not activated during
202 /// program assembly.
203 //==========================================================================================
205 {
206 return qtyOptimizations;
207 }
208
209 //==========================================================================================
210 /// Runs the program using the virtual machine.
211 ///
212 /// @param scope The evaluation scope.
213 /// @return The result value.
214 //==========================================================================================
215 Box Run(Scope& scope)
216 {
217 return VirtualMachine::Run(*this, scope);
218 }
219
220
221 // #############################################################################################
222 // Assemble methods
223 // #############################################################################################
224
225 //==========================================================================================
226 /// Has to be invoked to finalizes the program after.
227 /// No further invocations of assemble methods must be invoked after a call to this method.
228 //==========================================================================================
230 void AssembleFinalize();
231
232
233 //==========================================================================================
234 /// Add a command that produces a constant value. Used with literals.
235 /// @param value The value to produce.
236 /// @param idxInOriginal The index of the operator in the expression string.
237 /// @param idxInNormalized The index of the operator in the normalized expression
238 /// string.
239 //==========================================================================================
241 void AssembleConstant( Box& value,
242 integer idxInOriginal, integer idxInNormalized );
243
244 //==========================================================================================
245 /// Add a command that invokes a native function.
246 /// @param functionName The name of the function.
247 /// Used to query the corresponding native C++ callback
248 /// function from the compiler plug-ins.
249 /// @param idxInOriginal The index of the operator in the expression string.
250 /// @param idxInNormalized The index of the operator in the normalized expression
251 /// string.
252 /// @param isIdentifier To be \c true, if no parentheses were given. In this case,
253 /// was given as an 'identifier', what
254 /// means that no brackets '()' had been added.
255 /// @param qtyArgs The number of function args. If negative, this indicates
256 /// that the function name was given as an 'identifier', what
257 /// means that no brackets '()' had been added.
258 //==========================================================================================
260 void AssembleFunction( AString& functionName, bool isIdentifier, int qtyArgs,
261 integer idxInOriginal, integer idxInNormalized );
262
263 //==========================================================================================
264 /// Add a command that invokes a native function that implements an unary operator.
265 /// @param op The operator to add a command for.
266 /// @param idxInOriginal The index of the operator in the expression string.
267 /// @param idxInNormalized The index of the operator in the normalized expression string.
268 //==========================================================================================
270 void AssembleUnaryOp( String& op, integer idxInOriginal, integer idxInNormalized );
271
272 //==========================================================================================
273 /// Add a command that invokes a native function that implements a binary operator.
274 /// @param op The operator to add a command for.
275 /// @param idxInOriginal The index of the operator in the expression string.
276 /// @param idxInNormalized The index of the operator in the normalized expression string.
277 //==========================================================================================
279 void AssembleBinaryOp( String& op, integer idxInOriginal, integer idxInNormalized );
280
281 //==========================================================================================
282 /// To be called after the AST for \c Q was assembled. Adds an "jump on false" statement,
283 /// unless it is detected that \c Q is constant. In this case, only one of the subsequent
284 /// sub-expressions is assembled.
285 ///
286 /// Has to be followed by assembly of \c T, followed by
287 /// #AssembleCondFinalize_T and furthermore assembly of \c F, followed by
288 /// #AssembleCondFinalize_F.
289 ///
290 /// @param idxInOriginal The index of the operator in the expression string.
291 /// @param idxInNormalized The index of the operator in the normalized expression string.
292 //==========================================================================================
294 void AssembleCondFinalize_Q( integer idxInOriginal, integer idxInNormalized );
295
296 //==========================================================================================
297 /// End of ternary \c T expression. Jumps to end of \c f.
298 /// @param idxInOriginal The index of the operator in the expression string.
299 /// @param idxInNormalized The index of the operator in the normalized expression string.
300 //==========================================================================================
302 void AssembleCondFinalize_T( integer idxInOriginal, integer idxInNormalized );
303
304 //==========================================================================================
305 /// Finalizes a previously started conditional expression.
306 /// @param idxInOriginal The index of the operator in the expression string.
307 /// @param idxInNormalized The index of the operator in the normalized expression string.
308 //==========================================================================================
310 void AssembleCondFinalize_F( integer idxInOriginal, integer idxInNormalized );
311
312 // #############################################################################################
313 // Internals used during compilation
314 // #############################################################################################
315 protected:
316 /// Collects \p{qty} types from the result stack and stores them, respectively the constant
317 /// values in field \doxlinkproblem{structalib_1_1expressions_1_1Scope.html;af1f402e9cac81ef3ad0a982590344472;Scope::Stack;expressions::Scope::Stack}
318 /// stack of field \alib{expressions;ExpressionVal::ctScope}.
319 /// @param qty The number of types to collect.
320 /// @return \c true if all arguments collected were constants.
322 bool collectArgs( integer qty );
323
324};
325
326}}} // namespace [alib::expressions::detail]
327
328
329#endif // HPP_ALIB_EXPRESSIONS_DETAIL_PROGRAM
330
ALIB_API void AssembleCondFinalize_T(integer idxInOriginal, integer idxInNormalized)
Definition program.cpp:865
ALIB_API ~Program()
Destructor.
Definition program.cpp:43
ALIB_API bool collectArgs(integer qty)
Definition program.cpp:214
ALIB_API void AssembleCondFinalize_F(integer idxInOriginal, integer idxInNormalized)
Definition program.cpp:887
ALIB_API void AssembleUnaryOp(String &op, integer idxInOriginal, integer idxInNormalized)
Definition program.cpp:443
integer commandsCount
The number of commands.
Definition program.hpp:56
Compiler & compiler
The compiler that created this object.
Definition program.hpp:39
int qtyOptimizations
Counter of the number of optimization made during program assembly.
Definition program.hpp:63
ALIB_API Program(Compiler &pCompiler, ExpressionVal &pExpression, MonoAllocator *ctAlloc)
Definition program.cpp:34
ALIB_API const Box & ResultType() const
Definition program.hpp:168
ALIB_API void AssembleConstant(Box &value, integer idxInOriginal, integer idxInNormalized)
Definition program.cpp:237
ALIB_API void AssembleCondFinalize_Q(integer idxInOriginal, integer idxInNormalized)
Definition program.cpp:834
StdVectorMono< Expression > ctNestedExpressions
Definition program.hpp:60
VM::Command * commands
The array of commands.
Definition program.hpp:53
ALIB_API void AssembleFunction(AString &functionName, bool isIdentifier, int qtyArgs, integer idxInOriginal, integer idxInNormalized)
Definition program.cpp:251
ExpressionVal & expression
The expression that this program evaluates.
Definition program.hpp:42
VM::Command & At(VM::PC pc)
Definition program.hpp:191
ALIB_API void AssembleBinaryOp(String &op, integer idxInOriginal, integer idxInNormalized)
Definition program.cpp:627
#define ALIB_WARNINGS_RESTORE
Definition alib.hpp:849
#define ALIB_API
Definition alib.hpp:639
#define ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE
Definition alib.hpp:760
Definition alib.cpp:69
std::vector< T, SCAMono< T > > StdVectorMono
Type alias in namespace alib.
Definition stdvector.hpp:21
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:273
Compile-time information on conditional operator jump positions.
Definition program.hpp:81
VM::PC TJumpPos
The position of the jump command between T and F.
Definition program.hpp:91
CompileStorage(MonoAllocator &compileTimeAllocator)
Definition program.hpp:113
List< MonoAllocator, String > FunctionsWithNonMatchingArguments
Definition program.hpp:102
StdVectorMono< ConditionalInfo > ConditionalStack
Definition program.hpp:98
StdVectorMono< VirtualMachine::Command * > Assembly
Definition program.hpp:73
integer PC
Type definition for a program counter.
static ALIB_API alib::Box Run(Program &program, Scope &scope)