Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Compiler_Symbol_Bindings.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
42#ifndef COMPILER_SYMBOL_BINDINGS_H
43#define COMPILER_SYMBOL_BINDINGS_H
44
45#include <sstream>
46#include <string>
47
48#include <ah-source.H>
49#include <tpl_scope.H>
50
51namespace Aleph {
52using Compiler_Symbol_Id = size_t;
53
56{
59 Local,
60 Global,
61 Type
62};
63
65inline const char *compiler_symbol_kind_name(const Compiler_Symbol_Kind kind) noexcept
66{
67 switch (kind)
68 {
70 return "Function";
72 return "Parameter";
74 return "Local";
76 return "Global";
78 return "Type";
79 }
80
81 return "Unknown";
82}
83
93
101
127
130{
133
135 const std::string &name,
136 const Source_Span &span)
137 {
139 sym.id = symbols_.size() + 1;
140 sym.kind = kind;
141 sym.name = name;
142 sym.declaration_span = span;
143 sym.scope_depth = scopes_.depth() > 0 ? scopes_.depth() - 1 : 0;
144 symbols_.append(std::move(sym));
145 return symbols_.size();
146 }
147
148public:
151 {
152 symbols_.clear();
153 scopes_.clear();
154 }
155
158 {
159 return scopes_.depth();
160 }
161
164 {
165 return symbols_.size();
166 }
167
169 size_t enter_scope()
170 {
171 return scopes_.enter_scope();
172 }
173
176 {
178 }
179
181 [[nodiscard]] const Compiler_Symbol_Id *lookup(const std::string &name) const noexcept
182 {
183 return scopes_.lookup(name);
184 }
185
187 [[nodiscard]] const Compiler_Symbol_Id *lookup_local(const std::string &name) const noexcept
188 {
189 return scopes_.lookup_local(name);
190 }
191
199 const std::string &name,
200 const Source_Span &span)
201 {
203 << "Compiler_Symbol_Bindings::declare(): no active scope";
204
205 if (const auto *local = scopes_.lookup_local(name); local != nullptr)
207
208 const auto *shadowed = scopes_.lookup(name);
209 const auto id = append_symbol(kind, name, span);
210 const bool inserted = scopes_.insert(name, id);
212 << "Compiler_Symbol_Bindings::declare(): unexpected duplicate insertion for '" << name << "'";
213
214 return shadowed == nullptr
217 id,
218 *shadowed};
219 }
220
223 {
224 ah_out_of_range_error_unless(id > 0 and id <= symbols_.size())
225 << "Compiler_Symbol_Bindings::symbol(): invalid id " << id;
226 return symbols_.access(id - 1);
227 }
228
230 [[nodiscard]] std::string dump_symbols() const
231 {
232 std::ostringstream out;
233 out << "Symbols\n";
234 for (size_t i = 0; i < symbols_.size(); ++i)
235 {
236 const auto &sym = symbols_.access(i);
237 out << " #" << sym.id << " " << compiler_symbol_kind_name(sym.kind) << " " << sym.name
238 << " scope=" << sym.scope_depth << " @" << sym.declaration_span.to_string() << '\n';
239 }
240 return out.str();
241 }
242};
243} // namespace Aleph
244
245#endif
#define ah_runtime_error_unless(C)
Throws std::runtime_error if condition does NOT hold.
Definition ah-errors.H:255
#define ah_out_of_range_error_unless(C)
Throws std::out_of_range if condition does NOT hold.
Definition ah-errors.H:600
Source file and span management utilities for compiler-style tooling.
size_t size_t int32_t * out
Definition ca-c-api.h:120
Reusable lexical symbol table with stable ids and scope tracking.
Compiler_Symbol_Declare_Result declare(const Compiler_Symbol_Kind kind, const std::string &name, const Source_Span &span)
Declares one symbol in the current scope.
Compiler_Symbol_Id append_symbol(const Compiler_Symbol_Kind kind, const std::string &name, const Source_Span &span)
const Compiler_Symbol_Id * lookup_local(const std::string &name) const noexcept
Looks up one symbol id only in the current scope.
size_t depth() const noexcept
Returns the current lexical depth.
void clear() noexcept
Clears all scopes and previously recorded symbols.
const Compiler_Symbol & symbol(const Compiler_Symbol_Id id) const
Returns one symbol by its stable 1-based identifier.
std::string dump_symbols() const
Dumps the current symbol table in deterministic text form.
Scope< std::string, Compiler_Symbol_Id > scopes_
const Compiler_Symbol_Id * lookup(const std::string &name) const noexcept
Looks up one visible symbol id by name.
size_t symbol_count() const noexcept
Returns the number of recorded symbols.
void leave_scope()
Leaves the current lexical scope.
size_t enter_scope()
Enters one nested lexical scope.
Lexical scope stack for Key to Value associations.
Definition tpl_scope.H:88
void clear() noexcept
Resets the structure by removing all frames and bindings.
Definition tpl_scope.H:167
size_t enter_scope()
Creates a new visibility level.
Definition tpl_scope.H:179
Value * lookup(const Key &key) noexcept
Searches for a key traversing all active scopes (from inside out).
Definition tpl_scope.H:271
bool insert(const Key &key, const Value &value)
Registers a new association in the current scope.
Definition tpl_scope.H:215
Value * lookup_local(const Key &key) noexcept
Searches for a key only in the innermost (current) scope.
Definition tpl_scope.H:246
void leave_scope()
Destroys the current visibility level and all its associations.
Definition tpl_scope.H:195
size_t depth() const noexcept
Queries the current depth of the scope stack.
Definition tpl_scope.H:149
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
const char * compiler_symbol_kind_name(const Compiler_Symbol_Kind kind) noexcept
Returns a stable debug name for a symbol kind.
Compiler_Symbol_Kind
Generic symbol kinds reusable across multiple frontends.
Compiler_Symbol_Declare_Status
Result status for one declaration attempt.
and
Check uniqueness with explicit hash + equality functors.
size_t Compiler_Symbol_Id
@ Function
Function type such as fn(Int) -> Bool.
Structured result returned after one declaration attempt.
Compiler_Symbol_Id symbol_id
Newly inserted symbol or the conflicting local symbol.
bool inserted() const noexcept
Returns whether the declaration produced a new symbol.
Compiler_Symbol_Id related_symbol_id
Shadowed outer symbol or local duplicate.
bool shadowing() const noexcept
Returns whether the new declaration shadows one outer symbol.
bool duplicate_local() const noexcept
Returns whether the declaration collided with the current scope.
One reusable symbol record tracked by lexical bindings.
Compiler_Symbol_Id id
Stable symbol identifier, 1-based.
std::string name
Declared name.
Source_Span declaration_span
Declaration site.
Compiler_Symbol_Kind kind
Symbol category.
size_t scope_depth
Lexical depth where the symbol was introduced.
Half-open byte range inside a source file.
Definition ah-source.H:100
Lexical scope management using frame stacks.