uxn-tal 0.2.4

A Rust library for assembling TAL (Tal Assembly Language) files into UXN ROM files
Documentation
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
use std::{
    env, fs, io,
    path::Path,
    process::{Command, Stdio},
    time::Duration,
};

use uxn_tal::{Assembler, AssemblerError};

const DIS_IGNORE_COMMENT_DIFF: bool = true; // Set false to count differing comments as diffs

fn main() -> Result<(), AssemblerError> {
    let args: Vec<String> = env::args().collect();
    if args.len() != 2 {
        eprintln!("Usage: {} <file.tal>", args[0]);
        std::process::exit(1);
    }
    let tal_path = &args[1];
    let source = fs::read_to_string(tal_path).map_err(|e| serr(tal_path, &format!("read: {e}")))?;

    println!("== Source: {} ==", tal_path);
    for (i, line) in source.lines().enumerate() {
        println!("{:4}: {}", i + 1, line);
    }

    // 1. Internal assembler (non-fatal on error now)
    let mut internal_rom: Option<Vec<u8>> = None;
    let mut internal_err: Option<String> = None;
    let internal_out_path = format!("{tal_path}.uxntal.rom");

    let mut asm = Assembler::new();
    match asm.assemble(&source, Some(tal_path.to_string())) {
        Ok(bytes) => {
            fs::write(&internal_out_path, &bytes).ok();
            println!(
                "\n[uxntal] OK -> {} ({} bytes)",
                internal_out_path,
                bytes.len()
            );
            internal_rom = Some(bytes);
            // Emit symbol file from the same assembler instance.
            let sym_path = format!("{}.sym", &internal_out_path);
            let sym_bytes = asm.generate_symbol_file();
            let _ = fs::write(&sym_path, &sym_bytes);
            if have_uxncli() && Path::new("uxndis.rom").exists() {
                dump_disassembly(&internal_out_path);
            }
        }
        Err(e) => {
            println!("\n[uxntal] FAIL: {e}");
            internal_err = Some(format!("{e}"));
        }
    }

    // 2. External backends (always attempted)
    println!("\nRunning external backends...");
    let uxna = run_uxnasm(tal_path);
    let drif = run_drifblim(tal_path); // optional
    if have_uxncli() && Path::new("uxndis.rom").exists() {
        if internal_rom.is_some() { dump_disassembly(&internal_out_path); }
        for r in [&uxna, &drif] {
            if r.ok {
                if let Some(ref rp) = r.rom_path {
                    dump_disassembly(rp);
                }
            }
        }
    }

    // 3. Summary
    println!("\n== Backend Summary ==");
    println!(
        "  {:<9} {:<5} {:>8}  {}",
        "backend", "stat", "bytes", "output/summary"
    );
    if let Some(ref rom) = internal_rom {
        println!("  {:<9} {:<5} {:>8}  {}", "uxntal", "OK", rom.len(), internal_out_path);
    } else {
        // Show last 3 non-empty lines of internal error (previously only last 1)
        let summary = internal_err
            .as_ref()
            .map(|e| last_n_lines(e, 3))
            .unwrap_or_else(|| "-".into());
        println!("  {:<9} {:<5} {:>8}  {}", "uxntal", "FAIL", 0, summary);
    }
    for r in [&uxna, &drif] {
        let summary = if r.ok {
            r.rom_path.as_deref().unwrap_or("-").to_string()
        } else {
            let combined = if !r.stderr.trim().is_empty() {
                r.stderr.trim().to_string()
            } else {
                r.stdout.trim().to_string()
            };
            if combined.is_empty() {
                r.error.as_deref().unwrap_or("-").to_string()
            } else {
                last_n_lines(&combined, 3)
            }
        };
        println!(
            "  {:<9} {:<5} {:>8}  {}",
            r.name,
            if r.ok { "OK" } else { "FAIL" },
            r.bytes.len(),
            summary
        );
    }

    // 4. Detailed output (stdout/stderr trimmed)
    for r in [&uxna, &drif] {
        println!("\n== {} ==", r.name);
        if r.ok {
            println!(
                "{}: OK, {} bytes{}",
                r.name,
                r.bytes.len(),
                r.rom_path
                    .as_ref()
                    .map(|p| format!(", rom={}", p))
                    .unwrap_or_default()
            );
        } else {
            println!("{}: FAIL: {}", r.name, r.error.as_deref().unwrap_or("-"));
        }
        if !r.stdout.trim().is_empty() {
            println!("-- stdout --");
            if r.name == "uxnasm" {
                println!("{}", tail_block(&r.stdout, 4000));
            } else {
                println!("{}", trim_block(&r.stdout, 4000));
            }
        }
        if !r.stderr.trim().is_empty() {
            println!("-- stderr --");
            if r.name == "uxnasm" {
                println!("{}", tail_block(&r.stderr, 4000));
            } else {
                println!("{}", trim_block(&r.stderr, 4000));
            }
        }
    }

    // 5. Diffs (only if internal succeeded and external ok)
    if let Some(ref int_rom) = internal_rom {
        println!("\n== Byte Diffs vs uxntal ==");
        for r in [&uxna, &drif] {
            if r.ok {
                print!("uxntal vs {:<9}: ", r.name);
                if let Some(d) = first_byte_diff(int_rom, &r.bytes) {
                    println!(
                        "first diff at 0x{:04X}: {:02X} != {:02X}",
                        d.index, d.a, d.b
                    );
                } else {
                    println!("identical");
                }
            } else {
                println!("uxntal vs {:<9}: skipped ({} failed)", r.name, r.name);
            }
        }

        // Optional disassembly diff (needs uxndis.rom & uxncli)
        if have_uxncli() && Path::new("uxndis.rom").exists() {
            println!("\n== Disassembly Diffs (first differing line) ==");
            if dis_ok(&internal_out_path) {
                let uxntal_dis = disassemble(&internal_out_path).unwrap_or_default();
                // Print the internal ROM path for clarity
                println!("(disassemble) backend=uxntal rom={}", internal_out_path);
                for r in [&uxna, &drif] {
                    if r.ok {
                        if let Some(ref rp) = r.rom_path {
                            if dis_ok(rp) {
                                // NOTE: This is where each external backend (including 'drifblim') is disassembled.
                                // The next call to disassemble(rp) produces the B side of the diff.
                                println!("(disassemble) backend={} rom={}", r.name, rp);
                                match disassemble(rp) {
                                    Some(other_dis) => {
                                        match first_line_diff(&uxntal_dis, &other_dis) {
                                            Some((ln, a, b)) => {
                                                println!(
                                                    "uxntal vs {:<9} line {}:\n  uxntal: {}\n  {:<9}: {}",
                                                    r.name, ln, a, r.name, b
                                                );
                                            }
                                            None => println!("uxntal vs {:<9} identical", r.name),
                                        }
                                    }
                                    None => {
                                        println!("uxntal vs {:<9} disassembly unavailable (empty output)", r.name);
                                    }
                                }
                            } else {
                                println!("uxntal vs {:<9} disassembly unavailable", r.name);
                            }
                        }
                    }
                }
            } else {
                println!("(uxntal disassembly unavailable, skipping)");
            }
        } else {
            println!("\n(disassembly skipped: need uxndis.rom + uxncli)");
        }
    } else {
        println!("\nSkipping diffs (internal uxntal failed).");
    }

    // 6. Explicit note if user expected relative $label support (internal failure hint)
    if internal_err
        .as_ref()
        .map(|e| e.contains("Skip directive requires hex value"))
        .unwrap_or(false)
    {
        println!("\nNOTE: Internal assembler currently rejects $label (relative padding) which uxnasm accepts.");
    }

    Ok(())
}

