36#include <gtest/gtest.h>
42TEST(CompilerSymbolBindings, DeclaresVisibleSymbolsAcrossNestedScopes)
47 const auto global = bindings.
declare(Compiler_Symbol_Kind::Global,
"value", {});
48 ASSERT_TRUE(global.inserted());
52 const auto local = bindings.
declare(Compiler_Symbol_Kind::Local,
"temp", {});
53 ASSERT_TRUE(local.inserted());
56 ASSERT_NE(
value,
nullptr);
57 EXPECT_EQ(bindings.
symbol(*value).
kind, Compiler_Symbol_Kind::Global);
59 const auto * temp = bindings.
lookup(
"temp");
60 ASSERT_NE(temp,
nullptr);
61 EXPECT_EQ(bindings.
symbol(*temp).
kind, Compiler_Symbol_Kind::Local);
64TEST(CompilerSymbolBindings, ReportsDuplicatesAndShadowingStructurally)
69 const auto first = bindings.
declare(Compiler_Symbol_Kind::Global,
"answer", {});
70 ASSERT_TRUE(first.inserted());
72 const auto duplicate = bindings.
declare(Compiler_Symbol_Kind::Global,
"answer", {});
73 EXPECT_TRUE(duplicate.duplicate_local());
74 EXPECT_EQ(duplicate.symbol_id, first.symbol_id);
77 const auto shadow = bindings.
declare(Compiler_Symbol_Kind::Local,
"answer", {});
78 EXPECT_TRUE(shadow.inserted());
79 EXPECT_TRUE(shadow.shadowing());
80 EXPECT_EQ(shadow.related_symbol_id, first.symbol_id);
83TEST(CompilerSymbolBindings, DumpsSymbolsDeterministically)
88 bindings.enter_scope();
89 (void) bindings.declare(Compiler_Symbol_Kind::Parameter,
"x",
Source_Span{});
91 const auto dump = bindings.dump_symbols();
92 EXPECT_NE(dump.find(
"Symbols"), std::string::npos);
93 EXPECT_NE(dump.find(
"Function add"), std::string::npos);
94 EXPECT_NE(dump.find(
"Parameter x"), std::string::npos);
Reusable lexical bindings and symbol records for frontends.
size_t size_t int32_t value
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.
const Compiler_Symbol & symbol(const Compiler_Symbol_Id id) const
Returns one symbol by its stable 1-based identifier.
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.
size_t enter_scope()
Enters one nested lexical scope.
Main namespace for Aleph-w library functions.
Compiler_Symbol_Kind kind
Symbol category.
Half-open byte range inside a source file.