Add view, positional args, errors gold, and bytecode backend.
Reorganize examples into structure/keywords/errors, launch marqdo view for AST-backed browsing, and ship an M5 stack VM selectable via --backend. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
a29bf6e97b
commit
ac0a3fbced
48 files changed
+3007
-202
No files matched your search
+85
-10
@@ -1,8 +1,11 @@
|
||||
//! Marqdo reference interpreter (Phase I).
|
||||
//! Marqdo reference interpreter (Phase I + M5 bytecode prototype).
|
||||
//!
|
||||
//! Pipeline: load → line classify → parse → tree-walk eval.
|
||||
//! Pipeline: load → line classify → parse → tree-walk eval | bytecode VM.
|
||||
//! CLI also offers `view` for AST-backed browsing.
|
||||
|
||||
pub mod ast;
|
||||
pub mod bytecode;
|
||||
pub mod capture;
|
||||
pub mod debug;
|
||||
pub mod diagnostics;
|
||||
pub mod interp;
|
||||
@@ -10,16 +13,36 @@ pub mod lex;
|
||||
pub mod load;
|
||||
pub mod parse;
|
||||
pub mod value;
|
||||
pub mod view;
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
|
||||
use crate::ast::format_ast_dump;
|
||||
use crate::bytecode::{compile_module, Vm};
|
||||
use crate::capture::RunCapture;
|
||||
use crate::interp::Interpreter;
|
||||
use crate::lex::{classify_source, format_lines_dump};
|
||||
use crate::load::load_module;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
pub enum Backend {
|
||||
#[default]
|
||||
Tree,
|
||||
Bytecode,
|
||||
}
|
||||
|
||||
impl Backend {
|
||||
pub fn parse(s: &str) -> Result<Self> {
|
||||
match s {
|
||||
"tree" => Ok(Self::Tree),
|
||||
"bytecode" | "bc" => Ok(Self::Bytecode),
|
||||
other => bail!("unknown backend `{other}` (expected tree|bytecode)"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Options for a single `run` / dump invocation.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct RunOptions {
|
||||
@@ -27,7 +50,9 @@ pub struct RunOptions {
|
||||
pub dump_tokens: bool,
|
||||
pub dump_ast: bool,
|
||||
pub dump_sema: bool,
|
||||
pub dump_bytecode: bool,
|
||||
pub trace_eval: bool,
|
||||
pub backend: Backend,
|
||||
}
|
||||
|
||||
impl RunOptions {
|
||||
@@ -37,16 +62,23 @@ impl RunOptions {
|
||||
dump_tokens: true,
|
||||
dump_ast: true,
|
||||
dump_sema: true,
|
||||
dump_bytecode: true,
|
||||
trace_eval: true,
|
||||
backend: Backend::Tree,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn any_dump(&self) -> bool {
|
||||
self.dump_lines || self.dump_tokens || self.dump_ast || self.dump_sema || self.trace_eval
|
||||
self.dump_lines
|
||||
|| self.dump_tokens
|
||||
|| self.dump_ast
|
||||
|| self.dump_sema
|
||||
|| self.dump_bytecode
|
||||
|| self.trace_eval
|
||||
}
|
||||
}
|
||||
|
||||
/// Run a `.mq.md` program.
|
||||
/// Run a `.mq.md` program (prints to real stdout).
|
||||
pub fn run_file(path: &Path, opts: &RunOptions) -> Result<i32> {
|
||||
let source = std::fs::read_to_string(path)
|
||||
.map_err(|e| anyhow::anyhow!("failed to read {}: {e}", path.display()))?;
|
||||
@@ -86,13 +118,56 @@ pub fn run_file(path: &Path, opts: &RunOptions) -> Result<i32> {
|
||||
println!("=== marqdo: end sema ===");
|
||||
}
|
||||
|
||||
let mut interp = Interpreter::new(opts.trace_eval);
|
||||
if opts.trace_eval {
|
||||
eprintln!("=== marqdo: trace-eval ({path_label}) ===");
|
||||
match opts.backend {
|
||||
Backend::Tree => {
|
||||
let mut interp = Interpreter::new(Some(path), opts.trace_eval);
|
||||
if opts.trace_eval {
|
||||
eprintln!("=== marqdo: trace-eval ({path_label}) ===");
|
||||
}
|
||||
interp.run_module(&module)?;
|
||||
if opts.trace_eval {
|
||||
eprintln!("=== marqdo: end trace-eval ===");
|
||||
}
|
||||
}
|
||||
Backend::Bytecode => {
|
||||
let program = compile_module(&module)?;
|
||||
if opts.dump_bytecode {
|
||||
print!("{}", program.disassemble());
|
||||
}
|
||||
let mut vm = Vm::new();
|
||||
vm.run(&program)?;
|
||||
}
|
||||
}
|
||||
interp.run_module(&module)?;
|
||||
if opts.trace_eval {
|
||||
eprintln!("=== marqdo: end trace-eval ===");
|
||||
|
||||
// dump bytecode even on tree backend if requested
|
||||
if opts.backend == Backend::Tree && opts.dump_bytecode {
|
||||
let program = compile_module(&module)?;
|
||||
print!("{}", program.disassemble());
|
||||
}
|
||||
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
/// Run and capture stdout (used by `view` and tests).
|
||||
pub fn run_file_capture(path: &Path, opts: &RunOptions) -> Result<RunCapture> {
|
||||
let module = load_module(path)?;
|
||||
match opts.backend {
|
||||
Backend::Tree => {
|
||||
let mut interp = Interpreter::with_capture(Some(path), false);
|
||||
let value = interp.run_module(&module)?;
|
||||
Ok(RunCapture {
|
||||
stdout: interp.captured_stdout,
|
||||
value,
|
||||
})
|
||||
}
|
||||
Backend::Bytecode => {
|
||||
let program = compile_module(&module)?;
|
||||
let mut vm = Vm::with_capture();
|
||||
let value = vm.run(&program)?;
|
||||
Ok(RunCapture {
|
||||
stdout: vm.captured_stdout,
|
||||
value,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user