/* ---------------- Backend runners ---------------- */

struct BackendResult {
    name: &'static str,
    ok: bool,
    rom_path: Option<String>,
    bytes: Vec<u8>,
    stdout: String,
    stderr: String,
    error: Option<String>,
}

fn run_uxnasm(tal: &str) -> BackendResult {
    let out = format!("{tal}.uxnasm.rom");
    let (cmd, mut args): (&str, Vec<String>) = if in_wsl() {
        ("uxnasm", vec!["--verbose".to_string(), tal.to_string(), out.to_string()])
    } else {
        // Convert Windows paths to WSL paths so uxnasm inside WSL can access them
        let wsl_tal = wslize(tal);
        let wsl_out = wslize(&out);
        ("wsl", vec![
            "uxnasm".to_string(),
            "--verbose".to_string(),
            wsl_tal,
            wsl_out,
        ])
    };
    match spawn_capture(cmd, &mut args) {
        Ok((status, so, se)) if status.success() && Path::new(&out).exists() => {
            let out_clone = out.clone();
            BackendResult {
                name: "uxnasm",
                ok: true,
                rom_path: Some(out),
                bytes: fs::read(&out_clone).unwrap_or_default(),
                stdout: so,
                stderr: se,
                error: None,
            }
        },
        Ok((_s, so, se)) => BackendResult {
            name: "uxnasm",
            ok: false,
            rom_path: None,
            bytes: vec![],
            stdout: so,
            stderr: se,
            error: Some("uxnasm failed".into()),
        },
        Err(e) => BackendResult {
            name: "uxnasm",
            ok: false,
            rom_path: None,
            bytes: vec![],
            stdout: String::new(),
            stderr: String::new(),
            error: Some(format!("spawn error: {e}")),
        },
    }
}

