"""Splitting Ren'Py "simple expression" values out of a line (screens and ATL).""" import re OPERATORS = {"+", "-", "*", "/", "//", "%", "**", "==", "!=", "<", ">", "<=", ">=", "and", "or", "in", "is", "if", "else", "not"} def read_value(words: list[str], i: int) -> tuple[str, int]: """One property value starting at words[i]. Decompiled sources keep operators unparenthesised (``text "Day " + str(day)``), so a value continues while the next word is an operator.""" parts = [words[i]] i += 1 if parts[0] in ("not", "-") and i < len(words): parts.append(words[i]) i += 1 while i < len(words): if words[i].startswith("(") and re.search(r"[\w\])]$", parts[-1]): # `ToggleVariable ("x")` parts[-1] += words[i] i += 1 elif parts[-1].endswith(",") or parts[-1] in OPERATORS: # `A(), B()` / `a +` continue parts.append(words[i]) i += 1 elif i + 1 < len(words) and words[i] in OPERATORS: parts.extend([words[i], words[i + 1]]) i += 2 else: break return " ".join(parts), i