v4_cli 0.5.0

CLI tool for V4 VM bytecode deployment
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
use crate::Result;
use crate::protocol::ErrorCode;
use crate::repl::{CompileResult, Compiler};
use crate::serial::V4Serial;
use rustyline::DefaultEditor;
use rustyline::error::ReadlineError;
use std::time::Duration;

const DEFAULT_TIMEOUT: Duration = Duration::from_secs(5);

/// Run interactive REPL session
pub fn run_repl(port: &str, no_reset: bool) -> Result<()> {
    // Open serial connection
    let mut serial = V4Serial::open_default(port)?;

    // Create compiler
    let mut compiler = Compiler::new().map_err(crate::V4Error::Compilation)?;

    // Create line editor
    let mut rl = DefaultEditor::new().map_err(|e| crate::V4Error::Repl(e.to_string()))?;

    // Print welcome message
    println!("V4 REPL v{}", env!("CARGO_PKG_VERSION"));
    println!("Connected to {}", port);
    println!("Type 'bye' or press Ctrl+D to exit");
    println!("Type '.help' for help");
    println!();

    // Reset device (unless --no-reset is specified)
    if no_reset {
        println!("Skipping VM reset (--no-reset)\n");
        println!("Warning: Compiler context is empty. Existing device words may not be callable.");
        println!("Use '.reset' to reset both VM and compiler context.\n");
    } else {
        println!("Resetting device...");
        match serial.reset(DEFAULT_TIMEOUT) {
            Ok(ErrorCode::Ok) => println!("Device ready\n"),
            Ok(err) => println!("Warning: Reset returned {}\n", err.name()),
            Err(e) => println!("Warning: Reset failed: {}\n", e),
        }
    }

    // REPL loop
    loop {
        let readline = rl.readline("v4> ");

        match readline {
            Ok(line) => {
                let line = line.trim();

                // Skip empty lines
                if line.is_empty() {
                    continue;
                }

                // Add to history
                let _ = rl.add_history_entry(line);

                // Check for exit commands
                if line == "bye" || line == "quit" || line == ".exit" {
                    println!("Goodbye!");
                    break;
                }

                // Check for meta-commands
                if line.starts_with('.') {
                    if let Err(e) = handle_meta_command(line, &mut serial, &mut compiler) {
                        eprintln!("Error: {}", e);
                    }
                    continue;
                }

                // Compile Forth code
                let compiled = match compiler.compile(line) {
                    Ok(c) => c,
                    Err(e) => {
                        eprintln!("Error: {}", e);
                        continue;
                    }
                };

                // Execute on device
                if let Err(e) = execute_on_device(&mut serial, &compiled, &mut compiler) {
                    eprintln!("Error: {}", e);
                    continue;
                }

                // Success
                println!(" ok");
            }
            Err(ReadlineError::Interrupted) => {
                // Ctrl+C
                println!("^C");
                continue;
            }
            Err(ReadlineError::Eof) => {
                // Ctrl+D
                println!("Goodbye!");
                break;
            }
            Err(err) => {
                eprintln!("Error: {}", err);
                break;
            }
        }
    }

    Ok(())
}

/// Execute compiled bytecode on device
fn execute_on_device(
    serial: &mut V4Serial,
    compiled: &CompileResult,
    compiler: &mut Compiler,
) -> Result<()> {
    // Execute word definitions first
    for word in &compiled.words {
        eprintln!(
            "[DEBUG] Executing word '{}' ({} bytes): {:02x?}",
            word.name,
            word.bytecode.len(),
            word.bytecode
        );
        let response = serial.exec(&word.bytecode, DEFAULT_TIMEOUT)?;
        if response.error_code != ErrorCode::Ok {
            return Err(crate::V4Error::Device(format!(
                "Failed to register word '{}': {}",
                word.name,
                response.error_code.name()
            )));
        }

        // Register word index returned from device
        if let Some(&word_idx) = response.word_indices.first() {
            eprintln!(
                "[DEBUG] Device registered word '{}' at index {}",
                word.name, word_idx
            );
            compiler
                .register_word_index(&word.name, word_idx as i32)
                .map_err(crate::V4Error::Compilation)?;
        }
    }

    // Execute main bytecode
    if !compiled.bytecode.is_empty() {
        eprintln!(
            "[DEBUG] Executing main bytecode ({} bytes): {:02x?}",
            compiled.bytecode.len(),
            compiled.bytecode
        );
        let response = serial.exec(&compiled.bytecode, DEFAULT_TIMEOUT)?;
        if response.error_code != ErrorCode::Ok {
            return Err(crate::V4Error::Device(format!(
                "Execution failed: {}",
                response.error_code.name()
            )));
        }
    }

    Ok(())
}