fn run_drifblim(tal: &str) -> BackendResult {
    // Optional: must exist drifblim.rom driver
    if !Path::new("drifblim.rom").exists() {
        return BackendResult {
            name: "drifblim",
            ok: false,
            rom_path: None,
            bytes: vec![],
            stdout: String::new(),
            stderr: String::new(),
            error: Some("drifblim.rom missing".into()),
        };
    }
    let out = format!("{tal}.drifblim.rom");
    let (cmd, mut args): (&str, Vec<String>) = if in_wsl() {
        ("uxncli", vec!["drifblim.rom".to_string(), tal.to_string(), out.to_string()])
    } else {
        let wsl_tal = wslize(tal);
        let wsl_out = wslize(&out);
        ("wsl", vec![
            "uxncli".to_string(),
            "drifblim.rom".to_string(),
            wsl_tal,
            wsl_out,
        ])
    };
    match spawn_capture(cmd, &mut args) {
        Ok((status, so, se)) if status.success() && Path::new(&out).exists() => {
            let out_clone = out.clone();
            BackendResult {
                name: "drifblim",
                ok: true,
                rom_path: Some(out),
                bytes: fs::read(out_clone).unwrap_or_default(),
                stdout: so,
                stderr: se,
                error: None,
            }
        },
        Ok((_s, so, se)) => BackendResult {
            name: "drifblim",
            ok: false,
            rom_path: None,
            bytes: vec![],
            stdout: so,
            stderr: se,
            error: Some("drifblim run failed".into()),
        },
        Err(e) => BackendResult {
            name: "drifblim",
            ok: false,
            rom_path: None,
            bytes: vec![],
            stdout: String::new(),
            stderr: String::new(),
            error: Some(format!("spawn error: {e}")),
        },
    }
}

/* ---------------- Diff helpers ---------------- */

struct ByteDiff {
    index: usize,
    a: u8,
    b: u8,
}
fn first_byte_diff(a: &[u8], b: &[u8]) -> Option<ByteDiff> {
    let n = a.len().min(b.len());
    for i in 0..n {
        if a[i] != b[i] {
            return Some(ByteDiff {
                index: i,
                a: a[i],
                b: b[i],
            });
        }
    }
    if a.len() != b.len() {
        Some(ByteDiff {
            index: n,
            a: a.get(n).copied().unwrap_or(0),
            b: b.get(n).copied().unwrap_or(0),
        })
    } else {
        None
    }
}

fn first_line_diff(a: &str, b: &str) -> Option<(usize, String, String)> {
    for (i, (la, lb)) in a.lines().zip(b.lines()).enumerate() {
        if la == lb { continue; }
        if DIS_IGNORE_COMMENT_DIFF {
            let na = strip_dis_comment(la);
            let nb = strip_dis_comment(lb);
            if na == nb {
                // differ only in trailing comment -> ignore
                continue;
            }
        }
        return Some((i + 1, la.to_string(), lb.to_string()));
    }
    let ac = a.lines().count();
    let bc = b.lines().count();
    if ac > bc {
        a.lines()
            .nth(bc)
            .map(|extra| (bc + 1, extra.to_string(), String::new()))
    } else if bc > ac {
        b.lines()
            .nth(ac)
            .map(|extra| (ac + 1, String::new(), extra.to_string()))
    } else {
        None
    }
}

fn strip_dis_comment(line: &str) -> &str {
    match line.find('(') {
        Some(idx) => line[..idx].trim_end(),
        None => line.trim_end(),
    }
}

