-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.h
177 lines (143 loc) · 4.66 KB
/
node.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* @copyright
* Copyright (c) 2021 Rich Newman
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author
* Rich Newman
*/
#ifndef NODE_H_INCLUDED
#define NODE_H_INCLUDED
#include "is_valid.h"
#include <tao/pegtl/contrib/parse_tree.hpp>
#include <variant>
#include <functional>
namespace Calc::Node {
struct node;
/// Define the base members of the variant in the parse tree nodes.
/// Something that has scope, currently only the root, and a compound
/// statement.
struct parent
{
std::unique_ptr<node> scope_;
};
/// A constant value.
struct value {
int value_ = 0;
};
/// A reference to a symbol or variable.
struct symbol_ref {
node *symbol_ = nullptr;
};
/// An operation of some kind.
struct operation { };
/// A statement of some kind.
struct statement { };
/// A node that has some kind of symbol/identifier attached.
struct symbol_name {
std::string name_;
void set_name(const std::string &name) { name_ = name; }
};
/// Exit statements may have an attached identifier. (To terminate an
/// outer loop as well as an inner one.)
struct exit_statement_base : public symbol_name { };
/// A parent statement, (used for compound statements).
struct parent_stmt : public parent, public statement, public symbol_name { };
/// A scope node
struct scope_base {
scope_base() : function_(0) { }
/// Subscopes of this scope if any.
std::vector<node *> subscopes_;
/// The parent scope of this one, or nullptr for the top-level, (global)
/// scope.
node *parent_scope_ = nullptr;
/// Is this a function scope?
unsigned short function_: 1;
};
/// A function node kind
struct function_base : public parent
{
using Intrinsic = std::function<int(int)>;
using Kind = std::variant<Intrinsic, node *>;
Kind kind_;
std::string name_;
Intrinsic get_intrinsic()
{
if (std::holds_alternative<Intrinsic>(kind_)) {
return std::get<Intrinsic>(kind_);
}
return Intrinsic();
}
node *get_function()
{
if (std::holds_alternative<node*>(kind_)) {
return std::get<node *>(kind_);
}
return nullptr;
}
};
/// A function call base node kind
struct function_call_base : public operation, public symbol_ref { };
/// Used as a sentinel to end the list of variants.
struct error { };
#define xx(a, b) struct a : public b { };
#include "node_kind.def"
/// The variant which will hold information specific to the type of parse
/// tree node.
using node_kind =
std::variant <
std::monostate, ///< Not classified yet.
#define xx(a, b) a,
#include "node_kind.def"
error ///< Must be last node.
>;
/// Parse tree node is the same as a PEGTL basic node, but adds
/// the variant described above to differentiate the types of nodes.
struct node : public tao::pegtl::parse_tree::basic_node<node>
{
using inherited = tao::pegtl::parse_tree::basic_node<node>;
node() = default;
node(node &&) = default;
node(const node&) = delete;
~node() = default;
node& operator=(node &&) = default;
node& operator=(const node &) = delete;
template <typename U>
U* set_kind( U&& u) noexcept
{
if constexpr (is_valid<U, node_kind>::value) {
kind_ = std::forward<U>(u);
} else {
kind_ = std::monostate{};
}
return this->get_kind<U>();
}
template <typename U>
U* get_kind() noexcept
{
if (std::holds_alternative<U>(kind_)) {
return &std::get<U>(kind_);
}
return nullptr;
}
node_kind kind_;
};
using Ptr = std::unique_ptr<node>;
} // namespace Calc
#endif // NODE_H_INCLUDED