/// Handle meta-commands (.help, .ping, etc.)
fn handle_meta_command(line: &str, serial: &mut V4Serial, compiler: &mut Compiler) -> Result<()> {
    let parts: Vec<&str> = line.split_whitespace().collect();
    let command = parts[0];

    match command {
        ".help" => {
            print_help();
            Ok(())
        }
        ".ping" => {
            let err_code = serial.ping(DEFAULT_TIMEOUT)?;
            if err_code == ErrorCode::Ok {
                println!("Device is responsive");
            } else {
                println!("Device returned: {}", err_code.name());
            }
            Ok(())
        }
        ".reset" => {
            // Reset device VM
            let err_code = serial.reset(DEFAULT_TIMEOUT)?;
            if err_code != ErrorCode::Ok {
                return Err(crate::V4Error::Device(format!(
                    "Reset failed: {}",
                    err_code.name()
                )));
            }

            // Reset compiler context
            compiler.reset();

            println!("VM and compiler context reset");
            Ok(())
        }
        ".stack" => cmd_stack(serial),
        ".rstack" => cmd_rstack(serial),
        ".dump" => cmd_dump(serial, &parts[1..]),
        ".see" => cmd_see(serial, &parts[1..]),
        ".exit" => {
            // Handled in main loop
            Ok(())
        }
        _ => {
            println!("Unknown command: {}", command);
            println!("Type '.help' for available commands");
            Ok(())
        }
    }
}

fn print_help() {
    println!("Available commands:");
    println!("  .help              - Show this help");
    println!("  .ping              - Check device connection");
    println!("  .reset             - Reset VM and compiler context");
    println!("  .stack             - Show data and return stack contents");
    println!("  .rstack            - Show return stack with call trace");
    println!("  .dump [addr] [len] - Hexdump memory (default: continue from last)");
    println!("  .see <word_idx>    - Show word bytecode disassembly");
    println!("  .exit              - Exit REPL (same as 'bye')");
    println!("  bye                - Exit REPL");
    println!();
    println!("Forth language:");
    println!("  Any valid V4 Forth code");
    println!();
    println!("Control keys:");
    println!("  Ctrl+C   - Interrupt current line");
    println!("  Ctrl+D   - Exit REPL");
    println!("  ↑/↓      - Navigate command history");
}

/// Display data and return stacks
fn cmd_stack(serial: &mut V4Serial) -> Result<()> {
    let response = serial.query_stack(DEFAULT_TIMEOUT)?;
    if response.error_code != ErrorCode::Ok {
        return Err(crate::V4Error::Device(format!(
            "Query stack failed: {}",
            response.error_code.name()
        )));
    }

    let data = &response.data;
    if data.is_empty() {
        println!("No stack data received");
        return Ok(());
    }

    // Parse data stack
    let ds_depth = data[0] as usize;
    let mut pos = 1;

    println!("Data Stack (depth: {} / 256):", ds_depth);
    if ds_depth == 0 {
        println!("  <empty>");
    } else {
        for i in 0..ds_depth {
            if pos + 4 > data.len() {
                break;
            }
            let value =
                i32::from_le_bytes([data[pos], data[pos + 1], data[pos + 2], data[pos + 3]]);
            println!("  [{}]: 0x{:08X} ({})", i, value as u32, value);
            pos += 4;
        }
    }

    // Parse return stack
    if pos >= data.len() {
        return Ok(());
    }

    let rs_depth = data[pos] as usize;
    pos += 1;

    println!("\nReturn Stack (depth: {} / 64):", rs_depth);
    if rs_depth == 0 {
        println!("  <empty>");
    } else {
        for i in 0..rs_depth {
            if pos + 4 > data.len() {
                break;
            }
            let value =
                i32::from_le_bytes([data[pos], data[pos + 1], data[pos + 2], data[pos + 3]]);
            println!("  [{}]: 0x{:08X}", i, value as u32);
            pos += 4;
        }
    }

    Ok(())
}

