-
-
Save yanqui-uxo/55d2b77b35ae35cf62e40720bb024218 to your computer and use it in GitHub Desktop.
Tetanus esolang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import regex as re | |
import sys | |
# Please let me know about any bugs! I can be reached at Yanqui#2401 on Discord | |
# Last argument is program filename | |
prog_filename = sys.argv[-1] | |
with open(prog_filename) as f: | |
lines = f.read().split("\n") | |
pattern = re.compile(lines[0]) | |
replacement = lines[1] | |
if len(lines) > 2: | |
data = "\n".join(lines[2:]) | |
else: | |
data = "" | |
special_groups = {"output": [], "input": []} | |
ops = ["~", "`"] | |
not_escaped_lookbehind = r"(?<!(?<!\\)\\(?:\\\\)*)" | |
group_pattern = r"(?|\\(\d+)|\\g<([^>]+)>)" | |
op_pattern = r"(~`|`~|[~`])" | |
for group, op in re.findall( | |
rf"{not_escaped_lookbehind}{group_pattern}{op_pattern}", replacement): | |
if group.isdigit(): | |
group = int(group) | |
if "~" in op: | |
special_groups["output"].append(group) | |
if "`" in op: | |
special_groups["input"].append(group) | |
replacement = re.sub( | |
rf"{not_escaped_lookbehind}({group_pattern}){op_pattern}", r"\1", replacement | |
) | |
while True: | |
search = pattern.search(data) | |
if search: | |
data = pattern.sub(replacement, data) | |
for g in special_groups["output"]: | |
captured_text = search.group(g) | |
if captured_text: | |
print(captured_text, end="") | |
for g in special_groups["input"]: | |
if search.group(g): | |
data += input() | |
else: | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tetanus