Add diagnostics spans, CLI/view input feeding, and len/str/int builtins.

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>
This commit is contained in:
cflmyandCursor committed 2026-08-04 17:26:09 +08:00
1 parent 31a4b69bbd
commit 30cd39fd8f
25 files changed
+964 -111

No files matched your search

+106 -19
View File
@@ -2,11 +2,13 @@
use anyhow::{bail, Result};
use std::collections::HashMap;
use std::path::Path;
use crate::ast::{
Arg, BinaryOp, CallExpr, Expr, Function, InterpPart, Literal, Module, Stmt, UnaryOp,
};
use crate::bytecode::{FnChunk, Op, Program};
use crate::diagnostics::{bail_at, Span};
use crate::value::Value;
struct FlatFun {
@@ -15,9 +17,10 @@ struct FlatFun {
params: Vec<String>,
body: Vec<Stmt>,
children: Vec<usize>,
span: Span,
}
pub fn compile_module(module: &Module) -> Result<Program> {
pub fn compile_module(path: Option<&Path>, module: &Module) -> Result<Program> {
let mut flat = Vec::new();
for fun in &module.functions {
collect_fun(fun, None, &mut flat);
@@ -35,9 +38,10 @@ pub fn compile_module(module: &Module) -> Result<Program> {
})
.ok_or_else(|| anyhow::anyhow!("bytecode: no entry function"))?;
let path_buf = path.map(|p| p.to_path_buf());
let mut functions = Vec::new();
for i in 0..flat.len() {
functions.push(compile_function(i, &flat)?);
functions.push(compile_function(i, &flat, path_buf.as_deref())?);
}
Ok(Program { functions, entry })
@@ -51,6 +55,7 @@ fn collect_fun(fun: &Function, parent: Option<usize>, flat: &mut Vec<FlatFun>) -
params: fun.params.clone(),
body: fun.body.clone(),
children: Vec::new(),
span: fun.span,
});
let mut child_ids = Vec::new();
for child in &fun.children {
@@ -61,33 +66,39 @@ fn collect_fun(fun: &Function, parent: Option<usize>, flat: &mut Vec<FlatFun>) -
}
struct FnCompiler<'a> {
path: Option<&'a Path>,
flat: &'a [FlatFun],
fn_id: usize,
chunk: FnChunk,
locals: HashMap<String, u8>,
stmt_span: Span,
}
fn compile_function(fn_id: usize, flat: &[FlatFun]) -> Result<FnChunk> {
fn compile_function(fn_id: usize, flat: &[FlatFun], path: Option<&Path>) -> 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 {
path,
flat,
fn_id,
chunk: FnChunk {
name: fun.name.clone(),
params: fun.params.clone(),
code: Vec::new(),
spans: Vec::new(),
constants: Vec::new(),
locals: fun.params.clone(),
},
locals,
stmt_span: fun.span,
};
for stmt in &fun.body {
c.compile_stmt(stmt)?;
}
c.stmt_span = fun.span;
c.emit(Op::None_);
c.emit(Op::Return);
let mut names = vec![String::new(); c.locals.len()];
@@ -95,12 +106,18 @@ fn compile_function(fn_id: usize, flat: &[FlatFun]) -> Result<FnChunk> {
names[*slot as usize] = name.clone();
}
c.chunk.locals = names;
debug_assert_eq!(c.chunk.code.len(), c.chunk.spans.len());
Ok(c.chunk)
}
impl<'a> FnCompiler<'a> {
fn err(&self, message: impl Into<String>) -> anyhow::Error {
bail_at(self.path, self.stmt_span, message)
}
fn emit(&mut self, op: Op) {
self.chunk.code.push(op);
self.chunk.spans.push(self.stmt_span);
}
fn here(&self) -> u16 {
@@ -150,23 +167,29 @@ impl<'a> FnCompiler<'a> {
return Ok(i);
}
}
bail!("bytecode: unknown function `{name}`")
Err(self.err(format!("unknown function `{name}`")))
}
fn compile_stmt(&mut self, stmt: &Stmt) -> Result<()> {
match stmt {
Stmt::Assign { name, value, .. } => {
Stmt::Assign { name, value, span } => {
self.stmt_span = *span;
self.compile_expr(value)?;
let slot = self.local_slot(name);
self.emit(Op::SetLocal(slot));
self.emit(Op::Pop);
}
Stmt::Return { value, .. } => {
Stmt::Return { value, span } => {
self.stmt_span = *span;
self.compile_expr(value)?;
self.emit(Op::Return);
}
Stmt::Call { call, .. } => self.compile_call(call, true)?,
Stmt::Branch { arms, .. } => {
Stmt::Call { call, span } => {
self.stmt_span = *span;
self.compile_call(call, true)?;
}
Stmt::Branch { arms, span } => {
self.stmt_span = *span;
let mut end_jumps = Vec::new();
for (i, arm) in arms.iter().enumerate() {
let is_last = i + 1 == arms.len();
@@ -180,6 +203,7 @@ impl<'a> FnCompiler<'a> {
for st in &arm.body {
self.compile_stmt(st)?;
}
self.stmt_span = *span;
if !is_last {
let j = self.here();
self.emit(Op::Jump(0));
@@ -195,8 +219,11 @@ impl<'a> FnCompiler<'a> {
}
}
Stmt::While {
condition, body, ..
condition,
body,
span,
} => {
self.stmt_span = *span;
let loop_start = self.here();
self.compile_expr(condition)?;
let exit_j = self.here();
@@ -204,6 +231,7 @@ impl<'a> FnCompiler<'a> {
for st in body {
self.compile_stmt(st)?;
}
self.stmt_span = *span;
self.emit(Op::Jump(loop_start));
self.patch(exit_j, Op::JumpIfFalse(self.here()));
}
@@ -211,8 +239,9 @@ impl<'a> FnCompiler<'a> {
item,
collection,
body,
..
span,
} => {
self.stmt_span = *span;
self.compile_expr(&Expr::Var(collection.clone()))?;
let coll_slot = self.local_slot(&format!("__coll_{collection}"));
self.emit(Op::SetLocal(coll_slot));
@@ -242,6 +271,7 @@ impl<'a> FnCompiler<'a> {
self.compile_stmt(st)?;
}
self.stmt_span = *span;
self.emit(Op::GetLocal(i_slot));
let one = self.add_const(Value::Int(1));
self.emit(Op::Constant(one));
@@ -271,7 +301,7 @@ impl<'a> FnCompiler<'a> {
_ => None,
})
})
.ok_or_else(|| anyhow::anyhow!("bytecode: print needs text"))?;
.ok_or_else(|| self.err("print requires text (named or positional)"))?;
self.compile_expr(text_expr)?;
self.emit(Op::Print);
if !as_stmt {
@@ -279,7 +309,60 @@ impl<'a> FnCompiler<'a> {
}
return Ok(());
}
"input" => bail!("bytecode: input not supported"),
"input" => {
let prompt_expr = call
.args
.iter()
.find_map(|a| match a {
Arg::Named { name, value } if name == "prompt" => Some(value),
_ => None,
})
.or_else(|| {
call.args.iter().find_map(|a| match a {
Arg::Positional(e) => Some(e),
_ => None,
})
});
if let Some(e) = prompt_expr {
self.compile_expr(e)?;
} else {
let i = self.add_const(Value::Text(String::new()));
self.emit(Op::Constant(i));
}
self.emit(Op::Input);
if as_stmt {
self.emit(Op::Pop);
}
return Ok(());
}
"len" | "str" | "int" => {
let name = call.callee.as_str();
let value_expr = call
.args
.iter()
.find_map(|a| match a {
Arg::Named { name, value } if name == "value" => Some(value),
_ => None,
})
.or_else(|| {
call.args.iter().find_map(|a| match a {
Arg::Positional(e) => Some(e),
_ => None,
})
})
.ok_or_else(|| self.err(format!("{name} requires value")))?;
self.compile_expr(value_expr)?;
self.emit(match name {
"len" => Op::Len,
"str" => Op::Str,
"int" => Op::Int,
_ => unreachable!(),
});
if as_stmt {
self.emit(Op::Pop);
}
return Ok(());
}
_ => {}
}
@@ -303,11 +386,14 @@ impl<'a> FnCompiler<'a> {
self.compile_expr(positionals[pos_i])?;
pos_i += 1;
} else {
bail!("bytecode: missing argument for parameter `{p}`");
return Err(self.err(format!("missing argument for parameter `{p}`")));
}
}
if pos_i < positionals.len() {
bail!("bytecode: too many positional arguments");
return Err(self.err(format!(
"too many positional arguments ({} extra)",
positionals.len() - pos_i
)));
}
self.emit(Op::Call(fid as u16, params.len() as u8));
if as_stmt {
@@ -336,7 +422,7 @@ impl<'a> FnCompiler<'a> {
.locals
.get(name)
.copied()
.ok_or_else(|| anyhow::anyhow!("bytecode: undefined variable `{name}`"))?;
.ok_or_else(|| self.err(format!("undefined variable `{name}`")))?;
self.emit(Op::GetLocal(slot));
}
Expr::Interp(parts) => {
@@ -352,9 +438,11 @@ impl<'a> FnCompiler<'a> {
self.emit(Op::Constant(i));
}
InterpPart::Var(n) => {
let slot = self.locals.get(n).copied().ok_or_else(|| {
anyhow::anyhow!("bytecode: undefined variable `{n}`")
})?;
let slot = self
.locals
.get(n)
.copied()
.ok_or_else(|| self.err(format!("undefined variable `{n}`")))?;
self.emit(Op::GetLocal(slot));
}
}
@@ -375,7 +463,6 @@ impl<'a> FnCompiler<'a> {
left,
right,
} => {
// JumpIfFalse always pops the tested value.
self.compile_expr(left)?;
let j_false = self.here();
self.emit(Op::JumpIfFalse(0));
+12 -1
View File
@@ -6,6 +6,7 @@ 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)]
@@ -33,8 +34,15 @@ pub enum Op {
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),
@@ -46,6 +54,8 @@ 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>,
@@ -71,7 +81,8 @@ impl Program {
out.push_str(&format!(" const[{i}] = {c:?}\n"));
}
for (ip, op) in fun.code.iter().enumerate() {
out.push_str(&format!(" {ip:04} {op:?}\n"));
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");
+113 -51
View File
@@ -1,13 +1,20 @@
//! Stack VM for Marqdo bytecode.
use anyhow::{bail, Result};
use anyhow::Result;
use std::io::Write;
use std::path::{Path, PathBuf};
use crate::builtin::{builtin_int, builtin_len, builtin_str};
use crate::bytecode::{Op, Program};
use crate::diagnostics::{bail_at, Span};
use crate::input_feed::InputFeed;
use crate::value::Value;
pub struct Vm {
path: Option<PathBuf>,
capture: bool,
pub captured_stdout: String,
input: InputFeed,
}
struct Frame {
@@ -17,20 +24,29 @@ struct Frame {
}
impl Vm {
pub fn new() -> Self {
pub fn new(path: Option<&Path>) -> Self {
Self {
path: path.map(|p| p.to_path_buf()),
capture: false,
captured_stdout: String::new(),
input: InputFeed::new(false, Vec::new()),
}
}
pub fn with_capture() -> Self {
pub fn with_capture(path: Option<&Path>) -> Self {
Self {
path: path.map(|p| p.to_path_buf()),
capture: true,
captured_stdout: String::new(),
input: InputFeed::new(true, Vec::new()),
}
}
pub fn with_stdin(mut self, lines: Vec<String>) -> Self {
self.input = InputFeed::new(self.capture, lines);
self
}
fn emit_line(&mut self, text: &str) {
if self.capture {
self.captured_stdout.push_str(text);
@@ -40,12 +56,33 @@ impl Vm {
}
}
fn emit_prompt(&mut self, prompt: &str) {
if prompt.is_empty() {
return;
}
if self.capture {
self.captured_stdout.push_str(prompt);
} else {
print!("{prompt}");
let _ = std::io::stdout().flush();
}
}
fn err_at(&self, span: Span, message: impl Into<String>) -> anyhow::Error {
bail_at(self.path.as_deref(), span, message)
}
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 span = program.functions[entry]
.spans
.first()
.copied()
.unwrap_or(Span::new(1, 1));
return Err(self.err_at(span, "bytecode: entry function needs arguments"));
}
let mut frames = vec![Frame {
fn_idx: entry,
@@ -57,9 +94,12 @@ impl Vm {
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 span = fun.spans.last().copied().unwrap_or(Span::new(1, 1));
return Err(self.err_at(span, format!("bytecode: ip out of range in {}", fun.name)));
}
let op = fun.code[frame.ip];
let ip = frame.ip;
let op = fun.code[ip];
let span = fun.spans.get(ip).copied().unwrap_or(Span::new(1, 1));
frame.ip += 1;
match op {
@@ -84,78 +124,105 @@ impl Vm {
fr.slots[slot] = v;
}
Op::Add => {
let b = pop(&mut stack)?;
let a = pop(&mut stack)?;
stack.push(add(a, b)?);
let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
stack.push(add(a, b).map_err(|m| self.err_at(span, m))?);
}
Op::Sub => bin_int(&mut stack, |a, b| a - b)?,
Op::Mul => bin_int(&mut stack, |a, b| a * b)?,
Op::Sub => bin_int(&mut stack, |a, b| a - b).map_err(|m| self.err_at(span, m))?,
Op::Mul => bin_int(&mut stack, |a, b| a * b).map_err(|m| self.err_at(span, m))?,
Op::Div => {
let b = pop(&mut stack)?;
let a = pop(&mut stack)?;
let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
match (a, b) {
(Value::Int(_), Value::Int(0)) => bail!("division by zero"),
(Value::Int(_), Value::Int(0)) => {
return Err(self.err_at(span, "division by zero"));
}
(Value::Int(x), Value::Int(y)) => stack.push(Value::Int(x / y)),
_ => bail!("`/` needs ints"),
_ => return Err(self.err_at(span, "`/` needs ints")),
}
}
Op::Negate => match pop(&mut stack)? {
Op::Negate => match pop(&mut stack).map_err(|m| self.err_at(span, m))? {
Value::Int(n) => stack.push(Value::Int(-n)),
_ => bail!("unary `-` needs int"),
_ => return Err(self.err_at(span, "unary `-` needs int")),
},
Op::Not => {
let v = pop(&mut stack)?;
let v = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
stack.push(Value::Bool(!v.truthy()));
}
Op::Equal => {
let b = pop(&mut stack)?;
let a = pop(&mut stack)?;
let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
stack.push(Value::Bool(a == b));
}
Op::NotEqual => {
let b = pop(&mut stack)?;
let a = pop(&mut stack)?;
let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
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::Greater => cmp(&mut stack, |o| o.is_gt()).map_err(|m| self.err_at(span, m))?,
Op::GreaterEqual => {
cmp(&mut stack, |o| o.is_ge()).map_err(|m| self.err_at(span, m))?
}
Op::Less => cmp(&mut stack, |o| o.is_lt()).map_err(|m| self.err_at(span, m))?,
Op::Lessequal => cmp(&mut stack, |o| o.is_le()).map_err(|m| self.err_at(span, m))?,
Op::Jump(t) => {
frames.last_mut().unwrap().ip = t as usize;
}
Op::JumpIfFalse(t) => {
let v = pop(&mut stack)?;
let v = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
if !v.truthy() {
frames.last_mut().unwrap().ip = t as usize;
}
}
Op::Print => {
let v = pop(&mut stack)?;
let v = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
self.emit_line(&v.as_display());
}
Op::Input => {
let prompt = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
let text = prompt.as_display();
self.emit_prompt(&text);
let line = self.input.read_line().map_err(|e| {
if e.to_string().contains("input is not available") {
self.err_at(span, "input is not available under capture / view")
} else {
e
}
})?;
stack.push(Value::Text(line));
}
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.push(pop(&mut stack).map_err(|m| self.err_at(span, m))?);
}
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::Len => {
let v = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
let n = builtin_len(&v).map_err(|m| self.err_at(span, m))?;
stack.push(Value::Int(n));
}
Op::Str => {
let v = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
stack.push(builtin_str(&v));
}
Op::Int => {
let v = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
let n = builtin_int(&v).map_err(|m| self.err_at(span, m))?;
stack.push(Value::Int(n));
}
Op::GetIndex => {
let idx = pop(&mut stack)?;
let list = pop(&mut stack)?;
let idx = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
let list = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
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"),
_ => return Err(self.err_at(span, "GetIndex needs list and int")),
}
}
Op::Call(fid, argc) => {
@@ -163,16 +230,12 @@ impl Vm {
let argc = argc as usize;
let callee = &program.functions[fid];
if callee.params.len() != argc {
bail!("bytecode: call argc mismatch");
return Err(self.err_at(span, "call argument count 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)?;
slots[i] = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
}
// 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,
@@ -192,26 +255,25 @@ impl Vm {
}
}
fn pop(stack: &mut Vec<Value>) -> Result<Value> {
fn pop(stack: &mut Vec<Value>) -> Result<Value, String> {
stack
.pop()
.ok_or_else(|| anyhow::anyhow!("bytecode: stack underflow"))
.ok_or_else(|| "bytecode: stack underflow".to_string())
}
fn add(a: Value, b: Value) -> Result<Value> {
fn add(a: Value, b: Value) -> Result<Value, String> {
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"),
_ => Err("`+` needs ints or text".into()),
}
}
fn bin_int(stack: &mut Vec<Value>, f: impl Fn(i64, i64) -> i64) -> Result<()> {
fn bin_int(stack: &mut Vec<Value>, f: impl Fn(i64, i64) -> i64) -> Result<(), String> {
let b = pop(stack)?;
let a = pop(stack)?;
match (a, b) {
@@ -219,17 +281,17 @@ fn bin_int(stack: &mut Vec<Value>, f: impl Fn(i64, i64) -> i64) -> Result<()> {
stack.push(Value::Int(f(x, y)));
Ok(())
}
_ => bail!("binary op needs ints"),
_ => Err("binary op needs ints".into()),
}
}
fn cmp(stack: &mut Vec<Value>, pred: impl Fn(std::cmp::Ordering) -> bool) -> Result<()> {
fn cmp(stack: &mut Vec<Value>, pred: impl Fn(std::cmp::Ordering) -> bool) -> Result<(), String> {
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"),
_ => return Err("comparison needs two ints or two texts".into()),
};
stack.push(Value::Bool(pred(ord)));
Ok(())