ArkScript
A small, fast, functional and scripting language for video games
Node.hpp
Go to the documentation of this file.
1 /**
2  * @file Node.hpp
3  * @author Alexandre Plateau (lexplt.dev@gmail.com)
4  * @brief AST node used by the parser, optimizer and compiler
5  * @version 0.3
6  * @date 2020-10-27
7  *
8  * @copyright Copyright (c) 2020-2021
9  *
10  */
11 
12 #ifndef COMPILER_AST_NODE_HPP
13 #define COMPILER_AST_NODE_HPP
14 
15 #include <variant>
16 #include <ostream>
17 #include <string>
18 #include <vector>
19 
20 #include <Ark/Compiler/Common.hpp>
21 
22 namespace Ark::internal
23 {
24  /**
25  * @brief A node of an Abstract Syntax Tree for ArkScript
26  *
27  */
28  class Node
29  {
30  public:
31  using Value = std::variant<double, std::string, Keyword>;
32 
33  /**
34  * @brief Provide a statically initialized / correct and guaranteed to be initialized Node representing "true"
35  */
36  static const Node& getTrueNode();
37 
38  /**
39  * @brief Provide a statically initialized / correct and guaranteed to be initialized Node representing "false"
40  */
41  static const Node& getFalseNode();
42 
43  /**
44  * @brief Provide a statically initialized / correct and guaranteed to be initialized Node representing "Nil"
45  */
46  static const Node& getNilNode();
47 
48  /**
49  * @brief Provide a statically initialized / correct and guaranteed to be initialized Node representing "Empty List"
50  */
51  static const Node& getListNode();
52 
53  Node() = default;
54 
55  /**
56  * @brief Construct a new Node object
57  *
58  * @param value
59  */
60  explicit Node(long value) noexcept;
61 
62  /**
63  * @brief Construct a new Node object
64  *
65  * @param value
66  */
67  explicit Node(double value) noexcept;
68 
69  /**
70  * @brief Construct a new Node object
71  *
72  * @param value
73  */
74  explicit Node(const std::string& value) noexcept;
75 
76  /**
77  * @brief Construct a new Node object
78  *
79  * @param value
80  */
81  explicit Node(Keyword value) noexcept;
82 
83  /**
84  * @brief Construct a new Node object, does not set the value
85  *
86  * @param type
87  */
88  explicit Node(NodeType type) noexcept;
89 
90  /**
91  * @brief Construct a new Node object
92  *
93  * @param other
94  */
95  Node(const Node& other) noexcept;
96 
97  /**
98  * @brief Construct a new Node object
99  *
100  * @param other
101  */
102  Node& operator=(Node other) noexcept;
103 
104  /**
105  * @brief Construct a new Node object
106  *
107  * @param other
108  */
109  void swap(Node& other) noexcept;
110 
111  /**
112  * @brief Return the string held by the value (if the node type allows it)
113  *
114  * @return const std::string&
115  */
116  const std::string& string() const noexcept;
117 
118  /**
119  * @brief Return the number held by the value (if the node type allows it)
120  *
121  * @return double
122  */
123  double number() const noexcept;
124 
125  /**
126  * @brief Return the keyword held by the value (if the node type allows it)
127  *
128  * @return Keyword
129  */
130  Keyword keyword() const noexcept;
131 
132  /**
133  * @brief Every node has a list as well as a value so we can push_back on all node no matter their type
134  *
135  * @param node a sub-node to push on the list held by the current node
136  */
137  void push_back(const Node& node) noexcept;
138 
139  /**
140  * @brief Return the list of sub-nodes held by the node
141  *
142  * @return std::vector<Node>&
143  */
144  std::vector<Node>& list() noexcept;
145 
146  /**
147  * @brief Return the list of sub-nodes held by the node
148  *
149  * @return const std::vector<Node>&
150  */
151  const std::vector<Node>& constList() const noexcept;
152 
153  /**
154  * @brief Return the node type
155  *
156  * @return NodeType
157  */
158  NodeType nodeType() const noexcept;
159 
160  /**
161  * @brief Set the Node Type object
162  *
163  * @param type
164  */
165  void setNodeType(NodeType type) noexcept;
166 
167  /**
168  * @brief Set the String object
169  *
170  * @param value
171  */
172  void setString(const std::string& value) noexcept;
173 
174  /**
175  * @brief Set the Number object
176  *
177  * @param value
178  */
179  void setNumber(double value) noexcept;
180 
181  /**
182  * @brief Set the Keyword object
183  *
184  * @param kw
185  */
186  void setKeyword(Keyword kw) noexcept;
187 
188  /**
189  * @brief Set the Position of the node in the text
190  *
191  * @param line
192  * @param col
193  */
194  void setPos(std::size_t line, std::size_t col) noexcept;
195 
196  /**
197  * @brief Set the original Filename where the node was
198  *
199  * @param filename
200  */
201  void setFilename(const std::string& filename) noexcept;
202 
203  /**
204  * @brief Get the line at which this node was created
205  *
206  * @return std::size_t
207  */
208  std::size_t line() const noexcept;
209 
210  /**
211  * @brief Get the column at which this node was created
212  *
213  * @return std::size_t
214  */
215  std::size_t col() const noexcept;
216 
217  /**
218  * @brief Return the filename in which this node was created
219  *
220  * @return const std::string&
221  */
222  const std::string& filename() const noexcept;
223 
224  friend std::ostream& operator<<(std::ostream& os, const Node& N) noexcept;
225  friend void swap(Node& lhs, Node& rhs) noexcept;
226  friend bool operator==(const Node& A, const Node& B);
227  friend bool operator<(const Node& A, const Node& B);
228  friend bool operator!(const Node& A);
229 
230  private:
231  /**
232  * @brief Construct a new Node object.
233  * This is private because it is only used by the static member of this class
234  * to generate specialized versions of the node.
235  *
236  * @param value
237  * @param type
238  */
239  explicit Node(const std::string& value, NodeType const& type) noexcept;
242  std::vector<Node> m_list;
243  // position of the node in the original code, useful when it comes to parser errors
244  std::size_t m_line = 0, m_col = 0;
245  std::string m_filename = "";
246  };
247 
248  std::ostream& operator<<(std::ostream& os, const std::vector<Node>& N) noexcept;
249 
250  template <typename T>
251  Node make_node(T&& value, std::size_t line, std::size_t col, const std::string& file)
252  {
253  Node n(std::forward<T>(value));
254  n.setPos(line, col);
255  n.setFilename(file);
256  return n;
257  }
258 
259  inline Node make_node_list(std::size_t line, std::size_t col, const std::string& file)
260  {
261  Node n(NodeType::List);
262  n.setPos(line, col);
263  n.setFilename(file);
264  return n;
265  }
266 
267  inline std::string typeToString(const Node& node) noexcept
268  {
269  if (node.nodeType() == NodeType::Symbol)
270  {
271  if (node.string() == "nil")
272  return "Nil";
273  else if (node.string() == "true" || node.string() == "false")
274  return "Bool";
275  }
276 
277  auto c = static_cast<std::size_t>(node.nodeType());
278  return (c < nodeTypes.size()) ? std::string(nodeTypes[c]) : "???";
279  }
280 }
281 
282 #endif
Common code for the compiler.
A node of an Abstract Syntax Tree for ArkScript.
Definition: Node.hpp:29
NodeType nodeType() const noexcept
Return the node type.
Definition: Node.cpp:126
Node & operator=(Node other) noexcept
Construct a new Node object.
Definition: Node.cpp:72
static const Node & getListNode()
Provide a statically initialized / correct and guaranteed to be initialized Node representing "Empty ...
Definition: Node.cpp:28
void setNodeType(NodeType type) noexcept
Set the Node Type object.
Definition: Node.cpp:131
const std::string & filename() const noexcept
Return the filename in which this node was created.
Definition: Node.cpp:174
const std::string & string() const noexcept
Return the string held by the value (if the node type allows it)
Definition: Node.cpp:92
void setPos(std::size_t line, std::size_t col) noexcept
Set the Position of the node in the text.
Definition: Node.cpp:153
const std::vector< Node > & constList() const noexcept
Return the list of sub-nodes held by the node.
Definition: Node.cpp:119
std::variant< double, std::string, Keyword > Value
Definition: Node.hpp:31
std::vector< Node > m_list
Definition: Node.hpp:242
void setKeyword(Keyword kw) noexcept
Set the Keyword object.
Definition: Node.cpp:146
Keyword keyword() const noexcept
Return the keyword held by the value (if the node type allows it)
Definition: Node.cpp:102
void swap(Node &other) noexcept
Construct a new Node object.
Definition: Node.cpp:78
NodeType m_type
Definition: Node.hpp:240
std::string m_filename
Definition: Node.hpp:245
void setFilename(const std::string &filename) noexcept
Set the original Filename where the node was.
Definition: Node.cpp:159
std::size_t m_line
Definition: Node.hpp:244
void setNumber(double value) noexcept
Set the Number object.
Definition: Node.cpp:141
static const Node & getNilNode()
Provide a statically initialized / correct and guaranteed to be initialized Node representing "Nil".
Definition: Node.cpp:22
std::size_t col() const noexcept
Get the column at which this node was created.
Definition: Node.cpp:169
void push_back(const Node &node) noexcept
Every node has a list as well as a value so we can push_back on all node no matter their type.
Definition: Node.cpp:109
void setString(const std::string &value) noexcept
Set the String object.
Definition: Node.cpp:136
static const Node & getTrueNode()
Provide a statically initialized / correct and guaranteed to be initialized Node representing "true".
Definition: Node.cpp:10
double number() const noexcept
Return the number held by the value (if the node type allows it)
Definition: Node.cpp:97
static const Node & getFalseNode()
Provide a statically initialized / correct and guaranteed to be initialized Node representing "false"...
Definition: Node.cpp:16
std::size_t line() const noexcept
Get the line at which this node was created.
Definition: Node.cpp:164
std::size_t m_col
Definition: Node.hpp:244
std::vector< Node > & list() noexcept
Return the list of sub-nodes held by the node.
Definition: Node.cpp:114
std::string typeToString(const Node &node) noexcept
Definition: Node.hpp:267
constexpr std::array< std::string_view, 11 > nodeTypes
Definition: Common.hpp:43
Node make_node(T &&value, std::size_t line, std::size_t col, const std::string &file)
Definition: Node.hpp:251
NodeType
The different node types available.
Definition: Common.hpp:29
Keyword
The different keywords available.
Definition: Common.hpp:59
Node make_node_list(std::size_t line, std::size_t col, const std::string &file)
Definition: Node.hpp:259