37# include <gtest/gtest.h>
46TEST(CompilerScope, EnterLookupAndShadowAcrossNestedScopes)
50 EXPECT_TRUE(scope.
empty());
52 EXPECT_FALSE(scope.
empty());
54 EXPECT_TRUE(scope.
insert(
"x", 1));
55 EXPECT_FALSE(scope.
insert(
"x", 2));
56 ASSERT_NE(scope.
lookup(
"x"),
nullptr);
57 EXPECT_EQ(*scope.
lookup(
"x"), 1);
62 ASSERT_NE(scope.
lookup(
"x"),
nullptr);
63 EXPECT_EQ(*scope.
lookup(
"x"), 1);
66 EXPECT_TRUE(scope.
insert(
"x", 2));
67 ASSERT_NE(scope.
lookup(
"x"),
nullptr);
68 EXPECT_EQ(*scope.
lookup(
"x"), 2);
73 ASSERT_NE(scope.
lookup(
"x"),
nullptr);
74 EXPECT_EQ(*scope.
lookup(
"x"), 1);
77 EXPECT_TRUE(scope.
empty());
78 EXPECT_EQ(scope.
lookup(
"x"),
nullptr);
82TEST(CompilerScope, ClearDropsAllFramesAndBindings)
90 ASSERT_NE(scope.
lookup(
"a"),
nullptr);
91 ASSERT_NE(scope.
lookup(
"b"),
nullptr);
95 EXPECT_TRUE(scope.
empty());
96 EXPECT_EQ(scope.
lookup(
"a"),
nullptr);
97 EXPECT_EQ(scope.
lookup(
"b"),
nullptr);
101TEST(CompilerScope, ReenteringScopeAfterLeaveKeepsBindingsUsable)
105 ASSERT_TRUE(scope.
insert(
"root", 1));
108 ASSERT_TRUE(scope.
insert(
"first", 2));
111 ASSERT_NE(scope.
lookup(
"root"),
nullptr);
112 EXPECT_EQ(*scope.
lookup(
"root"), 1);
115 EXPECT_TRUE(scope.
insert(
"second", 3));
116 ASSERT_NE(scope.
lookup(
"second"),
nullptr);
117 EXPECT_EQ(*scope.
lookup(
"second"), 3);
118 ASSERT_NE(scope.
lookup(
"root"),
nullptr);
119 EXPECT_EQ(*scope.
lookup(
"root"), 1);
123TEST(CompilerScope, LeaveOnEmptyScopeThrows)
126 EXPECT_THROW(scope.
leave_scope(), std::runtime_error);
Lexical scope stack for Key to Value associations.
void clear() noexcept
Resets the structure by removing all frames and bindings.
size_t enter_scope()
Creates a new visibility level.
Value * lookup(const Key &key) noexcept
Searches for a key traversing all active scopes (from inside out).
bool empty() const noexcept
Indicates if the structure is empty of frames.
bool insert(const Key &key, const Value &value)
Registers a new association in the current scope.
Value * lookup_local(const Key &key) noexcept
Searches for a key only in the innermost (current) scope.
void leave_scope()
Destroys the current visibility level and all its associations.
Main namespace for Aleph-w library functions.
Lexical scope management using frame stacks.