From f365009de4949473ae1c53d66245f4e90be7c4aa Mon Sep 17 00:00:00 2001 From: tserg <8017125+tserg@users.noreply.github.com> Date: Sat, 21 Oct 2023 12:41:38 +0800 Subject: [PATCH] refactor constants namespace to helper --- vyper/semantics/analysis/module.py | 49 +++++++++++++++++------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/vyper/semantics/analysis/module.py b/vyper/semantics/analysis/module.py index 1f0680e0e4..b89e5e3685 100644 --- a/vyper/semantics/analysis/module.py +++ b/vyper/semantics/analysis/module.py @@ -56,6 +56,33 @@ def _find_cyclic_call(fn_names: list, self_members: dict) -> Optional[list]: return None +def _add_constants_to_namespace(module_nodes: list["VyperNode"], ns: Namespace): + const_var_decls = [ + n for n in module_nodes if isinstance(n, vy_ast.VariableDecl) and n.is_constant + ] + + while const_var_decls: + derived_nodes = 0 + + for c in const_var_decls: + name = c.get("target.id") + # Handle syntax errors downstream + if c.value is None: + continue + + val = prefold(c.value) + ns.add_constant(name, val) + + if val is not None: + derived_nodes += 1 + const_var_decls.remove(c) + + if not derived_nodes: + break + + return + + class ModuleAnalyzer(VyperNodeVisitorBase): scope_name = "module" @@ -68,28 +95,8 @@ def __init__( # TODO: Move computation out of constructor module_nodes = module_node.body.copy() - const_var_decls = [ - n for n in module_nodes if isinstance(n, vy_ast.VariableDecl) and n.is_constant - ] - - while const_var_decls: - derived_nodes = 0 - - for c in const_var_decls: - name = c.get("target.id") - # Handle syntax errors downstream - if c.value is None: - continue - - val = prefold(c.value) - self.namespace.add_constant(name, val) - - if val is not None: - derived_nodes += 1 - const_var_decls.remove(c) - if not derived_nodes: - break + _add_constants_to_namespace(module_nodes, namespace) while module_nodes: count = len(module_nodes)