Implement Phase I tree-walk interpreter for all examples.

Parse functions, calls, returns, branches, loops, tables, and imports so marqdo run passes the examples gold suite (M2–M4).

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-08-04 11:41:37 +08:00
1 parent 69b626e919
commit a29bf6e97b
12 files changed
+1791 -61

No files matched your search

+8 -2
View File
@@ -48,11 +48,17 @@ Marqdo:Markdown **标记即语法**。源文件 `.mq.md` 既是文稿也是程
- 映射 v0.1:[markdown-mapping.md](doc/design/markdown-mapping.md)
- 解释器路线图:[interpreter.md](doc/roadmap/interpreter.md)
- **Phase I(树遍历)已通**:`examples/` 下 hello / index / branch / loop / collection / with-import 均可 `marqdo run`
- 依赖(Rust,无 Flex/Bison):[dependencies.md](doc/design/dependencies.md)
- 选型:[ADR 0001 — Rust](doc/adr/0001-implementation-language.md)
下一步:按 [dev-setup.md](doc/dev-setup.md) 配置环境;M0 脚手架已就绪,M1 起实现词法。
```bash
cargo run -- run examples/index.mq.md
# Hello World!
```
下一步:按需加强语义诊断与表达式;Phase II 字节码见路线图 M5。
---
+6 -6
View File
@@ -153,12 +153,12 @@ spike/ # Python 存档,非运行时依赖
| 周序 | 交付 | 完成定义 |
|------|------|----------|
| M0 | 正式包 + CLI 空壳 + 金样例框架 | `marqdo run` 可调用;测试发现「未实现」 |
| M1 | 词法·行分类 + 最小 AST | 单测覆盖注释/代码行 |
| M2 | 解析:函数、输出调用、返回 | `hello` + `index` 金样例绿 |
| M3 | 分支、循环、表格、绑定 | `branch`/`loop`/`collection` 绿 |
| M4 | 导入与提升 | `with-import` 绿;**宣称 Phase I 达标** |
| M5 | 字节码设计文档 + 原型 opcode | 同金样例双后端一致 |
| M0 | 正式包 + CLI 空壳 + 金样例框架 | `marqdo run` 可调用;测试发现「未实现」 | ✅ |
| M1 | 词法·行分类 + 最小 AST | 单测覆盖注释/代码行 | ✅ |
| M2 | 解析:函数、输出调用、返回 | `hello` + `index` 金样例绿 | ✅ |
| M3 | 分支、循环、表格、绑定 | `branch`/`loop`/`collection` 绿 | ✅ |
| M4 | 导入与提升 | `with-import` 绿;**宣称 Phase I 达标** | ✅ |
| M5 | 字节码设计文档 + 原型 opcode | 同金样例双后端一致 | 未开始 |
**Phase I 完成前,不宣称「语言已实现」。**
**仅 hello 特判通过,不算完成。**
+2 -2
View File
@@ -4,7 +4,7 @@ title: 循环示例
# main
## while:倒数
倒数:
*`n` = 3*
@@ -12,7 +12,7 @@ title: 循环示例
> print text=`n`
*`n` = `n` - 1*
## foreach:遍历篮子
遍历篮子:
`篮子` =
+202
View File
@@ -0,0 +1,202 @@
//! Marqdo AST (Phase I).
use crate::diagnostics::Span;
#[derive(Debug, Clone)]
pub struct Module {
pub imports: Vec<String>,
pub functions: Vec<Function>,
}
#[derive(Debug, Clone)]
pub struct Function {
pub name: String,
pub level: u8,
pub span: Span,
pub params: Vec<String>,
pub body: Vec<Stmt>,
pub children: Vec<Function>,
}
#[derive(Debug, Clone)]
pub enum Stmt {
Assign {
name: String,
value: Expr,
span: Span,
},
Return {
value: Expr,
span: Span,
},
/// Statement-form call (`> …`); return value discarded.
Call {
call: CallExpr,
span: Span,
},
Branch {
arms: Vec<BranchArm>,
span: Span,
},
While {
condition: Expr,
body: Vec<Stmt>,
span: Span,
},
ForEach {
item: String,
collection: String,
body: Vec<Stmt>,
span: Span,
},
}
#[derive(Debug, Clone)]
pub struct BranchArm {
pub condition: Option<Expr>, // None = else
pub body: Vec<Stmt>,
}
#[derive(Debug, Clone)]
pub struct CallExpr {
pub callee: String,
pub args: Vec<(String, Expr)>,
}
#[derive(Debug, Clone)]
pub enum Expr {
Literal(Literal),
Var(String),
/// Text with embedded `` `var` `` segments.
Interp(Vec<InterpPart>),
Unary {
op: UnaryOp,
expr: Box<Expr>,
},
Binary {
op: BinaryOp,
left: Box<Expr>,
right: Box<Expr>,
},
Call(CallExpr),
List(Vec<Expr>),
}
#[derive(Debug, Clone)]
pub enum InterpPart {
Lit(String),
Var(String),
}
#[derive(Debug, Clone)]
pub enum Literal {
None,
Bool(bool),
Int(i64),
Text(String),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnaryOp {
Not,
Neg,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinaryOp {
Add,
Sub,
Mul,
Div,
Eq,
Ne,
Lt,
Le,
Gt,
Ge,
And,
Or,
}
/// Pretty-print AST for `--dump-ast`.
pub fn format_ast_dump(path: &str, module: &Module) -> String {
let mut out = String::new();
out.push_str(&format!("=== marqdo: ast ({path}) ===\n"));
if !module.imports.is_empty() {
out.push_str(&format!("(imports {:?})\n", module.imports));
}
for fun in &module.functions {
dump_fun(&mut out, fun, 0);
}
out.push_str("=== marqdo: end ast ===\n");
out
}
fn dump_fun(out: &mut String, fun: &Function, depth: usize) {
let pad = " ".repeat(depth);
out.push_str(&format!(
"{pad}(fun level={} {:?} params={:?} @{}\n",
fun.level, fun.name, fun.params, fun.span
));
for stmt in &fun.body {
dump_stmt(out, stmt, depth + 1);
}
for child in &fun.children {
dump_fun(out, child, depth + 1);
}
out.push_str(&format!("{pad})\n"));
}
fn dump_stmt(out: &mut String, stmt: &Stmt, depth: usize) {
let pad = " ".repeat(depth);
match stmt {
Stmt::Assign { name, value, span } => {
out.push_str(&format!("{pad}(assign {name:?} {value:?} @{span})\n"));
}
Stmt::Return { value, span } => {
out.push_str(&format!("{pad}(return {value:?} @{span})\n"));
}
Stmt::Call { call, span } => {
out.push_str(&format!(
"{pad}(call {:?} {:?} @{span})\n",
call.callee, call.args
));
}
Stmt::Branch { arms, span } => {
out.push_str(&format!("{pad}(branch @{span}\n"));
for arm in arms {
out.push_str(&format!("{pad} (arm cond={:?}\n", arm.condition));
for s in &arm.body {
dump_stmt(out, s, depth + 2);
}
out.push_str(&format!("{pad} )\n"));
}
out.push_str(&format!("{pad})\n"));
}
Stmt::While {
condition,
body,
span,
} => {
out.push_str(&format!("{pad}(while {condition:?} @{span}\n"));
for s in body {
dump_stmt(out, s, depth + 1);
}
out.push_str(&format!("{pad})\n"));
}
Stmt::ForEach {
item,
collection,
body,
span,
} => {
out.push_str(&format!(
"{pad}(foreach {item:?} in {collection:?} @{span}\n"
));
for s in body {
dump_stmt(out, s, depth + 1);
}
out.push_str(&format!("{pad})\n"));
}
}
}
+1 -1
View File
@@ -3,7 +3,7 @@
use std::fmt;
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Copy)]
pub struct Span {
pub line: u32,
pub col: u32,
+414
View File
@@ -0,0 +1,414 @@
//! Tree-walk interpreter (Phase I).
use anyhow::{bail, Result};
use std::collections::HashMap;
use crate::ast::{
BinaryOp, CallExpr, Expr, Function, InterpPart, Literal, Module, Stmt, UnaryOp,
};
use crate::value::Value;
pub struct Interpreter {
pub trace: bool,
}
struct Env {
vars: HashMap<String, Value>,
}
impl Env {
fn new() -> Self {
Self {
vars: HashMap::new(),
}
}
fn get(&self, name: &str) -> Option<&Value> {
self.vars.get(name)
}
fn set(&mut self, name: String, value: Value) {
self.vars.insert(name, value);
}
}
impl Interpreter {
pub fn new(trace: bool) -> Self {
Self { trace }
}
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");
}
let mut last = Value::None;
for fun in tops {
if fun.params.is_empty() {
last = self.run_function(module, fun, Env::new(), &[])?;
}
}
Ok(last)
}
fn run_function(
&mut self,
module: &Module,
fun: &Function,
mut env: Env,
args: &[(String, Value)],
) -> Result<Value> {
if self.trace {
eprintln!("enter fun {:?}", fun.name);
}
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);
}
}
let mut ret = Value::None;
for stmt in &fun.body {
if let Some(v) = self.exec_stmt(module, fun, &mut env, stmt)? {
ret = v;
break;
}
}
if self.trace {
eprintln!("leave fun {:?} => {ret}", fun.name);
}
Ok(ret)
}
/// Returns Some if function should return.
fn exec_stmt(
&mut self,
module: &Module,
fun: &Function,
env: &mut Env,
stmt: &Stmt,
) -> Result<Option<Value>> {
match stmt {
Stmt::Assign { name, value, span } => {
let v = self.eval_expr(module, fun, env, value)?;
if self.trace {
eprintln!(" assign {name} = {v:?} @{span}");
}
env.set(name.clone(), v);
Ok(None)
}
Stmt::Return { value, span } => {
let v = self.eval_expr(module, fun, env, value)?;
if self.trace {
eprintln!(" return {v:?} @{span}");
}
Ok(Some(v))
}
Stmt::Call { call, span } => {
if self.trace {
eprintln!(" call-stmt {} @{span}", call.callee);
}
let _ = self.eval_call(module, fun, env, call)?;
Ok(None)
}
Stmt::Branch { arms, span } => {
if self.trace {
eprintln!(" branch @{span}");
}
for arm in arms {
let take = match &arm.condition {
None => true,
Some(cond) => self.eval_expr(module, fun, env, cond)?.truthy(),
};
if take {
for s in &arm.body {
if let Some(v) = self.exec_stmt(module, fun, env, s)? {
return Ok(Some(v));
}
}
break;
}
}
Ok(None)
}
Stmt::While {
condition,
body,
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");
}
for s in body {
if let Some(v) = self.exec_stmt(module, fun, env, s)? {
return Ok(Some(v));
}
}
}
Ok(None)
}
Stmt::ForEach {
item,
collection,
body,
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 items = match coll {
Value::List(xs) => xs,
other => bail!("{span}: foreach needs a list, got {other:?}"),
};
for val in items {
env.set(item.clone(), val);
for s in body {
if let Some(v) = self.exec_stmt(module, fun, env, s)? {
return Ok(Some(v));
}
}
}
Ok(None)
}
}
}
fn eval_expr(
&mut self,
module: &Module,
fun: &Function,
env: &mut Env,
expr: &Expr,
) -> Result<Value> {
match expr {
Expr::Literal(lit) => Ok(lit_to_value(lit)),
Expr::Var(name) => env
.get(name)
.cloned()
.ok_or_else(|| anyhow::anyhow!("undefined variable `{name}`")),
Expr::Interp(parts) => {
let mut s = String::new();
for part in parts {
match part {
InterpPart::Lit(t) => s.push_str(t),
InterpPart::Var(n) => {
let v = env
.get(n)
.ok_or_else(|| anyhow::anyhow!("undefined variable `{n}`"))?;
s.push_str(&v.as_display());
}
}
}
Ok(Value::Text(s))
}
Expr::Unary { op, expr } => {
let v = self.eval_expr(module, fun, env, expr)?;
match op {
UnaryOp::Not => Ok(Value::Bool(!v.truthy())),
UnaryOp::Neg => match v {
Value::Int(n) => Ok(Value::Int(-n)),
_ => bail!("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() {
return Ok(Value::Bool(false));
}
let r = self.eval_expr(module, fun, env, right)?;
return Ok(Value::Bool(r.truthy()));
}
BinaryOp::Or => {
if l.truthy() {
return Ok(Value::Bool(true));
}
let r = self.eval_expr(module, fun, env, right)?;
return Ok(Value::Bool(r.truthy()));
}
_ => {}
}
let r = self.eval_expr(module, fun, env, right)?;
eval_binary(*op, &l, &r)
}
Expr::Call(call) => self.eval_call(module, fun, env, call),
Expr::List(items) => {
let mut out = Vec::new();
for it in items {
out.push(self.eval_expr(module, fun, env, it)?);
}
Ok(Value::List(out))
}
}
}
fn eval_call(
&mut self,
module: &Module,
fun: &Function,
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)?));
}
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}");
return Ok(Value::None);
}
"input" => {
let prompt = arg_vals
.iter()
.find(|(k, _)| k == "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();
}
}
return Ok(Value::Text(line));
}
_ => {}
}
let target = lookup_function(module, fun, &call.callee).ok_or_else(|| {
anyhow::anyhow!("unknown function `{}`", call.callee)
})?;
// Bind by param names: prefer named args matching params; also allow arg keys as env names.
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());
}
}
self.run_function(module, target, call_env, &[])
}
}
fn find_top<'a>(module: &'a Module, name: &str) -> Option<&'a Function> {
module.functions.iter().find(|f| f.name == name)
}
fn lookup_function<'a>(
module: &'a Module,
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)
}
fn find_in_tree<'a>(fun: &'a Function, name: &str) -> Option<&'a Function> {
for child in &fun.children {
if child.name == name {
return Some(child);
}
if let Some(f) = find_in_tree(child, name) {
return Some(f);
}
}
None
}
fn lit_to_value(lit: &Literal) -> Value {
match lit {
Literal::None => Value::None,
Literal::Bool(b) => Value::Bool(*b),
Literal::Int(n) => Value::Int(*n),
Literal::Text(s) => Value::Text(s.clone()),
}
}
fn eval_binary(op: BinaryOp, l: &Value, r: &Value) -> Result<Value> {
match op {
BinaryOp::Add => match (l, r) {
(Value::Int(a), Value::Int(b)) => Ok(Value::Int(a + b)),
(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:?}"),
},
BinaryOp::Sub => match (l, r) {
(Value::Int(a), Value::Int(b)) => Ok(Value::Int(a - b)),
_ => bail!("`-` needs ints"),
},
BinaryOp::Mul => match (l, r) {
(Value::Int(a), Value::Int(b)) => Ok(Value::Int(a * b)),
_ => bail!("`*` needs ints"),
},
BinaryOp::Div => match (l, r) {
(Value::Int(_), Value::Int(0)) => bail!("division by zero"),
(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::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()),
BinaryOp::Ge => cmp_ord(l, r, |o| o.is_ge()),
BinaryOp::And | BinaryOp::Or => unreachable!("handled above"),
}
}
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)))),
(Value::Text(a), Value::Text(b)) => Ok(Value::Bool(f(a.cmp(b)))),
_ => bail!("comparison needs two ints or two texts"),
}
}
+37 -19
View File
@@ -1,17 +1,24 @@
//! Marqdo reference interpreter (Phase I).
//!
//! Pipeline: load → line classify / lex → parse → sema → tree-walk eval.
//! Pipeline: load → line classify → parse → tree-walk eval.
pub mod ast;
pub mod debug;
pub mod diagnostics;
pub mod interp;
pub mod lex;
pub mod load;
pub mod parse;
pub mod value;
use std::path::Path;
use anyhow::{bail, Context, Result};
use anyhow::{bail, Result};
use crate::ast::format_ast_dump;
use crate::interp::Interpreter;
use crate::lex::{classify_source, format_lines_dump};
use crate::load::load_module;
/// Options for a single `run` / dump invocation.
#[derive(Debug, Clone, Default)]
@@ -39,42 +46,53 @@ impl RunOptions {
}
}
/// Run a `.mq.md` program (M1: line classify + dumps; eval still TODO).
/// Run a `.mq.md` program.
pub fn run_file(path: &Path, opts: &RunOptions) -> Result<i32> {
let source = std::fs::read_to_string(path)
.with_context(|| format!("failed to read {}", path.display()))?;
.map_err(|e| anyhow::anyhow!("failed to read {}: {e}", path.display()))?;
if source.trim().is_empty() {
bail!("{} is empty", path.display());
}
let path_label = path.display().to_string();
let lines = classify_source(&source);
if opts.dump_lines {
let lines = classify_source(&source);
print!("{}", format_lines_dump(&path_label, &lines));
}
if opts.dump_tokens {
println!("=== marqdo: tokens ({path_label}) ===");
println!("(tokens not implemented yet — M1+)");
println!("(fine-grained tokens not implemented yet — use --dump-lines)");
println!("=== marqdo: end tokens ===");
}
let module = load_module(path)?;
if opts.dump_ast {
println!("=== marqdo: ast ({path_label}) ===");
println!("(ast not implemented yet — M2)");
println!("=== marqdo: end ast ===");
print!("{}", format_ast_dump(&path_label, &module));
}
if opts.dump_sema {
println!("=== marqdo: sema ({path_label}) ===");
println!("(sema not implemented yet — M3+)");
println!(
"functions: {:?}",
module
.functions
.iter()
.map(|f| f.name.as_str())
.collect::<Vec<_>>()
);
println!("imports: {:?}", module.imports);
println!("=== marqdo: end sema ===");
}
let mut interp = Interpreter::new(opts.trace_eval);
if opts.trace_eval {
println!("=== marqdo: trace-eval ({path_label}) ===");
println!("(eval not implemented yet — M2+)");
println!("=== marqdo: end trace-eval ===");
eprintln!("=== marqdo: trace-eval ({path_label}) ===");
}
if source.is_empty() {
bail!("{} is empty", path.display());
interp.run_module(&module)?;
if opts.trace_eval {
eprintln!("=== marqdo: end trace-eval ===");
}
// Until eval exists: dumps are useful; run still fails loudly.
bail!("evaluation not implemented — see doc/roadmap/interpreter.md (M2+)")
Ok(0)
}
+51
View File
@@ -0,0 +1,51 @@
//! Load `.mq.md` files and merge frontmatter imports.
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use anyhow::{bail, Context, Result};
use crate::ast::{Function, Module};
use crate::parse::parse_source;
/// Load `path` and recursively merge imported modules' top-level functions.
pub fn load_module(path: &Path) -> Result<Module> {
let mut visited = HashSet::new();
load_module_inner(path, &mut visited)
}
fn load_module_inner(path: &Path, visited: &mut HashSet<PathBuf>) -> Result<Module> {
let canon = path
.canonicalize()
.unwrap_or_else(|_| path.to_path_buf());
if !visited.insert(canon.clone()) {
bail!("circular import involving {}", path.display());
}
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 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);
}
}
Ok(module)
}
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);
}
}
+424
View File
@@ -0,0 +1,424 @@
//! Expression parser (recursive descent over a single string).
use anyhow::{bail, Result};
use crate::ast::{BinaryOp, CallExpr, Expr, InterpPart, Literal, UnaryOp};
pub fn parse_expr(input: &str) -> Result<Expr> {
let mut p = Parser::new(input.trim());
let e = p.parse_or()?;
p.skip_ws();
if !p.rest().is_empty() {
bail!("trailing input in expression: {:?}", p.rest());
}
Ok(e)
}
/// Argument / template RHS: prefer a full expression; else interpolated text.
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())));
}
match parse_expr(s) {
Ok(e) => Ok(e),
Err(_) => Ok(parse_interp(s)),
}
}
pub fn parse_interp(s: &str) -> Expr {
let mut parts = Vec::new();
let mut rest = s;
while !rest.is_empty() {
if let Some(start) = rest.find('`') {
if start > 0 {
parts.push(InterpPart::Lit(rest[..start].to_string()));
}
let after = &rest[start + 1..];
if let Some(end) = after.find('`') {
let name = after[..end].to_string();
parts.push(InterpPart::Var(name));
rest = &after[end + 1..];
} else {
parts.push(InterpPart::Lit(format!("`{after}")));
break;
}
} else {
parts.push(InterpPart::Lit(rest.to_string()));
break;
}
}
if parts.len() == 1 {
match &parts[0] {
InterpPart::Lit(t) => return Expr::Literal(Literal::Text(t.clone())),
InterpPart::Var(n) => return Expr::Var(n.clone()),
}
}
Expr::Interp(parts)
}
struct Parser<'a> {
src: &'a str,
i: usize,
}
impl<'a> Parser<'a> {
fn new(src: &'a str) -> Self {
Self { src, i: 0 }
}
fn rest(&self) -> &str {
&self.src[self.i..]
}
fn skip_ws(&mut self) {
while let Some(c) = self.peek_char() {
if c.is_whitespace() {
self.bump_char();
} else {
break;
}
}
}
fn peek_char(&self) -> Option<char> {
self.rest().chars().next()
}
fn bump_char(&mut self) -> Option<char> {
let mut chars = self.rest().chars();
let c = chars.next()?;
self.i += c.len_utf8();
Some(c)
}
fn starts_with(&self, s: &str) -> bool {
self.rest().starts_with(s)
}
fn eat(&mut self, s: &str) -> bool {
if self.starts_with(s) {
self.i += s.len();
true
} else {
false
}
}
fn parse_or(&mut self) -> Result<Expr> {
let mut left = self.parse_and()?;
loop {
self.skip_ws();
if self.eat("or") {
// ensure word boundary
if self.peek_char().map(|c| c.is_alphanumeric() || c == '_').unwrap_or(false) {
self.i -= 2;
break;
}
let right = self.parse_and()?;
left = Expr::Binary {
op: BinaryOp::Or,
left: Box::new(left),
right: Box::new(right),
};
} else {
break;
}
}
Ok(left)
}
fn parse_and(&mut self) -> Result<Expr> {
let mut left = self.parse_cmp()?;
loop {
self.skip_ws();
if self.eat("and") {
if self.peek_char().map(|c| c.is_alphanumeric() || c == '_').unwrap_or(false) {
self.i -= 3;
break;
}
let right = self.parse_cmp()?;
left = Expr::Binary {
op: BinaryOp::And,
left: Box::new(left),
right: Box::new(right),
};
} else {
break;
}
}
Ok(left)
}
fn parse_cmp(&mut self) -> Result<Expr> {
let mut left = self.parse_add()?;
self.skip_ws();
let op = if self.eat("==") {
Some(BinaryOp::Eq)
} else if self.eat("!=") {
Some(BinaryOp::Ne)
} else if self.eat("<=") {
Some(BinaryOp::Le)
} else if self.eat(">=") {
Some(BinaryOp::Ge)
} else if self.eat("<") {
Some(BinaryOp::Lt)
} else if self.eat(">") {
Some(BinaryOp::Gt)
} else {
None
};
if let Some(op) = op {
let right = self.parse_add()?;
left = Expr::Binary {
op,
left: Box::new(left),
right: Box::new(right),
};
}
Ok(left)
}
fn parse_add(&mut self) -> Result<Expr> {
let mut left = self.parse_mul()?;
loop {
self.skip_ws();
if self.eat("+") {
let right = self.parse_mul()?;
left = Expr::Binary {
op: BinaryOp::Add,
left: Box::new(left),
right: Box::new(right),
};
} else if self.eat("-") {
let right = self.parse_mul()?;
left = Expr::Binary {
op: BinaryOp::Sub,
left: Box::new(left),
right: Box::new(right),
};
} else {
break;
}
}
Ok(left)
}
fn parse_mul(&mut self) -> Result<Expr> {
let mut left = self.parse_unary()?;
loop {
self.skip_ws();
if self.eat("*") {
let right = self.parse_unary()?;
left = Expr::Binary {
op: BinaryOp::Mul,
left: Box::new(left),
right: Box::new(right),
};
} else if self.eat("/") {
let right = self.parse_unary()?;
left = Expr::Binary {
op: BinaryOp::Div,
left: Box::new(left),
right: Box::new(right),
};
} else {
break;
}
}
Ok(left)
}
fn parse_unary(&mut self) -> Result<Expr> {
self.skip_ws();
if self.eat("not") {
if self.peek_char().map(|c| c.is_alphanumeric() || c == '_').unwrap_or(false) {
self.i -= 3;
} else {
let expr = self.parse_unary()?;
return Ok(Expr::Unary {
op: UnaryOp::Not,
expr: Box::new(expr),
});
}
}
if self.eat("-") {
let expr = self.parse_unary()?;
return Ok(Expr::Unary {
op: UnaryOp::Neg,
expr: Box::new(expr),
});
}
self.parse_primary()
}
fn parse_primary(&mut self) -> Result<Expr> {
self.skip_ws();
if self.eat("(") {
let e = self.parse_or()?;
self.skip_ws();
if !self.eat(")") {
bail!("expected ')'");
}
return Ok(e);
}
if self.eat("`") {
let start = self.i;
while let Some(c) = self.peek_char() {
if c == '`' {
break;
}
self.bump_char();
}
let name = self.src[start..self.i].to_string();
if !self.eat("`") {
bail!("unterminated `name`");
}
return Ok(Expr::Var(name));
}
if self.eat(">") {
// Inline call: `> name k=v`
self.skip_ws();
return Ok(Expr::Call(parse_call_tail(self.rest()).map(|(c, consumed)| {
self.i += consumed;
c
})?));
}
if self.starts_with("True") && is_word_end(self.rest(), 4) {
self.i += 4;
return Ok(Expr::Literal(Literal::Bool(true)));
}
if self.starts_with("False") && is_word_end(self.rest(), 5) {
self.i += 5;
return Ok(Expr::Literal(Literal::Bool(false)));
}
if self.starts_with("None") && is_word_end(self.rest(), 4) {
self.i += 4;
return Ok(Expr::Literal(Literal::None));
}
if self.peek_char().map(|c| c.is_ascii_digit()).unwrap_or(false) {
let start = self.i;
while self.peek_char().map(|c| c.is_ascii_digit()).unwrap_or(false) {
self.bump_char();
}
let n: i64 = self.src[start..self.i].parse()?;
return Ok(Expr::Literal(Literal::Int(n)));
}
// Bare identifier / text word
if self.peek_char().map(|c| c.is_alphanumeric() || c == '_' || !c.is_ascii()).unwrap_or(false)
{
let start = self.i;
while let Some(c) = self.peek_char() {
if c.is_whitespace() || matches!(c, '=' | '+' | '-' | '*' | '/' | '<' | '>' | '(' | ')' | '`' | '|' | ',')
{
break;
}
self.bump_char();
}
let word = &self.src[start..self.i];
return Ok(Expr::Literal(Literal::Text(word.to_string())));
}
bail!("unexpected expression input: {:?}", self.rest());
}
}
fn is_word_end(s: &str, len: usize) -> bool {
match s[len..].chars().next() {
None => true,
Some(c) => !(c.is_alphanumeric() || c == '_'),
}
}
/// Parse `name key=val key2=val2` from a string; 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 >
let mut parts = s.split_whitespace();
let callee = parts
.next()
.ok_or_else(|| anyhow::anyhow!("missing callee"))?
.to_string();
let after_callee_offset = {
let idx = s.find(&callee).unwrap();
idx + callee.len()
};
let mut args_str = s[after_callee_offset..].trim_start();
let mut args = Vec::new();
// 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");
}
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))
}
fn find_next_arg_boundary(after_eq: &str) -> usize {
// Look for ` <ident>=` pattern not inside backticks (UTF-8 safe).
let mut in_bt = false;
for (i, c) in after_eq.char_indices() {
if c == '`' {
in_bt = !in_bt;
continue;
}
if !in_bt && c.is_whitespace() {
let rest = &after_eq[i..];
let trimmed = rest.trim_start();
let skipped = rest.len() - trimmed.len();
if let Some(eq_at) = trimmed.find('=') {
let key = &trimmed[..eq_at];
if !key.is_empty()
&& !key.contains(char::is_whitespace)
&& key
.chars()
.all(|ch| ch.is_alphanumeric() || ch == '_' || !ch.is_ascii())
{
return i + skipped;
}
}
}
}
after_eq.len()
}
/// Parse `> callee args` call expression/statement text (without leading `>`).
pub fn parse_call_after_gt(after_gt: &str) -> Result<CallExpr> {
let (call, _) = parse_call_tail(after_gt.trim())?;
Ok(call)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cmp_and_arith() {
let e = parse_expr("`x` > 0").unwrap();
assert!(matches!(e, Expr::Binary { op: BinaryOp::Gt, .. }));
let e = parse_expr("`n` + 1").unwrap();
assert!(matches!(e, Expr::Binary { op: BinaryOp::Add, .. }));
}
#[test]
fn interp_hello() {
let e = parse_value_or_interp("Hello `谁`!").unwrap();
assert!(matches!(e, Expr::Interp(_)));
}
}
+551 -6
View File
@@ -1,9 +1,554 @@
//! Recursive-descent parser → AST (no Bison).
//! Implementation arrives in M1–M2.
//! Line-oriented recursive descent → AST (no Bison).
#![allow(dead_code)]
mod expr;
#[derive(Debug, Default)]
pub struct Module {
// Filled by parser in later milestones.
pub use expr::{parse_call_after_gt, parse_expr, parse_value_or_interp};
use anyhow::{bail, Result};
use crate::ast::{
BranchArm, Expr, Function, Literal, Module, Stmt,
};
use crate::diagnostics::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;
/// Parse full source (with optional frontmatter) into a module AST.
pub fn parse_source(source: &str) -> Result<Module> {
let lines = classify_source(source);
parse_classified(&lines)
}
pub fn parse_classified(lines: &[ClassifiedLine]) -> Result<Module> {
let mut cur = Cursor::new(lines);
let imports = cur.parse_frontmatter()?;
let mut functions = Vec::new();
while cur.skip_noise() {
let Some(line) = cur.peek() else { break };
let trimmed = line.text.trim();
if is_heading(trimmed) {
functions.push(cur.parse_function(1)?);
} else {
bail!(
"{}:1: expected function heading `# …`, got: {trimmed}",
line.line_no
);
}
}
Ok(Module { imports, functions })
}
struct Cursor<'a> {
lines: &'a [ClassifiedLine],
i: usize,
}
impl<'a> Cursor<'a> {
fn new(lines: &'a [ClassifiedLine]) -> Self {
Self { lines, i: 0 }
}
fn peek(&self) -> Option<&'a ClassifiedLine> {
self.lines.get(self.i)
}
fn bump(&mut self) -> Option<&'a ClassifiedLine> {
let l = self.lines.get(self.i)?;
self.i += 1;
Some(l)
}
/// Skip blank + comment; return true if a line remains.
fn skip_noise(&mut self) -> bool {
while let Some(l) = self.peek() {
match l.kind {
LineKind::Blank | LineKind::Comment => {
self.i += 1;
}
LineKind::Code => return true,
}
}
false
}
fn parse_frontmatter(&mut self) -> Result<Vec<String>> {
let mut imports = Vec::new();
if !self.skip_noise() {
return Ok(imports);
}
let Some(first) = self.peek() else {
return Ok(imports);
};
if first.text.trim() != "---" {
return Ok(imports);
}
self.bump();
while let Some(line) = self.peek() {
let t = line.text.trim();
if t == "---" {
self.bump();
break;
}
if t.starts_with('>') {
let path = t[1..].trim();
if path.ends_with(".mq.md") {
imports.push(path.to_string());
}
}
self.bump();
}
Ok(imports)
}
fn parse_function(&mut self, min_level: u8) -> Result<Function> {
let line = self.bump().unwrap();
let (level, name) = parse_heading(line.text.trim())
.ok_or_else(|| anyhow::anyhow!("{}:1: expected heading", line.line_no))?;
if level < min_level {
bail!("{}:1: heading level {level} < expected {min_level}", line.line_no);
}
let mut fun = Function {
name: name.to_string(),
level,
span: Span {
line: line.line_no,
col: 1,
},
params: Vec::new(),
body: Vec::new(),
children: Vec::new(),
};
// Parameter zone: indented `- name` pure identifiers.
while self.skip_noise() {
let Some(l) = self.peek() else { break };
let trimmed = l.text.trim();
if is_heading(trimmed) {
let (nl, _) = parse_heading(trimmed).unwrap();
if nl > level {
fun.children.push(self.parse_function(level + 1)?);
continue;
}
break;
}
if let Some(param) = parse_param_line(trimmed) {
// Only accept as param before any body stmt (params first).
if fun.body.is_empty() {
fun.params.push(param);
self.bump();
continue;
}
}
break;
}
// Body until next heading at level <= ours.
while self.skip_noise() {
let Some(l) = self.peek() else { break };
let trimmed = l.text.trim();
if is_frame(trimmed) {
self.bump();
continue;
}
if is_heading(trimmed) {
let (nl, _) = parse_heading(trimmed).unwrap();
if nl > level {
fun.children.push(self.parse_function(level + 1)?);
continue;
}
break;
}
// Nested params mid-body shouldn't happen; treat `-` as control flow.
if trimmed.starts_with('+') || is_ordered_branch(trimmed) {
fun.body.push(self.parse_branch()?);
continue;
}
if trimmed.starts_with('-') {
fun.body.push(self.parse_loop_or_err()?);
continue;
}
fun.body.push(self.parse_simple_stmt()?);
}
Ok(fun)
}
fn parse_simple_stmt(&mut self) -> Result<Stmt> {
let line = self.bump().unwrap();
let span = Span {
line: line.line_no,
col: 1,
};
let trimmed = line.text.trim();
if is_frame(trimmed) {
bail!("{span}: unexpected frame line as statement");
}
// Bold return **…**
if trimmed.starts_with("**") && trimmed.ends_with("**") && trimmed.len() >= 4 {
let inner = &trimmed[2..trimmed.len() - 2];
let value = parse_expr_str(inner)?;
return Ok(Stmt::Return { value, span });
}
// Italic statement *…*
if trimmed.starts_with('*') && trimmed.ends_with('*') && !trimmed.starts_with("**") {
let inner = &trimmed[1..trimmed.len() - 1];
return parse_assign_or_expr_stmt(inner, span);
}
// Call >
if trimmed.starts_with('>') {
let call = call_after_gt(trimmed[1..].trim())?;
return Ok(Stmt::Call { call, span });
}
// Bare assign `` `x` = … `` possibly followed by table
if trimmed.starts_with('`') {
if let Some(stmt) = self.parse_backtick_assign(trimmed, span.clone())? {
return Ok(stmt);
}
}
bail!("{span}: unrecognized statement: {trimmed}")
}
fn parse_backtick_assign(&mut self, trimmed: &str, span: Span) -> Result<Option<Stmt>> {
// `name` = [rhs]
let Some(rest) = trimmed.strip_prefix('`') else {
return Ok(None);
};
let Some(end) = rest.find('`') else {
return Ok(None);
};
let name = rest[..end].to_string();
let after = rest[end + 1..].trim_start();
if !after.starts_with('=') {
return Ok(None);
}
let rhs = after[1..].trim();
if rhs.is_empty() {
// Consume following table if present.
let list = self.consume_table()?;
return Ok(Some(Stmt::Assign {
name,
value: Expr::List(list.into_iter().map(Expr::Literal).collect()),
span,
}));
}
Ok(Some(Stmt::Assign {
name,
value: parse_value_or_interp(rhs)?,
span,
}))
}
fn consume_table(&mut self) -> Result<Vec<Literal>> {
self.skip_noise();
let mut rows = Vec::new();
let mut header_done = false;
let mut sep_done = false;
while let Some(l) = self.peek() {
if l.kind != LineKind::Code {
break;
}
let t = l.text.trim();
if !t.starts_with('|') {
break;
}
self.bump();
let cells = split_table_row(t);
if !header_done {
header_done = true;
continue;
}
if !sep_done && cells.iter().all(|c| c.chars().all(|ch| ch == '-' || ch == ':' || ch.is_whitespace() || ch == '|'))
{
// separator row like |----|
sep_done = true;
continue;
}
// data row — single column examples: take first cell
if let Some(cell) = cells.first() {
rows.push(Literal::Text(cell.clone()));
}
sep_done = true;
}
if !header_done {
bail!("expected GFM table after empty assignment");
}
Ok(rows)
}
fn parse_branch(&mut self) -> Result<Stmt> {
let first = self.peek().unwrap();
let span = Span {
line: first.line_no,
col: 1,
};
let base_indent = indent_of(&first.text);
let mut arms = Vec::new();
while self.skip_noise() {
let Some(l) = self.peek() else { break };
let ind = indent_of(&l.text);
if ind < base_indent {
break;
}
let trimmed = l.text.trim();
let cond_src = if let Some(rest) = trimmed.strip_prefix('+') {
rest.trim()
} else if let Some(rest) = strip_ordered(trimmed) {
rest
} else {
break;
};
if ind != base_indent {
break;
}
self.bump();
let condition = if cond_src == "*" || cond_src.is_empty() {
None
} else {
Some(parse_expr_str(cond_src)?)
};
let body = self.parse_indented_body(base_indent)?;
arms.push(BranchArm { condition, body });
}
if arms.is_empty() {
bail!("{span}: empty branch");
}
Ok(Stmt::Branch { arms, span })
}
fn parse_loop_or_err(&mut self) -> Result<Stmt> {
let line = self.bump().unwrap();
let span = Span {
line: line.line_no,
col: 1,
};
let base_indent = indent_of(&line.text);
let trimmed = line.text.trim();
let rest = trimmed.strip_prefix('-').unwrap().trim();
// foreach: [item](collection)
if let Some((item, coll)) = parse_foreach_header(rest) {
let body = self.parse_indented_body(base_indent)?;
return Ok(Stmt::ForEach {
item,
collection: coll,
body,
span,
});
}
// while: expression
let condition = parse_expr_str(rest)?;
let body = self.parse_indented_body(base_indent)?;
Ok(Stmt::While {
condition,
body,
span,
})
}
fn parse_indented_body(&mut self, parent_indent: usize) -> Result<Vec<Stmt>> {
let mut body = Vec::new();
while self.skip_noise() {
let Some(l) = self.peek() else { break };
let ind = indent_of(&l.text);
if ind <= parent_indent {
// Same-indent `+`/`-` ends body (next arm / sibling).
break;
}
let trimmed = l.text.trim();
if is_frame(trimmed) {
self.bump();
continue;
}
if is_heading(trimmed) {
break;
}
if trimmed.starts_with('+') || is_ordered_branch(trimmed) {
// Nested branch inside body
body.push(self.parse_branch()?);
continue;
}
if trimmed.starts_with('-') {
body.push(self.parse_loop_or_err()?);
continue;
}
body.push(self.parse_simple_stmt()?);
}
Ok(body)
}
}
fn parse_assign_or_expr_stmt(inner: &str, span: Span) -> Result<Stmt> {
let inner = inner.trim();
// `` `name` = … ``
if let Some(rest) = inner.strip_prefix('`') {
if let Some(end) = rest.find('`') {
let name = rest[..end].to_string();
let after = rest[end + 1..].trim_start();
if let Some(rhs) = after.strip_prefix('=') {
let rhs = rhs.trim();
// Inline call assign: `> fn …`
let value = if rhs.starts_with('>') {
Expr::Call(call_after_gt(rhs[1..].trim())?)
} else {
parse_value_or_interp(rhs)?
};
return Ok(Stmt::Assign {
name,
value,
span,
});
}
}
}
bail!("{span}: italic statement must be an assignment (`name` = …)")
}
fn parse_param_line(trimmed: &str) -> Option<String> {
let rest = trimmed.strip_prefix('-')?.trim();
if rest.is_empty() || rest.contains('`') || rest.contains('[') || rest.contains('=') {
return None;
}
if rest.split_whitespace().count() != 1 {
return None;
}
// Reject if looks like comparison / operator expr
if rest.contains('>') || rest.contains('<') || rest.contains('+') || rest.contains('*') {
return None;
}
Some(rest.to_string())
}
fn parse_foreach_header(rest: &str) -> Option<(String, String)> {
// [item](collection)
let rest = rest.trim();
if !rest.starts_with('[') {
return None;
}
let end_item = rest.find(']')?;
let item = rest[1..end_item].to_string();
let after = rest[end_item + 1..].trim_start();
if !after.starts_with('(') || !after.ends_with(')') {
return None;
}
let coll = after[1..after.len() - 1].to_string();
Some((item, coll))
}
fn parse_heading(trimmed: &str) -> Option<(u8, &str)> {
if !trimmed.starts_with('#') {
return None;
}
let mut level = 0u8;
for c in trimmed.chars() {
if c == '#' {
level = level.saturating_add(1);
} else {
break;
}
}
if level == 0 || level > 6 {
return None;
}
let name = trimmed[level as usize..].trim();
if name.is_empty() {
return None;
}
Some((level, name))
}
fn is_heading(trimmed: &str) -> bool {
parse_heading(trimmed).is_some()
}
fn is_frame(trimmed: &str) -> bool {
if trimmed.len() < 3 {
return false;
}
trimmed.chars().all(|c| c == '-') || trimmed.chars().all(|c| c == '*')
}
fn is_ordered_branch(trimmed: &str) -> bool {
strip_ordered(trimmed).is_some()
}
fn strip_ordered(trimmed: &str) -> Option<&str> {
let mut chars = trimmed.chars();
let mut saw_digit = false;
while let Some(c) = chars.next() {
if c.is_ascii_digit() {
saw_digit = true;
continue;
}
if c == '.' && saw_digit {
return Some(chars.as_str().trim_start());
}
return None;
}
None
}
fn indent_of(text: &str) -> usize {
text.chars()
.take_while(|c| *c == ' ' || *c == '\t')
.map(|c| if c == '\t' { 4 } else { 1 })
.sum()
}
fn split_table_row(line: &str) -> Vec<String> {
let t = line.trim();
let t = t.strip_prefix('|').unwrap_or(t);
let t = t.strip_suffix('|').unwrap_or(t);
t.split('|').map(|c| c.trim().to_string()).collect()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ast::Stmt;
#[test]
fn parse_hello() {
let src = include_str!("../../examples/hello.mq.md");
let m = parse_source(src).unwrap();
assert_eq!(m.functions.len(), 1);
assert_eq!(m.functions[0].name, "Hello World");
assert_eq!(m.functions[0].body.len(), 1);
assert!(matches!(m.functions[0].body[0], Stmt::Call { .. }));
}
#[test]
fn parse_index() {
let src = include_str!("../../examples/index.mq.md");
let m = parse_source(src).unwrap();
assert!(m.imports.is_empty());
let main = m.functions.iter().find(|f| f.name == "main").unwrap();
assert_eq!(main.children.len(), 1);
assert_eq!(main.children[0].name, "问候");
assert_eq!(main.children[0].params, vec!["谁".to_string()]);
}
#[test]
fn parse_branch() {
let src = include_str!("../../examples/branch.mq.md");
let m = parse_source(src).unwrap();
let main = &m.functions[0];
assert!(main.body.iter().any(|s| matches!(s, Stmt::Branch { .. })));
}
}
+42
View File
@@ -0,0 +1,42 @@
//! Runtime values (Phase I).
use std::fmt;
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
None,
Bool(bool),
Int(i64),
Text(String),
/// Single-column table rows (v0); multi-column later.
List(Vec<Value>),
}
impl Value {
pub fn truthy(&self) -> bool {
match self {
Value::None => false,
Value::Bool(b) => *b,
Value::Int(n) => *n != 0,
Value::Text(s) => !s.is_empty(),
Value::List(xs) => !xs.is_empty(),
}
}
pub fn as_display(&self) -> String {
match self {
Value::None => "None".into(),
Value::Bool(true) => "True".into(),
Value::Bool(false) => "False".into(),
Value::Int(n) => n.to_string(),
Value::Text(s) => s.clone(),
Value::List(_) => "<list>".into(),
}
}
}
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_display())
}
}
+53 -25
View File
@@ -1,37 +1,65 @@
use std::process::Command;
#[test]
fn cli_runs_and_reports_unimplemented() {
fn run(args: &[&str]) -> (i32, String, String) {
let output = Command::new(env!("CARGO_BIN_EXE_marqdo"))
.args(["run", "examples/hello.mq.md"])
.args(args)
.output()
.expect("failed to run marqdo");
let code = output.status.code().unwrap_or(1);
let stdout = String::from_utf8_lossy(&output.stdout).into_owned();
let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
(code, stdout, stderr)
}
assert!(
!output.status.success(),
"M0/M1 should exit non-zero until eval exists"
);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("not implemented") || stderr.contains("evaluation not implemented"),
"stderr was: {stderr}"
#[test]
fn hello_prints_greeting() {
let (code, stdout, stderr) = run(&["run", "examples/hello.mq.md"]);
assert_eq!(code, 0, "stderr={stderr}");
assert_eq!(stdout.trim_end(), "Hello World!");
}
#[test]
fn index_nested_call() {
let (code, stdout, stderr) = run(&["run", "examples/index.mq.md"]);
assert_eq!(code, 0, "stderr={stderr}");
assert_eq!(stdout.trim_end(), "Hello World!");
}
#[test]
fn branch_else_arm() {
let (code, stdout, stderr) = run(&["run", "examples/branch.mq.md"]);
assert_eq!(code, 0, "stderr={stderr}");
assert_eq!(stdout.trim_end(), "零");
}
#[test]
fn loop_while_and_foreach() {
let (code, stdout, stderr) = run(&["run", "examples/loop.mq.md"]);
assert_eq!(code, 0, "stderr={stderr}");
assert_eq!(
stdout.trim_end(),
"3\n2\n1\n今天吃苹果\n今天吃梨"
);
}
#[test]
fn cli_dump_lines_shows_classification() {
let output = Command::new(env!("CARGO_BIN_EXE_marqdo"))
.args(["run", "examples/hello.mq.md", "--dump-lines"])
.output()
.expect("failed to run marqdo");
fn collection_foreach() {
let (code, stdout, stderr) = run(&["run", "examples/collection.mq.md"]);
assert_eq!(code, 0, "stderr={stderr}");
assert_eq!(stdout.trim_end(), "苹果\n梨\n桃");
}
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.contains("=== marqdo: lines"),
"stdout was: {stdout}"
);
assert!(stdout.contains("Code"), "expected Code line: {stdout}");
assert!(stdout.contains("Comment") || stdout.contains("Blank"), "{stdout}");
assert!(stdout.contains("print"), "{stdout}");
assert!(!output.status.success());
#[test]
fn with_import() {
let (code, stdout, stderr) = run(&["run", "examples/with-import.mq.md"]);
assert_eq!(code, 0, "stderr={stderr}");
assert_eq!(stdout.trim_end(), "42\n你好,Marqdo!");
}
#[test]
fn hello_dump_lines_still_runs() {
let (code, stdout, stderr) = run(&["run", "examples/hello.mq.md", "--dump-lines"]);
assert_eq!(code, 0, "stderr={stderr}");
assert!(stdout.contains("=== marqdo: lines"));
assert!(stdout.contains("Hello World!"));
}