-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
36 lines (29 loc) · 810 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python3
from typing import Final
from sys import argv
import re
# this should accept pre-compiled `re.Pattern`s,
# but IDK how to do that properly
def retran_exec(src: str, replace: str | bytes, dat: str | bytes):
'''
this is intended to be imported
'''
prg: Final = re.compile(src, re.X)
while dat != (dat := prg.sub(replace, dat)):
yield dat
def main(*args: str):
if len(args) == 0:
print('usage: retran <program file> [input file]')
with open(args[0], 'r') as f:
src = f.read()
dat = ''
if len(args) > 1:
with open(args[1], 'r') as f:
dat = f.read()
# order is very important!
# `src` can contain \x2f, but `replace` can't
(src, replace) = src.split('//', 1)
for gen in retran_exec(src, replace, dat):
print(gen)
if __name__ == '__main__':
main(*argv[1:])