builtup4/bu4/isynced.py

32 lines
1.2 KiB
Python

# Copyright (c) PARRRATE T&V 2021. All rights reserved.
from pathlib import Path
from textwrap import fill
from time import time_ns
from bu4.combinatory.tocombinatory import to_combinatory
from bu4.evaluation.constructs.evalue import EValue
from bu4.evaluation.sync import sync
from bu4.linkable.toolchain.parse import parse
from bu4.linkable.toolchain.stdext import standard_extensions
from bu4.linkable.toolchain.transply import transply
from bu4.transform.transform import transform
__all__ = ('isynced',)
def isynced(source: str, *, combinatory=False) -> EValue:
bsource = transply(source)
parsed = parse(bsource, standard_extensions)
linked = transform(parsed.link(set()))
if combinatory:
linked = transform(to_combinatory(linked))
root = Path('compiled') / f'{round(time_ns())}'
root.mkdir(parents=True)
(root / 'source.bu4').write_text(source, encoding='utf-8')
indexed = transform(linked.index([]))
evaluable = indexed.attach([])
bytesrc = ' '.join(f'{chr(c)} ' if c in range(0x20, 0xff) else f'{c:02x}' for c in bytes(indexed))
(root / 'compiled.txt').write_text(fill(bytesrc, width=120), encoding='utf-8')
return sync(evaluable)