OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
ast.hpp
Go to the documentation of this file.
1//
2// Namespace ast
3//
4// Copyright (c) 2026, Paul Scherrer Institute, Villigen PSI, Switzerland
5// All rights reserved
6//
7// This file is part of OPAL.
8//
9// OPAL is free software: you can redistribute it and/or modify
10// it under the terms of the GNU General Public License as published by
11// the Free Software Foundation, either version 3 of the License, or
12// (at your option) any later version.
13//
14// You should have received a copy of the GNU General Public License
15// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
16//
17#ifndef AST_HPP
18#define AST_HPP
19
20#include <list>
21#include <memory>
22#include <ostream>
23#include <string>
24#include <type_traits>
25#include <utility>
26#include <variant>
27
28namespace client { namespace ast
29{
33 template <typename T>
35 {
36 public:
37 recursive_wrapper() : ptr_(std::make_unique<T>()) {}
38 recursive_wrapper(T const& value) : ptr_(std::make_unique<T>(value)) {}
39 recursive_wrapper(T&& value) : ptr_(std::make_unique<T>(std::move(value))) {}
40
41 recursive_wrapper(recursive_wrapper const& other) : ptr_(std::make_unique<T>(*other.ptr_)) {}
42 recursive_wrapper(recursive_wrapper&&) noexcept = default;
43
44 recursive_wrapper& operator=(recursive_wrapper const& other) {
45 if (this != &other) {
46 ptr_ = std::make_unique<T>(*other.ptr_);
47 }
48 return *this;
49 }
51
52 recursive_wrapper& operator=(T const& value) { *ptr_ = value; return *this; }
53 recursive_wrapper& operator=(T&& value) { *ptr_ = std::move(value); return *this; }
54
55 T& get() { return *ptr_; }
56 T const& get() const { return *ptr_; }
57
58 explicit operator T&() { return *ptr_; }
59 explicit operator T const&() const { return *ptr_; }
60
61 private:
62 std::unique_ptr<T> ptr_;
63 };
64
65 template <typename T>
66 struct is_recursive_wrapper : std::false_type {};
67 template <typename T>
68 struct is_recursive_wrapper<recursive_wrapper<T>> : std::true_type {};
69 template <typename T>
71
75 template <typename Visitor, typename Variant>
76 decltype(auto) apply_visitor(Visitor&& vis, Variant&& var)
77 {
78 return std::visit(
79 [&vis](auto&& alternative) -> decltype(auto) {
80 using Alternative = std::decay_t<decltype(alternative)>;
81 if constexpr (is_recursive_wrapper_v<Alternative>) {
82 return vis(alternative.get());
83 } else {
84 return vis(alternative);
85 }
86 },
87 std::forward<Variant>(var));
88 }
89
90 struct tagged
91 {
92 int id = 0; // Used to annotate the AST with the iterator position.
93 // This id is used as a key to a map<int, Iterator>
94 // (not really part of the AST.)
95 };
96
97 struct nil {};
98 struct unary;
99 struct function_call;
100 struct expression;
101
103 {
104 identifier(std::string const& name = "") : name(name) {}
105 std::string name;
106 };
107
109 {
110 quoted_string(std::string const& value = "") : value(value) {}
111 std::string value;
112 };
113
114 typedef std::variant<
115 nil
116 , bool
117 , unsigned int
118 , double
119 , identifier
123 >
125
126 typedef std::variant<
129 >
131
150
156
162
164 {
166 std::list<function_call_argument> args;
167 };
168
170 {
172 std::list<operation> rest;
173 };
174#if 0
175 // print functions for debugging
176 inline std::ostream& operator<<(std::ostream& out, nil)
177 {
178 out << "nil"; return out;
179 }
180#endif
181 inline std::ostream& operator<<(std::ostream& out, identifier const& id)
182 {
183 out << id.name; return out;
184 }
185}}
186
187#endif
constexpr bool is_recursive_wrapper_v
Definition ast.hpp:70
decltype(auto) apply_visitor(Visitor &&vis, Variant &&var)
Definition ast.hpp:76
@ op_not_equal
Definition ast.hpp:142
@ op_less_equal
Definition ast.hpp:144
@ op_greater
Definition ast.hpp:145
@ op_greater_equal
Definition ast.hpp:146
@ op_positive
Definition ast.hpp:138
@ op_negative
Definition ast.hpp:139
@ op_divide
Definition ast.hpp:137
std::variant< expression, quoted_string > function_call_argument
Definition ast.hpp:130
std::ostream & operator<<(std::ostream &out, identifier const &id)
Definition ast.hpp:181
std::variant< nil, bool, unsigned int, double, identifier, recursive_wrapper< unary >, recursive_wrapper< function_call >, recursive_wrapper< expression > > operand
Definition ast.hpp:124
recursive_wrapper & operator=(recursive_wrapper &&) noexcept=default
recursive_wrapper & operator=(T &&value)
Definition ast.hpp:53
std::unique_ptr< T > ptr_
Definition ast.hpp:62
T const & get() const
Definition ast.hpp:56
recursive_wrapper(recursive_wrapper &&) noexcept=default
recursive_wrapper(T const &value)
Definition ast.hpp:38
recursive_wrapper(recursive_wrapper const &other)
Definition ast.hpp:41
std::string name
Definition ast.hpp:105
identifier(std::string const &name="")
Definition ast.hpp:104
quoted_string(std::string const &value="")
Definition ast.hpp:110
operand operand_
Definition ast.hpp:154
optoken operator_
Definition ast.hpp:153
std::list< function_call_argument > args
Definition ast.hpp:166
identifier function_name
Definition ast.hpp:165
std::list< operation > rest
Definition ast.hpp:172