Skip to content

Commit

Permalink
Support list coercion
Browse files Browse the repository at this point in the history
  • Loading branch information
kg583 committed Jan 3, 2025
1 parent 0ccc2e3 commit d2ae9ac
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
3 changes: 2 additions & 1 deletion tests/tivars.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")]

Expand All @@ -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)]

Expand Down
2 changes: 1 addition & 1 deletion tivars/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
else:
__all__ = ["TIEntry", "TIFlashHeader",
"TIReal",
"TIRealList", "TIMatrix",
"TIRealList", "TIMatrix", "TIList",
"TIEquation", "TIString",
"TIProgram", "TIAsmProgram", "TIProtectedProgram", "TIProtectedAsmProgram",
"TIPicture", "TIMonoPicture",
Expand Down
30 changes: 22 additions & 8 deletions tivars/types/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
"""
Expand All @@ -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
"""
Expand All @@ -223,4 +237,4 @@ class TIComplexList(ListEntry, register=True):
_type_id = 0x0D


__all__ = ["TIRealList", "TIComplexList"]
__all__ = ["TIList", "TIRealList", "TIComplexList"]
2 changes: 1 addition & 1 deletion tivars/types/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit d2ae9ac

Please sign in to comment.