diff options
| author | Richard Qin <richard.qin.x@gmail.com> | 2026-04-03 02:46:12 +0800 |
|---|---|---|
| committer | Richard Qin <richard.qin.x@gmail.com> | 2026-04-03 02:46:12 +0800 |
| commit | 9ab592ce3765d07b6dbdb9c1d9b62dc8045e784e (patch) | |
| tree | 5b2bf93147980c5dabef80eff4c6209caa95f034 /src/core/symbol_table.cc | |
| parent | 1e35f078fe5ace02b22e6f4ba89f57ba4a58b8c9 (diff) | |
| download | ratp-9ab592ce3765d07b6dbdb9c1d9b62dc8045e784e.tar.gz ratp-9ab592ce3765d07b6dbdb9c1d9b62dc8045e784e.tar.xz | |
feat: implement core term, symbol, and clause storage logic with canonicalization support
Diffstat (limited to 'src/core/symbol_table.cc')
| -rw-r--r-- | src/core/symbol_table.cc | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/core/symbol_table.cc b/src/core/symbol_table.cc index b62ffd0..261805a 100644 --- a/src/core/symbol_table.cc +++ b/src/core/symbol_table.cc @@ -16,4 +16,47 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ +#include "atp/core/symbol_table.h" +#include "atp/core/types.h" +#include <cstddef> +#include <stdexcept> +#include <string_view> + +namespace atp { + +SymbolId SymbolTable::intern(std::string_view name, SymbolKind kind, uint16_t arity, + SortId sort) { + auto it = name_to_id_.find(name); + if (it != name_to_id_.end()) { + SymbolId id = it->second; + SymbolInfo& info = infos_[id]; + if (info.kind != kind || info.arity != arity) { + throw std::runtime_error("Symbol kind or arity mismatch for: " + std::string(name)); + } + return id; + } + auto new_id = static_cast<SymbolId>(infos_.size()); + infos_.push_back({.name = std::string(name), .kind = kind, .arity = arity, .sort = sort}); + std::string_view stable_name_view = infos_.back().name; + name_to_id_.emplace(stable_name_view, new_id); + return new_id; +} + +std::string_view SymbolTable::getName(SymbolId id) const { + return infos_[id].name; +} + +const SymbolInfo& SymbolTable::getInfo(SymbolId id) const { + return infos_[id]; +} + +bool SymbolTable::isVariable(SymbolId id) const { + return infos_[id].kind == SymbolKind::kVariable; +} + +size_t SymbolTable::size() const { + return infos_.size(); +} + +} // namespace atp |
