more dedupe

This commit is contained in:
Jesse Bannon 2023-11-24 00:51:27 -08:00
parent 172a26eaba
commit 253738181e

View file

@ -115,35 +115,18 @@ class Script:
f"do not increment from $0 to ${len(indices) - 1}." f"do not increment from $0 to ${len(indices) - 1}."
) )
def _ensure_custom_function_usage_num_input_arguments_valid(self): def _ensure_custom_function_usage_num_input_arguments_valid(
for variable_name, variable_definition in self._variables.items(): self, definitions: Dict[str, SyntaxTree], prefix: str
for nested_custom_function in variable_definition.custom_functions: ):
for name, definition in definitions.items():
for nested_custom_function in definition.custom_functions:
if nested_custom_function.num_input_args != ( if nested_custom_function.num_input_args != (
expected_num_args := len( expected_num_args := len(
self._functions[nested_custom_function.name].function_arguments self._functions[nested_custom_function.name].function_arguments
) )
): ):
raise InvalidCustomFunctionArguments( raise InvalidCustomFunctionArguments(
f"Variable {variable_name} has invalid usage of the custom " f"{prefix}{name} has invalid usage of the custom "
f"function %{nested_custom_function.name}: Expects {expected_num_args} "
f"argument{'s' if expected_num_args > 1 else ''} but received "
f"{nested_custom_function.num_input_args}"
)
# TODO: DEDUPLICATE
for function_name, function_definition in self._functions.items():
for nested_custom_function in function_definition.custom_functions:
if nested_custom_function.name == function_name:
# Do not need to validate a cycle that should not exist
continue
if nested_custom_function.num_input_args != (
expected_num_args := len(
self._functions[nested_custom_function.name].function_arguments
)
):
raise InvalidCustomFunctionArguments(
f"Custom function %{function_name} has invalid usage of the custom "
f"function %{nested_custom_function.name}: Expects {expected_num_args} " f"function %{nested_custom_function.name}: Expects {expected_num_args} "
f"argument{'s' if expected_num_args > 1 else ''} but received " f"argument{'s' if expected_num_args > 1 else ''} but received "
f"{nested_custom_function.num_input_args}" f"{nested_custom_function.num_input_args}"
@ -205,14 +188,17 @@ class Script:
self._ensure_no_custom_function_cycles() self._ensure_no_custom_function_cycles()
self._ensure_custom_function_arguments_valid() self._ensure_custom_function_arguments_valid()
self._ensure_no_variable_cycles() self._ensure_no_variable_cycles()
self._ensure_custom_function_usage_num_input_arguments_valid()
self._ensure_lambda_usage_num_input_arguments_valid( for prefix, definitions in (
definitions=self._variables, prefix="Variable " ("Variable ", self._variables),
) ("Custom function %", self._functions),
self._ensure_lambda_usage_num_input_arguments_valid( ):
definitions=self._functions, prefix="Custom function %" self._ensure_custom_function_usage_num_input_arguments_valid(
) prefix=prefix, definitions=definitions
)
self._ensure_lambda_usage_num_input_arguments_valid(
prefix=prefix, definitions=definitions
)
def __init__(self, script: Dict[str, str]): def __init__(self, script: Dict[str, str]):
function_names: Set[str] = { function_names: Set[str] = {