Unify bytecode errors with path:line:col, support --stdin-file and live view preset stdin, and ship the S0 standard library with gold coverage. Co-authored-by: Cursor <cursoragent@cursor.com>
92 lines
2.2 KiB
Rust
92 lines
2.2 KiB
Rust
//! Bytecode chunk + opcodes (M5 prototype).
|
|
|
|
mod compile;
|
|
mod vm;
|
|
|
|
pub use compile::compile_module;
|
|
pub use vm::Vm;
|
|
|
|
use crate::diagnostics::Span;
|
|
use crate::value::Value;
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub enum Op {
|
|
Constant(u16),
|
|
True,
|
|
False,
|
|
None_,
|
|
Pop,
|
|
GetLocal(u8),
|
|
SetLocal(u8),
|
|
Add,
|
|
Sub,
|
|
Mul,
|
|
Div,
|
|
Negate,
|
|
Not,
|
|
Equal,
|
|
NotEqual,
|
|
Greater,
|
|
GreaterEqual,
|
|
Less,
|
|
Lessequal,
|
|
/// Absolute jump to instruction index.
|
|
Jump(u16),
|
|
JumpIfFalse(u16),
|
|
Print,
|
|
/// Pop prompt text, print it (no newline), read one stdin line → push Text.
|
|
Input,
|
|
BuildList(u16),
|
|
/// Pop value → push `len` (text/list).
|
|
Len,
|
|
/// Pop value → push display text.
|
|
Str,
|
|
/// Pop value → push int (or fail).
|
|
Int,
|
|
GetIndex,
|
|
/// Call function by index; argc values are on the stack (param order).
|
|
Call(u16, u8),
|
|
Return,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct FnChunk {
|
|
pub name: String,
|
|
pub params: Vec<String>,
|
|
pub code: Vec<Op>,
|
|
/// Parallel to `code`: source location for diagnostics / debug.
|
|
pub spans: Vec<Span>,
|
|
pub constants: Vec<Value>,
|
|
/// Local slot names (params first).
|
|
pub locals: Vec<String>,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct Program {
|
|
pub functions: Vec<FnChunk>,
|
|
pub entry: usize,
|
|
}
|
|
|
|
impl Program {
|
|
pub fn disassemble(&self) -> String {
|
|
let mut out = String::new();
|
|
out.push_str("=== marqdo: bytecode ===\n");
|
|
for (fi, fun) in self.functions.iter().enumerate() {
|
|
let entry = if fi == self.entry { " <entry>" } else { "" };
|
|
out.push_str(&format!(
|
|
"fn[{fi}] {} params={:?} locals={:?}{entry}\n",
|
|
fun.name, fun.params, fun.locals
|
|
));
|
|
for (i, c) in fun.constants.iter().enumerate() {
|
|
out.push_str(&format!(" const[{i}] = {c:?}\n"));
|
|
}
|
|
for (ip, op) in fun.code.iter().enumerate() {
|
|
let sp = fun.spans.get(ip).copied().unwrap_or(Span::new(1, 1));
|
|
out.push_str(&format!(" {ip:04} {op:?} @{sp}\n"));
|
|
}
|
|
}
|
|
out.push_str("=== marqdo: end bytecode ===\n");
|
|
out
|
|
}
|
|
}
|