This commit is contained in:
Jesse Bannon 2023-09-20 16:22:38 -07:00
parent ca1fc6a227
commit 182bda9bf4
5 changed files with 38 additions and 6 deletions

View file

@ -1,5 +1,6 @@
from ytdl_sub.script.functions.numeric_functions import NumericFunctions
from ytdl_sub.script.functions.string_functions import StringFunctions from ytdl_sub.script.functions.string_functions import StringFunctions
class Functions(StringFunctions): class Functions(StringFunctions, NumericFunctions):
pass pass

View file

@ -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)

View file

@ -3,9 +3,9 @@ from typing import List
from typing import Optional from typing import Optional
from typing import Set from typing import Set
from ytdl_sub.script.functions import Resolvable
from ytdl_sub.script.parser import parse from ytdl_sub.script.parser import parse
from ytdl_sub.script.syntax_tree import SyntaxTree 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.script.types.variable import Variable
from ytdl_sub.utils.exceptions import StringFormattingException from ytdl_sub.utils.exceptions import StringFormattingException

View file

@ -133,13 +133,13 @@ class Function(VariableDependency):
received_type_names: List[str] = [] received_type_names: List[str] = []
for arg in self.args: for arg in self.args:
if isinstance(arg, Function): 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: else:
received_type_names.append(arg.__class__.__name__) received_type_names.append(arg.__class__.__name__)
received_args_str = f"({', '.join([name for name in received_type_names])})" 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 @property
def callable(self) -> Callable[..., Resolvable]: def callable(self) -> Callable[..., Resolvable]:

View file

@ -30,12 +30,12 @@ class Numeric(ResolvableT[NumericT], ABC, Generic[NumericT]):
@dataclass(frozen=True) @dataclass(frozen=True)
class Integer(ResolvableT[int]): class Integer(Numeric[int]):
pass pass
@dataclass(frozen=True) @dataclass(frozen=True)
class Float(ResolvableT[float]): class Float(Numeric[float]):
pass pass