simplified and working perfect

This commit is contained in:
Jesse Bannon 2023-11-22 09:38:09 -08:00
parent 333cd329a1
commit d135f603c0

View file

@ -45,40 +45,30 @@ class SyntaxTree(VariableDependency):
return String("".join([str(res) for res in resolved]))
@classmethod
def _get_custom_function_dependencies(
def _traverse_custom_function_dependencies(
cls,
custom_function_name: str,
custom_function_dependency: "SyntaxTree",
custom_functions: Dict[str, "SyntaxTree"],
deps: List[str],
) -> List[str]:
deps = copy.deepcopy(deps) # do not work with references
) -> None:
for dep in custom_function_dependency.custom_functions:
# Skip leaf functions since they will never cause a cycle
if not custom_functions[dep.name].custom_functions:
continue
deps.append(dep.name)
if custom_function_name in deps:
cycle_deps = [custom_function_name] + deps[0 : deps.index(custom_function_name) + 1]
if custom_function_name in deps + [dep.name]:
cycle_deps = [custom_function_name] + deps + [dep.name]
cycle_deps_str = " -> ".join([f"%{name}" for name in cycle_deps])
raise CycleDetected(f"Custom functions contain a cycle: {cycle_deps_str}")
deps += cls._get_custom_function_dependencies(
cls._traverse_custom_function_dependencies(
custom_function_name=custom_function_name,
custom_function_dependency=custom_functions[dep.name],
custom_functions=custom_functions,
deps=deps,
deps=deps + [dep.name],
)
return deps
@classmethod
def _ensure_no_custom_function_cycles(cls, custom_functions: Dict[str, "SyntaxTree"]):
for custom_function_name, custom_function in custom_functions.items():
_ = cls._get_custom_function_dependencies(
cls._traverse_custom_function_dependencies(
custom_function_name=custom_function_name,
custom_function_dependency=custom_function,
custom_functions=custom_functions,