Skip to content

Commit

Permalink
Use type ID references over constants
Browse files Browse the repository at this point in the history
  • Loading branch information
kg583 committed Nov 17, 2024
1 parent 585d33c commit 6478b68
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
23 changes: 11 additions & 12 deletions tivars/types/complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ def __complex__(self):
def __format__(self, format_spec: str) -> str:
def make_imag(entry: 'RealEntry') -> str:
match entry.type_id:
case 0x18 | 0x21:
case TIRealFraction.type_id | TIRealPiFraction.type_id:
return format(entry, format_spec).replace("/", "i/")

case 0x1C:
case TIRealRadical.type_id:
return f"{entry:{format_spec}} * i"

case _:
Expand Down Expand Up @@ -247,8 +247,8 @@ def imag_type(self) -> type['RealEntry']:
def clear(self):
super().clear()

self.real_subtype_id = 0x0C
self.imag_subtype_id = self._type_id if self._type_id is not None else 0x0C
self.real_subtype_id = TIComplex.type_id
self.imag_subtype_id = self._type_id if self._type_id is not None else TIComplex.type_id

def components(self) -> (RealEntry, RealEntry):
"""
Expand All @@ -258,13 +258,11 @@ def components(self) -> (RealEntry, RealEntry):
return self.real, self.imag

def get_min_os(self, data: bytes = None) -> OsVersion:
data = data or self.data

match max(data[0], data[9]):
case 0x0C:
match self.get_version(data):
case 0x00:
return TI_83.OS()

case 0x1B:
case 0x0B:
return TI_84P.OS("2.55")

case _:
Expand All @@ -274,10 +272,10 @@ def get_version(self, data: bytes = None) -> int:
data = data or self.data

match max(data[0], data[9]):
case 0x0C:
case TIComplex.type_id:
return 0x00

case 0x1B:
case TIComplexFraction.type_id:
return 0x0B

case _:
Expand Down Expand Up @@ -326,7 +324,8 @@ def load_string(self, string: str):
self.real = self.real_type(parts[0])

except (TypeError, ValueError):
for type_id in [0x00, 0x18, 0x1C, 0x20, 0x21]:
for type_id in [TIReal.type_id, TIRealFraction.type_id, TIRealRadical.type_id,
TIRealPi.type_id, TIRealPiFraction.type_id]:
if type_id == self.real_subtype_id:
continue

Expand Down
11 changes: 7 additions & 4 deletions tivars/types/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
from tivars.data import *
from tivars.models import *
from tivars.var import TIEntry, SizedEntry
from .appvar import *
from .gdb import TIGraphedEquation
from .list import *
from .tokenized import *


class TIGroup(SizedEntry, register=True):
Expand Down Expand Up @@ -75,10 +78,10 @@ def group(entries: Sequence[TIEntry], *, name: str = "GROUP") -> 'TIGroup':
vat[0] |= entry.raw.flags

match entry.type_id:
case 0x05 | 0x06 | 0x15 | 0x17:
case TIProgram.type_id | TIProtectedProgram.type_id | TIAppVar.type_id | TIGroup.type_id:
vat += bytearray([len(name), *name])

case 0x01 | 0x0D:
case TIRealList.type_id | TIComplexList.type_id:
vat += bytearray([len(name) + 1, *name, 0])

case _:
Expand Down Expand Up @@ -113,7 +116,7 @@ def ungroup(self, data: bytes = None) -> list[TIEntry]:
_, version = data.read(2)

match type_id := type_byte[0] & 63:
case 0x05 | 0x06 | 0x15 | 0x17:
case TIProgram.type_id | TIProtectedProgram.type_id | TIAppVar.type_id | TIGroup.type_id:
*_, page, length = data.read(4)

if length > 8:
Expand All @@ -122,7 +125,7 @@ def ungroup(self, data: bytes = None) -> list[TIEntry]:

name = data.read(length)

case 0x01 | 0x0D:
case TIRealList.type_id | TIComplexList.type_id:
*_, page, length = data.read(4)

if length > 7:
Expand Down
12 changes: 6 additions & 6 deletions tivars/types/tokenized.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,15 @@ def protect(self):
Cast this program to a protected program
"""

self.type_id = 0x06
self.type_id = TIProtectedProgram.type_id
self.coerce()

def unprotect(self):
"""
Cast this program to an unprotected program
"""

self.type_id = 0x05
self.type_id = TIProgram.type_id
self.coerce()

@Loader[bytes, bytearray, BytesIO]
Expand Down Expand Up @@ -359,13 +359,13 @@ def coerce(self):
doors &= b"\xEF\x68" in self.data and self.data.index(b"\xEF\x68") > 0

match self.type_id, any(token in self.data for token in self.asm_tokens) | doors:
case 0x05, False:
case TIProgram.type_id, False:
self.__class__ = TIProgram
case 0x05, True:
case TIProgram.type_id, True:
self.__class__ = TIAsmProgram
case 0x06, False:
case TIProtectedProgram.type_id, False:
self.__class__ = TIProtectedProgram
case 0x06, True:
case TIProtectedProgram.type_id, True:
self.__class__ = TIProtectedAsmProgram


Expand Down

0 comments on commit 6478b68

Please sign in to comment.