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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
"""Common AST node definitions for ppmck and pyxel MML."""
from dataclasses import dataclass, field
from typing import Any
@dataclass
class ASTNode:
"""Base class for all AST nodes."""
line: int
column: int
@dataclass
class Program(ASTNode):
"""Root program node."""
tracks: list[Track] = field(default_factory=list)
global_statements: list[Statement] = field(default_factory=list)
@dataclass
class Track(ASTNode):
"""A single track definition."""
track_id: str # "A" / "B" / "T" / "N" / "L" (ppmck) or "0"-"3" (pyxel)
channel: str # "Pulse1" / "Pulse2" / "Triangle" / "Noise" / "Loop"
mode: str # "ppmck" / "pyxel"
statements: list[Statement] = field(default_factory=list)
headers: list[Header] = field(default_factory=list)
@dataclass
class Header(ASTNode):
"""ppmck # header line."""
key: str
value: str
unterminated: bool = False
# --- Statements ---
@dataclass
class NoteStmt(ASTNode):
"""A note command (c, d, e, ...)."""
note_name: str # normalized to lowercase: "c"-"b"
accidental: int = 0 # +1 sharp, -1 flat, 0 none
length: int | None = None # None means use default length
dots: int = 0
@dataclass
class RestStmt(ASTNode):
"""A rest command (r / R)."""
length: int | None = None
dots: int = 0
@dataclass
class OctaveStmt(ASTNode):
"""Octave command or relative octave shift."""
value: int | None = None # absolute octave number
direction: str | None = None # "up" or "down" for relative shifts
@dataclass
class LengthStmt(ASTNode):
"""Default length command (l / L)."""
value: int
@dataclass
class VolumeStmt(ASTNode):
"""Volume command (v / V)."""
value: int
@dataclass
class DutyStmt(ASTNode):
"""Duty/tone command (q / @)."""
value: int
@dataclass
class TempoStmt(ASTNode):
"""Tempo command (t / T)."""
value: int
@dataclass
class GateTimeStmt(ASTNode):
"""Gate time command (Q, pyxel only)."""
value: int
@dataclass
class TransposeStmt(ASTNode):
"""Transpose command (K, pyxel only)."""
value: int
@dataclass
class DetuneStmt(ASTNode):
"""Detune command (Y, pyxel only)."""
value: int
@dataclass
class QuantizeStmt(ASTNode):
"""ppmck quantize command (q1-q8)."""
value: int
@dataclass
class TieStmt(ASTNode):
"""Tie/slur command (&)."""
target: NoteStmt | RestStmt | int | None = None
@dataclass
class TieCmdStmt(ASTNode):
"""ppmck tie command (^). Extends duration, pitch from previous note."""
target: NoteStmt | int | None = None
@dataclass
class DetuneCmdStmt(ASTNode):
"""ppmck detune command (D<num>)."""
value: int
@dataclass
class RelativeVolumeStmt(ASTNode):
"""ppmck relative volume (v+num / v-num)."""
delta: int # positive=up, negative=down
@dataclass
class RepeatStartStmt(ASTNode):
"""Start of repeat section ([)."""
pass
@dataclass
class RepeatBreakStmt(ASTNode):
"""Last-pass break marker (|) inside a ppmck repeat."""
pass
@dataclass
class RepeatEndStmt(ASTNode):
"""End of repeat section (] with optional count)."""
count: int | None = None # None means infinite
@dataclass
class BarStmt(ASTNode):
"""Bar line (|), visual only."""
pass
@dataclass
class ExtCmdStmt(ASTNode):
"""pyxel @ENV / @VIB / @GLI command."""
cmd: str # "ENV" / "VIB" / "GLI"
slot: int
params: list[int] = field(default_factory=list)
# --- Envelope / LFO / vibrato statements (IR kept, synthesis warning) ---
@dataclass
class VolumeEnvelopeUseStmt(ASTNode):
"""ppmck volume envelope use (@v<num>)."""
slot: int
@dataclass
class VolumeEnvelopeDefStmt(ASTNode):
"""ppmck volume envelope definition (@v<num> = {...|...})."""
slot: int
points: list[int] = field(default_factory=list)
loop_points: list[int] = field(default_factory=list)
@dataclass
class DutyEnvelopeDefStmt(ASTNode):
"""ppmck duty envelope definition (@<num> = {...|...})."""
slot: int
points: list[int] = field(default_factory=list)
loop_points: list[int] = field(default_factory=list)
@dataclass
class DutyEnvelopeUseStmt(ASTNode):
"""ppmck duty envelope use (@@<num>)."""
slot: int
@dataclass
class LfoDefStmt(ASTNode):
"""ppmck LFO definition (@MP<num> = {p1,p2,p3,p4})."""
slot: int
delay: int
speed: int
depth: int
transition: int
@dataclass
class LfoUseStmt(ASTNode):
"""ppmck LFO use (MP<num>)."""
slot: int
@dataclass
class LfoOffStmt(ASTNode):
"""ppmck LFO off (MPOF)."""
pass
@dataclass
class PitchEnvDefStmt(ASTNode):
"""ppmck pitch envelope definition (@EP<num> = {...|...})."""
slot: int
points: list[int] = field(default_factory=list)
loop_points: list[int] = field(default_factory=list)
@dataclass
class PitchEnvUseStmt(ASTNode):
"""ppmck pitch envelope use (EP<num>)."""
slot: int
@dataclass
class PitchEnvOffStmt(ASTNode):
"""ppmck pitch envelope off (EPOF)."""
pass
@dataclass
class NoteEnvDefStmt(ASTNode):
"""ppmck note envelope definition (@EN<num> = {...|...})."""
slot: int
points: list[int] = field(default_factory=list)
loop_points: list[int] = field(default_factory=list)
@dataclass
class NoteEnvUseStmt(ASTNode):
"""ppmck note envelope use (EN<num>)."""
slot: int
@dataclass
class NoteEnvOffStmt(ASTNode):
"""ppmck note envelope off (ENOF)."""
pass
# --- Stage 2 statements (kept in AST but ignored by semantic analyzer) ---
@dataclass
class CountLengthStmt(ASTNode):
"""ppmck count length note (c%48)."""
note_name: str
accidental: int
count: int
@dataclass
class SweepStmt(ASTNode):
"""ppmck sweep command (s1,2)."""
speed: int
depth: int
@dataclass
class NoiseModeStmt(ASTNode):
"""ppmck noise mode command (m0 / m1)."""
mode: int
Statement = (
NoteStmt
| RestStmt
| OctaveStmt
| LengthStmt
| VolumeStmt
| DutyStmt
| TempoStmt
| GateTimeStmt
| TransposeStmt
| DetuneStmt
| QuantizeStmt
| TieStmt
| TieCmdStmt
| DetuneCmdStmt
| RelativeVolumeStmt
| RepeatStartStmt
| RepeatBreakStmt
| RepeatEndStmt
| BarStmt
| ExtCmdStmt
| VolumeEnvelopeUseStmt
| VolumeEnvelopeDefStmt
| DutyEnvelopeDefStmt
| DutyEnvelopeUseStmt
| LfoDefStmt
| LfoUseStmt
| LfoOffStmt
| PitchEnvDefStmt
| PitchEnvUseStmt
| PitchEnvOffStmt
| NoteEnvDefStmt
| NoteEnvUseStmt
| NoteEnvOffStmt
| CountLengthStmt
| SweepStmt
| NoiseModeStmt
)
def _node_to_dict(node: Any) -> Any:
"""Recursively convert an AST node to a JSON-compatible dict."""
if isinstance(node, list):
return [_node_to_dict(item) for item in node]
if not isinstance(node, ASTNode):
return node
data: dict[str, Any] = {"__type__": node.__class__.__name__}
for key, value in node.__dict__.items():
data[key] = _node_to_dict(value)
return data
def ast_to_dict(node: ASTNode) -> dict[str, Any]:
"""Convert an AST node to a JSON-compatible dict."""
return _node_to_dict(node)
|