Files
marqdo/src/bytecode/mod.rs
T
cflmyandCursor ac0a3fbced Add view, positional args, errors gold, and bytecode backend.
Reorganize examples into structure/keywords/errors, launch marqdo view for AST-backed browsing, and ship an M5 stack VM selectable via --backend.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-04 12:30:40 +08:00

81 lines
1.8 KiB
Rust

//! Bytecode chunk + opcodes (M5 prototype).
mod compile;
mod vm;
pub use compile::compile_module;
pub use vm::Vm;
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,
BuildList(u16),
Len,
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>,
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() {
out.push_str(&format!(" {ip:04} {op:?}\n"));
}
}
out.push_str("=== marqdo: end bytecode ===\n");
out
}
}