data: add Static and Ignored types

This commit is contained in:
Shiz 2021-06-26 15:39:34 +02:00
parent 6924c94a13
commit 0b592af0e6
2 changed files with 50 additions and 3 deletions

View File

@ -4,7 +4,7 @@ from .core.io import Stream, Segment
from .core.meta import Wrapper, Generic
from .core.expr import Expr
from .types.data import Nothing, Data, data
from .types.data import Nothing, Static, Ignored, Data, data
from .types.num import *
from .types.str import Str, StrType
from .types.struct import StructType, Struct
@ -19,7 +19,8 @@ __all__ = [x.__name__ for x in {
Wrapper, Default, Sized, Ref, Transform, Mapped, Enum,
Switch, If,
AlignTo, AlignedTo,
Nothing, Data,
Nothing, Data, Static, Ignored,
Int, Bool, Float, Str, StrType,
Arr, Tuple,
StructType, Struct, Generic,

View File

@ -1,6 +1,8 @@
from typing import Optional as O, Union as U
from typing import Optional as O, Union as U, Any, Generic as G, TypeVar
from ..core.base import Type, Context, PossibleDynamic as D
from ..core.io import Stream, Pos
from ..core.meta import Wrapper
from ..core.expr import BaseExpr
class Nothing(Type[None]):
@ -50,3 +52,47 @@ class Data(Type[bytes]):
return context.peek(self.size)
data = Data()
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: Cntext, 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__()}'