diff --git a/tests/tivars.py b/tests/tivars.py index e5bd31b..7472725 100644 --- a/tests/tivars.py +++ b/tests/tivars.py @@ -4,7 +4,6 @@ from decimal import Decimal from tivars.models import * -from tivars.tokenizer import * from tivars.types import * from tivars import TIHeader, TIVar, TIFlashHeader @@ -355,6 +354,7 @@ def test_coercion(self): class ArrayTests(unittest.TestCase): def test_real_list(self): test_real_list = TIRealList.open("tests/data/var/RealList.8xl") + self.assertEqual(test_real_list, TIList.open("tests/data/var/RealList.8xl")) test_list = [TIReal("-1.0"), TIReal("2.0"), TIReal("999")] @@ -371,6 +371,7 @@ def test_real_list(self): def test_complex_list(self): test_comp_list = TIComplexList.open("tests/data/var/ComplexList.8xl") + self.assertEqual(test_comp_list, TIList.open("tests/data/var/ComplexList.8xl")) test_list = [TIComplex(1 + 1j), TIComplex("-3 + 2i"), TIComplex(4 + 0j)] diff --git a/tivars/types/__init__.py b/tivars/types/__init__.py index 2e56c78..10016c9 100644 --- a/tivars/types/__init__.py +++ b/tivars/types/__init__.py @@ -28,7 +28,7 @@ else: __all__ = ["TIEntry", "TIFlashHeader", "TIReal", - "TIRealList", "TIMatrix", + "TIRealList", "TIMatrix", "TIList", "TIEquation", "TIString", "TIProgram", "TIAsmProgram", "TIProtectedProgram", "TIProtectedAsmProgram", "TIPicture", "TIMonoPicture", diff --git a/tivars/types/list.py b/tivars/types/list.py index 78ea29b..eddfdfc 100644 --- a/tivars/types/list.py +++ b/tivars/types/list.py @@ -11,8 +11,8 @@ from tivars.models import * from tivars.tokenizer import * from tivars.var import TIEntry -from .complex import ComplexEntry -from .real import RealEntry +from .complex import * +from .real import * class ListName(Name): @@ -67,7 +67,7 @@ def set(cls, value: _T, **kwargs) -> bytes: return super().set(varname[-5:]) -class ListEntry(TIEntry): +class TIList(TIEntry): """ Base class for all list entries @@ -154,9 +154,9 @@ def supported_by(self, model: TIModel) -> bool: def load_bytes(self, data: bytes | BytesIO): super().load_bytes(data) - if self.calc_data_length // self._E.min_data_length != self.length: + if self._E.min_data_length and self.calc_data_length // self._E.min_data_length != self.length: warn(f"The list has an unexpected length " - f"(expected {self.calc_data_length // self._E.min_data_length}, got {self.length}).", + f"(expected {self.length}, got {self.calc_data_length // self._E.min_data_length}).", BytesWarning) @Loader[Sequence] @@ -189,8 +189,22 @@ def load_string(self, string: str): self.load_list(lst) + def coerce(self): + match self.data[0] & 31: + case TIReal.type_id | TIUndefinedReal.type_id | TIRealFraction.type_id \ + | TIRealRadical.type_id | TIRealPi.type_id | TIRealPiFraction.type_id: + self.__class__ = TIRealList -class TIRealList(ListEntry, register=True): + case TIComplex.type_id | TIComplexFraction.type_id \ + | TIComplexRadical.type_id | TIComplexPi.type_id | TIComplexPiFraction.type_id: + self.__class__ = TIComplexList + + case _: + warn("List contains unrecognized type(s); no coercion will occur.", + UserWarning) + + +class TIRealList(TIList, register=True): """ Parser for lists of real numbers """ @@ -207,7 +221,7 @@ class TIRealList(ListEntry, register=True): _type_id = 0x01 -class TIComplexList(ListEntry, register=True): +class TIComplexList(TIList, register=True): """ Parser for lists of complex numbers """ @@ -223,4 +237,4 @@ class TIComplexList(ListEntry, register=True): _type_id = 0x0D -__all__ = ["TIRealList", "TIComplexList"] +__all__ = ["TIList", "TIRealList", "TIComplexList"] diff --git a/tivars/types/matrix.py b/tivars/types/matrix.py index 6fc939d..aece23b 100644 --- a/tivars/types/matrix.py +++ b/tivars/types/matrix.py @@ -142,7 +142,7 @@ def load_bytes(self, data: bytes | BytesIO): if self.calc_data_length // RealEntry.min_data_length != self.size: warn(f"The matrix has an unexpected size " - f"(expected {self.calc_data_length // RealEntry.min_data_length}, got {self.size}).", + f"(expected {self.size}, got {self.calc_data_length // RealEntry.min_data_length}).", BytesWarning) def load_data_section(self, data: BytesIO):