AI 24/25 Project Software
Documentation for the AI 24/25 course programming project software
Loading...
Searching...
No Matches
decorated_abstract_syntax_tree.h
1#ifndef PARSER_DECORATED_ABSTRACT_SYNTAX_TREE_H
2#define PARSER_DECORATED_ABSTRACT_SYNTAX_TREE_H
3
4#include "downward/cli/plugins/plugin.h"
5
6#include "downward/utils/logging.h"
7
8#include <any>
9#include <memory>
10#include <string>
11#include <vector>
12
13namespace plugins {
14class Options;
15}
16
17namespace downward::cli::parser {
18
19// TODO: if we can get rid of lazy values, this class could be moved to the cc
20// file.
21class ConstructContext : public utils::Context {
22 std::unordered_map<std::string, std::any> variables;
23
24public:
25 void set_variable(const std::string& name, const std::any& value);
26 void remove_variable(const std::string& name);
27 bool has_variable(const std::string& name) const;
28 std::any get_variable(const std::string& name) const;
29};
30
31class DecoratedASTNode {
32public:
33 virtual ~DecoratedASTNode() = default;
34 std::any construct() const;
35 virtual std::any construct(ConstructContext& context) const = 0;
36 virtual void dump(std::string indent = "+") const = 0;
37
38 // TODO: This is here only for the iterated search. Once we switch to
39 // builders, we won't need it any more.
40 virtual std::unique_ptr<DecoratedASTNode> clone() const = 0;
41 virtual std::shared_ptr<DecoratedASTNode> clone_shared() const = 0;
42};
43using DecoratedASTNodePtr = std::unique_ptr<DecoratedASTNode>;
44
45class LazyValue {
46 ConstructContext context;
47 DecoratedASTNodePtr node;
48 std::any construct_any() const;
49
50public:
51 LazyValue(const DecoratedASTNode& node, const ConstructContext& context);
52 LazyValue(const LazyValue& other);
53
54 template <typename T>
55 T construct() const
56 {
57 std::any constructed = construct_any();
58 return plugins::OptionsAnyCaster<T>::cast(constructed);
59 }
60
61 std::vector<LazyValue> construct_lazy_list();
62};
63
64class FunctionArgument {
65 std::string key;
66 DecoratedASTNodePtr value;
67
68 // TODO: This is here only for the iterated search. Once we switch to
69 // builders, we won't need it any more.
70 bool lazy_construction;
71
72public:
73 FunctionArgument(
74 const std::string& key,
75 DecoratedASTNodePtr value,
76 bool lazy_construction);
77
78 std::string get_key() const;
79 const DecoratedASTNode& get_value() const;
80 void dump(const std::string& indent) const;
81
82 // TODO: This is here only for the iterated search. Once we switch to
83 // builders, we won't need it any more.
84 bool is_lazily_constructed() const;
85 FunctionArgument(const FunctionArgument& other);
86};
87
88class DecoratedLetNode : public DecoratedASTNode {
89 std::string variable_name;
90 DecoratedASTNodePtr variable_definition;
91 DecoratedASTNodePtr nested_value;
92
93public:
94 DecoratedLetNode(
95 const std::string& variable_name,
96 DecoratedASTNodePtr variable_definition,
97 DecoratedASTNodePtr nested_value);
98
99 std::any construct(ConstructContext& context) const override;
100 void dump(std::string indent) const override;
101
102 // TODO: once we get rid of lazy construction, this should no longer be
103 // necessary.
104 virtual std::unique_ptr<DecoratedASTNode> clone() const override;
105 virtual std::shared_ptr<DecoratedASTNode> clone_shared() const override;
106 DecoratedLetNode(const DecoratedLetNode& other);
107};
108
109class DecoratedFunctionCallNode : public DecoratedASTNode {
110 std::shared_ptr<const plugins::Feature> feature;
111 std::vector<FunctionArgument> arguments;
112 std::string unparsed_config;
113
114public:
115 DecoratedFunctionCallNode(
116 const std::shared_ptr<const plugins::Feature>& feature,
117 std::vector<FunctionArgument>&& arguments,
118 const std::string& unparsed_config);
119
120 std::any construct(ConstructContext& context) const override;
121 void dump(std::string indent) const override;
122
123 // TODO: once we get rid of lazy construction, this should no longer be
124 // necessary.
125 virtual std::unique_ptr<DecoratedASTNode> clone() const override;
126 virtual std::shared_ptr<DecoratedASTNode> clone_shared() const override;
127 DecoratedFunctionCallNode(const DecoratedFunctionCallNode& other);
128};
129
130class DecoratedListNode : public DecoratedASTNode {
131 std::vector<DecoratedASTNodePtr> elements;
132
133public:
134 DecoratedListNode(std::vector<DecoratedASTNodePtr>&& elements);
135
136 std::any construct(ConstructContext& context) const override;
137 void dump(std::string indent) const override;
138
139 // TODO: once we get rid of lazy construction, this should no longer be
140 // necessary.
141 virtual std::unique_ptr<DecoratedASTNode> clone() const override;
142 virtual std::shared_ptr<DecoratedASTNode> clone_shared() const override;
143 DecoratedListNode(const DecoratedListNode& other);
144 const std::vector<DecoratedASTNodePtr>& get_elements() const
145 {
146 return elements;
147 }
148};
149
150class VariableNode : public DecoratedASTNode {
151 std::string name;
152
153public:
154 VariableNode(const std::string& name);
155
156 std::any construct(ConstructContext& context) const override;
157 void dump(std::string indent) const override;
158
159 // TODO: once we get rid of lazy construction, this should no longer be
160 // necessary.
161 virtual std::unique_ptr<DecoratedASTNode> clone() const override;
162 virtual std::shared_ptr<DecoratedASTNode> clone_shared() const override;
163 VariableNode(const VariableNode& other);
164};
165
166class BoolLiteralNode : public DecoratedASTNode {
167 std::string value;
168
169public:
170 BoolLiteralNode(const std::string& value);
171
172 std::any construct(ConstructContext& context) const override;
173 void dump(std::string indent) const override;
174
175 // TODO: once we get rid of lazy construction, this should no longer be
176 // necessary.
177 virtual std::unique_ptr<DecoratedASTNode> clone() const override;
178 virtual std::shared_ptr<DecoratedASTNode> clone_shared() const override;
179 BoolLiteralNode(const BoolLiteralNode& other);
180};
181
182class StringLiteralNode : public DecoratedASTNode {
183 std::string value;
184
185public:
186 StringLiteralNode(const std::string& value);
187
188 std::any construct(ConstructContext& context) const override;
189 void dump(std::string indent) const override;
190
191 // TODO: once we get rid of lazy construction, this should no longer be
192 // necessary.
193 virtual std::unique_ptr<DecoratedASTNode> clone() const override;
194 virtual std::shared_ptr<DecoratedASTNode> clone_shared() const override;
195 StringLiteralNode(const StringLiteralNode& other);
196};
197
198class IntLiteralNode : public DecoratedASTNode {
199 std::string value;
200
201public:
202 IntLiteralNode(const std::string& value);
203
204 std::any construct(ConstructContext& context) const override;
205 void dump(std::string indent) const override;
206
207 // TODO: once we get rid of lazy construction, this should no longer be
208 // necessary.
209 virtual std::unique_ptr<DecoratedASTNode> clone() const override;
210 virtual std::shared_ptr<DecoratedASTNode> clone_shared() const override;
211 IntLiteralNode(const IntLiteralNode& other);
212};
213
214class FloatLiteralNode : public DecoratedASTNode {
215 std::string value;
216
217public:
218 FloatLiteralNode(const std::string& value);
219
220 std::any construct(ConstructContext& context) const override;
221 void dump(std::string indent) const override;
222
223 // TODO: once we get rid of lazy construction, this should no longer be
224 // necessary.
225 virtual std::unique_ptr<DecoratedASTNode> clone() const override;
226 virtual std::shared_ptr<DecoratedASTNode> clone_shared() const override;
227 FloatLiteralNode(const FloatLiteralNode& other);
228};
229
230class SymbolNode : public DecoratedASTNode {
231 std::string value;
232
233public:
234 SymbolNode(const std::string& value);
235
236 std::any construct(ConstructContext& context) const override;
237 void dump(std::string indent) const override;
238
239 // TODO: once we get rid of lazy construction, this should no longer be
240 // necessary.
241 virtual std::unique_ptr<DecoratedASTNode> clone() const override;
242 virtual std::shared_ptr<DecoratedASTNode> clone_shared() const override;
243 SymbolNode(const SymbolNode& other);
244};
245
246class ConvertNode : public DecoratedASTNode {
247 DecoratedASTNodePtr value;
248 const plugins::Type& from_type;
249 const plugins::Type& to_type;
250
251public:
252 ConvertNode(
253 DecoratedASTNodePtr value,
254 const plugins::Type& from_type,
255 const plugins::Type& to_type);
256
257 std::any construct(ConstructContext& context) const override;
258 void dump(std::string indent) const override;
259
260 // TODO: once we get rid of lazy construction, this should no longer be
261 // necessary.
262 virtual std::unique_ptr<DecoratedASTNode> clone() const override;
263 virtual std::shared_ptr<DecoratedASTNode> clone_shared() const override;
264 ConvertNode(const ConvertNode& other);
265};
266
267class CheckBoundsNode : public DecoratedASTNode {
268 DecoratedASTNodePtr value;
269 DecoratedASTNodePtr min_value;
270 DecoratedASTNodePtr max_value;
271
272public:
273 CheckBoundsNode(
274 DecoratedASTNodePtr value,
275 DecoratedASTNodePtr min_value,
276 DecoratedASTNodePtr max_value);
277
278 std::any construct(ConstructContext& context) const override;
279 void dump(std::string indent) const override;
280
281 // TODO: once we get rid of lazy construction, this should no longer be
282 // necessary.
283 virtual std::unique_ptr<DecoratedASTNode> clone() const override;
284 virtual std::shared_ptr<DecoratedASTNode> clone_shared() const override;
285 CheckBoundsNode(const CheckBoundsNode& other);
286};
287} // namespace downward::cli::parser
288#endif