ALib C++ Library
Library Version: 2402 R1
Documentation generated by doxygen
Loading...
Searching...
No Matches
ast.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_AST
9#define HPP_ALIB_EXPRESSIONS_DETAIL_AST
10
11#if !defined (HPP_ALIB_EXPRESSIONS_EXPRESSION)
13#endif
14
15ALIB_ASSERT_MODULE(EXPRESSIONS)
16
17#if !defined (HPP_ALIB_MONOMEM_LIST)
18# include "alib/monomem/list.hpp"
19#endif
20
21namespace alib { namespace expressions { namespace detail {
22
23class Program;
24
25/**
26 * Base class for nodes of abstract syntax trees of module \alib_expressions.
27 * Note that AST objects (and their data) are allocated in a \alib{monomem;MonoAllocator} and
28 * hence have empty destructors.
29 */
30struct AST
31{
32 /** The type of node. */
33 enum class Types
34 {
35 Literal ,
36 Identifier ,
37 Function ,
38 UnaryOp ,
39 BinaryOp ,
40 TernaryOp ,
41 };
42
43 /** Type of derived this AST node */
45
46 /** Position in original expression string. */
48
49 /** Deleted default constructor. */
50 AST() = delete;
51
52 /**
53 * Constructor
54 * @param type The node type.
55 * @param position The index of this AST in the expression string.
56 */
57 AST( Types type, integer position )
58 : NodeType(type)
59 , Position(position)
60 {}
61
62 /** Virtual destructor. */
63 virtual ~AST() {}
64
65 /**
66 * Recursively compiles nested nodes and invokes one of the add-methods of program for itself.
67 * @param program The program to be compiled.
68 * @param allocator An allocator usable for temporary objects.
69 * Its memory is invalid after the compilation process.
70 * @param[out] normalized The normalized string, built during recursive compilation of the AST.
71 */
72 virtual void Assemble( Program& program, MonoAllocator& allocator, AString & normalized ) = 0;
73
74 /**
75 * Recursively walks through the tree and performs optimizations, dependent on given flags.
76 *
77 * As of today, the only optimization performed in this AST itself is to combine nested unary
78 * '+' and '-' operators on literals.
79 * @param normalization The compiler flags denoting the normalization settings.
80 * @return A potentially replaced AST or itself.
81 */
82 virtual AST* Optimize( Normalization normalization ) = 0;
83};
84
85
86/**
87 * Abstract syntax tree node representing identifiers.
88 */
89struct ASTLiteral : public AST
90{
91 /**
92 * This is a list of flags that store information obbout the format of the literal given
93 * in the expression string. This information is used with the generation of a normalized
94 * version of the literal.
95 */
96 enum class NFHint
97 {
98 NONE , ///< No hint.
99 Scientific , ///< Float was given in scientific format
100 Hexadecimal , ///< Integral value was given in hexadecimal format.
101 Octal , ///< Integral value was given in octal format.
102 Binary , ///< Integral value was given in binary format.
103 };
104
105 Box Value; ///< The value of the literal.
106 NFHint Format; ///< The value of the literal.
107
108 /**
109 * Constructs a string literal.
110 * @param string The value of the literal.
111 * @param position The index of this AST in the expression string.
112 */
113 ASTLiteral( const String& string, integer position )
114 : AST(Types::Literal, position)
115 , Value( string )
116 , Format(NFHint::NONE)
117 {
118 }
119
120 /**
121 * Constructs an integer literal.
122 * @param value The value of the literal.
123 * @param position The index of this AST in the expression string.
124 * @param hint A hint about the format that expression string used to express the literal.
125 */
126 ASTLiteral( integer value, integer position, NFHint hint= NFHint::NONE )
127 : AST(Types::Literal, position)
128 , Format(hint)
129 {
130 Value= value;
131 }
132
133 /**
134 * Constructs a floating point literal.
135 * @param value The value of the literal.
136 * @param position The index of this AST in the expression string.
137 * @param hint Optional information about the number format that was detected while parsing.
138 */
139 ASTLiteral( double value, integer position, NFHint hint= NFHint::NONE )
140 : AST(Types::Literal, position)
141 , Format(hint)
142 {
143 Value= value;
144 }
145
146 /**
147 * Implements abstract method.
148 * @param program The program to be compiled.
149 * @param allocator An allocator usable for temporary objects.
150 * @param[out] normalized The normalized string, built during recursive compilation of the AST.
151 */
152 virtual void Assemble( Program& program , MonoAllocator& allocator,
153 AString & normalized ) override;
154
155 /**
156 * Implements abstract method.
157 * @param normalization The compiler flags denoting the normalization settings.
158 * @return A potentially replaced AST or itself.
159 */
160 virtual AST* Optimize( Normalization normalization ) override;
161};
162
163
164/**
165 * Abstract syntax tree node representing identifiers.
166 */
167struct ASTIdentifier : public AST
168{
169 String Name; ///< The name of the identifier as parsed from the expression string.
170
171 /**
172 * Constructor providing all fields.
173 * @param name The name of the identifier.
174 * @param position The index of this AST in the expression string.
175 */
176 explicit
177 ASTIdentifier( const String& name, integer position )
178 : AST(Types::Identifier, position)
179 , Name(name)
180 {}
181
182 /**
183 * Implements abstract method.
184 * @param program The program to be compiled.
185 * @param allocator An allocator usable for temporary objects.
186 * @param[out] normalized The normalized string, built during recursive compilation of the AST.
187 */
188 virtual void Assemble( Program& program , MonoAllocator& allocator,
189 AString & normalized ) override;
190
191 /**
192 * Implements abstract method.
193 * @param normalization The compiler flags denoting the normalization settings.
194 * @return A potentially replaced AST or itself.
195 */
196 virtual AST* Optimize( Normalization normalization ) override;
197};
198
199/**
200 * Abstract syntax tree node representing a function call.
201 */
202struct ASTFunction : public AST
203{
204 String Name; ///< The function name as parsed.
205 List<AST*> Arguments; ///< The argument nodes.
206
207 /**
208 * Constructor providing name, but not arguments, yet.
209 * @param name The name of the function
210 * @param position The index of this AST in the expression string.
211 * @param pAllocator Allocator used to clone the given \p{name} and for storing arguments.
212 */
213 explicit
214 ASTFunction(const String name, integer position, MonoAllocator &pAllocator )
215 : AST(Types::Function, position)
216 , Name ( pAllocator.EmplaceString(name) )
217 , Arguments(&pAllocator )
218 {}
219
220 /**
221 * Virtual destructor.
222 */
223 virtual ~ASTFunction() override
224 {}
225
226 /**
227 * Implements abstract method.
228 * @param program The program to be compiled.
229 * @param allocator An allocator usable for temporary objects.
230 * @param[out] normalized The normalized string, built during recursive compilation of the AST.
231 */
232 virtual void Assemble( Program& program , MonoAllocator& allocator,
233 AString & normalized ) override;
234
235 /**
236 * Implements abstract method.
237 * @param normalization The compiler flags denoting the normalization settings.
238 * @return A potentially replaced AST or itself.
239 */
240 virtual AST* Optimize( Normalization normalization ) override;
241};
242
243
244/**
245 * Abstract syntax tree node representing unary operators.
246 */
247struct ASTUnaryOp : public AST
248{
249 String Operator; ///< The operator symbol.
250 AST* Argument; ///< The argument node.
251
252 /**
253 * Constructor providing all fields.
254 * @param op The operator symbol.
255 * @param argument The argument of the operator.
256 * @param position The index of this AST in the expression string.
257 */
258 explicit
259 ASTUnaryOp(const String& op, AST* argument, integer position )
260 : AST(Types::UnaryOp, position)
261 , Operator(op), Argument(argument)
262 {}
263
264 /**
265 * Virtual destructor.
266 */
267 virtual ~ASTUnaryOp() override
268 {}
269
270 /**
271 * Implements abstract method.
272 * @param program The program to be compiled.
273 * @param allocator An allocator usable for temporary objects.
274 * @param[out] normalized The normalized string, built during recursive compilation of the AST.
275 */
276 virtual void Assemble( Program& program, MonoAllocator& allocator, AString & normalized ) override;
277
278 /**
279 * Implements abstract method.
280 * @param normalization The compiler flags denoting the normalization settings.
281 * @return A potentially replaced AST or itself.
282 */
283 virtual AST* Optimize( Normalization normalization ) override;
284};
285
286/**
287 * Abstract syntax tree node representing binary operators.
288 */
289struct ASTBinaryOp : public AST
290{
291 String Operator; ///< The operator symbol.
292 AST* Lhs; ///< The left-hand-side expression node.
293 AST* Rhs; ///< The right-hand-side expression node.
294 /**
295 * Constructor providing all fields.
296 * @param lhs Left-hand side node.
297 * @param rhs Right-hand side node.
298 * @param op The operator symbol.
299 * @param position The index of this AST in the expression string.
300 */
301 explicit
302 ASTBinaryOp( const String& op, AST* lhs, AST* rhs, integer position )
303 : AST(Types::BinaryOp, position)
304 , Operator(op), Lhs(lhs), Rhs(rhs)
305 {}
306
307 /**
308 * Virtual destructor.
309 */
310 virtual ~ASTBinaryOp() override
311 {}
312
313 /**
314 * Implements abstract method.
315 * @param program The program to be compiled.
316 * @param allocator An allocator usable for temporary objects.
317 * @param[out] normalized The normalized string, built during recursive compilation of the AST.
318 */
319 virtual void Assemble( Program& program, MonoAllocator& allocator, AString & normalized ) override;
320
321 /**
322 * Implements abstract method.
323 * @param normalization The compiler flags denoting the normalization settings.
324 * @return A potentially replaced AST or itself.
325 */
326 virtual AST* Optimize( Normalization normalization ) override;
327};
328
329/**
330 * Abstract syntax tree node representing ternary operator <c>Q ? T : F</c>.
331 */
332struct ASTConditional : public AST
333{
334 AST* Q; ///< The question.
335 AST* T; ///< The true-result.
336 AST* F; ///< The false-result.
337 integer ColonPosition; ///< The index of the colon in the expression string.
338
339 /**
340 * Constructor providing all fields.
341 * @param q The question.
342 * @param t The true-result.
343 * @param f The false-result.
344 * @param position The index of the question mark in the expression string.
345 * @param colonPosition The index of the colon in the expression string.
346 */
347 explicit
348 ASTConditional(AST* q, AST* t, AST* f, integer position, integer colonPosition)
349 : AST(Types::TernaryOp, position)
350 , Q(q), T(t), F(f)
351 , ColonPosition(colonPosition)
352 {}
353
354 /**
355 * Virtual destructor.
356 */
357 virtual ~ASTConditional() override
358 {}
359
360 /**
361 * Implements abstract method.
362 * @param program The program to be compiled.
363 * @param allocator An allocator usable for temporary objects.
364 * @param[out] normalized The normalized string, built during recursive compilation of the AST.
365 */
366 virtual void Assemble( Program& program, MonoAllocator& allocator, AString & normalized ) override;
367
368 /**
369 * Implements abstract method.
370 * @param normalization The compiler flags denoting the normalization settings.
371 * @return A potentially replaced AST or itself.
372 */
373 virtual AST* Optimize( Normalization normalization ) override;
374};
375
376}}} // namespace [alib::expressions::detail]
377
378#endif // HPP_ALIB_EXPRESSIONS_DETAIL_AST
#define ALIB_ASSERT_MODULE(modulename)
Definition alib.hpp:190
Definition alib.cpp:57
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:286
String Operator
The operator symbol.
Definition ast.hpp:291
ASTBinaryOp(const String &op, AST *lhs, AST *rhs, integer position)
Definition ast.hpp:302
virtual AST * Optimize(Normalization normalization) override
Definition ast.cpp:72
virtual void Assemble(Program &program, MonoAllocator &allocator, AString &normalized) override
Definition ast.cpp:408
AST * Rhs
The right-hand-side expression node.
Definition ast.hpp:293
AST * Lhs
The left-hand-side expression node.
Definition ast.hpp:292
virtual ~ASTBinaryOp() override
Definition ast.hpp:310
integer ColonPosition
The index of the colon in the expression string.
Definition ast.hpp:337
ASTConditional(AST *q, AST *t, AST *f, integer position, integer colonPosition)
Definition ast.hpp:348
virtual AST * Optimize(Normalization normalization) override
Definition ast.cpp:79
virtual void Assemble(Program &program, MonoAllocator &allocator, AString &normalized) override
Definition ast.cpp:547
ASTFunction(const String name, integer position, MonoAllocator &pAllocator)
Definition ast.hpp:214
String Name
The function name as parsed.
Definition ast.hpp:204
virtual AST * Optimize(Normalization normalization) override
Definition ast.cpp:65
virtual void Assemble(Program &program, MonoAllocator &allocator, AString &normalized) override
Definition ast.cpp:192
List< AST * > Arguments
The argument nodes.
Definition ast.hpp:205
virtual ~ASTFunction() override
Definition ast.hpp:223
ASTIdentifier(const String &name, integer position)
Definition ast.hpp:177
String Name
The name of the identifier as parsed from the expression string.
Definition ast.hpp:169
virtual AST * Optimize(Normalization normalization) override
Definition ast.cpp:64
virtual void Assemble(Program &program, MonoAllocator &allocator, AString &normalized) override
Definition ast.cpp:177
ASTLiteral(integer value, integer position, NFHint hint=NFHint::NONE)
Definition ast.hpp:126
Box Value
The value of the literal.
Definition ast.hpp:105
@ Scientific
Float was given in scientific format.
@ Hexadecimal
Integral value was given in hexadecimal format.
@ Binary
Integral value was given in binary format.
@ Octal
Integral value was given in octal format.
virtual AST * Optimize(Normalization normalization) override
Definition ast.cpp:63
NFHint Format
The value of the literal.
Definition ast.hpp:106
virtual void Assemble(Program &program, MonoAllocator &allocator, AString &normalized) override
Definition ast.cpp:126
ASTLiteral(double value, integer position, NFHint hint=NFHint::NONE)
Definition ast.hpp:139
ASTLiteral(const String &string, integer position)
Definition ast.hpp:113
String Operator
The operator symbol.
Definition ast.hpp:249
virtual ~ASTUnaryOp() override
Definition ast.hpp:267
AST * Argument
The argument node.
Definition ast.hpp:250
virtual AST * Optimize(Normalization normalization) override
Definition ast.cpp:89
virtual void Assemble(Program &program, MonoAllocator &allocator, AString &normalized) override
Definition ast.cpp:290
ASTUnaryOp(const String &op, AST *argument, integer position)
Definition ast.hpp:259
AST(Types type, integer position)
Definition ast.hpp:57
virtual AST * Optimize(Normalization normalization)=0
virtual void Assemble(Program &program, MonoAllocator &allocator, AString &normalized)=0