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