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:
cflmyandCursor committed 2026-08-04 12:30:40 +08:00
1 parent a29bf6e97b
commit ac0a3fbced
48 files changed
+3007 -202

No files matched your search

+7 -1
View File
@@ -60,7 +60,13 @@ pub struct BranchArm {
#[derive(Debug, Clone)]
pub struct CallExpr {
pub callee: String,
pub args: Vec<(String, Expr)>,
pub args: Vec<Arg>,
}
#[derive(Debug, Clone)]
pub enum Arg {
Positional(Expr),
Named { name: String, value: Expr },
}
#[derive(Debug, Clone)]
+453
View File
@@ -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 &params {
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(())
}
}
+80
View File
@@ -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
}
}
+236
View File
@@ -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(())
}
+9
View File
@@ -0,0 +1,9 @@
//! Captured program run (for tests and `view`).
use crate::value::Value;
#[derive(Debug, Clone)]
pub struct RunCapture {
pub stdout: String,
pub value: Value,
}
+45 -2
View File
@@ -1,16 +1,59 @@
//! Diagnostics with file/line/column.
//! Expanded in M1+.
use std::fmt;
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, Copy)]
use thiserror::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Span {
pub line: u32,
pub col: u32,
}
impl Span {
pub fn new(line: u32, col: u32) -> Self {
Self { line, col }
}
}
impl fmt::Display for Span {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", self.line, self.col)
}
}
/// Unified diagnostic: `path:line:col: message` (path omitted if unknown).
#[derive(Debug, Error, Clone)]
#[error("{}", self.format_message())]
pub struct Diagnostic {
pub path: Option<PathBuf>,
pub span: Span,
pub message: String,
}
impl Diagnostic {
pub fn new(path: Option<&Path>, span: Span, message: impl Into<String>) -> Self {
Self {
path: path.map(|p| p.to_path_buf()),
span,
message: message.into(),
}
}
pub fn at(path: &Path, span: Span, message: impl Into<String>) -> Self {
Self::new(Some(path), span, message)
}
pub fn format_message(&self) -> String {
match &self.path {
Some(p) => format!("{}:{}: {}", p.display(), self.span, self.message),
None => format!("{}: {}", self.span, self.message),
}
}
}
/// Convenience for `anyhow` sites that already have a span.
pub fn bail_at(path: Option<&Path>, span: Span, message: impl Into<String>) -> anyhow::Error {
anyhow::Error::new(Diagnostic::new(path, span, message))
}
+149 -54
View File
@@ -1,15 +1,21 @@
//! Tree-walk interpreter (Phase I).
use anyhow::{bail, Result};
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use crate::ast::{
BinaryOp, CallExpr, Expr, Function, InterpPart, Literal, Module, Stmt, UnaryOp,
Arg, BinaryOp, CallExpr, Expr, Function, InterpPart, Literal, Module, Stmt, UnaryOp,
};
use crate::diagnostics::{bail_at, Span};
use crate::value::Value;
pub struct Interpreter {
pub path: Option<PathBuf>,
pub trace: bool,
capture: bool,
pub captured_stdout: String,
current_span: Span,
}
struct Env {
@@ -32,16 +38,49 @@ impl Env {
}
}
enum EvArg {
Positional(Value),
Named(String, Value),
}
impl Interpreter {
pub fn new(trace: bool) -> Self {
Self { trace }
pub fn new(path: Option<&Path>, trace: bool) -> Self {
Self {
path: path.map(|p| p.to_path_buf()),
trace,
capture: false,
captured_stdout: String::new(),
current_span: Span::new(1, 1),
}
}
pub fn with_capture(path: Option<&Path>, trace: bool) -> Self {
Self {
path: path.map(|p| p.to_path_buf()),
trace,
capture: true,
captured_stdout: String::new(),
current_span: Span::new(1, 1),
}
}
fn err(&self, message: impl Into<String>) -> anyhow::Error {
bail_at(self.path.as_deref(), self.current_span, message)
}
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_module(&mut self, module: &Module) -> Result<Value> {
if let Some(main) = find_top(module, "main") {
return self.run_function(module, main, Env::new(), &[]);
}
// No main: run each level-1 function with empty args (hello-style demos).
let tops: Vec<&Function> = module.functions.iter().filter(|f| f.level == 1).collect();
if tops.is_empty() {
bail!("no `# main` and no level-1 function to run");
@@ -68,7 +107,6 @@ impl Interpreter {
for (k, v) in args {
env.set(k.clone(), v.clone());
}
// Bind missing params to None
for p in &fun.params {
if env.get(p).is_none() {
env.set(p.clone(), Value::None);
@@ -88,7 +126,6 @@ impl Interpreter {
Ok(ret)
}
/// Returns Some if function should return.
fn exec_stmt(
&mut self,
module: &Module,
@@ -98,6 +135,7 @@ impl Interpreter {
) -> Result<Option<Value>> {
match stmt {
Stmt::Assign { name, value, span } => {
self.current_span = *span;
let v = self.eval_expr(module, fun, env, value)?;
if self.trace {
eprintln!(" assign {name} = {v:?} @{span}");
@@ -106,6 +144,7 @@ impl Interpreter {
Ok(None)
}
Stmt::Return { value, span } => {
self.current_span = *span;
let v = self.eval_expr(module, fun, env, value)?;
if self.trace {
eprintln!(" return {v:?} @{span}");
@@ -113,6 +152,7 @@ impl Interpreter {
Ok(Some(v))
}
Stmt::Call { call, span } => {
self.current_span = *span;
if self.trace {
eprintln!(" call-stmt {} @{span}", call.callee);
}
@@ -120,6 +160,7 @@ impl Interpreter {
Ok(None)
}
Stmt::Branch { arms, span } => {
self.current_span = *span;
if self.trace {
eprintln!(" branch @{span}");
}
@@ -144,15 +185,15 @@ impl Interpreter {
body,
span,
} => {
self.current_span = *span;
if self.trace {
eprintln!(" while @{span}");
}
// Guard against infinite loops in buggy programs.
let mut guard = 0u32;
while self.eval_expr(module, fun, env, condition)?.truthy() {
guard += 1;
if guard > 1_000_000 {
bail!("{span}: while loop exceeded iteration limit");
return Err(self.err("while loop exceeded iteration limit"));
}
for s in body {
if let Some(v) = self.exec_stmt(module, fun, env, s)? {
@@ -168,16 +209,18 @@ impl Interpreter {
body,
span,
} => {
self.current_span = *span;
if self.trace {
eprintln!(" foreach {item} in {collection} @{span}");
}
let coll = env
.get(collection)
.cloned()
.ok_or_else(|| anyhow::anyhow!("{span}: undefined collection `{collection}`"))?;
let coll = env.get(collection).cloned().ok_or_else(|| {
self.err(format!("undefined collection `{collection}`"))
})?;
let items = match coll {
Value::List(xs) => xs,
other => bail!("{span}: foreach needs a list, got {other:?}"),
other => {
return Err(self.err(format!("foreach needs a list, got {other:?}")));
}
};
for val in items {
env.set(item.clone(), val);
@@ -204,7 +247,7 @@ impl Interpreter {
Expr::Var(name) => env
.get(name)
.cloned()
.ok_or_else(|| anyhow::anyhow!("undefined variable `{name}`")),
.ok_or_else(|| self.err(format!("undefined variable `{name}`"))),
Expr::Interp(parts) => {
let mut s = String::new();
for part in parts {
@@ -213,7 +256,7 @@ impl Interpreter {
InterpPart::Var(n) => {
let v = env
.get(n)
.ok_or_else(|| anyhow::anyhow!("undefined variable `{n}`"))?;
.ok_or_else(|| self.err(format!("undefined variable `{n}`")))?;
s.push_str(&v.as_display());
}
}
@@ -226,13 +269,12 @@ impl Interpreter {
UnaryOp::Not => Ok(Value::Bool(!v.truthy())),
UnaryOp::Neg => match v {
Value::Int(n) => Ok(Value::Int(-n)),
_ => bail!("unary `-` needs int"),
_ => Err(self.err("unary `-` needs int")),
},
}
}
Expr::Binary { op, left, right } => {
let l = self.eval_expr(module, fun, env, left)?;
// Short-circuit and/or
match op {
BinaryOp::And => {
if !l.truthy() {
@@ -251,7 +293,7 @@ impl Interpreter {
_ => {}
}
let r = self.eval_expr(module, fun, env, right)?;
eval_binary(*op, &l, &r)
eval_binary(*op, &l, &r).map_err(|e| self.err(e.to_string()))
}
Expr::Call(call) => self.eval_call(module, fun, env, call),
Expr::List(items) => {
@@ -271,26 +313,41 @@ impl Interpreter {
env: &mut Env,
call: &CallExpr,
) -> Result<Value> {
let mut arg_vals = Vec::new();
for (k, e) in &call.args {
arg_vals.push((k.clone(), self.eval_expr(module, fun, env, e)?));
let mut ev_args = Vec::new();
for arg in &call.args {
match arg {
Arg::Positional(e) => {
ev_args.push(EvArg::Positional(self.eval_expr(module, fun, env, e)?));
}
Arg::Named { name, value } => {
ev_args.push(EvArg::Named(
name.clone(),
self.eval_expr(module, fun, env, value)?,
));
}
}
}
match call.callee.as_str() {
"print" => {
let text = arg_vals
.iter()
.find(|(k, _)| k == "text")
.map(|(_, v)| v.as_display())
.ok_or_else(|| anyhow::anyhow!("print requires text=..."))?;
println!("{text}");
let bound = bind_args(&["text".into()], &ev_args, false)
.map_err(|m| self.err(m))?;
let text = bound
.get("text")
.map(|v| v.as_display())
.ok_or_else(|| self.err("print requires text (named or positional)"))?;
self.emit_line(&text);
return Ok(Value::None);
}
"input" => {
let prompt = arg_vals
.iter()
.find(|(k, _)| k == "prompt")
.map(|(_, v)| v.as_display())
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}");
@@ -310,27 +367,71 @@ impl Interpreter {
_ => {}
}
let target = lookup_function(module, fun, &call.callee).ok_or_else(|| {
anyhow::anyhow!("unknown function `{}`", call.callee)
})?;
let target = lookup_function(module, fun, &call.callee)
.ok_or_else(|| self.err(format!("unknown function `{}`", call.callee)))?;
// Bind by param names: prefer named args matching params; also allow arg keys as env names.
let bound = bind_args(&target.params, &ev_args, false).map_err(|m| self.err(m))?;
let mut call_env = Env::new();
for p in &target.params {
if let Some((_, v)) = arg_vals.iter().find(|(k, _)| k == p) {
call_env.set(p.clone(), v.clone());
}
}
for (k, v) in &arg_vals {
if call_env.get(k).is_none() {
call_env.set(k.clone(), v.clone());
}
for (k, v) in bound {
call_env.set(k, v);
}
self.run_function(module, target, call_env, &[])
}
}
/// Bind evaluated args onto parameter names. Err is a plain message (caller adds span/path).
fn bind_args(
params: &[String],
args: &[EvArg],
allow_missing: bool,
) -> std::result::Result<HashMap<String, Value>, String> {
let mut named: HashMap<String, Value> = HashMap::new();
let mut positionals = Vec::new();
for a in args {
match a {
EvArg::Positional(v) => positionals.push(v.clone()),
EvArg::Named(k, v) => {
if named.contains_key(k) {
return Err(format!("duplicate named argument `{k}`"));
}
named.insert(k.clone(), v.clone());
}
}
}
let mut out: HashMap<String, Value> = HashMap::new();
let mut used_named = HashSet::new();
let mut pos_i = 0usize;
for p in params {
if let Some(v) = named.get(p) {
out.insert(p.clone(), v.clone());
used_named.insert(p.clone());
} else if pos_i < positionals.len() {
out.insert(p.clone(), positionals[pos_i].clone());
pos_i += 1;
} else if !allow_missing {
return Err(format!("missing argument for parameter `{p}`"));
}
}
if pos_i < positionals.len() {
return Err(format!(
"too many positional arguments ({} extra)",
positionals.len() - pos_i
));
}
for (k, v) in named {
if !used_named.contains(&k) && !out.contains_key(&k) {
out.insert(k, v);
}
}
Ok(out)
}
fn find_top<'a>(module: &'a Module, name: &str) -> Option<&'a Function> {
module.functions.iter().find(|f| f.name == name)
}
@@ -340,11 +441,9 @@ fn lookup_function<'a>(
current: &'a Function,
name: &str,
) -> Option<&'a Function> {
// Nested children of current (hoisted).
if let Some(f) = find_in_tree(current, name) {
return Some(f);
}
// Top-level / imported.
module.functions.iter().find(|f| f.name == name)
}
@@ -376,7 +475,7 @@ fn eval_binary(op: BinaryOp, l: &Value, r: &Value) -> Result<Value> {
(Value::Text(a), Value::Text(b)) => Ok(Value::Text(format!("{a}{b}"))),
(Value::Text(a), b) => Ok(Value::Text(format!("{a}{}", b.as_display()))),
(a, Value::Text(b)) => Ok(Value::Text(format!("{}{b}", a.as_display()))),
_ => bail!("`+` needs ints or text, got {l:?} and {r:?}"),
_ => bail!("`+` needs ints or text"),
},
BinaryOp::Sub => match (l, r) {
(Value::Int(a), Value::Int(b)) => Ok(Value::Int(a - b)),
@@ -391,8 +490,8 @@ fn eval_binary(op: BinaryOp, l: &Value, r: &Value) -> Result<Value> {
(Value::Int(a), Value::Int(b)) => Ok(Value::Int(a / b)),
_ => bail!("`/` needs ints"),
},
BinaryOp::Eq => Ok(Value::Bool(values_eq(l, r))),
BinaryOp::Ne => Ok(Value::Bool(!values_eq(l, r))),
BinaryOp::Eq => Ok(Value::Bool(l == r)),
BinaryOp::Ne => Ok(Value::Bool(l != r)),
BinaryOp::Lt => cmp_ord(l, r, |o| o.is_lt()),
BinaryOp::Le => cmp_ord(l, r, |o| o.is_le()),
BinaryOp::Gt => cmp_ord(l, r, |o| o.is_gt()),
@@ -401,10 +500,6 @@ fn eval_binary(op: BinaryOp, l: &Value, r: &Value) -> Result<Value> {
}
}
fn values_eq(l: &Value, r: &Value) -> bool {
l == r
}
fn cmp_ord(l: &Value, r: &Value, f: impl Fn(std::cmp::Ordering) -> bool) -> Result<Value> {
match (l, r) {
(Value::Int(a), Value::Int(b)) => Ok(Value::Bool(f(a.cmp(b)))),
+1 -1
View File
@@ -122,7 +122,7 @@ mod tests {
#[test]
fn classify_hello_fixture() {
let src = include_str!("../../examples/hello.mq.md");
let src = include_str!("../../examples/structure/hello.mq.md");
let lines = classify_source(src);
assert!(lines.iter().any(|l| l.kind == LineKind::Code && l.text.contains("print")));
assert!(lines.iter().any(|l| l.kind == LineKind::Comment));
+85 -10
View File
@@ -1,8 +1,11 @@
//! Marqdo reference interpreter (Phase I).
//! Marqdo reference interpreter (Phase I + M5 bytecode prototype).
//!
//! Pipeline: load → line classify → parse → tree-walk eval.
//! Pipeline: load → line classify → parse → tree-walk eval | bytecode VM.
//! CLI also offers `view` for AST-backed browsing.
pub mod ast;
pub mod bytecode;
pub mod capture;
pub mod debug;
pub mod diagnostics;
pub mod interp;
@@ -10,16 +13,36 @@ pub mod lex;
pub mod load;
pub mod parse;
pub mod value;
pub mod view;
use std::path::Path;
use anyhow::{bail, Result};
use crate::ast::format_ast_dump;
use crate::bytecode::{compile_module, Vm};
use crate::capture::RunCapture;
use crate::interp::Interpreter;
use crate::lex::{classify_source, format_lines_dump};
use crate::load::load_module;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Backend {
#[default]
Tree,
Bytecode,
}
impl Backend {
pub fn parse(s: &str) -> Result<Self> {
match s {
"tree" => Ok(Self::Tree),
"bytecode" | "bc" => Ok(Self::Bytecode),
other => bail!("unknown backend `{other}` (expected tree|bytecode)"),
}
}
}
/// Options for a single `run` / dump invocation.
#[derive(Debug, Clone, Default)]
pub struct RunOptions {
@@ -27,7 +50,9 @@ pub struct RunOptions {
pub dump_tokens: bool,
pub dump_ast: bool,
pub dump_sema: bool,
pub dump_bytecode: bool,
pub trace_eval: bool,
pub backend: Backend,
}
impl RunOptions {
@@ -37,16 +62,23 @@ impl RunOptions {
dump_tokens: true,
dump_ast: true,
dump_sema: true,
dump_bytecode: true,
trace_eval: true,
backend: Backend::Tree,
}
}
pub fn any_dump(&self) -> bool {
self.dump_lines || self.dump_tokens || self.dump_ast || self.dump_sema || self.trace_eval
self.dump_lines
|| self.dump_tokens
|| self.dump_ast
|| self.dump_sema
|| self.dump_bytecode
|| self.trace_eval
}
}
/// Run a `.mq.md` program.
/// Run a `.mq.md` program (prints to real stdout).
pub fn run_file(path: &Path, opts: &RunOptions) -> Result<i32> {
let source = std::fs::read_to_string(path)
.map_err(|e| anyhow::anyhow!("failed to read {}: {e}", path.display()))?;
@@ -86,13 +118,56 @@ pub fn run_file(path: &Path, opts: &RunOptions) -> Result<i32> {
println!("=== marqdo: end sema ===");
}
let mut interp = Interpreter::new(opts.trace_eval);
if opts.trace_eval {
eprintln!("=== marqdo: trace-eval ({path_label}) ===");
match opts.backend {
Backend::Tree => {
let mut interp = Interpreter::new(Some(path), opts.trace_eval);
if opts.trace_eval {
eprintln!("=== marqdo: trace-eval ({path_label}) ===");
}
interp.run_module(&module)?;
if opts.trace_eval {
eprintln!("=== marqdo: end trace-eval ===");
}
}
Backend::Bytecode => {
let program = compile_module(&module)?;
if opts.dump_bytecode {
print!("{}", program.disassemble());
}
let mut vm = Vm::new();
vm.run(&program)?;
}
}
interp.run_module(&module)?;
if opts.trace_eval {
eprintln!("=== marqdo: end trace-eval ===");
// dump bytecode even on tree backend if requested
if opts.backend == Backend::Tree && opts.dump_bytecode {
let program = compile_module(&module)?;
print!("{}", program.disassemble());
}
Ok(0)
}
/// Run and capture stdout (used by `view` and tests).
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 value = interp.run_module(&module)?;
Ok(RunCapture {
stdout: interp.captured_stdout,
value,
})
}
Backend::Bytecode => {
let program = compile_module(&module)?;
let mut vm = Vm::with_capture();
let value = vm.run(&program)?;
Ok(RunCapture {
stdout: vm.captured_stdout,
value,
})
}
}
}
+21 -4
View File
@@ -6,6 +6,7 @@ use std::path::{Path, PathBuf};
use anyhow::{bail, Context, Result};
use crate::ast::{Function, Module};
use crate::diagnostics::Diagnostic;
use crate::parse::parse_source;
/// Load `path` and recursively merge imported modules' top-level functions.
@@ -24,15 +25,13 @@ fn load_module_inner(path: &Path, visited: &mut HashSet<PathBuf>) -> Result<Modu
let source = std::fs::read_to_string(path)
.with_context(|| format!("failed to read {}", path.display()))?;
let mut module = parse_source(&source)
.with_context(|| format!("parse error in {}", path.display()))?;
let mut module = parse_source(&source).map_err(|e| attach_path(path, e))?;
let base = path.parent().unwrap_or_else(|| Path::new("."));
let imports = module.imports.clone();
for rel in imports {
let dep_path = base.join(&rel);
let dep = load_module_inner(&dep_path, visited)?;
// Merge imported top-level functions (public API of the file).
for fun in dep.functions {
merge_top_level(&mut module.functions, fun);
}
@@ -41,9 +40,27 @@ fn load_module_inner(path: &Path, visited: &mut HashSet<PathBuf>) -> Result<Modu
Ok(module)
}
fn attach_path(path: &Path, err: anyhow::Error) -> anyhow::Error {
if let Some(d) = err.downcast_ref::<Diagnostic>() {
if d.path.is_none() {
return Diagnostic::at(path, d.span, d.message.clone()).into();
}
return err;
}
// Legacy `line:col: message` from parse bail!
let msg = err.to_string();
if let Some((loc, rest)) = msg.split_once(": ") {
if let Some((line_s, col_s)) = loc.split_once(':') {
if let (Ok(line), Ok(col)) = (line_s.parse::<u32>(), col_s.parse::<u32>()) {
return Diagnostic::at(path, crate::diagnostics::Span::new(line, col), rest).into();
}
}
}
err.context(format!("in {}", path.display()))
}
fn merge_top_level(into: &mut Vec<Function>, fun: Function) {
if let Some(existing) = into.iter_mut().find(|f| f.name == fun.name) {
// Later definition wins (entry file overrides import).
*existing = fun;
} else {
into.push(fun);
+58 -22
View File
@@ -2,49 +2,72 @@ use std::path::PathBuf;
use std::process::ExitCode;
use anyhow::Result;
use clap::{Parser, Subcommand};
use clap::{Parser, Subcommand, ValueEnum};
use marqdo::RunOptions;
use marqdo::view::{serve, ViewOptions};
use marqdo::{Backend, RunOptions};
#[derive(Parser, Debug)]
#[command(name = "marqdo", version, about = "Marqdo interpreter — run .mq.md programs")]
#[command(name = "marqdo", version, about = "Marqdo interpreter — run and view .mq.md programs")]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Clone, Copy, Debug, ValueEnum)]
enum BackendCli {
Tree,
Bytecode,
}
impl From<BackendCli> for Backend {
fn from(b: BackendCli) -> Self {
match b {
BackendCli::Tree => Backend::Tree,
BackendCli::Bytecode => Backend::Bytecode,
}
}
}
#[derive(Subcommand, Debug)]
enum Commands {
/// Execute a Marqdo source file (defaults to ./index.mq.md)
Run {
/// Path to a `.mq.md` file
#[arg(value_name = "FILE")]
file: Option<PathBuf>,
/// Dump line classification
#[arg(long, value_enum, default_value_t = BackendCli::Tree)]
backend: BackendCli,
#[arg(long)]
dump_lines: bool,
/// Dump tokens (placeholder until lexer fills in)
#[arg(long)]
dump_tokens: bool,
/// Dump AST (placeholder until parser exists)
#[arg(long)]
dump_ast: bool,
/// Dump semantic info (placeholder)
#[arg(long)]
dump_sema: bool,
/// Trace evaluation (placeholder)
#[arg(long)]
dump_bytecode: bool,
#[arg(long)]
trace_eval: bool,
/// Enable all dumps
#[arg(long)]
dump_all: bool,
},
/// Browse `.mq.md` structure + execution in a local webpage
View {
#[arg(value_name = "PATH")]
path: Option<PathBuf>,
#[arg(long, default_value = "127.0.0.1")]
host: String,
#[arg(long, default_value_t = 7429)]
port: u16,
#[arg(long)]
no_open: bool,
},
}
fn main() -> ExitCode {
@@ -62,10 +85,12 @@ fn try_main() -> Result<i32> {
match cli.command {
Commands::Run {
file,
backend,
dump_lines,
dump_tokens,
dump_ast,
dump_sema,
dump_bytecode,
trace_eval,
dump_all,
} => {
@@ -78,19 +103,30 @@ fn try_main() -> Result<i32> {
dump_tokens,
dump_ast,
dump_sema,
dump_bytecode,
trace_eval,
backend: backend.into(),
}
};
// dump_all already set; individual flags merge if not dump_all
if !dump_all {
opts.dump_lines = dump_lines;
opts.dump_tokens = dump_tokens;
opts.dump_ast = dump_ast;
opts.dump_sema = dump_sema;
opts.trace_eval = trace_eval;
if dump_all {
opts.backend = backend.into();
}
marqdo::run_file(&path, &opts)?;
Ok(0)
}
Commands::View {
path,
host,
port,
no_open,
} => {
serve(ViewOptions {
path: path.unwrap_or_else(|| PathBuf::from(".")),
host,
port,
open_browser: !no_open,
})?;
Ok(0)
}
}
}
+93 -25
View File
@@ -15,17 +15,41 @@ pub fn parse_expr(input: &str) -> Result<Expr> {
}
/// Argument / template RHS: prefer a full expression; else interpolated text.
/// Hyphenated prose (`none-falsy`) is not parsed as subtraction.
pub fn parse_value_or_interp(input: &str) -> Result<Expr> {
let s = input.trim();
if s.is_empty() {
return Ok(Expr::Literal(Literal::Text(String::new())));
}
if has_hyphenated_prose(s) {
return Ok(parse_interp(s));
}
match parse_expr(s) {
Ok(e) => Ok(e),
Err(_) => Ok(parse_interp(s)),
}
}
fn has_hyphenated_prose(s: &str) -> bool {
let chars: Vec<char> = s.chars().collect();
for i in 1..chars.len().saturating_sub(1) {
if chars[i] != '-' {
continue;
}
let left = chars[i - 1];
let right = chars[i + 1];
if left.is_whitespace() || right.is_whitespace() || left == '`' || right == '`' {
continue;
}
// `none-falsy` / `Hello-World` — not `` `n` - 1 ``
if left.is_alphanumeric() || right.is_alphanumeric() || !left.is_ascii() || !right.is_ascii()
{
return true;
}
}
false
}
pub fn parse_interp(s: &str) -> Expr {
let mut parts = Vec::new();
let mut rest = s;
@@ -329,18 +353,20 @@ fn is_word_end(s: &str, len: usize) -> bool {
}
}
/// Parse `name key=val key2=val2` from a string; returns call + bytes consumed.
/// Parse `name key=val …` / positional args; returns call + bytes consumed.
pub fn parse_call_tail(s: &str) -> Result<(CallExpr, usize)> {
let s = s.trim_start();
let trimmed_lead = s.len(); // not used — work on s
let _ = trimmed_lead;
let original_len = s.len(); // we'll compute from full — caller passes rest after >
use crate::ast::Arg;
let mut parts = s.split_whitespace();
let callee = parts
.next()
.ok_or_else(|| anyhow::anyhow!("missing callee"))?
.to_string();
let s = s.trim_start();
let original_len = s.len();
let callee = {
let mut parts = s.split_whitespace();
parts
.next()
.ok_or_else(|| anyhow::anyhow!("missing callee"))?
.to_string()
};
let after_callee_offset = {
let idx = s.find(&callee).unwrap();
@@ -348,26 +374,68 @@ pub fn parse_call_tail(s: &str) -> Result<(CallExpr, usize)> {
};
let mut args_str = s[after_callee_offset..].trim_start();
let mut args = Vec::new();
let mut seen_named = false;
// Support one or more `key=value`; value may contain spaces until next ` key=` pattern.
while !args_str.is_empty() {
let eq = args_str
.find('=')
.ok_or_else(|| anyhow::anyhow!("expected key=value in call args, got {args_str:?}"))?;
let key = args_str[..eq].trim().to_string();
if key.is_empty() {
bail!("empty argument name");
if let Some((key, after_eq)) = try_named_arg(args_str) {
seen_named = true;
let val_end = find_next_arg_boundary(after_eq);
let val_raw = after_eq[..val_end].trim_end();
args.push(Arg::Named {
name: key,
value: parse_value_or_interp(val_raw)?,
});
args_str = after_eq[val_end..].trim_start();
} else {
if seen_named {
bail!("positional argument after named argument is not allowed");
}
let (tok, rest) = split_first_token(args_str);
if tok.is_empty() {
break;
}
args.push(Arg::Positional(parse_value_or_interp(tok)?));
args_str = rest.trim_start();
}
let after_eq = &args_str[eq + 1..];
// Value runs until ` word=` where word is next key, or end.
let val_end = find_next_arg_boundary(after_eq);
let val_raw = after_eq[..val_end].trim_end();
args.push((key, parse_value_or_interp(val_raw)?));
args_str = after_eq[val_end..].trim_start();
}
let consumed = original_len; // entire rest consumed for stmt-form calls
Ok((CallExpr { callee, args }, consumed))
Ok((CallExpr { callee, args }, original_len))
}
/// If `s` begins with `ident=`, return (ident, rest_after_eq).
fn try_named_arg(s: &str) -> Option<(String, &str)> {
let s = s.trim_start();
for (i, c) in s.char_indices() {
if c == '=' {
if i == 0 {
return None;
}
let key = &s[..i];
if key.chars().any(|ch| ch.is_whitespace()) {
return None;
}
return Some((key.to_string(), &s[i + 1..]));
}
if c.is_whitespace() {
return None;
}
}
None
}
fn split_first_token(s: &str) -> (&str, &str) {
let s = s.trim_start();
if s.starts_with('`') {
// `` `name` `` as one token
if let Some(end) = s[1..].find('`') {
let end = end + 2; // include closing `
return (&s[..end], &s[end..]);
}
}
match s.find(char::is_whitespace) {
Some(i) => (&s[..i], &s[i..]),
None => (s, ""),
}
}
fn find_next_arg_boundary(after_eq: &str) -> usize {
+5 -5
View File
@@ -9,7 +9,7 @@ use anyhow::{bail, Result};
use crate::ast::{
BranchArm, Expr, Function, Literal, Module, Stmt,
};
use crate::diagnostics::Span;
use crate::diagnostics::{Diagnostic, Span};
use crate::lex::{classify_source, ClassifiedLine, LineKind};
use crate::parse::expr::parse_call_after_gt as call_after_gt;
use crate::parse::expr::parse_expr as parse_expr_str;
@@ -217,7 +217,7 @@ impl<'a> Cursor<'a> {
}
}
bail!("{span}: unrecognized statement: {trimmed}")
Err(Diagnostic::new(None, span, format!("unrecognized statement: {trimmed}")).into())
}
fn parse_backtick_assign(&mut self, trimmed: &str, span: Span) -> Result<Option<Stmt>> {
@@ -525,7 +525,7 @@ mod tests {
#[test]
fn parse_hello() {
let src = include_str!("../../examples/hello.mq.md");
let src = include_str!("../../examples/structure/hello.mq.md");
let m = parse_source(src).unwrap();
assert_eq!(m.functions.len(), 1);
assert_eq!(m.functions[0].name, "Hello World");
@@ -535,7 +535,7 @@ mod tests {
#[test]
fn parse_index() {
let src = include_str!("../../examples/index.mq.md");
let src = include_str!("../../examples/structure/nested-call.mq.md");
let m = parse_source(src).unwrap();
assert!(m.imports.is_empty());
let main = m.functions.iter().find(|f| f.name == "main").unwrap();
@@ -546,7 +546,7 @@ mod tests {
#[test]
fn parse_branch() {
let src = include_str!("../../examples/branch.mq.md");
let src = include_str!("../../examples/structure/branch.mq.md");
let m = parse_source(src).unwrap();
let main = &m.functions[0];
assert!(main.body.iter().any(|s| matches!(s, Stmt::Branch { .. })));
+339
View File
@@ -0,0 +1,339 @@
//! HTML shell for `marqdo view`.
use std::path::Path;
use crate::view::render::FileViewModel;
pub fn escape(s: &str) -> String {
s.chars()
.map(|c| match c {
'&' => "&amp;".into(),
'<' => "&lt;".into(),
'>' => "&gt;".into(),
'"' => "&quot;".into(),
'\'' => "&#39;".into(),
_ => c.to_string(),
})
.collect()
}
pub fn layout(title: &str, nav: &str, main: &str) -> String {
format!(
r##"<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>{title} · marqdo view</title>
<link rel="preconnect" href="https://fonts.googleapis.com"/>
<link href="https://fonts.googleapis.com/css2?family=Fraunces:opsz,wght@9..144,600;9..144,700&family=IBM+Plex+Mono:wght@400;500&family=Sora:wght@400;600&display=swap" rel="stylesheet"/>
<style>
:root {{
--bg0: #0f1c1a;
--bg1: #162824;
--panel: #1c322d;
--ink: #e7f2ee;
--muted: #9bb5ac;
--accent: #3ecf8e;
--accent-dim: #2a9b6a;
--call: #5ec8ff;
--branch: #f0b429;
--loop: #ff7a59;
--ret: #c4a7ff;
--err: #ff6b6b;
--line: rgba(255,255,255,0.08);
}}
* {{ box-sizing: border-box; }}
body {{
margin: 0;
min-height: 100vh;
font-family: "Sora", system-ui, sans-serif;
color: var(--ink);
background:
radial-gradient(1200px 600px at 10% -10%, #1a3d34 0%, transparent 55%),
radial-gradient(900px 500px at 100% 0%, #243528 0%, transparent 50%),
linear-gradient(165deg, var(--bg0), var(--bg1));
}}
a {{ color: var(--accent); text-decoration: none; }}
a:hover {{ text-decoration: underline; }}
.shell {{
display: grid;
grid-template-columns: minmax(220px, 280px) 1fr;
min-height: 100vh;
}}
@media (max-width: 800px) {{
.shell {{ grid-template-columns: 1fr; }}
}}
.nav {{
border-right: 1px solid var(--line);
background: rgba(0,0,0,0.22);
padding: 1.25rem 1rem 2rem;
overflow: auto;
}}
.brand {{
font-family: "Fraunces", Georgia, serif;
font-size: 1.45rem;
font-weight: 700;
letter-spacing: -0.02em;
margin: 0 0 0.25rem;
}}
.brand span {{ color: var(--accent); }}
.tagline {{
color: var(--muted);
font-size: 0.8rem;
margin: 0 0 1.25rem;
}}
.nav h2 {{
font-size: 0.7rem;
text-transform: uppercase;
letter-spacing: 0.12em;
color: var(--muted);
margin: 1rem 0 0.4rem;
}}
.nav ul {{ list-style: none; padding: 0; margin: 0; }}
.nav li {{ margin: 0.15rem 0; }}
.nav a.file {{
display: block;
padding: 0.35rem 0.5rem;
border-radius: 6px;
color: var(--ink);
font-family: "IBM Plex Mono", monospace;
font-size: 0.78rem;
}}
.nav a.file:hover, .nav a.file.active {{
background: rgba(62, 207, 142, 0.12);
text-decoration: none;
}}
.folder {{ color: var(--muted); font-size: 0.75rem; margin-top: 0.75rem; }}
main.panel {{
padding: 1.5rem 1.75rem 3rem;
overflow: auto;
}}
h1.page {{
font-family: "Fraunces", Georgia, serif;
font-size: 1.75rem;
margin: 0 0 0.5rem;
}}
.meta {{ color: var(--muted); font-size: 0.85rem; margin-bottom: 1.5rem; }}
section.block {{
margin: 1.25rem 0 1.75rem;
}}
section.block > h2 {{
font-size: 0.75rem;
text-transform: uppercase;
letter-spacing: 0.1em;
color: var(--muted);
margin: 0 0 0.65rem;
border-bottom: 1px solid var(--line);
padding-bottom: 0.35rem;
}}
.card {{
background: var(--panel);
border: 1px solid var(--line);
border-radius: 10px;
padding: 0.85rem 1rem;
margin: 0.55rem 0;
}}
.fun-card {{ border-left: 3px solid var(--accent); }}
.call-card {{ border-left: 3px solid var(--call); }}
.branch-card {{ border-left: 3px solid var(--branch); }}
.loop-card {{ border-left: 3px solid var(--loop); }}
.ret-card {{ border-left: 3px solid var(--ret); }}
.badge {{
display: inline-block;
font-family: "IBM Plex Mono", monospace;
font-size: 0.7rem;
padding: 0.1rem 0.4rem;
border-radius: 4px;
background: rgba(255,255,255,0.06);
margin-right: 0.35rem;
}}
.params {{ margin: 0.35rem 0; }}
.chip {{
display: inline-block;
font-family: "IBM Plex Mono", monospace;
font-size: 0.75rem;
padding: 0.15rem 0.45rem;
margin: 0.15rem;
border-radius: 999px;
background: rgba(62, 207, 142, 0.15);
color: var(--accent);
}}
.nested {{ margin-left: 0.85rem; padding-left: 0.75rem; border-left: 1px dashed var(--line); }}
.arm {{
margin: 0.4rem 0;
padding: 0.5rem 0.65rem;
background: rgba(0,0,0,0.2);
border-radius: 8px;
}}
.out {{
font-family: "IBM Plex Mono", monospace;
font-size: 0.85rem;
white-space: pre-wrap;
background: #0a1210;
border-radius: 8px;
padding: 0.85rem 1rem;
border: 1px solid var(--line);
}}
.out.ok {{ box-shadow: inset 3px 0 0 var(--accent-dim); }}
.out.fail {{ box-shadow: inset 3px 0 0 var(--err); color: #ffc9c9; }}
.source {{
font-family: "IBM Plex Mono", monospace;
font-size: 0.78rem;
white-space: pre-wrap;
background: #0a1210;
border-radius: 8px;
padding: 0.85rem 1rem;
border: 1px solid var(--line);
line-height: 1.45;
}}
.err {{ color: var(--err); }}
.comment-card {{
border-left: 3px solid var(--muted);
background: rgba(255,255,255,0.03);
}}
.comment-text {{
color: var(--muted);
font-size: 0.9rem;
line-height: 1.45;
}}
code.expr {{
font-family: "IBM Plex Mono", monospace;
font-size: 0.82rem;
color: var(--ink);
}}
.welcome {{ max-width: 36rem; }}
.welcome p {{ color: var(--muted); line-height: 1.55; }}
</style>
</head>
<body>
<div class="shell">
<aside class="nav">
<p class="brand">marq<span>do</span> view</p>
<p class="tagline">Markup structure · live output</p>
{nav}
</aside>
<main class="panel">
{main}
</main>
</div>
</body>
</html>
"##,
title = escape(title),
nav = nav,
main = main
)
}
pub fn nav_html(files: &[std::path::PathBuf], active: Option<&str>) -> String {
let mut out = String::from("<h2>Files</h2><ul>");
let mut last_folder = String::new();
for f in files {
let rel = f.to_string_lossy().replace('\\', "/");
let folder = Path::new(&rel)
.parent()
.map(|p| p.to_string_lossy().to_string())
.unwrap_or_default();
if folder != last_folder {
if !folder.is_empty() {
out.push_str(&format!(
"</ul><div class=\"folder\">{}</div><ul>",
escape(&folder)
));
}
last_folder = folder;
}
let name = Path::new(&rel)
.file_name()
.and_then(|s| s.to_str())
.unwrap_or(&rel);
let active_cls = if active == Some(rel.as_str()) {
" active"
} else {
""
};
let href = format!("/file?path={}", urlencoding_encode(&rel));
out.push_str(&format!(
"<li><a class=\"file{active_cls}\" href=\"{href}\">{}</a></li>",
escape(name)
));
}
out.push_str("</ul>");
out
}
fn urlencoding_encode(s: &str) -> String {
let mut out = String::new();
for b in s.bytes() {
match b {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' | b'/' => {
out.push(b as char);
}
_ => out.push_str(&format!("%{b:02X}")),
}
}
out
}
pub fn page_index(files: &[std::path::PathBuf], only: Option<&Path>) -> String {
let nav = nav_html(files, None);
let main = if let Some(p) = only {
let rel = p.to_string_lossy().replace('\\', "/");
format!(
"<div class=\"welcome\"><h1 class=\"page\">Single file</h1><p class=\"meta\">Open <a href=\"/file?path={}\">{}</a></p></div>",
urlencoding_encode(&rel),
escape(&rel)
)
} else {
format!(
"<div class=\"welcome\"><h1 class=\"page\">Browse</h1><p>Select a <code>.mq.md</code> file in the index. Structure is rendered from the AST; output comes from the same pipeline as <code>marqdo run</code>.</p><p class=\"meta\">{} file(s)</p></div>",
files.len()
)
};
layout("index", &nav, &main)
}
pub fn page_file(files: &[std::path::PathBuf], rel: &str, vm: &FileViewModel) -> String {
let nav = nav_html(files, Some(rel));
let status = if vm.ok { "ok" } else { "fail" };
let out_text = if vm.ok {
if vm.stdout.is_empty() {
"(no stdout)".to_string()
} else {
escape(&vm.stdout)
}
} else {
escape(&vm.stderr)
};
let main = format!(
r#"
<h1 class="page">{title}</h1>
<p class="meta">{rel}</p>
<section class="block">
<h2>Structure</h2>
{structure}
</section>
<section class="block">
<h2>Execution</h2>
<div class="out {status}">{out}</div>
</section>
<section class="block">
<h2>Source</h2>
<pre class="source">{source}</pre>
</section>
"#,
title = escape(
Path::new(rel)
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or(rel)
),
rel = escape(rel),
structure = vm.structure_html,
status = status,
out = out_text,
source = escape(&vm.source),
);
layout(rel, &nav, &main)
}
+307
View File
@@ -0,0 +1,307 @@
//! `marqdo view` — local HTML browser for `.mq.md` structure + output.
mod html;
mod render;
use std::fs;
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::process::Command;
use anyhow::{bail, Context, Result};
use tiny_http::{Header, Method, Request, Response, Server, StatusCode};
use crate::view::html::{escape, page_file, page_index};
use crate::view::render::{render_module_structure, FileViewModel};
use crate::{run_file_capture, RunOptions};
pub struct ViewOptions {
pub path: PathBuf,
pub host: String,
pub port: u16,
pub open_browser: bool,
}
struct ViewRoot {
/// Directory that paths are resolved against.
root: PathBuf,
/// If Some, only this relative file is in scope (single-file mode).
only_file: Option<PathBuf>,
files: Vec<PathBuf>,
}
/// Block serving until Ctrl+C (process kill).
pub fn serve(opts: ViewOptions) -> Result<()> {
let root_info = build_root(&opts.path)?;
let addr: SocketAddr = format!("{}:{}", opts.host, opts.port)
.parse()
.context("invalid host/port")?;
let server = Server::http(addr).map_err(|e| anyhow::anyhow!("listen {addr}: {e}"))?;
let url = format!("http://{}:{}/", opts.host, opts.port);
eprintln!("marqdo view: {url}");
eprintln!("root: {}", root_info.root.display());
eprintln!("{} .mq.md file(s). Ctrl+C to stop.", root_info.files.len());
if opts.open_browser {
let _ = open_url(&url);
}
for request in server.incoming_requests() {
if let Err(err) = handle(&root_info, request) {
eprintln!("view request error: {err:#}");
}
}
Ok(())
}
fn build_root(path: &Path) -> Result<ViewRoot> {
let path = if path.exists() {
path.canonicalize().unwrap_or_else(|_| path.to_path_buf())
} else {
bail!("path not found: {}", path.display());
};
if path.is_file() {
if !is_mq_md(&path) {
bail!("not a .mq.md file: {}", path.display());
}
let root = path
.parent()
.map(|p| p.to_path_buf())
.unwrap_or_else(|| PathBuf::from("."));
let rel = PathBuf::from(path.file_name().unwrap());
return Ok(ViewRoot {
root,
only_file: Some(rel.clone()),
files: vec![rel],
});
}
if !path.is_dir() {
bail!("not a file or directory: {}", path.display());
}
let mut files = Vec::new();
collect_mq_md(&path, &path, &mut files)?;
files.sort();
Ok(ViewRoot {
root: path,
only_file: None,
files,
})
}
fn collect_mq_md(root: &Path, dir: &Path, out: &mut Vec<PathBuf>) -> Result<()> {
for entry in fs::read_dir(dir).with_context(|| format!("read_dir {}", dir.display()))? {
let entry = entry?;
let p = entry.path();
if p.is_dir() {
collect_mq_md(root, &p, out)?;
} else if is_mq_md(&p) {
let rel = p.strip_prefix(root).unwrap_or(&p).to_path_buf();
out.push(rel);
}
}
Ok(())
}
fn is_mq_md(path: &Path) -> bool {
path.file_name()
.and_then(|s| s.to_str())
.map(|s| s.ends_with(".mq.md"))
.unwrap_or(false)
}
fn handle(root: &ViewRoot, request: Request) -> Result<()> {
let url = request.url().to_string();
let method = request.method().clone();
if method != Method::Get {
let resp = Response::from_string("method not allowed").with_status_code(StatusCode(405));
let _ = request.respond(resp);
return Ok(());
}
let (path_part, query) = split_url(&url);
match path_part {
"/" | "/index.html" => {
let body = page_index(&root.files, root.only_file.as_deref());
respond_html(request, body)
}
"/file" => {
let rel = query_param(query, "path").unwrap_or_default();
let resolved = resolve_rel(root, &rel)?;
let vm = build_file_view(&resolved, &rel)?;
let body = page_file(&root.files, &rel, &vm);
respond_html(request, body)
}
"/api/tree" => {
let json = files_json(&root.files);
respond_json(request, json)
}
_ => {
let resp = Response::from_string("not found").with_status_code(StatusCode(404));
let _ = request.respond(resp);
Ok(())
}
}
}
fn build_file_view(abs: &Path, rel: &str) -> 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),
Err(e) => format!(
"<div class=\"err\">parse/load error: {}</div>",
escape(&format!("{e:#}"))
),
};
let (stdout, stderr, ok) = match run_file_capture(abs, &RunOptions::default()) {
Ok(cap) => (cap.stdout, String::new(), true),
Err(e) => (String::new(), format!("{e:#}"), false),
};
Ok(FileViewModel {
rel_path: rel.to_string(),
source,
structure_html: structure,
stdout,
stderr,
ok,
})
}
fn resolve_rel(root: &ViewRoot, rel: &str) -> Result<PathBuf> {
if rel.is_empty() {
bail!("missing path");
}
let rel_path = PathBuf::from(rel.replace('\\', "/"));
if rel_path
.components()
.any(|c| matches!(c, std::path::Component::ParentDir))
{
bail!("path escapes root");
}
if let Some(only) = &root.only_file {
if &rel_path != only {
bail!("file not in view scope");
}
} else if !root.files.iter().any(|f| f == &rel_path) {
// also allow forward-slash normalized compare
let norm = rel_path.to_string_lossy().replace('\\', "/");
if !root
.files
.iter()
.any(|f| f.to_string_lossy().replace('\\', "/") == norm)
{
bail!("unknown file: {rel}");
}
}
let abs = root.root.join(&rel_path);
let canon = abs.canonicalize().unwrap_or(abs.clone());
let root_canon = root.root.canonicalize().unwrap_or_else(|_| root.root.clone());
if !canon.starts_with(&root_canon) {
bail!("path escapes root");
}
if !is_mq_md(&canon) {
bail!("not a .mq.md file");
}
Ok(canon)
}
fn split_url(url: &str) -> (&str, &str) {
match url.split_once('?') {
Some((p, q)) => (p, q),
None => (url, ""),
}
}
fn query_param<'a>(query: &'a str, key: &str) -> Option<String> {
for pair in query.split('&') {
let mut kv = pair.splitn(2, '=');
let k = kv.next()?;
let v = kv.next().unwrap_or("");
if k == key {
return Some(urlencoding_decode(v));
}
}
None
}
fn urlencoding_decode(s: &str) -> String {
// Minimal decode for %XX and +
let mut out = String::new();
let bytes = s.as_bytes();
let mut i = 0;
while i < bytes.len() {
match bytes[i] {
b'+' => {
out.push(' ');
i += 1;
}
b'%' if i + 2 < bytes.len() => {
let hex = &s[i + 1..i + 3];
if let Ok(v) = u8::from_str_radix(hex, 16) {
out.push(v as char);
i += 3;
} else {
out.push('%');
i += 1;
}
}
c => {
out.push(c as char);
i += 1;
}
}
}
out
}
fn files_json(files: &[PathBuf]) -> String {
let list: Vec<String> = files
.iter()
.map(|f| format!("\"{}\"", escape_json(&f.to_string_lossy().replace('\\', "/"))))
.collect();
format!("{{\"files\":[{}]}}", list.join(","))
}
fn escape_json(s: &str) -> String {
s.replace('\\', "\\\\").replace('"', "\\\"")
}
fn respond_html(request: Request, body: String) -> Result<()> {
let header = Header::from_bytes(&b"Content-Type"[..], &b"text/html; charset=utf-8"[..])
.map_err(|_| anyhow::anyhow!("header"))?;
let resp = Response::from_string(body).with_header(header);
request.respond(resp).context("respond")?;
Ok(())
}
fn respond_json(request: Request, body: String) -> Result<()> {
let header = Header::from_bytes(&b"Content-Type"[..], &b"application/json; charset=utf-8"[..])
.map_err(|_| anyhow::anyhow!("header"))?;
let resp = Response::from_string(body).with_header(header);
request.respond(resp).context("respond")?;
Ok(())
}
fn open_url(url: &str) -> Result<()> {
#[cfg(target_os = "windows")]
{
Command::new("cmd")
.args(["/C", "start", "", url])
.spawn()
.context("open browser")?;
}
#[cfg(target_os = "macos")]
{
Command::new("open").arg(url).spawn().context("open browser")?;
}
#[cfg(all(unix, not(target_os = "macos")))]
{
Command::new("xdg-open")
.arg(url)
.spawn()
.context("open browser")?;
}
Ok(())
}
+424
View File
@@ -0,0 +1,424 @@
//! Render Marqdo AST as HTML fragments for `view`.
//!
//! Comments are not in the AST; they are interleaved from classified source lines.
//! Expressions are shown in surface syntax, never Rust `Debug`.
use crate::ast::{
Arg, BinaryOp, CallExpr, Expr, Function, InterpPart, Literal, Module, Stmt, UnaryOp,
};
use crate::lex::{classify_source, ClassifiedLine, LineKind};
use crate::view::html::escape;
pub struct FileViewModel {
#[allow(dead_code)]
pub rel_path: String,
pub source: String,
pub structure_html: String,
pub stdout: String,
pub stderr: String,
pub ok: bool,
}
pub fn render_module_structure(module: &Module, source: &str) -> String {
let lines = classify_source(source);
let mut out = String::new();
if !module.imports.is_empty() {
out.push_str("<div class=\"card\"><span class=\"badge\">import</span>");
for imp in &module.imports {
out.push_str(&format!("<span class=\"chip\">{}</span>", escape(imp)));
}
out.push_str("</div>");
}
let body_start = skip_frontmatter_end(&lines).unwrap_or(1);
let mut cursor = body_start;
if module.functions.is_empty() {
out.push_str(&emit_comments(&lines, cursor, u32::MAX));
if out.is_empty() {
out.push_str("<p class=\"meta\">(empty module)</p>");
}
return out;
}
// Leading comments before first function
let first = module.functions[0].span.line;
out.push_str(&emit_comments(&lines, cursor, first));
cursor = first;
for (i, fun) in module.functions.iter().enumerate() {
if i > 0 {
out.push_str(&emit_comments(&lines, cursor, fun.span.line));
}
let (html, next) = render_fun(fun, &lines, 0);
out.push_str(&html);
cursor = next;
}
// Trailing comments after last function tree
out.push_str(&emit_comments(&lines, cursor, u32::MAX));
out
}
/// Line after closing frontmatter `---`, or 1 if none.
fn skip_frontmatter_end(lines: &[ClassifiedLine]) -> Option<u32> {
let first_code = lines.iter().find(|l| l.kind == LineKind::Code)?;
if first_code.text.trim() != "---" {
return Some(1);
}
for l in lines.iter().skip_while(|l| l.line_no <= first_code.line_no) {
if l.kind == LineKind::Code && l.text.trim() == "---" {
return Some(l.line_no + 1);
}
}
Some(1)
}
fn emit_comments(lines: &[ClassifiedLine], from_line: u32, before_line: u32) -> String {
let mut s = String::new();
for l in lines {
if l.line_no < from_line {
continue;
}
if l.line_no >= before_line {
break;
}
if l.kind == LineKind::Comment {
let text = l.text.trim();
if text.is_empty() {
continue;
}
s.push_str(&format!(
"<div class=\"card comment-card\"><span class=\"badge\">comment</span><span class=\"comment-text\">{}</span></div>",
escape(text)
));
}
}
s
}
fn render_fun(fun: &Function, lines: &[ClassifiedLine], depth: usize) -> (String, u32) {
let mut s = String::new();
let nest = if depth > 0 { " nested" } else { "" };
s.push_str(&format!("<div class=\"card fun-card{nest}\">"));
s.push_str(&format!(
"<div><span class=\"badge\">fn · h{}</span><strong>{}</strong></div>",
fun.level,
escape(&fun.name)
));
if !fun.params.is_empty() {
s.push_str("<div class=\"params\">");
for p in &fun.params {
s.push_str(&format!("<span class=\"chip\">{}</span>", escape(p)));
}
s.push_str("</div>");
}
let mut cursor = fun.span.line + 1;
// Comments / gap before first body stmt or child
let first_body = fun.body.first().map(stmt_start);
let first_child = fun.children.first().map(|c| c.span.line);
let first_content = match (first_body, first_child) {
(Some(a), Some(b)) => Some(a.min(b)),
(Some(a), None) => Some(a),
(None, Some(b)) => Some(b),
(None, None) => None,
};
if let Some(fc) = first_content {
s.push_str(&emit_comments(lines, cursor, fc));
cursor = fc;
}
// Interleave body stmts and nested functions by source line
let mut bi = 0usize;
let mut ci = 0usize;
while bi < fun.body.len() || ci < fun.children.len() {
let stmt_line = fun.body.get(bi).map(stmt_start);
let child_line = fun.children.get(ci).map(|c| c.span.line);
let take_stmt = match (stmt_line, child_line) {
(Some(sl), Some(cl)) => sl <= cl,
(Some(_), None) => true,
(None, Some(_)) => false,
(None, None) => break,
};
if take_stmt {
let stmt = &fun.body[bi];
let start = stmt_start(stmt);
s.push_str(&emit_comments(lines, cursor, start));
let (html, end) = render_stmt(stmt, lines);
s.push_str(&html);
cursor = end;
bi += 1;
} else {
let child = &fun.children[ci];
s.push_str(&emit_comments(lines, cursor, child.span.line));
let (html, end) = render_fun(child, lines, depth + 1);
s.push_str(&format!("<div class=\"nested\">{html}</div>"));
cursor = end;
ci += 1;
}
}
s.push_str("</div>");
(s, cursor)
}
fn stmt_start(stmt: &Stmt) -> u32 {
match stmt {
Stmt::Assign { span, .. }
| Stmt::Return { span, .. }
| Stmt::Call { span, .. }
| Stmt::Branch { span, .. }
| Stmt::While { span, .. }
| Stmt::ForEach { span, .. } => span.line,
}
}
fn stmt_end_line(stmt: &Stmt) -> u32 {
match stmt {
Stmt::Assign { span, .. } | Stmt::Return { span, .. } | Stmt::Call { span, .. } => {
span.line + 1
}
Stmt::Branch { arms, span, .. } => {
let mut end = span.line + 1;
for arm in arms {
for st in &arm.body {
end = end.max(stmt_end_line(st));
}
}
end
}
Stmt::While { body, span, .. } | Stmt::ForEach { body, span, .. } => {
let mut end = span.line + 1;
for st in body {
end = end.max(stmt_end_line(st));
}
end
}
}
}
fn render_stmt(stmt: &Stmt, lines: &[ClassifiedLine]) -> (String, u32) {
let end = stmt_end_line(stmt);
let html = match stmt {
Stmt::Assign { name, value, .. } => format!(
"<div class=\"card\"><span class=\"badge\">bind</span><code class=\"expr\">`{}` = {}</code></div>",
escape(name),
escape(&expr_display(value))
),
Stmt::Return { value, .. } => format!(
"<div class=\"card ret-card\"><span class=\"badge\">return</span><code class=\"expr\">{}</code></div>",
escape(&expr_display(value))
),
Stmt::Call { call, .. } => format!(
"<div class=\"card call-card\"><span class=\"badge\">call</span><code class=\"expr\">{}</code></div>",
escape(&call_display(call))
),
Stmt::Branch { arms, .. } => {
let mut inner =
String::from("<div class=\"card branch-card\"><span class=\"badge\">branch</span>");
let mut arm_cursor = stmt_start(stmt) + 1;
for arm in arms {
let label = match &arm.condition {
None => "else".to_string(),
Some(c) => expr_display(c),
};
// comments before first stmt in arm (approx: between arms hard; use body starts)
let arm_first = arm.body.first().map(stmt_start);
inner.push_str(&format!(
"<div class=\"arm\"><span class=\"badge\">arm</span><code class=\"expr\">{}</code>",
escape(&label)
));
if let Some(af) = arm_first {
inner.push_str(&emit_comments(lines, arm_cursor, af));
arm_cursor = af;
}
inner.push_str("<div class=\"nested\">");
for st in &arm.body {
let start = stmt_start(st);
inner.push_str(&emit_comments(lines, arm_cursor, start));
let (h, e) = render_stmt(st, lines);
inner.push_str(&h);
arm_cursor = e;
}
inner.push_str("</div></div>");
}
inner.push_str("</div>");
inner
}
Stmt::While {
condition, body, ..
} => {
let mut inner = format!(
"<div class=\"card loop-card\"><span class=\"badge\">while</span><code class=\"expr\">{}</code><div class=\"nested\">",
escape(&expr_display(condition))
);
let mut cursor = stmt_start(stmt) + 1;
for st in body {
let start = stmt_start(st);
inner.push_str(&emit_comments(lines, cursor, start));
let (h, e) = render_stmt(st, lines);
inner.push_str(&h);
cursor = e;
}
inner.push_str("</div></div>");
inner
}
Stmt::ForEach {
item,
collection,
body,
..
} => {
let mut inner = format!(
"<div class=\"card loop-card\"><span class=\"badge\">foreach</span><code class=\"expr\">[{}]({})</code><div class=\"nested\">",
escape(item),
escape(collection)
);
let mut cursor = stmt_start(stmt) + 1;
for st in body {
let start = stmt_start(st);
inner.push_str(&emit_comments(lines, cursor, start));
let (h, e) = render_stmt(st, lines);
inner.push_str(&h);
cursor = e;
}
inner.push_str("</div></div>");
inner
}
};
(html, end)
}
fn call_display(call: &CallExpr) -> String {
let mut s = format!("> {}", call.callee);
for a in &call.args {
match a {
Arg::Positional(e) => {
s.push(' ');
s.push_str(&expr_display(e));
}
Arg::Named { name, value } => {
s.push(' ');
s.push_str(name);
s.push('=');
s.push_str(&expr_display(value));
}
}
}
s
}
/// Surface-syntax preview for view (not Debug).
pub fn expr_display(expr: &Expr) -> String {
expr_prec(expr, 0)
}
fn expr_prec(expr: &Expr, parent_prec: u8) -> String {
match expr {
Expr::Literal(lit) => lit_display(lit),
Expr::Var(name) => format!("`{name}`"),
Expr::Interp(parts) => {
let mut s = String::new();
for p in parts {
match p {
InterpPart::Lit(t) => s.push_str(t),
InterpPart::Var(n) => {
s.push('`');
s.push_str(n);
s.push('`');
}
}
}
s
}
Expr::Unary { op, expr } => {
let prec = 5;
let inner = match op {
UnaryOp::Not => format!("not {}", expr_prec(expr, prec)),
UnaryOp::Neg => format!("-{}", expr_prec(expr, prec)),
};
wrap(inner, prec, parent_prec)
}
Expr::Binary { op, left, right } => {
let (sym, prec) = bin_sym_prec(*op);
let inner = format!(
"{} {} {}",
expr_prec(left, prec),
sym,
expr_prec(right, prec + 1) // left-assoc bias
);
wrap(inner, prec, parent_prec)
}
Expr::Call(call) => call_display(call),
Expr::List(items) => {
let parts: Vec<String> = items.iter().map(|e| expr_prec(e, 0)).collect();
format!("[{}]", parts.join(", "))
}
}
}
fn wrap(inner: String, prec: u8, parent_prec: u8) -> String {
if prec < parent_prec {
format!("({inner})")
} else {
inner
}
}
fn bin_sym_prec(op: BinaryOp) -> (&'static str, u8) {
match op {
BinaryOp::Or => ("or", 1),
BinaryOp::And => ("and", 2),
BinaryOp::Eq => ("==", 3),
BinaryOp::Ne => ("!=", 3),
BinaryOp::Lt => ("<", 3),
BinaryOp::Le => ("<=", 3),
BinaryOp::Gt => (">", 3),
BinaryOp::Ge => (">=", 3),
BinaryOp::Add => ("+", 4),
BinaryOp::Sub => ("-", 4),
BinaryOp::Mul => ("*", 5),
BinaryOp::Div => ("/", 5),
}
}
fn lit_display(lit: &Literal) -> String {
match lit {
Literal::None => "None".into(),
Literal::Bool(true) => "True".into(),
Literal::Bool(false) => "False".into(),
Literal::Int(n) => n.to_string(),
Literal::Text(t) => t.clone(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ast::{BinaryOp, Expr, Literal};
#[test]
fn expr_gt_is_surface() {
let e = Expr::Binary {
op: BinaryOp::Gt,
left: Box::new(Expr::Var("x".into())),
right: Box::new(Expr::Literal(Literal::Int(0))),
};
assert_eq!(expr_display(&e), "`x` > 0");
assert!(!expr_display(&e).contains("Binary"));
}
#[test]
fn structure_keeps_comments_and_surface_expr() {
let src = include_str!("../../examples/structure/nested-call.mq.md");
let module = crate::parse::parse_source(src).unwrap();
let html = render_module_structure(&module, src);
assert!(html.contains("comment-card"));
assert!(html.contains("print"));
assert!(!html.contains("Binary {"));
assert!(!html.contains("Literal(Int"));
}
}