Files
marqdo/src/value.rs
T
cflmyandCursor a23fdedfa2 Implement table semantic T0-T4: map/records/footnote get
- 1-col table stays a List; >=2-col becomes a column-oriented Map
  (1 data row -> scalar values, >=2 -> Map of Lists).
- First header @ / 行 / row opts into row-oriented List of Maps
  (marker column excluded); missing field cols is a diagnostic.
- Footnote index: `xs`[^1] (1-based list), `m`[^key] (map key), chained.
  Errors for [^0], out-of-range, missing key, duplicate headers.
- foreach walks list items or map keys; Map values now display as {k: v}.
- Sync both tree and bytecode backends; new gold + error fixtures, docs
  (markdown-mapping, roadmap, skill, public), and public 07/08 pages.
- Fix subtask-spawn fixture return value to child_ok (child-ok parses as
  subtraction).

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-10 09:50:21 +08:00

77 lines
2.3 KiB
Rust

//! Runtime values (Phase I).
use std::fmt;
use crate::formula::Expr as FormulaExpr;
/// Bound Markdown code fence (`type` → `code`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CodeBlock {
pub lang: String,
pub source: String,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
None,
Bool(bool),
Int(i64),
/// Floating-point number (`type` → `num`).
Num(f64),
Text(String),
/// Vertical (1-column) table rows, or nested list values in a column map.
List(Vec<Value>),
/// String-keyed map (insertion order): JSON objects / horizontal tables.
Map(Vec<(String, Value)>),
/// Parsed symbolic expression (`type` → `formula`).
Formula(FormulaExpr),
/// External language source from ```lang fence (`type` → `code`).
Code(CodeBlock),
}
impl Value {
pub fn truthy(&self) -> bool {
match self {
Value::None => false,
Value::Bool(b) => *b,
Value::Int(n) => *n != 0,
Value::Num(n) => *n != 0.0 && !n.is_nan(),
Value::Text(s) => !s.is_empty(),
Value::List(xs) => !xs.is_empty(),
Value::Map(xs) => !xs.is_empty(),
Value::Formula(_) => true,
Value::Code(c) => !c.source.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::Num(n) => crate::formula::format_num(*n),
Value::Text(s) => s.clone(),
Value::List(xs) => {
let parts: Vec<String> = xs.iter().map(|v| v.as_display()).collect();
format!("[{}]", parts.join(", "))
}
Value::Map(pairs) => {
let parts: Vec<String> = pairs
.iter()
.map(|(k, v)| format!("{k}: {}", v.as_display()))
.collect();
format!("{{{}}}", parts.join(", "))
}
Value::Formula(e) => e.as_display(),
Value::Code(c) => format!("```{}\n{}\n```", c.lang, c.source),
}
}
}
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.as_display())
}
}