tcalc 0.2.0
 
Loading...
Searching...
No Matches
node.hpp
Go to the documentation of this file.
1
12#pragma once
13
14#include <cctype>
15#include <cstdint>
16#include <memory>
17#include <string>
18#include <type_traits>
19#include <unordered_map>
20
21#include "tcalc/common.hpp"
22
23namespace tcalc::ast {
24
55
56inline const std::unordered_map<NodeType, std::string> NODE_TYPE_NAMES = {
57 { NodeType::BINARY_PLUS, "BINARY_PLUS" },
58 { NodeType::BINARY_MINUS, "BINARY_MINUS" },
59 { NodeType::BINARY_MULTIPLY, "BINARY_MULTIPLY" },
60 { NodeType::BINARY_DIVIDE, "BINARY_DIVIDE" },
61 { NodeType::BINARY_EQUAL, "BINARY_EQUAL" },
62 { NodeType::BINARY_NOT_EQUAL, "BINARY_NOT_EQUAL" },
63 { NodeType::BINARY_GREATER, "BINARY_GREATER" },
64 { NodeType::BINARY_GREATER_EQUAL, "BINARY_GREATER_EQUAL" },
65 { NodeType::BINARY_LESS, "BINARY_LESS" },
66 { NodeType::BINARY_LESS_EQUAL, "BINARY_LESS_EQUAL" },
67 { NodeType::BINARY_AND, "BINARY_AND" },
68 { NodeType::BINARY_OR, "BINARY_OR" },
69 { NodeType::UNARY_PLUS, "UNARY_PLUS" },
70 { NodeType::UNARY_MINUS, "UNARY_MINUS" },
71 { NodeType::UNARY_NOT, "UNARY_NOT" },
72 { NodeType::NUMBER, "NUMBER" },
73 { NodeType::VARREF, "VARREF" },
74 { NodeType::VARASSIGN, "VARASSIGN" },
75 { NodeType::FCALL, "FCALL" },
76 { NodeType::FDEF, "FDEF" },
77 { NodeType::IF, "IF" },
78 { NodeType::PROGRAM, "PROGRAM" },
79 { NodeType::IMPORT, "IMPORT" },
80};
86class Node
87{
88private:
89 NodeType _type;
90
91public:
97 explicit Node(NodeType type)
98 : _type{ type }
99 {
100 }
101
102 virtual ~Node() = default;
103
109 [[nodiscard]] TCALC_INLINE auto type() const noexcept { return _type; }
110};
111
117template<typename NT = Node,
118 typename = std::enable_if_t<std::is_base_of_v<Node, NT>>>
119using NodePtr = std::shared_ptr<NT>;
120
121}
Base class for AST nodes.
Definition node.hpp:87
Node(NodeType type)
Construct a new Node object.
Definition node.hpp:97
TCALC_INLINE auto type() const noexcept
Get the node type.
Definition node.hpp:109
tcalc common header.
const std::unordered_map< NodeType, std::string > NODE_TYPE_NAMES
Definition node.hpp:56
std::shared_ptr< NT > NodePtr
Shared pointer to a node.
Definition node.hpp:119
NodeType
AST node type.
Definition node.hpp:30