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

+32
View File
@@ -0,0 +1,32 @@
//! Built-in helpers shared by tree-walk and bytecode.
use crate::value::Value;
/// `len`: text → Unicode scalar count; list → element count.
pub fn builtin_len(v: &Value) -> Result<i64, String> {
match v {
Value::Text(s) => Ok(s.chars().count() as i64),
Value::List(xs) => Ok(xs.len() as i64),
_ => Err("len needs text or list".into()),
}
}
/// `str`: display conversion (same surface as `print`).
pub fn builtin_str(v: &Value) -> Value {
Value::Text(v.as_display())
}
/// `int`: Int passthrough; Bool → 0/1; Text → parse trimmed decimal.
pub fn builtin_int(v: &Value) -> Result<i64, String> {
match v {
Value::Int(n) => Ok(*n),
Value::Bool(true) => Ok(1),
Value::Bool(false) => Ok(0),
Value::Text(s) => {
let t = s.trim();
t.parse::<i64>()
.map_err(|_| format!("cannot convert to int: {s:?}"))
}
_ => Err("int needs int, bool, or text".into()),
}
}
+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(())
+85
View File
@@ -0,0 +1,85 @@
//! Preset stdin lines for `input` (CLI `--stdin-file`, view form, tests).
use std::collections::VecDeque;
use std::fs;
use std::io::{self, BufRead};
use std::path::Path;
use anyhow::{Context, Result};
/// Split text into input lines (strips `\n` / `\r\n`; empty → no lines).
pub fn split_stdin_text(text: &str) -> Vec<String> {
if text.is_empty() {
return Vec::new();
}
text.lines()
.map(|l| l.trim_end_matches('\r').to_string())
.collect()
}
pub fn load_stdin_file(path: &Path) -> Result<Vec<String>> {
let text = fs::read_to_string(path)
.with_context(|| format!("failed to read stdin file {}", path.display()))?;
Ok(split_stdin_text(&text))
}
#[derive(Debug, Clone, Default)]
pub struct InputFeed {
queue: VecDeque<String>,
capture: bool,
}
impl InputFeed {
pub fn new(capture: bool, lines: impl Into<Vec<String>>) -> Self {
Self {
queue: lines.into().into(),
capture,
}
}
/// Next line from the preset queue, or real stdin when not capturing.
pub fn read_line(&mut self) -> Result<String> {
if let Some(line) = self.queue.pop_front() {
return Ok(line);
}
if self.capture {
anyhow::bail!("input is not available under capture / view");
}
let mut line = String::new();
io::stdin().lock().read_line(&mut line)?;
if line.ends_with('\n') {
line.pop();
if line.ends_with('\r') {
line.pop();
}
}
Ok(line)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{run_file_capture, RunOptions};
#[test]
fn capture_accepts_preset_stdin() {
let cap = run_file_capture(
Path::new("tests/keywords/input.mq.md"),
&RunOptions {
stdin_lines: vec!["Alice".into()],
..RunOptions::default()
},
)
.expect("capture with stdin");
assert_eq!(cap.stdout.trim_end(), "Name:Hello Alice!");
}
#[test]
fn split_keeps_blank_middle_lines() {
assert_eq!(
split_stdin_text("a\n\nb\n"),
vec!["a".to_string(), String::new(), "b".to_string()]
);
}
}
+56 -15
View File
@@ -7,7 +7,9 @@ use std::path::{Path, PathBuf};
use crate::ast::{
Arg, BinaryOp, CallExpr, Expr, Function, InterpPart, Literal, Module, Stmt, UnaryOp,
};
use crate::builtin::{builtin_int, builtin_len, builtin_str};
use crate::diagnostics::{bail_at, Span};
use crate::input_feed::InputFeed;
use crate::value::Value;
pub struct Interpreter {
@@ -16,6 +18,7 @@ pub struct Interpreter {
capture: bool,
pub captured_stdout: String,
current_span: Span,
input: InputFeed,
}
struct Env {
@@ -51,6 +54,7 @@ impl Interpreter {
capture: false,
captured_stdout: String::new(),
current_span: Span::new(1, 1),
input: InputFeed::new(false, Vec::new()),
}
}
@@ -61,9 +65,15 @@ impl Interpreter {
capture: true,
captured_stdout: String::new(),
current_span: Span::new(1, 1),
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 err(&self, message: impl Into<String>) -> anyhow::Error {
bail_at(self.path.as_deref(), self.current_span, message)
}
@@ -77,6 +87,19 @@ impl Interpreter {
}
}
fn emit_prompt(&mut self, prompt: &str) {
if prompt.is_empty() {
return;
}
if self.capture {
self.captured_stdout.push_str(prompt);
} else {
print!("{prompt}");
use std::io::Write;
let _ = std::io::stdout().flush();
}
}
pub fn run_module(&mut self, module: &Module) -> Result<Value> {
if let Some(main) = find_top(module, "main") {
return self.run_function(module, main, Env::new(), &[]);
@@ -340,30 +363,48 @@ impl Interpreter {
return Ok(Value::None);
}
"input" => {
if self.capture {
return Err(self.err("input is not available under capture / view"));
}
let bound = bind_args(&["prompt".into()], &ev_args, true)
.map_err(|m| self.err(m))?;
let prompt = bound
.get("prompt")
.map(|v| v.as_display())
.unwrap_or_default();
if !prompt.is_empty() {
print!("{prompt}");
use std::io::Write;
let _ = std::io::stdout().flush();
}
let mut line = String::new();
std::io::stdin().read_line(&mut line)?;
if line.ends_with('\n') {
line.pop();
if line.ends_with('\r') {
line.pop();
self.emit_prompt(&prompt);
let line = self.input.read_line().map_err(|e| {
if e.to_string().contains("input is not available") {
self.err("input is not available under capture / view")
} else {
e
}
}
})?;
return Ok(Value::Text(line));
}
"len" => {
let bound = bind_args(&["value".into()], &ev_args, false)
.map_err(|m| self.err(m))?;
let v = bound
.get("value")
.ok_or_else(|| self.err("len requires value"))?;
let n = builtin_len(v).map_err(|m| self.err(m))?;
return Ok(Value::Int(n));
}
"str" => {
let bound = bind_args(&["value".into()], &ev_args, false)
.map_err(|m| self.err(m))?;
let v = bound
.get("value")
.ok_or_else(|| self.err("str requires value"))?;
return Ok(builtin_str(v));
}
"int" => {
let bound = bind_args(&["value".into()], &ev_args, false)
.map_err(|m| self.err(m))?;
let v = bound
.get("value")
.ok_or_else(|| self.err("int requires value"))?;
let n = builtin_int(v).map_err(|m| self.err(m))?;
return Ok(Value::Int(n));
}
_ => {}
}
+14 -7
View File
@@ -4,11 +4,13 @@
//! CLI also offers `view` for AST-backed browsing.
pub mod ast;
pub mod builtin;
pub mod bytecode;
pub mod capture;
pub mod catalog;
pub mod debug;
pub mod diagnostics;
pub mod input_feed;
pub mod interp;
pub mod lex;
pub mod load;
@@ -54,6 +56,8 @@ pub struct RunOptions {
pub dump_bytecode: bool,
pub trace_eval: bool,
pub backend: Backend,
/// Preset lines consumed by `input` (from `--stdin-file` or view).
pub stdin_lines: Vec<String>,
}
impl RunOptions {
@@ -66,6 +70,7 @@ impl RunOptions {
dump_bytecode: true,
trace_eval: true,
backend: Backend::Tree,
stdin_lines: Vec::new(),
}
}
@@ -121,7 +126,8 @@ pub fn run_file(path: &Path, opts: &RunOptions) -> Result<i32> {
match opts.backend {
Backend::Tree => {
let mut interp = Interpreter::new(Some(path), opts.trace_eval);
let mut interp = Interpreter::new(Some(path), opts.trace_eval)
.with_stdin(opts.stdin_lines.clone());
if opts.trace_eval {
eprintln!("=== marqdo: trace-eval ({path_label}) ===");
}
@@ -131,18 +137,18 @@ pub fn run_file(path: &Path, opts: &RunOptions) -> Result<i32> {
}
}
Backend::Bytecode => {
let program = compile_module(&module)?;
let program = compile_module(Some(path), &module)?;
if opts.dump_bytecode {
print!("{}", program.disassemble());
}
let mut vm = Vm::new();
let mut vm = Vm::new(Some(path)).with_stdin(opts.stdin_lines.clone());
vm.run(&program)?;
}
}
// dump bytecode even on tree backend if requested
if opts.backend == Backend::Tree && opts.dump_bytecode {
let program = compile_module(&module)?;
let program = compile_module(Some(path), &module)?;
print!("{}", program.disassemble());
}
@@ -154,7 +160,8 @@ pub fn run_file_capture(path: &Path, opts: &RunOptions) -> Result<RunCapture> {
let module = load_module(path)?;
match opts.backend {
Backend::Tree => {
let mut interp = Interpreter::with_capture(Some(path), false);
let mut interp = Interpreter::with_capture(Some(path), false)
.with_stdin(opts.stdin_lines.clone());
let value = interp.run_module(&module)?;
Ok(RunCapture {
stdout: interp.captured_stdout,
@@ -162,8 +169,8 @@ pub fn run_file_capture(path: &Path, opts: &RunOptions) -> Result<RunCapture> {
})
}
Backend::Bytecode => {
let program = compile_module(&module)?;
let mut vm = Vm::with_capture();
let program = compile_module(Some(path), &module)?;
let mut vm = Vm::with_capture(Some(path)).with_stdin(opts.stdin_lines.clone());
let value = vm.run(&program)?;
Ok(RunCapture {
stdout: vm.captured_stdout,
+15 -1
View File
@@ -5,6 +5,7 @@ use anyhow::Result;
use clap::{Parser, Subcommand, ValueEnum};
use marqdo::catalog::{write_catalog, CatalogOptions};
use marqdo::input_feed::load_stdin_file;
use marqdo::view::{serve, write_static, OutputOptions, ViewOptions};
use marqdo::{Backend, RunOptions};
@@ -54,6 +55,10 @@ enum Commands {
trace_eval: bool,
#[arg(long)]
dump_all: bool,
/// Feed `input` from a text file (one line per call), instead of / after the terminal.
#[arg(long, value_name = "FILE")]
stdin_file: Option<PathBuf>,
},
/// Browse `.mq.md` structure + execution (live server or static output)
View {
@@ -129,10 +134,18 @@ fn try_main() -> Result<i32> {
dump_bytecode,
trace_eval,
dump_all,
stdin_file,
} => {
let path = file.unwrap_or_else(|| PathBuf::from("index.mq.md"));
let stdin_lines = match stdin_file {
Some(p) => load_stdin_file(&p)?,
None => Vec::new(),
};
let mut opts = if dump_all {
RunOptions::dump_all()
RunOptions {
stdin_lines: stdin_lines.clone(),
..RunOptions::dump_all()
}
} else {
RunOptions {
dump_lines,
@@ -142,6 +155,7 @@ fn try_main() -> Result<i32> {
dump_bytecode,
trace_eval,
backend: backend.into(),
stdin_lines,
}
};
if dump_all {
+54
View File
@@ -223,6 +223,45 @@ section.block > h2 {
font-weight: 600;
margin: 0 0 0.85rem;
}
.stdin-form {
margin: 0 0 1rem;
display: grid;
gap: 0.45rem;
max-width: 36rem;
}
.stdin-form label {
font-size: 0.85rem;
color: var(--text);
}
.stdin-form .hint {
color: var(--muted);
font-size: 0.8rem;
margin-left: 0.35rem;
}
.stdin-form textarea {
font: inherit;
font-family: var(--mono);
font-size: 0.9rem;
padding: 0.65rem 0.75rem;
border: 1px solid var(--border);
border-radius: 8px;
background: var(--surface);
color: var(--text);
resize: vertical;
min-height: 4.5rem;
}
.stdin-form button {
justify-self: start;
font: inherit;
font-size: 0.9rem;
padding: 0.45rem 0.9rem;
border: 1px solid var(--border);
border-radius: 8px;
background: var(--text);
color: var(--bg);
cursor: pointer;
}
.stdin-form button:hover { opacity: 0.88; }
.structure {
background: var(--surface);
border: 1px solid var(--line);
@@ -581,6 +620,19 @@ pub fn page_file(files: &[PathBuf], rel: &str, vm: &FileViewModel, links: &LinkM
} else {
escape(&vm.stderr)
};
let stdin_panel = match links {
LinkMode::Live => format!(
r#"<form class="stdin-form" method="get" action="/file">
<input type="hidden" name="path" value="{path}"/>
<label for="stdin">Preset input <span class="hint">one line per input call</span></label>
<textarea id="stdin" name="stdin" rows="3" placeholder="Alice">{stdin}</textarea>
<button type="submit">Run with input</button>
</form>"#,
path = escape(rel),
stdin = escape(&vm.preset_stdin),
),
LinkMode::Static { .. } => String::new(),
};
let title = Path::new(rel)
.file_name()
.and_then(|s| s.to_str())
@@ -596,6 +648,7 @@ pub fn page_file(files: &[PathBuf], rel: &str, vm: &FileViewModel, links: &LinkM
</section>
<section class="block">
<h2>Execution</h2>
{stdin_panel}
<div class="out {status}">{out}</div>
</section>
<section class="block">
@@ -608,6 +661,7 @@ pub fn page_file(files: &[PathBuf], rel: &str, vm: &FileViewModel, links: &LinkM
status_label = status_label,
rel = escape(rel),
structure = vm.structure_html,
stdin_panel = stdin_panel,
out = out_text,
source = escape(&vm.source),
);
+16 -4
View File
@@ -14,6 +14,7 @@ use std::process::Command;
use anyhow::{bail, Context, Result};
use tiny_http::{Header, Method, Request, Response, Server, StatusCode};
use crate::input_feed::split_stdin_text;
use crate::view::html::{escape, page_file, page_index, LinkMode};
use crate::view::render::{render_module_structure, FileViewModel};
use crate::{run_file_capture, RunOptions};
@@ -129,7 +130,7 @@ fn handle(root: &ViewRoot, request: Request) -> Result<()> {
if let Some(first) = root.files.first() {
let rel = first.to_string_lossy().replace('\\', "/");
let resolved = resolve_rel(root, &rel)?;
let vm = build_file_view(&resolved, &rel)?;
let vm = build_file_view(&resolved, &rel, &[])?;
let body = page_file(&root.files, &rel, &vm, &LinkMode::Live);
respond_html(request, body)
} else {
@@ -139,8 +140,10 @@ fn handle(root: &ViewRoot, request: Request) -> Result<()> {
}
"/file" => {
let rel = query_param(query, "path").unwrap_or_default();
let stdin_raw = query_param(query, "stdin").unwrap_or_default();
let stdin_lines = split_stdin_text(&stdin_raw);
let resolved = resolve_rel(root, &rel)?;
let vm = build_file_view(&resolved, &rel)?;
let vm = build_file_view(&resolved, &rel, &stdin_lines)?;
let body = page_file(&root.files, &rel, &vm, &LinkMode::Live);
respond_html(request, body)
}
@@ -156,7 +159,11 @@ fn handle(root: &ViewRoot, request: Request) -> Result<()> {
}
}
pub(crate) fn build_file_view(abs: &Path, rel: &str) -> Result<FileViewModel> {
pub(crate) fn build_file_view(
abs: &Path,
rel: &str,
stdin_lines: &[String],
) -> Result<FileViewModel> {
let source = fs::read_to_string(abs).with_context(|| format!("read {}", abs.display()))?;
let structure = match crate::load::load_module(abs) {
Ok(module) => render_module_structure(&module, &source),
@@ -165,7 +172,11 @@ pub(crate) fn build_file_view(abs: &Path, rel: &str) -> Result<FileViewModel> {
escape(&tidy_user_error(&format!("{e:#}"), abs, rel))
),
};
let (stdout, stderr, ok) = match run_file_capture(abs, &RunOptions::default()) {
let opts = RunOptions {
stdin_lines: stdin_lines.to_vec(),
..RunOptions::default()
};
let (stdout, stderr, ok) = match run_file_capture(abs, &opts) {
Ok(cap) => (cap.stdout, String::new(), true),
Err(e) => (
String::new(),
@@ -180,6 +191,7 @@ pub(crate) fn build_file_view(abs: &Path, rel: &str) -> Result<FileViewModel> {
stdout,
stderr,
ok,
preset_stdin: stdin_lines.join("\n"),
})
}
+1 -1
View File
@@ -25,7 +25,7 @@ pub fn write_static(opts: OutputOptions) -> Result<()> {
for rel_pb in &root.files {
let rel = rel_pb.to_string_lossy().replace('\\', "/");
let abs = resolve_abs(&root, &rel)?;
let mut vm = build_file_view(&abs, &rel)?;
let mut vm = build_file_view(&abs, &rel, &[])?;
if opts.no_exec {
vm.stdout.clear();
vm.stderr = "execution skipped (--no-exec)".into();
+2
View File
@@ -18,6 +18,8 @@ pub struct FileViewModel {
pub stdout: String,
pub stderr: String,
pub ok: bool,
/// Text shown in the live-view preset stdin box.
pub preset_stdin: String,
}
pub fn render_module_structure(module: &Module, source: &str) -> String {