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