-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompiler_Symbol_Bindings.H
More file actions
245 lines (209 loc) · 8.11 KB
/
Copy pathCompiler_Symbol_Bindings.H
File metadata and controls
245 lines (209 loc) · 8.11 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
Aleph_w
Data structures & Algorithms
version 2.0.0b
https://github.com/lrleon/Aleph-w
This file is part of Aleph-w library
Copyright (c) 2002-2026 Leandro Rabindranath Leon
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.
*/
/** @file Compiler_Symbol_Bindings.H
* @brief Reusable lexical bindings and symbol records for frontends.
*
* This header provides a small, language-agnostic layer for lexical
* declaration tracking on top of `tpl_scope.H`. It is intended for frontends
* that need stable symbol ids, shadowing-aware insertions, and deterministic
* dumps without adopting the MVP AST or semantic analyzer.
*
* @ingroup Utilities
*/
#ifndef COMPILER_SYMBOL_BINDINGS_H
#define COMPILER_SYMBOL_BINDINGS_H
#include <sstream>
#include <string>
#include <ah-source.H>
#include <tpl_scope.H>
namespace Aleph {
using Compiler_Symbol_Id = size_t;
/** @brief Generic symbol kinds reusable across multiple frontends. */
enum class Compiler_Symbol_Kind
{
Function,
Parameter,
Local,
Global,
Type
};
/** @brief Returns a stable debug name for a symbol kind. */
inline const char *compiler_symbol_kind_name(const Compiler_Symbol_Kind kind) noexcept
{
switch (kind)
{
case Compiler_Symbol_Kind::Function:
return "Function";
case Compiler_Symbol_Kind::Parameter:
return "Parameter";
case Compiler_Symbol_Kind::Local:
return "Local";
case Compiler_Symbol_Kind::Global:
return "Global";
case Compiler_Symbol_Kind::Type:
return "Type";
}
return "Unknown";
}
/** @brief One reusable symbol record tracked by lexical bindings. */
struct Compiler_Symbol
{
Compiler_Symbol_Id id = 0; ///< Stable symbol identifier, 1-based.
Compiler_Symbol_Kind kind = Compiler_Symbol_Kind::Local; ///< Symbol category.
std::string name; ///< Declared name.
Source_Span declaration_span; ///< Declaration site.
size_t scope_depth = 0; ///< Lexical depth where the symbol was introduced.
};
/** @brief Result status for one declaration attempt. */
enum class Compiler_Symbol_Declare_Status
{
Inserted,
Duplicate_Local,
Inserted_Shadowing
};
/** @brief Structured result returned after one declaration attempt. */
struct Compiler_Symbol_Declare_Result
{
Compiler_Symbol_Declare_Status status = Compiler_Symbol_Declare_Status::Inserted;
Compiler_Symbol_Id symbol_id = 0; ///< Newly inserted symbol or the conflicting local symbol.
Compiler_Symbol_Id related_symbol_id = 0; ///< Shadowed outer symbol or local duplicate.
/** @brief Returns whether the declaration produced a new symbol. */
[[nodiscard]] bool inserted() const noexcept
{
return status != Compiler_Symbol_Declare_Status::Duplicate_Local;
}
/** @brief Returns whether the declaration collided with the current scope. */
[[nodiscard]] bool duplicate_local() const noexcept
{
return status == Compiler_Symbol_Declare_Status::Duplicate_Local;
}
/** @brief Returns whether the new declaration shadows one outer symbol. */
[[nodiscard]] bool shadowing() const noexcept
{
return status == Compiler_Symbol_Declare_Status::Inserted_Shadowing;
}
};
/** @brief Reusable lexical symbol table with stable ids and scope tracking. */
class Compiler_Symbol_Bindings
{
DynArray<Compiler_Symbol> symbols_;
Scope<std::string, Compiler_Symbol_Id> scopes_;
Compiler_Symbol_Id append_symbol(const Compiler_Symbol_Kind kind,
const std::string &name,
const Source_Span &span)
{
Compiler_Symbol sym;
sym.id = symbols_.size() + 1;
sym.kind = kind;
sym.name = name;
sym.declaration_span = span;
sym.scope_depth = scopes_.depth() > 0 ? scopes_.depth() - 1 : 0;
symbols_.append(std::move(sym));
return symbols_.size();
}
public:
/** @brief Clears all scopes and previously recorded symbols. */
void clear() noexcept
{
symbols_.clear();
scopes_.clear();
}
/** @brief Returns the current lexical depth. */
[[nodiscard]] size_t depth() const noexcept
{
return scopes_.depth();
}
/** @brief Returns the number of recorded symbols. */
[[nodiscard]] size_t symbol_count() const noexcept
{
return symbols_.size();
}
/** @brief Enters one nested lexical scope. */
size_t enter_scope()
{
return scopes_.enter_scope();
}
/** @brief Leaves the current lexical scope. */
void leave_scope()
{
scopes_.leave_scope();
}
/** @brief Looks up one visible symbol id by name. */
[[nodiscard]] const Compiler_Symbol_Id *lookup(const std::string &name) const noexcept
{
return scopes_.lookup(name);
}
/** @brief Looks up one symbol id only in the current scope. */
[[nodiscard]] const Compiler_Symbol_Id *lookup_local(const std::string &name) const noexcept
{
return scopes_.lookup_local(name);
}
/** @brief Declares one symbol in the current scope.
*
* Duplicate declarations in the current scope do not insert a new symbol.
* Shadowing of outer scopes does insert a new symbol and is reported in the
* returned status so callers can decide whether it is allowed.
*/
[[nodiscard]] Compiler_Symbol_Declare_Result declare(const Compiler_Symbol_Kind kind,
const std::string &name,
const Source_Span &span)
{
ah_runtime_error_unless(scopes_.depth() > 0)
<< "Compiler_Symbol_Bindings::declare(): no active scope";
if (const auto *local = scopes_.lookup_local(name); local != nullptr)
return {Compiler_Symbol_Declare_Status::Duplicate_Local, *local, *local};
const auto *shadowed = scopes_.lookup(name);
const auto id = append_symbol(kind, name, span);
const bool inserted = scopes_.insert(name, id);
ah_runtime_error_unless(inserted)
<< "Compiler_Symbol_Bindings::declare(): unexpected duplicate insertion for '" << name << "'";
return shadowed == nullptr
? Compiler_Symbol_Declare_Result{Compiler_Symbol_Declare_Status::Inserted, id, 0}
: Compiler_Symbol_Declare_Result{Compiler_Symbol_Declare_Status::Inserted_Shadowing,
id,
*shadowed};
}
/** @brief Returns one symbol by its stable 1-based identifier. */
[[nodiscard]] const Compiler_Symbol &symbol(const Compiler_Symbol_Id id) const
{
ah_out_of_range_error_unless(id > 0 and id <= symbols_.size())
<< "Compiler_Symbol_Bindings::symbol(): invalid id " << id;
return symbols_.access(id - 1);
}
/** @brief Dumps the current symbol table in deterministic text form. */
[[nodiscard]] std::string dump_symbols() const
{
std::ostringstream out;
out << "Symbols\n";
for (size_t i = 0; i < symbols_.size(); ++i)
{
const auto &sym = symbols_.access(i);
out << " #" << sym.id << " " << compiler_symbol_kind_name(sym.kind) << " " << sym.name
<< " scope=" << sym.scope_depth << " @" << sym.declaration_span.to_string() << '\n';
}
return out.str();
}
};
} // namespace Aleph
#endif