/* ---------------- Disassembly ---------------- */
fn disassemble(rom_path: &str) -> Option<String> {
    if !have_uxncli() || !Path::new("uxndis.rom").exists() {
        return None;
    }
    let (cmd, mut args): (&str, Vec<String>) = if in_wsl() {
        ("uxncli", vec!["uxndis.rom".to_string(), rom_path.to_string()])
    } else {
        let wsl_rom_path = wslize(rom_path);
        ("wsl", vec!["uxncli".to_string(), "uxndis.rom".to_string(), wsl_rom_path])
    };
    if let Ok((status, out, _)) = spawn_capture(cmd, &mut args) {
        if status.success() {
            // Treat empty output as failure to avoid blank B-side in diff
            if out.trim().is_empty() {
                return None;
            }
            return Some(out);
        }
    }
    None
}

fn dis_ok(rom_path: &str) -> bool {
    have_uxncli() && Path::new("uxndis.rom").exists() && Path::new(rom_path).exists()
}

fn dump_disassembly(rom_path: &str) {
    if !dis_ok(rom_path) { return; }
    if let Some(text) = disassemble(rom_path) {
        let path = format!("{rom_path}.dis.txt");
        let _ = std::fs::write(&path, text);
    }
}

/* ---------------- System helpers ---------------- */

fn spawn_capture(
    cmd: &str,
    args: &mut [impl AsRef<str>],
) -> io::Result<(std::process::ExitStatus, String, String)> {
    let mut c = Command::new(cmd);
    for a in args {
        c.arg(a.as_ref());
    }
    c.stdout(Stdio::piped()).stderr(Stdio::piped());
    let mut child = c.spawn()?;
    let stdout = {
        let mut s = String::new();
        if let Some(mut out) = child.stdout.take() {
            let _ = io::Read::read_to_string(&mut out, &mut s);
        }
        s
    };
    let stderr = {
        let mut s = String::new();
        if let Some(mut er) = child.stderr.take() {
            let _ = io::Read::read_to_string(&mut er, &mut s);
        }
        s
    };
    let status = child.wait()?;
    // Tiny delay to let filesystem flush outputs (mostly for WSL)
    std::thread::sleep(Duration::from_millis(5));
    Ok((status, stdout, stderr))
}

fn in_wsl() -> bool {
    std::env::var("WSL_DISTRO_NAME").is_ok()
        || (Path::new("/proc/version").exists()
            && fs::read_to_string("/proc/version")
                .unwrap_or_default()
                .to_lowercase()
                .contains("microsoft"))
}

fn which(bin: &str) -> Option<String> {
    let path = env::var_os("PATH")?;
    for p in env::split_paths(&path) {
        let cand = p.join(bin);
        if cand.is_file() {
            return cand.to_str().map(|s| s.to_string());
        }
    }
    None
}

fn have_uxncli() -> bool {
    if in_wsl() {
        which("uxncli").is_some()
    } else {
        which("wsl").is_some()
    }
}

/* ---------- Path translation (Windows host -> WSL) ---------- */
fn wslize(p: &str) -> String {
    // Fast path: already looks like a Unix path
    if p.starts_with('/') { return p.to_string(); }
    // Drive letter?
    if p.len() > 2 && p.as_bytes()[1] == b':' {
        let drive = p.chars().next().unwrap().to_ascii_lowercase();
        let rest = p[2..].replace('\\', "/");
        if rest.is_empty() { return format!("/mnt/{drive}"); }
        return format!("/mnt/{drive}/{}", rest.trim_start_matches('/'));
    }
    p.replace('\\', "/")
}

/* ---------------- Misc helpers ---------------- */

fn serr(path: &str, msg: &str) -> AssemblerError {
    AssemblerError::SyntaxError {
        path: path.to_string(),
        line: 0,
        position: 0,
        message: msg.to_string(),
        source_line: String::new(),
    }
}

fn trim_block(s: &str, max: usize) -> String {
    if s.len() <= max {
        s.to_string()
    } else {
        format!("{}...\n[truncated {} bytes]", &s[..max], s.len() - max)
    }
}

// NEW: tail printer for uxnasm output
fn tail_block(s: &str, max: usize) -> String {
    if s.len() <= max {
        s.to_string()
    } else {
        let start = s.len() - max;
        format!("...\n{}\n[truncated first {} bytes]", &s[start..], start)
    }
}

fn last_n_lines(s: &str, n: usize) -> String {
    let mut lines: Vec<&str> = s.lines().filter(|l| !l.trim().is_empty()).collect();
    if lines.is_empty() {
        return String::new();
    }
    if lines.len() > n {
        lines = lines.split_off(lines.len() - n);
    }
    lines.join(" | ")
}