/// Display return stack with call trace
fn cmd_rstack(serial: &mut V4Serial) -> Result<()> {
    let response = serial.query_stack(DEFAULT_TIMEOUT)?;
    if response.error_code != ErrorCode::Ok {
        return Err(crate::V4Error::Device(format!(
            "Query stack failed: {}",
            response.error_code.name()
        )));
    }

    let data = &response.data;
    if data.is_empty() {
        println!("No stack data received");
        return Ok(());
    }

    // Skip data stack
    let ds_depth = data[0] as usize;
    let mut pos = 1 + ds_depth * 4;

    if pos >= data.len() {
        println!("No return stack data available");
        return Ok(());
    }

    let rs_depth = data[pos] as usize;
    pos += 1;

    println!("Return Stack (depth: {} / 64):", rs_depth);
    if rs_depth == 0 {
        println!("  <empty>");
        return Ok(());
    }

    println!("\nCall trace (most recent first):");
    for i in 0..rs_depth {
        if pos + 4 > data.len() {
            break;
        }
        let value = i32::from_le_bytes([data[pos], data[pos + 1], data[pos + 2], data[pos + 3]]);
        println!("  [{:2}]: 0x{:08X}", i, value as u32);
        pos += 4;
    }

    Ok(())
}

/// Hexdump memory at address
fn cmd_dump(serial: &mut V4Serial, args: &[&str]) -> Result<()> {
    // TODO: Track last dump address for continuation
    let addr: u32 = if args.is_empty() {
        0 // Default to address 0
    } else {
        args[0]
            .parse()
            .map_err(|_| crate::V4Error::Cli(format!("Invalid address: {}", args[0])))?
    };

    let len: u16 = if args.len() < 2 {
        256 // Default to 256 bytes
    } else {
        args[1]
            .parse::<u16>()
            .map_err(|_| crate::V4Error::Cli(format!("Invalid length: {}", args[1])))?
            .min(256)
    };

    let response = serial.query_memory(addr, len, DEFAULT_TIMEOUT)?;
    if response.error_code != ErrorCode::Ok {
        return Err(crate::V4Error::Device(format!(
            "Query memory failed: {}",
            response.error_code.name()
        )));
    }

    let data = &response.data;
    println!("Memory dump at 0x{:08X} ({} bytes):\n", addr, data.len());

    // Display in 16-byte rows
    for (i, chunk) in data.chunks(16).enumerate() {
        let offset = addr + (i * 16) as u32;
        print!("{:08X}  ", offset);

        // Hex values
        for (j, byte) in chunk.iter().enumerate() {
            print!("{:02X} ", byte);
            if j == 7 {
                print!(" ");
            }
        }

        // Padding if last row is incomplete
        for j in chunk.len()..16 {
            print!("   ");
            if j == 7 {
                print!(" ");
            }
        }

        // ASCII representation
        print!(" |");
        for byte in chunk {
            let c = if *byte >= 0x20 && *byte <= 0x7E {
                *byte as char
            } else {
                '.'
            };
            print!("{}", c);
        }
        println!("|");
    }

    Ok(())
}

/// Show word bytecode disassembly
fn cmd_see(serial: &mut V4Serial, args: &[&str]) -> Result<()> {
    if args.is_empty() {
        return Err(crate::V4Error::Cli("Usage: .see <word_index>".to_string()));
    }

    let word_idx: u16 = args[0]
        .parse()
        .map_err(|_| crate::V4Error::Cli(format!("Invalid word index: {}", args[0])))?;

    let response = serial.query_word(word_idx, DEFAULT_TIMEOUT)?;
    if response.error_code != ErrorCode::Ok {
        return Err(crate::V4Error::Device(format!(
            "Query word failed: {}",
            response.error_code.name()
        )));
    }

    let data = &response.data;
    if data.is_empty() {
        println!("No word data received");
        return Ok(());
    }

    // Parse response: [NAME_LEN][NAME...][CODE_LEN_L][CODE_LEN_H][CODE...]
    let name_len = data[0] as usize;
    let mut pos = 1;

    let name = if name_len > 0 && pos + name_len <= data.len() {
        String::from_utf8_lossy(&data[pos..pos + name_len]).to_string()
    } else {
        "<anonymous>".to_string()
    };
    pos += name_len;

    if pos + 2 > data.len() {
        println!("Incomplete word data");
        return Ok(());
    }

    let code_len = u16::from_le_bytes([data[pos], data[pos + 1]]) as usize;
    pos += 2;

    println!("Word: {}", name);
    println!("Index: {}", word_idx);
    println!("Bytecode length: {} bytes\n", code_len);

    if code_len == 0 || pos >= data.len() {
        println!("No bytecode");
        return Ok(());
    }

    println!("Disassembly:");
    println!("Offset  Bytes");
    println!("------  -------------------------");

    let code = &data[pos..];
    for (i, chunk) in code.chunks(16).enumerate() {
        print!("{:04X}    ", i * 16);
        for byte in chunk {
            print!("{:02X} ", byte);
        }
        println!();
    }

    println!("\nNote: Use V4-front disassembler for opcode names.");

    Ok(())
}