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