data: rename Static to Implied, add compatibility wrapper

This commit is contained in:
Shiz 2022-05-10 13:27:54 +02:00
parent 493c4a03c5
commit 76fab6716d
2 changed files with 51 additions and 45 deletions

View File

@ -5,7 +5,7 @@ from .core.expr import BaseExpr, Expr, const, infer, bool_, not_, and_, or_, in_
from .core.meta import Wrapper, Generic
del core
from .types.data import Nothing, Static, Ignored, Pad, Data, data, Bits, bit, nibble
from .types.data import Nothing, Implied, Ignored, Pad, Data, data, Bits, bit, nibble
from .types.num import (
Bool, Int, Float, bool, int8, uint8, byte,
int16, uint16, int16be, int16le, uint16be, uint16le, word,
@ -24,4 +24,7 @@ from .types.control import Switch, If
from .types.io import Sized, Terminated, Ref, AlignTo, AlignedTo, Lazy
del types
# Compatibility
Static = Calc = Implied
__all__ = [k for k in globals() if not k.startswith('_')]

View File

@ -5,6 +5,8 @@ from ..core.meta import Wrapper
from ..core.expr import BaseExpr
T = TypeVar('T')
class Nothing(Type[None]):
def __init__(self) -> None:
pass
@ -27,6 +29,51 @@ class Nothing(Type[None]):
def __repr__(self) -> str:
return '{__name__}.Nothing()'
class Implied(G[T], Type[T]):
""" Parse/dump nothing, yield value. """
__slots__ = ('value',)
def __init__(self, value: U[BaseExpr[T], T]) -> None:
self.value = value
def parse(self, context: Context, stream: Stream) -> T:
return context.get(self.value)
def dump(self, context: Context, stream: Stream, value: T) -> None:
context.put(self.value, value)
def sizeof(self, context: Context, value: O[T]) -> O[Pos]:
return 0
def default(self, context: Context) -> T:
return context.peek(self.value)
def __str__(self) -> str:
return f'={self.value}'
def __repr__(self) -> str:
return f'{__name__}.Static({self.value!r})'
class Ignored(G[T], Wrapper[T]):
""" Parse/dump something, yield nothing. """
def __init__(self, child: Type[T]) -> None:
super().__init__(child)
def parse(self, context: Context, stream: Stream) -> None:
super().parse(context, stream)
def dump(self, context: Context, stream: Stream, value: None) -> None:
super().dump(context, stream, super().default(context))
def default(self, context: Context) -> None:
return None
def __str__(self) -> str:
return f'(void){super().__str__()}'
def __repr__(self) -> str:
return f'{__name__}.Ignored({super().__repr__()}'
class Pad(Type[None]):
""" Seek something, yield nothing. """
__slots__ = ('amount', 'value')
@ -122,47 +169,3 @@ class Bits(Type[int]):
bit = Bits(1)
nibble = Bits(4)
T = TypeVar('T')
class Static(G[T], Type[T]):
def __init__(self, value: U[BaseExpr[T], T]) -> None:
self.value = value
def parse(self, context: Context, stream: Stream) -> T:
return context.get(self.value)
def dump(self, context: Context, stream: Stream, value: T) -> None:
context.put(self.value, value)
def sizeof(self, context: Context, value: O[T]) -> O[Pos]:
return 0
def default(self, context: Context) -> T:
return context.peek(self.value)
def __str__(self) -> str:
return f'={self.value}'
def __repr__(self) -> str:
return f'{__name__}.Static({self.value!r})'
class Ignored(G[T], Wrapper[T]):
def __init__(self, child: Type[T]) -> None:
super().__init__(child)
def parse(self, context: Context, stream: Stream) -> None:
super().parse(context, stream)
def emit(self, context: Context, stream: Stream, value: None) -> None:
super().emit(context, stream, super().default(context))
def default(self, context: Context) -> None:
return None
def __str__(self) -> str:
return f'(void){super().__str__()}'
def __repr__(self) -> str:
return f'{__name__}.Ignored({super().__repr__()}'