diff --git a/src/ytdl_sub/script/functions/__init__.py b/src/ytdl_sub/script/functions/__init__.py index 9b48ab2a..b16fa070 100644 --- a/src/ytdl_sub/script/functions/__init__.py +++ b/src/ytdl_sub/script/functions/__init__.py @@ -1,5 +1,6 @@ +from ytdl_sub.script.functions.numeric_functions import NumericFunctions from ytdl_sub.script.functions.string_functions import StringFunctions -class Functions(StringFunctions): +class Functions(StringFunctions, NumericFunctions): pass diff --git a/src/ytdl_sub/script/functions/numeric_functions.py b/src/ytdl_sub/script/functions/numeric_functions.py new file mode 100644 index 00000000..ba86bde2 --- /dev/null +++ b/src/ytdl_sub/script/functions/numeric_functions.py @@ -0,0 +1,31 @@ +from ytdl_sub.script.types.resolvable import Float +from ytdl_sub.script.types.resolvable import Integer +from ytdl_sub.script.types.resolvable import Numeric + + +def _to_numeric(value: int | float) -> Numeric: + if int(value) == value: + return Integer(value=value) + return Float(value=value) + + +class NumericFunctions: + @staticmethod + def add(left: Numeric, right: Numeric) -> Numeric: + return _to_numeric(left.value + right.value) + + @staticmethod + def sub(left: Numeric, right: Numeric) -> Numeric: + return _to_numeric(left.value - right.value) + + @staticmethod + def mul(left: Numeric, right: Numeric) -> Numeric: + return _to_numeric(left.value * right.value) + + @staticmethod + def div(left: Numeric, right: Numeric) -> Numeric: + return _to_numeric(left.value / right.value) + + @staticmethod + def mod(value: Integer, modulo: Integer) -> Integer: + return Integer(value=value.value % modulo.value) diff --git a/src/ytdl_sub/script/overrides_resolver.py b/src/ytdl_sub/script/overrides_resolver.py index 34a58a9d..39955f78 100644 --- a/src/ytdl_sub/script/overrides_resolver.py +++ b/src/ytdl_sub/script/overrides_resolver.py @@ -3,9 +3,9 @@ from typing import List from typing import Optional from typing import Set -from ytdl_sub.script.functions import Resolvable from ytdl_sub.script.parser import parse from ytdl_sub.script.syntax_tree import SyntaxTree +from ytdl_sub.script.types.resolvable import Resolvable from ytdl_sub.script.types.variable import Variable from ytdl_sub.utils.exceptions import StringFormattingException diff --git a/src/ytdl_sub/script/types/function.py b/src/ytdl_sub/script/types/function.py index 238c4cc9..baf1b3d2 100644 --- a/src/ytdl_sub/script/types/function.py +++ b/src/ytdl_sub/script/types/function.py @@ -133,13 +133,13 @@ class Function(VariableDependency): received_type_names: List[str] = [] for arg in self.args: if isinstance(arg, Function): - received_type_names.append(f"{arg.name}(...)->{arg.output_type.__name__}") + received_type_names.append(f"%{arg.name}(...)->{arg.output_type.__name__}") else: received_type_names.append(arg.__class__.__name__) received_args_str = f"({', '.join([name for name in received_type_names])})" - return f"Expected {self.input_spec.expected_args_str()}.\nReceived ({received_args_str})" + return f"Expected {self.input_spec.expected_args_str()}.\nReceived {received_args_str}" @property def callable(self) -> Callable[..., Resolvable]: diff --git a/src/ytdl_sub/script/types/resolvable.py b/src/ytdl_sub/script/types/resolvable.py index 7e5aab97..815cebb1 100644 --- a/src/ytdl_sub/script/types/resolvable.py +++ b/src/ytdl_sub/script/types/resolvable.py @@ -30,12 +30,12 @@ class Numeric(ResolvableT[NumericT], ABC, Generic[NumericT]): @dataclass(frozen=True) -class Integer(ResolvableT[int]): +class Integer(Numeric[int]): pass @dataclass(frozen=True) -class Float(ResolvableT[float]): +class Float(Numeric[float]): pass