Debian Codegraph @

examples/c++/glr/ast.hh

raw
1 /*
2 Copyright (C) 2020-2021 Free Software Foundation, Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18 #include <cstddef> // nullptr_t
19 #include <iostream>
20 #include <memory>
21
22 // Type erasure 101 <https://stackoverflow.com/a/26199467/1353549>.
23 class NodeInterface;
24
25 class Node
26 {
27 public:
28 Node (const Node& node) = default;
29 Node (Node&& node) = default;
30 Node () = default;
31 ~Node () = default;
32
33 template <typename T,
34 // SFINAE block using this ctor as a copy/move ctor:
35 std::enable_if_t<!std::is_same<Node, std::decay_t<T>>::value, int>* = nullptr>
36 Node (T&& t);
37
38 Node& operator= (const Node& node) = default;
39 Node& operator= (Node&& node) = default;
40
41 explicit operator bool () const
42 {
43 return impl_ != nullptr;
44 }
45
46 std::ostream& print (std::ostream& o) const;
47
48 std::shared_ptr<NodeInterface> impl_;
49 };
50
51 static std::ostream&
52 operator<< (std::ostream& o, const Node &node)
53 {
54 return node.print (o);
55 }
56
57 class NodeInterface
58 {
59 public:
60 virtual ~NodeInterface () = default;
61 virtual std::ostream& print (std::ostream& o) const = 0;
62 };
63
64
65 std::ostream& Node::print (std::ostream& o) const
66 {
67 if (impl_)
68 impl_->print (o);
69 return o;
70 }
71
72
73 template <typename T,
74 std::enable_if_t<!std::is_same<std::nullptr_t, std::decay_t<T>>::value, int>* = nullptr>
75 struct NodeImpl : public NodeInterface
76 {
77 template <typename U>
78 explicit NodeImpl (U&& u)
79 : t{std::forward<U> (u)}
80 {}
81 virtual ~NodeImpl () = default;
82 virtual std::ostream& print (std::ostream& o) const
83 {
84 return o << t;
85 }
86
87 T t;
88 };
89
90
91 template <typename T,
92 std::enable_if_t<!std::is_same<Node, std::decay_t<T>>::value, int>*>
93 Node::Node (T&& t)
94 : impl_ (new NodeImpl<std::decay_t<T>>{std::forward<T> (t)})
95 {}
96
97 class Nterm
98 {
99 public:
100 Nterm (std::string form,
101 Node child0 = Node (), Node child1 = Node (), Node child2 = Node ())
102 : form_ (std::move (form))
103 {
104 children_[0] = child0;
105 children_[1] = child1;
106 children_[2] = child2;
107 }
108
109 friend std::ostream& operator<< (std::ostream& o, const Nterm& t)
110 {
111 o << t.form_;
112 if (t.children_[0])
113 {
114 o << '(' << t.children_[0];
115 if (t.children_[1])
116 o << ", " << t.children_[1];
117 if (t.children_[2])
118 o << ", " << t.children_[2];
119 o << ')';
120 }
121 return o;
122 }
123
124 private:
125 std::string form_;
126 Node children_[3];
127 };
128
129
130
131 class Term
132 {
133 public:
134 Term (std::string text)
135 : text_ (std::move (text))
136 {}
137
138 friend std::ostream& operator<< (std::ostream& o, const Term& t)
139 {
140 return o << t.text_;
141 }
142
143 private:
144 std::string text_;
145 };
146
147 #ifdef TEST
148 int main ()
149 {
150 Node n0;
151 std::cout << n0 << '\n';
152
153 Node n;
154 n = n0;
155 std::cout << n0 << '\n';
156
157 Term t1 = Term ("T");
158 std::cout << t1 << '\n';
159
160 n = t1;
161 std::cout << n << '\n';
162 std::cout << Nterm ("+", t1, t1) << '\n';
163
164 auto n1
165 = Nterm ("<OR>",
166 Nterm ("<declare>", Term ("T"), Term ("x")),
167 Nterm ("<cast>", Term ("x"), Term ("T")));
168 std::cout << n1 << '\n';
169
170 n = n1;
171 std::cout << n1 << '\n';
172 }
173 #endif