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