Add bilingual public docs, keyword aliases, and frontmatter stdin for view.

English/Chinese user pages sit side by side; L0 builtins and lib/ text|表 dual APIs land with gold coverage. Capture/view input demos use stdin:/输入: so static export and first page load succeed.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-08-05 09:14:26 +08:00
1 parent fd36b92224
commit a5d30c00b3
68 files changed
+1812 -241

No files matched your search

+78 -14
View File
@@ -120,6 +120,40 @@ impl<'a> FnCompiler<'a> {
self.chunk.spans.push(self.stmt_span);
}
fn named_or_first<'b>(&self, call: &'b CallExpr, name: &str) -> Option<&'b Expr> {
call.args
.iter()
.find_map(|a| match a {
Arg::Named { name: n, value } if n == name => Some(value),
_ => None,
})
.or_else(|| {
call.args.iter().find_map(|a| match a {
Arg::Positional(e) => Some(e),
_ => None,
})
})
}
fn named_or_pos<'b>(&self, call: &'b CallExpr, name: &str, pos: usize) -> Option<&'b Expr> {
if let Some(e) = call.args.iter().find_map(|a| match a {
Arg::Named { name: n, value } if n == name => Some(value),
_ => None,
}) {
return Some(e);
}
let mut i = 0usize;
for a in &call.args {
if let Arg::Positional(e) = a {
if i == pos {
return Some(e);
}
i += 1;
}
}
None
}
fn here(&self) -> u16 {
self.chunk.code.len() as u16
}
@@ -286,6 +320,10 @@ impl<'a> FnCompiler<'a> {
}
fn compile_call(&mut self, call: &CallExpr, as_stmt: bool) -> Result<()> {
let mut call = call.clone();
call.callee =
crate::aliases::normalize_call_callee_and_args(&call.callee, &mut call.args);
match call.callee.as_str() {
"print" => {
let text_expr = call
@@ -335,27 +373,18 @@ impl<'a> FnCompiler<'a> {
}
return Ok(());
}
"len" | "str" | "int" => {
"len" | "str" | "int" | "type" | "trim" => {
let name = call.callee.as_str();
let value_expr = call
.args
.iter()
.find_map(|a| match a {
Arg::Named { name, value } if name == "value" => Some(value),
_ => None,
})
.or_else(|| {
call.args.iter().find_map(|a| match a {
Arg::Positional(e) => Some(e),
_ => None,
})
})
let value_expr = self
.named_or_first(&call, "value")
.ok_or_else(|| self.err(format!("{name} requires value")))?;
self.compile_expr(value_expr)?;
self.emit(match name {
"len" => Op::Len,
"str" => Op::Str,
"int" => Op::Int,
"type" => Op::TypeOf,
"trim" => Op::Trim,
_ => unreachable!(),
});
if as_stmt {
@@ -363,6 +392,41 @@ impl<'a> FnCompiler<'a> {
}
return Ok(());
}
"split" | "join" => {
let name = call.callee.as_str();
let value_expr = self
.named_or_pos(&call, "value", 0)
.ok_or_else(|| self.err(format!("{name} requires value")))?;
let sep_expr = self
.named_or_pos(&call, "sep", 1)
.ok_or_else(|| self.err(format!("{name} requires sep")))?;
self.compile_expr(value_expr)?;
self.compile_expr(sep_expr)?;
self.emit(match name {
"split" => Op::Split,
"join" => Op::Join,
_ => unreachable!(),
});
if as_stmt {
self.emit(Op::Pop);
}
return Ok(());
}
"at" => {
let value_expr = self
.named_or_pos(&call, "value", 0)
.ok_or_else(|| self.err("at requires value"))?;
let index_expr = self
.named_or_pos(&call, "index", 1)
.ok_or_else(|| self.err("at requires index"))?;
self.compile_expr(value_expr)?;
self.compile_expr(index_expr)?;
self.emit(Op::GetIndex);
if as_stmt {
self.emit(Op::Pop);
}
return Ok(());
}
_ => {}
}
+8
View File
@@ -43,6 +43,14 @@ pub enum Op {
Str,
/// Pop value → push int (or fail).
Int,
/// Pop value → push type name text.
TypeOf,
/// Pop text → push trimmed text.
Trim,
/// Pop sep, then value → push list of text parts.
Split,
/// Pop sep, then list → push joined text.
Join,
GetIndex,
/// Call function by index; argc values are on the stack (param order).
Call(u16, u8),
+26 -10
View File
@@ -4,7 +4,10 @@ use anyhow::Result;
use std::io::Write;
use std::path::{Path, PathBuf};
use crate::builtin::{builtin_int, builtin_len, builtin_str};
use crate::builtin::{
builtin_at, builtin_int, builtin_join, builtin_len, builtin_split, builtin_str, builtin_trim,
builtin_type,
};
use crate::bytecode::{Op, Program};
use crate::debug::emit_trace;
use crate::diagnostics::{bail_at, Span};
@@ -218,8 +221,9 @@ impl Vm {
let text = prompt.as_display();
self.emit_prompt(&text);
let line = self.input.read_line().map_err(|e| {
if e.to_string().contains("input is not available") {
self.err_at(span, "input is not available under capture / view")
let msg = e.to_string();
if msg.contains("input needs a line") {
self.err_at(span, msg)
} else {
e
}
@@ -249,16 +253,28 @@ impl Vm {
let n = builtin_int(&v).map_err(|m| self.err_at(span, m))?;
stack.push(Value::Int(n));
}
Op::TypeOf => {
let v = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
stack.push(builtin_type(&v));
}
Op::Trim => {
let v = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
stack.push(builtin_trim(&v).map_err(|m| self.err_at(span, m))?);
}
Op::Split => {
let sep = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
let value = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
stack.push(builtin_split(&value, &sep).map_err(|m| self.err_at(span, m))?);
}
Op::Join => {
let sep = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
let value = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
stack.push(builtin_join(&value, &sep).map_err(|m| self.err_at(span, m))?);
}
Op::GetIndex => {
let idx = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
let list = pop(&mut stack).map_err(|m| self.err_at(span, m))?;
match (list, idx) {
(Value::List(xs), Value::Int(i)) if i >= 0 && (i as usize) < xs.len() => {
stack.push(xs[i as usize].clone());
}
(Value::List(_), Value::Int(_)) => stack.push(Value::None),
_ => return Err(self.err_at(span, "GetIndex needs list and int")),
}
stack.push(builtin_at(&list, &idx).map_err(|m| self.err_at(span, m))?);
}
Op::Call(fid, argc) => {
let fid = fid as usize;