tfbpseudo 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
tfbpseudo/__init__.py ADDED
@@ -0,0 +1,29 @@
1
+ from .compiler import Compiler, compile_source
2
+ from .decompiler import Decompiler, decompile_script
3
+ from .errors import CompileError, DecompileError, ParseError
4
+ from .lexer import Token, tokenize
5
+ from .nodes import Behavior, Block, Flow, Prescript, Script, Statement, Variable
6
+ from .parser import Parser
7
+
8
+ # Imported for its side effect: it declares every opcode in both directions.
9
+ from . import methods # noqa: E402,F401 isort:skip
10
+
11
+ __all__ = [
12
+ "Behavior",
13
+ "Block",
14
+ "CompileError",
15
+ "Compiler",
16
+ "DecompileError",
17
+ "Flow",
18
+ "Decompiler",
19
+ "ParseError",
20
+ "Parser",
21
+ "Prescript",
22
+ "Script",
23
+ "Statement",
24
+ "Token",
25
+ "Variable",
26
+ "compile_source",
27
+ "decompile_script",
28
+ "tokenize",
29
+ ]
tfbpseudo/arguments.py ADDED
@@ -0,0 +1,271 @@
1
+ """Opcode arguments, spelled the same way by the compiler and the decompiler.
2
+
3
+ Nearly every opcode is a fixed list of operands, so rather than writing the two
4
+ directions out by hand for each one -- where they could drift apart -- an
5
+ opcode declares its operands once in `methods.py` and both handlers are built
6
+ from that declaration. Each Argument here knows how to read one argument's
7
+ tokens into the opcode's fields and how to write those fields back out.
8
+
9
+ An argument may be `optional`, in which case it is the trailing ones that go
10
+ missing: `spawnActor` has a byte that is only in some files, `useCamera` has a
11
+ second duration only for a real transition. `present` decides whether to write
12
+ one back out, and everything after an absent argument is absent too.
13
+ """
14
+
15
+ from collections.abc import Callable
16
+ from dataclasses import dataclass
17
+ from enum import IntEnum
18
+ from typing import TYPE_CHECKING, Any
19
+
20
+ from tfbpseudo.errors import CompileError, DecompileError
21
+ from tfbpseudo.lexer import Token, synth_token
22
+ from tfbpseudo.rhs import REL_OP_BY_TOKEN, float_text
23
+ from tfbscript.opcodes.base import Opcode
24
+ from tfbscript.opcodes.enums import RelOp
25
+
26
+ if TYPE_CHECKING:
27
+ from tfbpseudo.compiler import Compiler
28
+ from tfbpseudo.decompiler import Decompiler
29
+
30
+ @dataclass(frozen=True)
31
+ class Argument:
32
+ """One argument of an opcode."""
33
+
34
+ name: str # the opcode field it sets, and what errors call it
35
+ optional: bool = False
36
+ default: Any = None # what the field is when the argument is left out
37
+ # For the opcodes whose payload shape is recorded in a private field
38
+ # rather than shown by the argument's own value.
39
+ present_if: Callable[[Opcode], bool] | None = None
40
+
41
+ def read(
42
+ self, tokens: list[Token], compiler: "Compiler"
43
+ ) -> dict[str, object]: # pragma: no cover - overridden
44
+ raise NotImplementedError
45
+
46
+ def write(
47
+ self, opcode: Opcode, decompiler: "Decompiler"
48
+ ) -> list[Token]: # pragma: no cover - overridden
49
+ raise NotImplementedError
50
+
51
+ def absent(self) -> dict[str, object]:
52
+ """The fields to set when this argument is not written."""
53
+ return {self.name: self.default}
54
+
55
+ def present(self, opcode: Opcode) -> bool:
56
+ """Whether to write this argument back out."""
57
+ if self.present_if is not None:
58
+ return self.present_if(opcode)
59
+
60
+ return getattr(opcode, self.name) != self.default
61
+
62
+ def error(self, tokens: list[Token], msg: str) -> CompileError:
63
+ anchor = tokens[0] if tokens else Token("EOF", "", 0, 0)
64
+ return CompileError(f"{self.name}: {msg}", anchor.line, anchor.col)
65
+
66
+ def one_token(self, tokens: list[Token], kind: str, what: str) -> Token:
67
+ if len(tokens) != 1 or tokens[0].kind != kind:
68
+ raise self.error(tokens, f"expected {what}")
69
+
70
+ return tokens[0]
71
+
72
+
73
+ @dataclass(frozen=True)
74
+ class Ref(Argument):
75
+ """A reference: `Zoo Loop`, `@myself.health`, `players#first`.
76
+
77
+ `expects` is the kind of variable the opcode wants named here -- "sound"
78
+ for `playSound`, "set" for the membership ops -- where that is the same in
79
+ every script. It is what the editor completes, not a rule: a reference can
80
+ reach the right kind through a field of something else, so nothing here
81
+ refuses one that does not say so up front.
82
+ """
83
+
84
+ expects: str | None = None
85
+
86
+ def read(self, tokens: list[Token], compiler: "Compiler") -> dict[str, object]:
87
+ return {self.name: compiler.get_ref_by_tokens(tokens)}
88
+
89
+ def write(self, opcode: Opcode, decompiler: "Decompiler") -> list[Token]:
90
+ return decompiler.ref_tokens(getattr(opcode, self.name))
91
+
92
+
93
+ @dataclass(frozen=True)
94
+ class Value(Argument):
95
+ """A right-hand side: an immediate, a reference, or an expression."""
96
+
97
+ def read(self, tokens: list[Token], compiler: "Compiler") -> dict[str, object]:
98
+ return {self.name: compiler.get_rhs_by_tokens(tokens)}
99
+
100
+ def write(self, opcode: Opcode, decompiler: "Decompiler") -> list[Token]:
101
+ return decompiler.rhs_tokens(getattr(opcode, self.name))
102
+
103
+
104
+ @dataclass(frozen=True)
105
+ class Condition(Argument):
106
+ """`lhs <op> rhs` in one argument, setting three fields at once."""
107
+
108
+ rel_op_name: str = "rel_op"
109
+ rhs_name: str = "rhs"
110
+
111
+ def read(self, tokens: list[Token], compiler: "Compiler") -> dict[str, object]:
112
+ lhs, rel_op, rhs = compiler.get_condition_by_tokens(tokens)
113
+ return {self.name: lhs, self.rel_op_name: rel_op, self.rhs_name: rhs}
114
+
115
+ def absent(self) -> dict[str, object]:
116
+ # All three fields keep whatever the opcode's dataclass defaults are.
117
+ return {}
118
+
119
+ def write(self, opcode: Opcode, decompiler: "Decompiler") -> list[Token]:
120
+ return decompiler.condition_tokens(
121
+ getattr(opcode, self.name),
122
+ getattr(opcode, self.rel_op_name),
123
+ getattr(opcode, self.rhs_name),
124
+ )
125
+
126
+
127
+ @dataclass(frozen=True)
128
+ class Comparison(Argument):
129
+ """A bare relational operator, for the ops that carry one on its own."""
130
+
131
+ def read(self, tokens: list[Token], compiler: "Compiler") -> dict[str, object]:
132
+ if len(tokens) != 1 or tokens[0].value not in REL_OP_BY_TOKEN:
133
+ raise self.error(tokens, f"expected one of {' '.join(REL_OP_BY_TOKEN)}")
134
+
135
+ return {self.name: REL_OP_BY_TOKEN[tokens[0].value]}
136
+
137
+ def write(self, opcode: Opcode, decompiler: "Decompiler") -> list[Token]:
138
+ rel_op: RelOp = getattr(opcode, self.name)
139
+ return [synth_token("OP", rel_op.symbol())]
140
+
141
+
142
+ @dataclass(frozen=True)
143
+ class Choice(Argument):
144
+ """One of an enum's members, written by name -- `slow_move`, `randomly` --
145
+ or by its raw number when a file has one the enum has no name for."""
146
+
147
+ enum: type[IntEnum] = RelOp
148
+
149
+ def read(self, tokens: list[Token], compiler: "Compiler") -> dict[str, object]:
150
+ if len(tokens) != 1 or tokens[0].kind not in ("NAME", "NUMBER", "STRING"):
151
+ raise self.error(tokens, f"expected one of {self.choices()}")
152
+
153
+ token = tokens[0]
154
+
155
+ if token.kind == "NUMBER":
156
+ try:
157
+ return {self.name: self.enum(int(token.value, 0))}
158
+ except ValueError:
159
+ raise self.error(
160
+ tokens, f"{token.value} is not a {self.enum.__name__} value"
161
+ ) from None
162
+
163
+ folded = token.value.casefold()
164
+ for member in self.enum:
165
+ if member.name.casefold() == folded:
166
+ return {self.name: member}
167
+
168
+ raise self.error(
169
+ tokens, f"{token.value!r} is not one of {self.choices()}"
170
+ )
171
+
172
+ def write(self, opcode: Opcode, decompiler: "Decompiler") -> list[Token]:
173
+ value = getattr(opcode, self.name)
174
+
175
+ if not isinstance(value, self.enum):
176
+ return [synth_token("NUMBER", str(int(value)))]
177
+
178
+ return [synth_token("NAME", value.name)]
179
+
180
+ def choices(self) -> str:
181
+ names = [member.name for member in self.enum]
182
+ if len(names) > 6:
183
+ return ", ".join(names[:6]) + f", ... ({len(names)} in all)"
184
+ return ", ".join(names)
185
+
186
+
187
+ @dataclass(frozen=True)
188
+ class Text(Argument):
189
+ """A quoted string the opcode stores verbatim."""
190
+
191
+ def read(self, tokens: list[Token], compiler: "Compiler") -> dict[str, object]:
192
+ return {self.name: self.one_token(tokens, "STRING", "a quoted string").value}
193
+
194
+ def write(self, opcode: Opcode, decompiler: "Decompiler") -> list[Token]:
195
+ return [synth_token("STRING", getattr(opcode, self.name))]
196
+
197
+
198
+ @dataclass(frozen=True)
199
+ class Number(Argument):
200
+ """A plain number the opcode stores as-is, not a right-hand side."""
201
+
202
+ is_float: bool = False
203
+
204
+ def read(self, tokens: list[Token], compiler: "Compiler") -> dict[str, object]:
205
+ negative = False
206
+ rest = tokens
207
+
208
+ if tokens and tokens[0].kind == "OP" and tokens[0].value in ("+", "-"):
209
+ negative = tokens[0].value == "-"
210
+ rest = tokens[1:]
211
+
212
+ token = self.one_token(rest, "NUMBER", "a number")
213
+ text = token.value
214
+
215
+ if self.is_float:
216
+ value = float(text)
217
+ else:
218
+ if "." in text:
219
+ raise self.error(tokens, f"expected a whole number, got {text}")
220
+ value = int(text, 0)
221
+
222
+ return {self.name: -value if negative else value}
223
+
224
+ def write(self, opcode: Opcode, decompiler: "Decompiler") -> list[Token]:
225
+ value = getattr(opcode, self.name)
226
+ text = float_text(float(value)) if self.is_float else str(int(value))
227
+
228
+ return [synth_token("NUMBER", text)]
229
+
230
+
231
+ def read_arguments(
232
+ arguments: list[list[Token]],
233
+ spec: list[Argument],
234
+ opcode: Opcode,
235
+ compiler: "Compiler",
236
+ ) -> None:
237
+ """Read each argument into `opcode`'s fields, filling in the ones that were
238
+ left out with their defaults."""
239
+ for index, argument in enumerate(spec):
240
+ if index < len(arguments):
241
+ fields = argument.read(arguments[index], compiler)
242
+ else:
243
+ fields = argument.absent()
244
+
245
+ for name, value in fields.items():
246
+ setattr(opcode, name, value)
247
+
248
+
249
+ def write_arguments(
250
+ spec: list[Argument], opcode: Opcode, decompiler: "Decompiler"
251
+ ) -> list[list[Token]]:
252
+ """Write `opcode`'s fields back out, stopping at the first optional
253
+ argument that is not there -- nothing after it can be written either."""
254
+ out: list[list[Token]] = []
255
+
256
+ for argument in spec:
257
+ if argument.optional and not argument.present(opcode):
258
+ break
259
+
260
+ out.append(argument.write(opcode, decompiler))
261
+
262
+ for argument in spec[len(out) :]:
263
+ if not argument.optional:
264
+ raise DecompileError(
265
+ f"`{argument.name}` is not optional but comes after an argument "
266
+ + "that is missing, so this opcode cannot be written out"
267
+ )
268
+
269
+ return out
270
+
271
+