Skip to content

Commit

Permalink
refactor constants namespace to helper
Browse files Browse the repository at this point in the history
  • Loading branch information
tserg committed Oct 21, 2023
1 parent e04ecf5 commit f365009
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions vyper/semantics/analysis/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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)
Expand Down

0 comments on commit f365009

Please sign in to comment.