builtup4/bu4/parsing/toolchain/transply.py

41 lines
1.1 KiB
Python

from io import BytesIO
from bu4.parsing.codes import *
__all__ = ('transply',)
def transply(source: str) -> bytes:
b = BytesIO()
for c in source:
if c.isspace():
pass
elif c in '/':
b.write(bytes([CODE_CALL]))
elif c in '{':
b.write(bytes([CODE_CALL, CODE_QOPN]))
elif c in '(':
b.write(bytes([CODE_MAKE]))
elif c in '|':
b.write(bytes([CODE_QCLS, CODE_MAKE]))
elif c in '})':
b.write(bytes([CODE_NULL]))
elif c in '#':
b.write(bytes([CODE_SKIP]))
elif c in '?':
b.write(bytes([CODE_QOPN, CODE_NULL, CODE_QCLS]))
elif c in ']':
b.write(bytes([CODE_NULL, CODE_QCLS]))
elif c in '[':
b.write(bytes([CODE_QOPN, CODE_NAME]))
elif c in '"':
b.write(bytes([CODE_QUOT]))
elif c in '<':
b.write(bytes([CODE_QOPN]))
elif c in '>':
b.write(bytes([CODE_QCLS]))
else:
value = c.encode()
b.write(value if len(value) == 1 else value.hex().encode())
return b.getvalue()