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