Skip to content

Commit 498cbb6

Browse files
committed
Add queries to check is a name is reserved
Prior to this change, code which was checking whether a name was reserved in a given scope would need to fetch the relevant set of names and check whether the name was in it directly. This change adds query functions which allow the checks to be performed directly. The query functions recurse to the parent scope, rather than needing to fetch a new set containing this scope's names and the parent scope's.
1 parent 19309c8 commit 498cbb6

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/fluent_compiler/codegen.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,36 @@ def names_in_use(self):
122122
names = names | self.parent_scope.names_in_use()
123123
return names
124124

125+
def is_name_in_use(self, name: str) -> bool:
126+
if name in self.names:
127+
return True
128+
129+
if self.parent_scope is None:
130+
return False
131+
132+
return self.parent_scope.is_name_in_use(name)
133+
125134
def function_arg_reserved_names(self):
126135
names = self._function_arg_reserved_names
127136
if self.parent_scope is not None:
128137
names = names | self.parent_scope.function_arg_reserved_names()
129138
return names
130139

140+
def is_name_reserved_function_arg(self, name: str) -> bool:
141+
if name in self._function_arg_reserved_names:
142+
return True
143+
144+
if self.parent_scope is None:
145+
return False
146+
147+
return self.parent_scope.is_name_reserved_function_arg(name)
148+
131149
def all_reserved_names(self):
132150
return self.names_in_use() | self.function_arg_reserved_names()
133151

152+
def is_name_reserved(self, name: str) -> bool:
153+
return self.is_name_in_use(name) or self.is_name_reserved_function_arg(name)
154+
134155
def reserve_name(self, requested, function_arg=False, is_builtin=False, properties=None):
135156
"""
136157
Reserve a name as being in use in a scope.

0 commit comments

Comments
 (0)