ALib C++ Framework
by
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
expression.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 #"mainpage_license".
7//==================================================================================================
8
9ALIB_EXPORT namespace alib { namespace expressions {
10namespace detail {
11class ProgramBase;
13}
14
15// forwards
16class Compiler;
17struct Scope;
18
19//==================================================================================================
20/// This is a central class of library module \alib_expressions_nl representing compiled,
21/// evaluable expressions. Instances of this type are always embedded in the automatic pointer
22/// #"Expression", which are received with the method #"Compiler::Compile;2*".
23///
24/// For information about general use and features of this class consult the
25/// #"alib_mod_expressions;ALib Expressions User Manual".
26///
27/// ## Friends ##
28/// class #"Compiler"
29/// class #"detail::Program;*"
30//==================================================================================================
32{
33 #if !DOXYGEN
34 // The compiler that compiles us.
35 friend class Compiler;
36
37 // The program that evaluates us.
38 friend class detail::ProgramBase;
39
40 // The virtual machine our program runs on.
41 friend struct detail::VirtualMachineBase;
42 #endif
43
44 protected:
45 /// The allocator, provided with construction.
46 /// This usually is the 'self-contained' instance of type #"Expression".
47 /// This allocator is forwarded to the #"ctScope" and
48 /// #"DbgLock;locked" after compilation.
50
51 /// Compile-time scope object. Used to allocate constant program object copies.
52 /// Also passed to the compiler plug-ins during compilation to add pre-calculated
53 /// data.
55
56 /// The name of the expression (if named, otherwise resourced, usually "ANONYMOUS" ).
58
59 /// The compiled expression program.
61
62 /// The original source string of the expression.
64
65 /// The normalized string as a result of compilation.
67
68 /// The normalized string generated on request out of optimized expression program.
70
71 public:
72 #if ALIB_DEBUG
73 /// Provides the time needed to parse the expression into an abstract syntax tree.
74 ///
75 /// Note: This field is available only with debug-builds of the library.
76 Ticks::Duration DbgParseTime;
77
78 /// Provides the time needed to parse the expression into an abstract syntax tree.
79 ///
80 /// Note: This field is available only with debug-builds of the library.
81 Ticks::Duration DbgAssemblyTime;
82
83 /// Provides the time needed for the last evaluation of the expression.
84 ///
85 /// Note: This field is available only with debug-builds of the library.
86 Ticks::Duration DbgLastEvaluationTime;
87 #endif
88
89 /// Constructor.
90 /// Expressions are created using #"Compiler::Compile;*" and thus, this
91 /// constructor is available for the compiler only.
92 /// \note
93 /// The common way to assert accessibility would be to make this constructor protected
94 /// and make class \b Compiler a friend. This is not possible, as this type is
95 /// to be constructed by container type #"SharedVal". Therefore, an
96 /// unused parameter of a protected type has to be passed, which can be created only
97 /// by friend \b Compiler.
98 ///
99 /// @param allocator The allocator to use. Usually this is the self-contained allocator
100 /// of type #"Expression"
101 /// @param sourceString The original string that is to be compiled.
102 /// @param pCTScope The compile-time scope.
104 const String& sourceString,
105 Scope* pCTScope );
106
107 /// Destructor.
109
110 /// The name of the expression. A name is only available if the expression was created with
111 /// #"Compiler::AddNamed;*". This might be 'automatically' done when nested
112 /// expressions get compiled and the compiler supports retrieval of expression strings by
113 /// name from some custom location (or built-in \alib variable mechanics).
114 ///
115 /// Otherwise, the name is \b "ANONYMOUS", which is a resourced string of key
116 /// \c "ANON_EXPR_NAME".
117 ///
118 /// @return The expression's name.
120 String Name();
121
122 /// Evaluates the expression by executing the compiled \p{program}.
123 ///
124 /// @return The result of this evaluation of this expression node.
126
127 /// Evaluates the expression by executing the compiled \p{program}.
128 ///
129 /// With debug-builds of this library, \alib_assertions may be raised.
130 /// Usually this indicates that a native callback function returned a value of erroneous
131 /// type, which usually are caused by erroneous compiler plug-ins, respectively the native
132 /// callback functions that those provide.
133 ///
134 /// The assertion will most probably give detailed information.
135 ///
136 /// @param scope The evaluation scope.
137 /// @return The result of this evaluation of this expression node.
138 ALIB_DLL Box Evaluate(Scope& scope);
139
140
141 /// Returns the originally given expression string.
142 ///
143 /// @return The original expression string.
145
146 /// Returns a normalized version of the original expression string.
147 ///
148 /// The result of normalization can be tweaked with the flags in field configuration field
149 /// #"Compiler::CfgNormalization;*". In any case, unnecessary (multiple)
150 /// whitespaces and brackets are removed. Consult the documentation of enumeration
151 /// #"Normalization" for details of the options.
152 ///
153 /// It is guaranteed that the normalized version of the expression string is parsable and
154 /// leads to the identical evaluation program as the original expression string.
155 ///
156 /// Software might choose to write back normalized expressions, for example, into
157 /// configuration files.
158 ///
159 /// \note
160 /// This method does not perform the normalization, but returns a normalized version of the
161 /// parsed expression string, which was created with the compilation of the expression.
162 /// A normalized string is always created.
163 ///
164 /// @return The normalized expression string.
166
167 /// Returns a normalized expression string reflecting an optimized version of this
168 /// expression.
169 /// The number of optimizations performed during compilation of the expression can be
170 /// received by invoking
171 /// #"Program::CtdOptimizations"
172 /// on the program returned by #"GetProgram". If this is \c 0, then the expression string
173 /// returned here matches the normalized expression string received with
174 /// #"GetNormalizedString".
175 ///
176 /// \note
177 /// On the first invocation, the string is generated once. For this, an abstract syntax
178 /// tree is created by decompiling the optimized program. This in turn is assembled
179 /// back to a program (by omitting the generation of commands and without invoking
180 /// on compiler plug-ins, etc.) which generates the normalized expression string from the
181 /// AST.
182 ///
183 /// @return The expression string requested.
186
187
188 /// Returns the program that evaluates the expression.
189 /// @return The result of this evaluation of this expression node.
191
192 /// Returns the number of #"VirtualMachine::Command"s that the program encompasses.
193 /// @return The program's length.
195
196 /// Tests whether the expression always results to the same value (and hence is independent from
197 /// the expression scope. In this case, the end-user might be notified about that fact, because
198 /// probably the expression is incorrect.<br>
199 /// The implementation of this method checks, if
200 /// - the expression-program has the length of \c 1, and
201 /// - the single command is <b>CONSTANT</b>.
202 ///
203 /// \attention
204 /// As explained in the chapter #"alib_expressions_details_optimizations", the compiler is
205 /// not enabled to perform all optimizations, which might in theory be possible. Thus, the
206 /// rule for this method's result is: If \c true is returned it is sure that the expression
207 /// is constant, if \c false is returned, it might still be!
208 /// @return The best possible guess about whether this expression is constant.
209 ALIB_DLL bool IsConstant();
210
211 /// @return The number of optimizations or \c -1 if optimizations were not activated during
212 /// program assembly.
214};
215
216
217/// Implements #"TSharedMonoVal" with type #"ExpressionVal".
218/// Therefore, the relevant documentation is found with class #"ExpressionVal",
219/// which can be accessed through this type using <c>operator->()</c>.
220///
221/// The result of combining both is an automatic pointer to a \b %ExpressionVal that is
222/// "self-contained" in the first buffer of a #"MonoAllocator" together with the
223/// allocator itself.
224/// The expression is deleted and all associated memory is freed when the last copy of the pointer
225/// goes out of scope.
226///
227/// \note
228/// With the documentation of type \b %TSharedMonoVal,
229/// #"alib_contmono_smv_naming;two naming schemes" are suggested.
230/// Here, the second scheme is used because the method #"Compiler::Compile;*"
231/// will always return this encapsulated object. Expressions are shared by definition.
232struct Expression : monomem::TSharedMonoVal<ExpressionVal, HeapAllocator, void>
233{
234 protected:
235 #if !DOXYGEN
236 // The compiler builds this type which by design does not expose a constructor.
237 friend class Compiler;
238 #endif
239
240 #if DOXYGEN
241 /// Forbid this method by making it protected.
242 /// @tparam TArgs The argument types used for re-constructing \p{T}.
243 /// @param args The arguments to re-construct the instance of \p{T}.
244 template<typename... TArgs>
245 void Reset( TArgs&&... args );
246 #else
248 #endif
249
250 /// Constructor.
251 /// Calls the constructor of parent \b TSharedMonoVal and then invokes
252 /// #"TSharedMonoVal::ConstructT;*" passing the mono allocator that the
253 /// parent creates this instance in.
254 /// @param initialBufferSizeInKB The initial size of memory buffers.
255 /// Passed to the allocator given with parent class
256 /// #"TSharedMonoVal".<br>
257 /// @param bufferGrowthInPercent Optional growth factor in percent, applied to the buffer size
258 /// with each next buffer allocation.
259 Expression( size_t initialBufferSizeInKB, unsigned bufferGrowthInPercent )
260 : TSharedMonoVal(initialBufferSizeInKB, bufferGrowthInPercent ) {}
261
262 public:
263 /// Constructs an empty instance, hence a cleared automatic pointer.
264 Expression() =default;
265
266 /// Constructs an empty instance from \c std::nullptr.
267 /// This constructor is necessary to allow assignment of \c nullptr to values of this type,
268 /// which clears the automatic pointer.
269 Expression(std::nullptr_t) noexcept {}
270
271}; // struct Expression
272
273
274#if ALIB_DEBUG
275/// Lists a virtual machine program.
276///
277/// Note: This method is available only with debug-builds of the library.
278/// @param expression The expression to list the virtual machine program for.
279/// @return The program listing.
281AString DbgList(Expression expression);
282#endif
283
284} // namespace alib[::expressions]
285
286/// Type alias in namespace \b alib.
288
289} // namespace [alib]
290
291
292namespace alib::strings {
293#if DOXYGEN
294namespace APPENDABLES {
295#endif
296//==================================================================================================
297/// Specialization of functor #"AppendableTraits" for type
298/// #"ExpressionVal".
299//==================================================================================================
301{
302 /// Appends the result of #"ExpressionVal::GetNormalizedString;*" to the \p{target}.
303 /// @param target The \b AString that method \b Append was invoked on.
304 /// @param src The expression to append.
306 { target << src.GetNormalizedString(); }
307};
308
309//==================================================================================================
310/// Specialization of functor #"AppendableTraits" for type
311/// #"Expression".
312//==================================================================================================
314{
315 /// Appends the result of #"ExpressionVal::GetNormalizedString;*" to the
316 /// \p{target}.
317 ///
318 /// @param target The \b AString that method \b Append was invoked on.
319 /// @param src The expression to append.
320 void operator()( AString& target, const expressions::Expression& src )
321 { target << src->GetNormalizedString(); }
322};
323
324#if DOXYGEN
325}
326#endif
327} // namespace [alib::strings]
#define ALIB_DLL
Definition alib.inl:573
#define ALIB_EXPORT
Definition alib.inl:562
detail::ProgramBase * GetProgram()
AString optimizedString
The normalized string generated on request out of optimized expression program.
detail::ProgramBase * program
The compiled expression program.
String originalString
The original source string of the expression.
ExpressionVal(MonoAllocator &allocator, const String &sourceString, Scope *pCTScope)
String name
The name of the expression (if named, otherwise resourced, usually "ANONYMOUS" ).
Ticks::Duration DbgLastEvaluationTime
AString normalizedString
The normalized string as a result of compilation.
Base class exported by the main module #"F;ALib.Expressions.H" for technical reasons.
Definition compiler.inl:506
void Reset(TArgs &&... args)
TSharedMonoVal(HeapAllocator &allocator, size_t initialBufferSizeInKB, unsigned bufferGrowthInPercent)
AString DbgList(Expression expression)
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
boxing::Box Box
Type alias in namespace alib.
Definition box.inl:1135
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2172
expressions::Expression Expression
Type alias in namespace alib.
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
characters::character character
Type alias in namespace alib.
void Reset(TArgs &&... args)
Expression(size_t initialBufferSizeInKB, unsigned bufferGrowthInPercent)
Expression(std::nullptr_t) noexcept
Expression()=default
Constructs an empty instance, hence a cleared automatic pointer.
Base class exported by the main module #"F;ALib.Expressions.H" for technical reasons.
Definition compiler.inl:525