release: v2.2.0 call one function in any .mq.md

The same page is the document, the knowledge and the program. marqdo call runs that function with arguments, so reuse, tools and tests do not need a second skill system.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-10-30 09:07:23 +08:00
1 parent 4018e9928a
commit 1730f26fc7
42 files changed
+1917 -116

No files matched your search

+21 -3
View File
@@ -33,6 +33,16 @@ struct FlatFun {
}
pub fn compile_module(path: Option<&Path>, module: &Module) -> Result<Program> {
compile_with(path, module, true)
}
/// Compile every function even when the file has no single program entry.
/// `Program::entry` is then only a placeholder; callers must use [`Vm::call_function`].
pub fn compile_for_call(path: Option<&Path>, module: &Module) -> Result<Program> {
compile_with(path, module, false)
}
fn compile_with(path: Option<&Path>, module: &Module, require_entry: bool) -> Result<Program> {
let mut flat = Vec::new();
let mut next_scope = 1u32;
let _exports = collect_module_tree(module, &mut flat, 0, &mut next_scope);
@@ -40,7 +50,7 @@ pub fn compile_module(path: Option<&Path>, module: &Module) -> Result<Program> {
bail!("bytecode: no functions to compile");
}
let entry = flat
let chosen = flat
.iter()
.position(|f| f.name == "main" && f.parent.is_none())
.or_else(|| {
@@ -55,8 +65,14 @@ pub fn compile_module(path: Option<&Path>, module: &Module) -> Result<Program> {
} else {
None
}
})
.ok_or_else(|| anyhow::anyhow!("bytecode: no `# main` object"))?;
});
let entry = match chosen {
Some(index) => index,
None if !require_entry => 0,
None => bail!("bytecode: no `# main` object"),
};
// Imports are compiled first, so a missing entry must take paths from the file's own functions.
let path_owner = if chosen.is_some() { entry } else { flat.len() - 1 };
let mut objects = Vec::new();
let mut methods = Vec::new();
@@ -95,6 +111,7 @@ pub fn compile_module(path: Option<&Path>, module: &Module) -> Result<Program> {
let parents: Vec<Option<usize>> = flat.iter().map(|f| f.parent).collect();
let children: Vec<Vec<usize>> = flat.iter().map(|f| f.children.clone()).collect();
let path_index = flat[path_owner].path_index.clone();
Ok(Program {
functions,
@@ -104,6 +121,7 @@ pub fn compile_module(path: Option<&Path>, module: &Module) -> Result<Program> {
methods,
parents,
children,
path_index,
})
}
+5 -1
View File
@@ -3,9 +3,11 @@
mod compile;
mod vm;
pub use compile::compile_module;
pub use compile::{compile_for_call, compile_module};
pub use vm::Vm;
use std::collections::HashMap;
use crate::diagnostics::Span;
use crate::value::Value;
@@ -100,6 +102,8 @@ pub struct Program {
pub parents: Vec<Option<usize>>,
/// Parallel to `functions`: child fn indices.
pub children: Vec<Vec<usize>>,
/// Dotted path (`计算器.加法`, `inc.加一`) → function index. Entry module exports plus imports.
pub path_index: HashMap<Vec<String>, usize>,
}
impl Program {
+42 -4
View File
@@ -102,7 +102,6 @@ impl Vm {
}
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 {
@@ -113,14 +112,53 @@ impl Vm {
.unwrap_or(Span::new(1, 1));
return Err(self.err_at(span, "bytecode: entry function needs arguments"));
}
self.execute(program, entry, &[])
}
/// Run `fn_idx` with named arguments already bound to local slots.
/// Does not fall back to [`Program::entry`].
pub fn call_function(
&mut self,
program: &Program,
fn_idx: usize,
args: &[(String, Value)],
) -> Result<Value> {
if fn_idx >= program.functions.len() {
return Err(self.err_at(
Span::new(1, 1),
format!("bytecode: function index {fn_idx} is out of range"),
));
}
self.execute(program, fn_idx, args)
}
fn execute(
&mut self,
program: &Program,
fn_idx: usize,
args: &[(String, Value)],
) -> Result<Value> {
let mut stack: Vec<Value> = Vec::new();
let mut slots = vec![Value::None; program.functions[fn_idx].locals.len().max(1)];
for (name, value) in args {
if let Some(i) = program.functions[fn_idx]
.locals
.iter()
.position(|local| local == name)
{
if i < slots.len() {
slots[i] = value.clone();
}
}
}
let mut frames = vec![Frame {
fn_idx: entry,
fn_idx,
ip: 0,
slots: vec![Value::None; program.functions[entry].locals.len().max(1)],
slots,
retag_matrix: None,
}];
self.host
.push_call_frame(&program.functions[entry].name.clone());
.push_call_frame(&program.functions[fn_idx].name.clone());
loop {
let frame = frames.last_mut().unwrap();