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