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