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>
This commit is contained in:
1 parent
a29bf6e97b
commit
ac0a3fbced
48 files changed
+3007
-202
No files matched your search
@@ -0,0 +1,453 @@
|
||||
//! AST → bytecode Program.
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::ast::{
|
||||
Arg, BinaryOp, CallExpr, Expr, Function, InterpPart, Literal, Module, Stmt, UnaryOp,
|
||||
};
|
||||
use crate::bytecode::{FnChunk, Op, Program};
|
||||
use crate::value::Value;
|
||||
|
||||
struct FlatFun {
|
||||
name: String,
|
||||
parent: Option<usize>,
|
||||
params: Vec<String>,
|
||||
body: Vec<Stmt>,
|
||||
children: Vec<usize>,
|
||||
}
|
||||
|
||||
pub fn compile_module(module: &Module) -> Result<Program> {
|
||||
let mut flat = Vec::new();
|
||||
for fun in &module.functions {
|
||||
collect_fun(fun, None, &mut flat);
|
||||
}
|
||||
if flat.is_empty() {
|
||||
bail!("bytecode: no functions to compile");
|
||||
}
|
||||
|
||||
let entry = flat
|
||||
.iter()
|
||||
.position(|f| f.name == "main" && f.parent.is_none())
|
||||
.or_else(|| {
|
||||
flat.iter()
|
||||
.position(|f| f.parent.is_none() && f.params.is_empty())
|
||||
})
|
||||
.ok_or_else(|| anyhow::anyhow!("bytecode: no entry function"))?;
|
||||
|
||||
let mut functions = Vec::new();
|
||||
for i in 0..flat.len() {
|
||||
functions.push(compile_function(i, &flat)?);
|
||||
}
|
||||
|
||||
Ok(Program { functions, entry })
|
||||
}
|
||||
|
||||
fn collect_fun(fun: &Function, parent: Option<usize>, flat: &mut Vec<FlatFun>) -> usize {
|
||||
let id = flat.len();
|
||||
flat.push(FlatFun {
|
||||
name: fun.name.clone(),
|
||||
parent,
|
||||
params: fun.params.clone(),
|
||||
body: fun.body.clone(),
|
||||
children: Vec::new(),
|
||||
});
|
||||
let mut child_ids = Vec::new();
|
||||
for child in &fun.children {
|
||||
child_ids.push(collect_fun(child, Some(id), flat));
|
||||
}
|
||||
flat[id].children = child_ids;
|
||||
id
|
||||
}
|
||||
|
||||
struct FnCompiler<'a> {
|
||||
flat: &'a [FlatFun],
|
||||
fn_id: usize,
|
||||
chunk: FnChunk,
|
||||
locals: HashMap<String, u8>,
|
||||
}
|
||||
|
||||
fn compile_function(fn_id: usize, flat: &[FlatFun]) -> Result<FnChunk> {
|
||||
let fun = &flat[fn_id];
|
||||
let mut locals = HashMap::new();
|
||||
for (i, p) in fun.params.iter().enumerate() {
|
||||
locals.insert(p.clone(), i as u8);
|
||||
}
|
||||
let mut c = FnCompiler {
|
||||
flat,
|
||||
fn_id,
|
||||
chunk: FnChunk {
|
||||
name: fun.name.clone(),
|
||||
params: fun.params.clone(),
|
||||
code: Vec::new(),
|
||||
constants: Vec::new(),
|
||||
locals: fun.params.clone(),
|
||||
},
|
||||
locals,
|
||||
};
|
||||
for stmt in &fun.body {
|
||||
c.compile_stmt(stmt)?;
|
||||
}
|
||||
c.emit(Op::None_);
|
||||
c.emit(Op::Return);
|
||||
let mut names = vec![String::new(); c.locals.len()];
|
||||
for (name, slot) in &c.locals {
|
||||
names[*slot as usize] = name.clone();
|
||||
}
|
||||
c.chunk.locals = names;
|
||||
Ok(c.chunk)
|
||||
}
|
||||
|
||||
impl<'a> FnCompiler<'a> {
|
||||
fn emit(&mut self, op: Op) {
|
||||
self.chunk.code.push(op);
|
||||
}
|
||||
|
||||
fn here(&self) -> u16 {
|
||||
self.chunk.code.len() as u16
|
||||
}
|
||||
|
||||
fn patch(&mut self, at: u16, op: Op) {
|
||||
self.chunk.code[at as usize] = op;
|
||||
}
|
||||
|
||||
fn add_const(&mut self, v: Value) -> u16 {
|
||||
if let Some(i) = self.chunk.constants.iter().position(|c| c == &v) {
|
||||
return i as u16;
|
||||
}
|
||||
let i = self.chunk.constants.len() as u16;
|
||||
self.chunk.constants.push(v);
|
||||
i
|
||||
}
|
||||
|
||||
fn local_slot(&mut self, name: &str) -> u8 {
|
||||
if let Some(&s) = self.locals.get(name) {
|
||||
return s;
|
||||
}
|
||||
let s = self.locals.len() as u8;
|
||||
self.locals.insert(name.to_string(), s);
|
||||
s
|
||||
}
|
||||
|
||||
fn resolve_call(&self, name: &str) -> Result<usize> {
|
||||
let fun = &self.flat[self.fn_id];
|
||||
for &cid in &fun.children {
|
||||
if self.flat[cid].name == name {
|
||||
return Ok(cid);
|
||||
}
|
||||
}
|
||||
let mut parent = fun.parent;
|
||||
while let Some(pid) = parent {
|
||||
for &cid in &self.flat[pid].children {
|
||||
if self.flat[cid].name == name {
|
||||
return Ok(cid);
|
||||
}
|
||||
}
|
||||
parent = self.flat[pid].parent;
|
||||
}
|
||||
for (i, f) in self.flat.iter().enumerate() {
|
||||
if f.parent.is_none() && f.name == name {
|
||||
return Ok(i);
|
||||
}
|
||||
}
|
||||
bail!("bytecode: unknown function `{name}`")
|
||||
}
|
||||
|
||||
fn compile_stmt(&mut self, stmt: &Stmt) -> Result<()> {
|
||||
match stmt {
|
||||
Stmt::Assign { name, value, .. } => {
|
||||
self.compile_expr(value)?;
|
||||
let slot = self.local_slot(name);
|
||||
self.emit(Op::SetLocal(slot));
|
||||
self.emit(Op::Pop);
|
||||
}
|
||||
Stmt::Return { value, .. } => {
|
||||
self.compile_expr(value)?;
|
||||
self.emit(Op::Return);
|
||||
}
|
||||
Stmt::Call { call, .. } => self.compile_call(call, true)?,
|
||||
Stmt::Branch { arms, .. } => {
|
||||
let mut end_jumps = Vec::new();
|
||||
for (i, arm) in arms.iter().enumerate() {
|
||||
let is_last = i + 1 == arms.len();
|
||||
let mut else_jump = None;
|
||||
if let Some(cond) = &arm.condition {
|
||||
self.compile_expr(cond)?;
|
||||
let jf = self.here();
|
||||
self.emit(Op::JumpIfFalse(0));
|
||||
else_jump = Some(jf);
|
||||
}
|
||||
for st in &arm.body {
|
||||
self.compile_stmt(st)?;
|
||||
}
|
||||
if !is_last {
|
||||
let j = self.here();
|
||||
self.emit(Op::Jump(0));
|
||||
end_jumps.push(j);
|
||||
}
|
||||
if let Some(jf) = else_jump {
|
||||
self.patch(jf, Op::JumpIfFalse(self.here()));
|
||||
}
|
||||
}
|
||||
let end = self.here();
|
||||
for j in end_jumps {
|
||||
self.patch(j, Op::Jump(end));
|
||||
}
|
||||
}
|
||||
Stmt::While {
|
||||
condition, body, ..
|
||||
} => {
|
||||
let loop_start = self.here();
|
||||
self.compile_expr(condition)?;
|
||||
let exit_j = self.here();
|
||||
self.emit(Op::JumpIfFalse(0));
|
||||
for st in body {
|
||||
self.compile_stmt(st)?;
|
||||
}
|
||||
self.emit(Op::Jump(loop_start));
|
||||
self.patch(exit_j, Op::JumpIfFalse(self.here()));
|
||||
}
|
||||
Stmt::ForEach {
|
||||
item,
|
||||
collection,
|
||||
body,
|
||||
..
|
||||
} => {
|
||||
self.compile_expr(&Expr::Var(collection.clone()))?;
|
||||
let coll_slot = self.local_slot(&format!("__coll_{collection}"));
|
||||
self.emit(Op::SetLocal(coll_slot));
|
||||
self.emit(Op::Pop);
|
||||
let i_slot = self.local_slot(&format!("__i_{collection}"));
|
||||
let zero = self.add_const(Value::Int(0));
|
||||
self.emit(Op::Constant(zero));
|
||||
self.emit(Op::SetLocal(i_slot));
|
||||
self.emit(Op::Pop);
|
||||
let item_slot = self.local_slot(item);
|
||||
|
||||
let loop_start = self.here();
|
||||
self.emit(Op::GetLocal(i_slot));
|
||||
self.emit(Op::GetLocal(coll_slot));
|
||||
self.emit(Op::Len);
|
||||
self.emit(Op::Less);
|
||||
let exit_j = self.here();
|
||||
self.emit(Op::JumpIfFalse(0));
|
||||
|
||||
self.emit(Op::GetLocal(coll_slot));
|
||||
self.emit(Op::GetLocal(i_slot));
|
||||
self.emit(Op::GetIndex);
|
||||
self.emit(Op::SetLocal(item_slot));
|
||||
self.emit(Op::Pop);
|
||||
|
||||
for st in body {
|
||||
self.compile_stmt(st)?;
|
||||
}
|
||||
|
||||
self.emit(Op::GetLocal(i_slot));
|
||||
let one = self.add_const(Value::Int(1));
|
||||
self.emit(Op::Constant(one));
|
||||
self.emit(Op::Add);
|
||||
self.emit(Op::SetLocal(i_slot));
|
||||
self.emit(Op::Pop);
|
||||
self.emit(Op::Jump(loop_start));
|
||||
self.patch(exit_j, Op::JumpIfFalse(self.here()));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn compile_call(&mut self, call: &CallExpr, as_stmt: bool) -> Result<()> {
|
||||
match call.callee.as_str() {
|
||||
"print" => {
|
||||
let text_expr = call
|
||||
.args
|
||||
.iter()
|
||||
.find_map(|a| match a {
|
||||
Arg::Named { name, value } if name == "text" => Some(value),
|
||||
_ => None,
|
||||
})
|
||||
.or_else(|| {
|
||||
call.args.iter().find_map(|a| match a {
|
||||
Arg::Positional(e) => Some(e),
|
||||
_ => None,
|
||||
})
|
||||
})
|
||||
.ok_or_else(|| anyhow::anyhow!("bytecode: print needs text"))?;
|
||||
self.compile_expr(text_expr)?;
|
||||
self.emit(Op::Print);
|
||||
if !as_stmt {
|
||||
self.emit(Op::None_);
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
"input" => bail!("bytecode: input not supported"),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
let fid = self.resolve_call(&call.callee)?;
|
||||
let params = self.flat[fid].params.clone();
|
||||
let mut named: HashMap<String, &Expr> = HashMap::new();
|
||||
let mut positionals = Vec::new();
|
||||
for a in &call.args {
|
||||
match a {
|
||||
Arg::Positional(e) => positionals.push(e),
|
||||
Arg::Named { name, value } => {
|
||||
named.insert(name.clone(), value);
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut pos_i = 0;
|
||||
for p in ¶ms {
|
||||
if let Some(e) = named.get(p) {
|
||||
self.compile_expr(e)?;
|
||||
} else if pos_i < positionals.len() {
|
||||
self.compile_expr(positionals[pos_i])?;
|
||||
pos_i += 1;
|
||||
} else {
|
||||
bail!("bytecode: missing argument for parameter `{p}`");
|
||||
}
|
||||
}
|
||||
if pos_i < positionals.len() {
|
||||
bail!("bytecode: too many positional arguments");
|
||||
}
|
||||
self.emit(Op::Call(fid as u16, params.len() as u8));
|
||||
if as_stmt {
|
||||
self.emit(Op::Pop);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn compile_expr(&mut self, expr: &Expr) -> Result<()> {
|
||||
match expr {
|
||||
Expr::Literal(lit) => match lit {
|
||||
Literal::None => self.emit(Op::None_),
|
||||
Literal::Bool(true) => self.emit(Op::True),
|
||||
Literal::Bool(false) => self.emit(Op::False),
|
||||
Literal::Int(n) => {
|
||||
let i = self.add_const(Value::Int(*n));
|
||||
self.emit(Op::Constant(i));
|
||||
}
|
||||
Literal::Text(t) => {
|
||||
let i = self.add_const(Value::Text(t.clone()));
|
||||
self.emit(Op::Constant(i));
|
||||
}
|
||||
},
|
||||
Expr::Var(name) => {
|
||||
let slot = self
|
||||
.locals
|
||||
.get(name)
|
||||
.copied()
|
||||
.ok_or_else(|| anyhow::anyhow!("bytecode: undefined variable `{name}`"))?;
|
||||
self.emit(Op::GetLocal(slot));
|
||||
}
|
||||
Expr::Interp(parts) => {
|
||||
if parts.is_empty() {
|
||||
let i = self.add_const(Value::Text(String::new()));
|
||||
self.emit(Op::Constant(i));
|
||||
return Ok(());
|
||||
}
|
||||
for (idx, part) in parts.iter().enumerate() {
|
||||
match part {
|
||||
InterpPart::Lit(t) => {
|
||||
let i = self.add_const(Value::Text(t.clone()));
|
||||
self.emit(Op::Constant(i));
|
||||
}
|
||||
InterpPart::Var(n) => {
|
||||
let slot = self.locals.get(n).copied().ok_or_else(|| {
|
||||
anyhow::anyhow!("bytecode: undefined variable `{n}`")
|
||||
})?;
|
||||
self.emit(Op::GetLocal(slot));
|
||||
}
|
||||
}
|
||||
if idx > 0 {
|
||||
self.emit(Op::Add);
|
||||
}
|
||||
}
|
||||
}
|
||||
Expr::Unary { op, expr } => {
|
||||
self.compile_expr(expr)?;
|
||||
match op {
|
||||
UnaryOp::Not => self.emit(Op::Not),
|
||||
UnaryOp::Neg => self.emit(Op::Negate),
|
||||
}
|
||||
}
|
||||
Expr::Binary {
|
||||
op: BinaryOp::And,
|
||||
left,
|
||||
right,
|
||||
} => {
|
||||
// JumpIfFalse always pops the tested value.
|
||||
self.compile_expr(left)?;
|
||||
let j_false = self.here();
|
||||
self.emit(Op::JumpIfFalse(0));
|
||||
self.compile_expr(right)?;
|
||||
let j_false2 = self.here();
|
||||
self.emit(Op::JumpIfFalse(0));
|
||||
self.emit(Op::True);
|
||||
let j_end = self.here();
|
||||
self.emit(Op::Jump(0));
|
||||
let f2 = self.here();
|
||||
self.patch(j_false2, Op::JumpIfFalse(f2));
|
||||
self.emit(Op::False);
|
||||
let j_end2 = self.here();
|
||||
self.emit(Op::Jump(0));
|
||||
let f1 = self.here();
|
||||
self.patch(j_false, Op::JumpIfFalse(f1));
|
||||
self.emit(Op::False);
|
||||
let end = self.here();
|
||||
self.patch(j_end, Op::Jump(end));
|
||||
self.patch(j_end2, Op::Jump(end));
|
||||
}
|
||||
Expr::Binary {
|
||||
op: BinaryOp::Or,
|
||||
left,
|
||||
right,
|
||||
} => {
|
||||
self.compile_expr(left)?;
|
||||
let j_else = self.here();
|
||||
self.emit(Op::JumpIfFalse(0));
|
||||
self.emit(Op::True);
|
||||
let j_end = self.here();
|
||||
self.emit(Op::Jump(0));
|
||||
let else_ip = self.here();
|
||||
self.patch(j_else, Op::JumpIfFalse(else_ip));
|
||||
self.compile_expr(right)?;
|
||||
let j_f = self.here();
|
||||
self.emit(Op::JumpIfFalse(0));
|
||||
self.emit(Op::True);
|
||||
let j_end2 = self.here();
|
||||
self.emit(Op::Jump(0));
|
||||
let f = self.here();
|
||||
self.patch(j_f, Op::JumpIfFalse(f));
|
||||
self.emit(Op::False);
|
||||
let end = self.here();
|
||||
self.patch(j_end, Op::Jump(end));
|
||||
self.patch(j_end2, Op::Jump(end));
|
||||
}
|
||||
Expr::Binary { op, left, right } => {
|
||||
self.compile_expr(left)?;
|
||||
self.compile_expr(right)?;
|
||||
self.emit(match op {
|
||||
BinaryOp::Add => Op::Add,
|
||||
BinaryOp::Sub => Op::Sub,
|
||||
BinaryOp::Mul => Op::Mul,
|
||||
BinaryOp::Div => Op::Div,
|
||||
BinaryOp::Eq => Op::Equal,
|
||||
BinaryOp::Ne => Op::NotEqual,
|
||||
BinaryOp::Lt => Op::Less,
|
||||
BinaryOp::Le => Op::Lessequal,
|
||||
BinaryOp::Gt => Op::Greater,
|
||||
BinaryOp::Ge => Op::GreaterEqual,
|
||||
BinaryOp::And | BinaryOp::Or => unreachable!(),
|
||||
});
|
||||
}
|
||||
Expr::Call(call) => self.compile_call(call, false)?,
|
||||
Expr::List(items) => {
|
||||
for it in items {
|
||||
self.compile_expr(it)?;
|
||||
}
|
||||
self.emit(Op::BuildList(items.len() as u16));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
//! 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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,236 @@
|
||||
//! Stack VM for Marqdo bytecode.
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
|
||||
use crate::bytecode::{Op, Program};
|
||||
use crate::value::Value;
|
||||
|
||||
pub struct Vm {
|
||||
capture: bool,
|
||||
pub captured_stdout: String,
|
||||
}
|
||||
|
||||
struct Frame {
|
||||
fn_idx: usize,
|
||||
ip: usize,
|
||||
slots: Vec<Value>,
|
||||
}
|
||||
|
||||
impl Vm {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
capture: false,
|
||||
captured_stdout: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_capture() -> Self {
|
||||
Self {
|
||||
capture: true,
|
||||
captured_stdout: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn emit_line(&mut self, text: &str) {
|
||||
if self.capture {
|
||||
self.captured_stdout.push_str(text);
|
||||
self.captured_stdout.push('\n');
|
||||
} else {
|
||||
println!("{text}");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(&mut self, program: &Program) -> Result<Value> {
|
||||
let mut stack: Vec<Value> = Vec::new();
|
||||
let entry = program.entry;
|
||||
let arity = program.functions[entry].params.len();
|
||||
if arity != 0 {
|
||||
bail!("bytecode: entry function needs arguments");
|
||||
}
|
||||
let mut frames = vec![Frame {
|
||||
fn_idx: entry,
|
||||
ip: 0,
|
||||
slots: vec![Value::None; program.functions[entry].locals.len().max(1)],
|
||||
}];
|
||||
|
||||
loop {
|
||||
let frame = frames.last_mut().unwrap();
|
||||
let fun = &program.functions[frame.fn_idx];
|
||||
if frame.ip >= fun.code.len() {
|
||||
bail!("bytecode: ip out of range in {}", fun.name);
|
||||
}
|
||||
let op = fun.code[frame.ip];
|
||||
frame.ip += 1;
|
||||
|
||||
match op {
|
||||
Op::Constant(i) => stack.push(fun.constants[i as usize].clone()),
|
||||
Op::True => stack.push(Value::Bool(true)),
|
||||
Op::False => stack.push(Value::Bool(false)),
|
||||
Op::None_ => stack.push(Value::None),
|
||||
Op::Pop => {
|
||||
stack.pop();
|
||||
}
|
||||
Op::GetLocal(i) => {
|
||||
let v = frames.last().unwrap().slots[i as usize].clone();
|
||||
stack.push(v);
|
||||
}
|
||||
Op::SetLocal(i) => {
|
||||
let v = stack.last().cloned().unwrap_or(Value::None);
|
||||
let slot = i as usize;
|
||||
let fr = frames.last_mut().unwrap();
|
||||
if fr.slots.len() <= slot {
|
||||
fr.slots.resize(slot + 1, Value::None);
|
||||
}
|
||||
fr.slots[slot] = v;
|
||||
}
|
||||
Op::Add => {
|
||||
let b = pop(&mut stack)?;
|
||||
let a = pop(&mut stack)?;
|
||||
stack.push(add(a, b)?);
|
||||
}
|
||||
Op::Sub => bin_int(&mut stack, |a, b| a - b)?,
|
||||
Op::Mul => bin_int(&mut stack, |a, b| a * b)?,
|
||||
Op::Div => {
|
||||
let b = pop(&mut stack)?;
|
||||
let a = pop(&mut stack)?;
|
||||
match (a, b) {
|
||||
(Value::Int(_), Value::Int(0)) => bail!("division by zero"),
|
||||
(Value::Int(x), Value::Int(y)) => stack.push(Value::Int(x / y)),
|
||||
_ => bail!("`/` needs ints"),
|
||||
}
|
||||
}
|
||||
Op::Negate => match pop(&mut stack)? {
|
||||
Value::Int(n) => stack.push(Value::Int(-n)),
|
||||
_ => bail!("unary `-` needs int"),
|
||||
},
|
||||
Op::Not => {
|
||||
let v = pop(&mut stack)?;
|
||||
stack.push(Value::Bool(!v.truthy()));
|
||||
}
|
||||
Op::Equal => {
|
||||
let b = pop(&mut stack)?;
|
||||
let a = pop(&mut stack)?;
|
||||
stack.push(Value::Bool(a == b));
|
||||
}
|
||||
Op::NotEqual => {
|
||||
let b = pop(&mut stack)?;
|
||||
let a = pop(&mut stack)?;
|
||||
stack.push(Value::Bool(a != b));
|
||||
}
|
||||
Op::Greater => cmp(&mut stack, |o| o.is_gt())?,
|
||||
Op::GreaterEqual => cmp(&mut stack, |o| o.is_ge())?,
|
||||
Op::Less => cmp(&mut stack, |o| o.is_lt())?,
|
||||
Op::Lessequal => cmp(&mut stack, |o| o.is_le())?,
|
||||
Op::Jump(t) => {
|
||||
frames.last_mut().unwrap().ip = t as usize;
|
||||
}
|
||||
Op::JumpIfFalse(t) => {
|
||||
let v = pop(&mut stack)?;
|
||||
if !v.truthy() {
|
||||
frames.last_mut().unwrap().ip = t as usize;
|
||||
}
|
||||
}
|
||||
Op::Print => {
|
||||
let v = pop(&mut stack)?;
|
||||
self.emit_line(&v.as_display());
|
||||
}
|
||||
Op::BuildList(n) => {
|
||||
let n = n as usize;
|
||||
let mut items = Vec::with_capacity(n);
|
||||
for _ in 0..n {
|
||||
items.push(pop(&mut stack)?);
|
||||
}
|
||||
items.reverse();
|
||||
stack.push(Value::List(items));
|
||||
}
|
||||
Op::Len => match pop(&mut stack)? {
|
||||
Value::List(xs) => stack.push(Value::Int(xs.len() as i64)),
|
||||
_ => bail!("len needs list"),
|
||||
},
|
||||
Op::GetIndex => {
|
||||
let idx = pop(&mut stack)?;
|
||||
let list = pop(&mut stack)?;
|
||||
match (list, idx) {
|
||||
(Value::List(xs), Value::Int(i)) if i >= 0 && (i as usize) < xs.len() => {
|
||||
stack.push(xs[i as usize].clone());
|
||||
}
|
||||
(Value::List(_), Value::Int(_)) => stack.push(Value::None),
|
||||
_ => bail!("GetIndex needs list and int"),
|
||||
}
|
||||
}
|
||||
Op::Call(fid, argc) => {
|
||||
let fid = fid as usize;
|
||||
let argc = argc as usize;
|
||||
let callee = &program.functions[fid];
|
||||
if callee.params.len() != argc {
|
||||
bail!("bytecode: call argc mismatch");
|
||||
}
|
||||
let mut slots = vec![Value::None; callee.locals.len().max(argc).max(1)];
|
||||
for i in (0..argc).rev() {
|
||||
slots[i] = pop(&mut stack)?;
|
||||
}
|
||||
// args were pushed in param order; we popped reverse so slots[0] is last param — fix:
|
||||
// stack had p0, p1, p2 (top). pop → p2, p1, p0. So assign slots[argc-1-i] or pop into reverse.
|
||||
// Actually we did: for i in (0..argc).rev() { slots[i] = pop() }
|
||||
// first pop → slots[argc-1] = top = last param. Good if push order was p0..plast.
|
||||
frames.push(Frame {
|
||||
fn_idx: fid,
|
||||
ip: 0,
|
||||
slots,
|
||||
});
|
||||
}
|
||||
Op::Return => {
|
||||
let ret = pop(&mut stack).unwrap_or(Value::None);
|
||||
frames.pop();
|
||||
if frames.is_empty() {
|
||||
return Ok(ret);
|
||||
}
|
||||
stack.push(ret);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn pop(stack: &mut Vec<Value>) -> Result<Value> {
|
||||
stack
|
||||
.pop()
|
||||
.ok_or_else(|| anyhow::anyhow!("bytecode: stack underflow"))
|
||||
}
|
||||
|
||||
fn add(a: Value, b: Value) -> Result<Value> {
|
||||
match (a, b) {
|
||||
(Value::Int(x), Value::Int(y)) => Ok(Value::Int(x + y)),
|
||||
(Value::Text(x), Value::Text(y)) => Ok(Value::Text(format!("{x}{y}"))),
|
||||
(Value::Text(x), y) => Ok(Value::Text(format!("{x}{}", y.as_display()))),
|
||||
(x, Value::Text(y)) => Ok(Value::Text(format!("{}{y}", x.as_display()))),
|
||||
// Int + display for interp GetLocal Int + Lit text
|
||||
(Value::Int(x), y) => Ok(Value::Text(format!("{}{}", x, y.as_display()))),
|
||||
(x, Value::Int(y)) => Ok(Value::Text(format!("{}{y}", x.as_display()))),
|
||||
_ => bail!("`+` needs ints or text"),
|
||||
}
|
||||
}
|
||||
|
||||
fn bin_int(stack: &mut Vec<Value>, f: impl Fn(i64, i64) -> i64) -> Result<()> {
|
||||
let b = pop(stack)?;
|
||||
let a = pop(stack)?;
|
||||
match (a, b) {
|
||||
(Value::Int(x), Value::Int(y)) => {
|
||||
stack.push(Value::Int(f(x, y)));
|
||||
Ok(())
|
||||
}
|
||||
_ => bail!("binary op needs ints"),
|
||||
}
|
||||
}
|
||||
|
||||
fn cmp(stack: &mut Vec<Value>, pred: impl Fn(std::cmp::Ordering) -> bool) -> Result<()> {
|
||||
let b = pop(stack)?;
|
||||
let a = pop(stack)?;
|
||||
let ord = match (&a, &b) {
|
||||
(Value::Int(x), Value::Int(y)) => x.cmp(y),
|
||||
(Value::Text(x), Value::Text(y)) => x.cmp(y),
|
||||
_ => bail!("comparison needs two ints or two texts"),
|
||||
};
|
||||
stack.push(Value::Bool(pred(ord)));
|
||||
Ok(())
|
||||
}
|
||||
Reference in new issue
Block a user