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>
This commit is contained in:
1 parent
3463628165
commit
a23fdedfa2
37 files changed
+846
-51
No files matched your search
+7
-1
@@ -139,8 +139,14 @@ pub enum Expr {
|
||||
},
|
||||
Call(CallExpr),
|
||||
List(Vec<Expr>),
|
||||
/// Object literal / table row (`map` value).
|
||||
/// Object literal / horizontal table (`map` value).
|
||||
Map(Vec<(String, Expr)>),
|
||||
/// Footnote index: `` `xs`[^1] `` / `` `m`[^key] `` (see tables-maps-footnotes).
|
||||
Index {
|
||||
base: Box<Expr>,
|
||||
/// Label inside `[^…]` (digits → 1-based list index; else map key).
|
||||
label: String,
|
||||
},
|
||||
/// Parsed `$$…$$` formula tree (from assignment RHS).
|
||||
Formula(FormulaExpr),
|
||||
/// Bound ```lang … ``` fence (`code` value).
|
||||
|
||||
@@ -113,3 +113,57 @@ pub fn builtin_at(value: &Value, index: &Value) -> Result<Value, String> {
|
||||
}
|
||||
Ok(xs[i as usize].clone())
|
||||
}
|
||||
|
||||
/// Foreach surface: list items unchanged; map → list of keys (insertion order).
|
||||
pub fn builtin_iter_items(value: &Value) -> Result<Value, String> {
|
||||
match value {
|
||||
Value::List(xs) => Ok(Value::List(xs.clone())),
|
||||
Value::Map(pairs) => Ok(Value::List(
|
||||
pairs
|
||||
.iter()
|
||||
.map(|(k, _)| Value::Text(k.clone()))
|
||||
.collect(),
|
||||
)),
|
||||
_ => Err("foreach needs a list or map".into()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Footnote get: `` `xs`[^1] `` (1-based list) or `` `m`[^key] `` (map key).
|
||||
pub fn builtin_footnote_get(base: &Value, label: &str) -> Result<Value, String> {
|
||||
let is_digits = !label.is_empty() && label.bytes().all(|b| b.is_ascii_digit());
|
||||
match base {
|
||||
Value::List(xs) => {
|
||||
if !is_digits {
|
||||
return Err(format!(
|
||||
"list footnote index must be digits (1-based), got `[^{label}]`"
|
||||
));
|
||||
}
|
||||
let n: i64 = label
|
||||
.parse()
|
||||
.map_err(|_| format!("invalid list index `[^{label}]`"))?;
|
||||
if n == 0 {
|
||||
return Err("`[^0]` is invalid; list indices are 1-based".into());
|
||||
}
|
||||
if n < 1 || (n as usize) > xs.len() {
|
||||
return Err(format!(
|
||||
"list index {n} out of range (len {})",
|
||||
xs.len()
|
||||
));
|
||||
}
|
||||
Ok(xs[(n as usize) - 1].clone())
|
||||
}
|
||||
Value::Map(pairs) => {
|
||||
if is_digits {
|
||||
return Err(
|
||||
"map footnote must be a key name, not pure digits (keys are text)".into(),
|
||||
);
|
||||
}
|
||||
pairs
|
||||
.iter()
|
||||
.find(|(k, _)| k == label)
|
||||
.map(|(_, v)| v.clone())
|
||||
.ok_or_else(|| format!("missing map key `{label}`"))
|
||||
}
|
||||
_ => Err("footnote index needs list or map".into()),
|
||||
}
|
||||
}
|
||||
@@ -373,6 +373,7 @@ impl<'a> FnCompiler<'a> {
|
||||
} => {
|
||||
self.stmt_span = *span;
|
||||
self.compile_expr(&Expr::Var(collection.clone()))?;
|
||||
self.emit(Op::IterItems);
|
||||
let coll_slot = self.local_slot(&format!("__coll_{collection}"));
|
||||
self.emit(Op::SetLocal(coll_slot));
|
||||
self.emit(Op::Pop);
|
||||
@@ -903,6 +904,12 @@ impl<'a> FnCompiler<'a> {
|
||||
}
|
||||
self.emit(Op::BuildMap(pairs.len() as u16));
|
||||
}
|
||||
Expr::Index { base, label } => {
|
||||
self.compile_expr(base)?;
|
||||
let li = self.add_const(Value::Text(label.clone()));
|
||||
self.emit(Op::Constant(li));
|
||||
self.emit(Op::FootnoteGet);
|
||||
}
|
||||
Expr::Formula(e) => {
|
||||
let i = self.add_const(Value::Formula(e.clone()));
|
||||
self.emit(Op::Constant(i));
|
||||
|
||||
@@ -54,6 +54,10 @@ pub enum Op {
|
||||
/// Pop sep, then list → push joined text.
|
||||
Join,
|
||||
GetIndex,
|
||||
/// Pop list/map → push list of foreach items (list elements or map keys).
|
||||
IterItems,
|
||||
/// Pop footnote label text, then base → push indexed value (1-based list / map key).
|
||||
FootnoteGet,
|
||||
/// Call function by index; argc values are on the stack (param order).
|
||||
Call(u16, u8),
|
||||
/// Pop function name text → call zero-arg top-level or lexically visible function.
|
||||
|
||||
+19
-1
@@ -5,7 +5,8 @@ use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::builtin::{
|
||||
builtin_at, builtin_int, builtin_join, builtin_len, builtin_split, builtin_str, builtin_trim,
|
||||
builtin_at, builtin_footnote_get, builtin_int, builtin_iter_items, builtin_join,
|
||||
builtin_len, builtin_split, builtin_str, builtin_trim,
|
||||
builtin_type,
|
||||
};
|
||||
use crate::host::{call_host, HostContext, HostFn};
|
||||
@@ -310,6 +311,23 @@ impl Vm {
|
||||
let list = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
|
||||
stack.push(builtin_at(&list, &idx).map_err(|m| self.err_at(span, m))?);
|
||||
}
|
||||
Op::IterItems => {
|
||||
let v = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
|
||||
stack.push(builtin_iter_items(&v).map_err(|m| self.err_at(span, m))?);
|
||||
}
|
||||
Op::FootnoteGet => {
|
||||
let label = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
|
||||
let base = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
|
||||
let label = match label {
|
||||
Value::Text(s) => s,
|
||||
_ => {
|
||||
return Err(self.err_at(span, "footnote label must be text"));
|
||||
}
|
||||
};
|
||||
stack.push(
|
||||
builtin_footnote_get(&base, &label).map_err(|m| self.err_at(span, m))?,
|
||||
);
|
||||
}
|
||||
Op::HostCall(id, argc) => {
|
||||
let hf = HostFn::from_u16(id)
|
||||
.ok_or_else(|| self.err_at(span, format!("unknown host id {id}")))?;
|
||||
|
||||
+10
-6
@@ -8,7 +8,8 @@ use crate::ast::{
|
||||
Arg, BinaryOp, CallExpr, Expr, Function, InterpPart, Literal, Module, Param, Stmt, UnaryOp,
|
||||
};
|
||||
use crate::builtin::{
|
||||
builtin_at, builtin_int, builtin_join, builtin_len, builtin_split, builtin_str, builtin_trim,
|
||||
builtin_at, builtin_footnote_get, builtin_int, builtin_iter_items, builtin_join,
|
||||
builtin_len, builtin_split, builtin_str, builtin_trim,
|
||||
builtin_type,
|
||||
};
|
||||
use crate::debug::{emit_trace, DebugController};
|
||||
@@ -339,11 +340,10 @@ impl Interpreter {
|
||||
let coll = env.get(collection).cloned().ok_or_else(|| {
|
||||
self.err(format!("undefined collection `{collection}`"))
|
||||
})?;
|
||||
let items = match coll {
|
||||
Value::List(xs) => xs,
|
||||
other => {
|
||||
return Err(self.err(format!("foreach needs a list, got {other:?}")));
|
||||
}
|
||||
let items = match builtin_iter_items(&coll) {
|
||||
Ok(Value::List(xs)) => xs,
|
||||
Ok(_) => unreachable!(),
|
||||
Err(m) => return Err(self.err(m)),
|
||||
};
|
||||
for val in items {
|
||||
env.set(item.clone(), val);
|
||||
@@ -433,6 +433,10 @@ impl Interpreter {
|
||||
}
|
||||
Ok(Value::Map(out))
|
||||
}
|
||||
Expr::Index { base, label } => {
|
||||
let v = self.eval_expr(module, fun, env, base)?;
|
||||
builtin_footnote_get(&v, label).map_err(|m| self.err(m))
|
||||
}
|
||||
Expr::Formula(e) => Ok(Value::Formula(e.clone())),
|
||||
Expr::Code(c) => Ok(Value::Code(c.clone())),
|
||||
}
|
||||
|
||||
+39
-1
@@ -362,7 +362,20 @@ impl<'a> Parser<'a> {
|
||||
if !self.eat("`") {
|
||||
bail!("unterminated `name`");
|
||||
}
|
||||
return Ok(Expr::Var(name));
|
||||
let mut expr = Expr::Var(name);
|
||||
// Postfix footnotes: `xs`[^1] / `m`[^key] / chained `m`[^k][^1]
|
||||
loop {
|
||||
self.skip_ws();
|
||||
if !self.starts_with("[^") {
|
||||
break;
|
||||
}
|
||||
let label = self.parse_footnote_label()?;
|
||||
expr = Expr::Index {
|
||||
base: Box::new(expr),
|
||||
label,
|
||||
};
|
||||
}
|
||||
return Ok(expr);
|
||||
}
|
||||
if self.eat(">") {
|
||||
// Inline call: `> name k=v`
|
||||
@@ -484,6 +497,31 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
Ok(Expr::Interp(parts))
|
||||
}
|
||||
|
||||
/// Parse `[^label]` after a value; label is text until `]`.
|
||||
fn parse_footnote_label(&mut self) -> Result<String> {
|
||||
if !self.eat("[^") {
|
||||
bail!("expected [^ footnote index");
|
||||
}
|
||||
let start = self.i;
|
||||
while let Some(c) = self.peek_char() {
|
||||
if c == ']' {
|
||||
break;
|
||||
}
|
||||
if c == '\n' || c == '\r' {
|
||||
bail!("unterminated footnote index [^…]");
|
||||
}
|
||||
self.bump_char();
|
||||
}
|
||||
let label = self.src[start..self.i].trim().to_string();
|
||||
if !self.eat("]") {
|
||||
bail!("unterminated footnote index [^…]");
|
||||
}
|
||||
if label.is_empty() {
|
||||
bail!("empty footnote index `[^]`");
|
||||
}
|
||||
Ok(label)
|
||||
}
|
||||
}
|
||||
|
||||
fn is_word_end(s: &str, len: usize) -> bool {
|
||||
|
||||
+109
-17
@@ -327,13 +327,13 @@ impl<'a> Cursor<'a> {
|
||||
return self.consume_code_fence(span);
|
||||
}
|
||||
let start_i = self.i;
|
||||
let list = self.consume_table()?;
|
||||
let table = self.consume_table()?;
|
||||
let end_line = if self.i > start_i {
|
||||
self.lines[self.i - 1].line_no
|
||||
} else {
|
||||
span.line
|
||||
};
|
||||
return Ok((Expr::List(list), end_line));
|
||||
return Ok((table, end_line));
|
||||
}
|
||||
if let Some(expr) = try_parse_inline_formula(rhs)? {
|
||||
return Ok((expr, span.line));
|
||||
@@ -456,10 +456,15 @@ impl<'a> Cursor<'a> {
|
||||
Ok((Expr::Formula(expr), end_line))
|
||||
}
|
||||
|
||||
fn consume_table(&mut self) -> Result<Vec<Expr>> {
|
||||
/// GFM table after empty RHS:
|
||||
/// - 1-col → List
|
||||
/// - first header `@` / `行` / `row` → List of row Maps (marker col excluded)
|
||||
/// - else ≥2-col → Map (column-oriented)
|
||||
fn consume_table(&mut self) -> Result<Expr> {
|
||||
self.skip_noise();
|
||||
let mut header: Vec<String> = Vec::new();
|
||||
let mut rows: Vec<Expr> = Vec::new();
|
||||
let mut header_line: u32 = 1;
|
||||
let mut data_rows: Vec<Vec<String>> = Vec::new();
|
||||
let mut header_done = false;
|
||||
let mut sep_done = false;
|
||||
while let Some(l) = self.peek() {
|
||||
@@ -470,10 +475,12 @@ impl<'a> Cursor<'a> {
|
||||
if !t.starts_with('|') {
|
||||
break;
|
||||
}
|
||||
let line_no = l.line_no;
|
||||
self.bump();
|
||||
let cells = split_table_row(t);
|
||||
if !header_done {
|
||||
header = cells;
|
||||
header_line = line_no;
|
||||
header_done = true;
|
||||
continue;
|
||||
}
|
||||
@@ -486,23 +493,59 @@ impl<'a> Cursor<'a> {
|
||||
continue;
|
||||
}
|
||||
sep_done = true;
|
||||
if header.len() <= 1 {
|
||||
if let Some(cell) = cells.first() {
|
||||
rows.push(Expr::Literal(cell_literal(cell)));
|
||||
}
|
||||
} else {
|
||||
let mut pairs = Vec::new();
|
||||
for (i, key) in header.iter().enumerate() {
|
||||
let raw = cells.get(i).map(|s| s.as_str()).unwrap_or("");
|
||||
pairs.push((key.clone(), Expr::Literal(cell_literal(raw))));
|
||||
}
|
||||
rows.push(Expr::Map(pairs));
|
||||
}
|
||||
data_rows.push(cells);
|
||||
}
|
||||
if !header_done {
|
||||
bail!("expected GFM table after empty assignment");
|
||||
}
|
||||
Ok(rows)
|
||||
let row_oriented = header
|
||||
.first()
|
||||
.map(|h| is_row_table_marker(h))
|
||||
.unwrap_or(false);
|
||||
if row_oriented {
|
||||
return build_row_oriented_table(header, header_line, data_rows);
|
||||
}
|
||||
if header.len() <= 1 {
|
||||
let mut rows = Vec::new();
|
||||
for cells in data_rows {
|
||||
if let Some(cell) = cells.first() {
|
||||
rows.push(Expr::Literal(cell_literal(cell)));
|
||||
}
|
||||
}
|
||||
return Ok(Expr::List(rows));
|
||||
}
|
||||
// Duplicate header keys are errors (column identity must be unique).
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
for key in &header {
|
||||
if !seen.insert(key.clone()) {
|
||||
return Err(Diagnostic::new(
|
||||
None,
|
||||
Span {
|
||||
line: header_line,
|
||||
col: 1,
|
||||
},
|
||||
format!("duplicate table header `{key}`"),
|
||||
)
|
||||
.into());
|
||||
}
|
||||
}
|
||||
let n_rows = data_rows.len();
|
||||
let mut pairs = Vec::with_capacity(header.len());
|
||||
for (col, key) in header.iter().enumerate() {
|
||||
if n_rows == 1 {
|
||||
let raw = data_rows[0].get(col).map(|s| s.as_str()).unwrap_or("");
|
||||
pairs.push((key.clone(), Expr::Literal(cell_literal(raw))));
|
||||
} else {
|
||||
// 0 rows → empty lists; ≥2 rows → column lists.
|
||||
let mut col_vals = Vec::with_capacity(n_rows);
|
||||
for cells in &data_rows {
|
||||
let raw = cells.get(col).map(|s| s.as_str()).unwrap_or("");
|
||||
col_vals.push(Expr::Literal(cell_literal(raw)));
|
||||
}
|
||||
pairs.push((key.clone(), Expr::List(col_vals)));
|
||||
}
|
||||
}
|
||||
Ok(Expr::Map(pairs))
|
||||
}
|
||||
|
||||
fn parse_branch(&mut self) -> Result<Stmt> {
|
||||
@@ -899,6 +942,55 @@ fn cell_literal(raw: &str) -> Literal {
|
||||
Literal::Text(t.to_string())
|
||||
}
|
||||
|
||||
/// First-column header that opts into row-oriented records (`List` of `Map`).
|
||||
fn is_row_table_marker(header: &str) -> bool {
|
||||
matches!(header.trim(), "@" | "行" | "row")
|
||||
}
|
||||
|
||||
fn build_row_oriented_table(
|
||||
header: Vec<String>,
|
||||
header_line: u32,
|
||||
data_rows: Vec<Vec<String>>,
|
||||
) -> Result<Expr> {
|
||||
if header.len() < 2 {
|
||||
return Err(Diagnostic::new(
|
||||
None,
|
||||
Span {
|
||||
line: header_line,
|
||||
col: 1,
|
||||
},
|
||||
"row-oriented table needs field columns after `@` / `行` / `row`",
|
||||
)
|
||||
.into());
|
||||
}
|
||||
let fields: Vec<String> = header[1..].to_vec();
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
for key in &fields {
|
||||
if !seen.insert(key.clone()) {
|
||||
return Err(Diagnostic::new(
|
||||
None,
|
||||
Span {
|
||||
line: header_line,
|
||||
col: 1,
|
||||
},
|
||||
format!("duplicate table header `{key}`"),
|
||||
)
|
||||
.into());
|
||||
}
|
||||
}
|
||||
let mut rows = Vec::with_capacity(data_rows.len());
|
||||
for cells in data_rows {
|
||||
let mut pairs = Vec::with_capacity(fields.len());
|
||||
for (i, key) in fields.iter().enumerate() {
|
||||
// Skip marker column (index 0); fields start at column 1.
|
||||
let raw = cells.get(i + 1).map(|s| s.as_str()).unwrap_or("");
|
||||
pairs.push((key.clone(), Expr::Literal(cell_literal(raw))));
|
||||
}
|
||||
rows.push(Expr::Map(pairs));
|
||||
}
|
||||
Ok(Expr::List(rows))
|
||||
}
|
||||
|
||||
fn split_table_row(line: &str) -> Vec<String> {
|
||||
let t = line.trim();
|
||||
let t = t.strip_prefix('|').unwrap_or(t);
|
||||
|
||||
+9
-3
@@ -19,9 +19,9 @@ pub enum Value {
|
||||
/// Floating-point number (`type` → `num`).
|
||||
Num(f64),
|
||||
Text(String),
|
||||
/// Single-column table rows, or list of row maps for multi-column tables.
|
||||
/// Vertical (1-column) table rows, or nested list values in a column map.
|
||||
List(Vec<Value>),
|
||||
/// JSON object / string-keyed map (insertion order).
|
||||
/// String-keyed map (insertion order): JSON objects / horizontal tables.
|
||||
Map(Vec<(String, Value)>),
|
||||
/// Parsed symbolic expression (`type` → `formula`).
|
||||
Formula(FormulaExpr),
|
||||
@@ -56,7 +56,13 @@ impl Value {
|
||||
let parts: Vec<String> = xs.iter().map(|v| v.as_display()).collect();
|
||||
format!("[{}]", parts.join(", "))
|
||||
}
|
||||
Value::Map(_) => "<map>".into(),
|
||||
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),
|
||||
}
|
||||
|
||||
@@ -127,6 +127,7 @@ fn collect_input_prompts_expr(expr: &Expr, out: &mut Vec<String>) {
|
||||
collect_input_prompts_expr(e, out);
|
||||
}
|
||||
}
|
||||
Expr::Index { base, .. } => collect_input_prompts_expr(base, out),
|
||||
Expr::Literal(_) | Expr::Var(_) | Expr::Interp(_) | Expr::Formula(_) | Expr::Code(_) => {}
|
||||
}
|
||||
}
|
||||
@@ -691,6 +692,9 @@ fn expr_prec(expr: &Expr, parent_prec: u8) -> String {
|
||||
.collect();
|
||||
format!("{{{}}}", parts.join(", "))
|
||||
}
|
||||
Expr::Index { base, label } => {
|
||||
format!("{}[^{}]", expr_prec(base, 8), label)
|
||||
}
|
||||
Expr::Formula(e) => format!("$$ {} $$", e.as_display()),
|
||||
Expr::Code(c) => format!("```{} …```", c.lang),
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user