Skip to content

Commit b2f7164

Browse files
hyperpolymathclaude
andcommitted
Parser bridge, 79 parse tests, CLI parse/check commands, 6 new examples
Parser (parser.rs): - Complete Pest→AST bridge with #[derive(Parser)] - Handles all grammar rules: data, control, agents, protocols, supervisors, behaviours, functions, choreographies, locales, imports, type declarations, reversibility blocks - parse_program() public API for .007 source → Program AST Tests (parser_tests.rs): - 79 new tests (105 total with existing 29 eval tests) - Coverage: data expressions, agents, control statements, branches with given/traced/trace, protocols, supervisors, behaviours, functions (@total/@pure/@impure), choreographies (all step types), locales (all 5 constructors), imports, type declarations, reversibility, comments, error cases, example file integration CLI (main.rs): - `oo7 parse <file>` — parse and dump AST as JSON - `oo7 check <file>` — validate syntax, report declaration count - `oo7 demo` — existing demo preserved Examples: - type_system.007 — aliases, records, enums, generics - choreography.007 — comm, parallel, branch, loop, decision - locales.007 — Local, GPU, Remote, Edge, Cluster - imports.007 — import, import...as, from...import - reversibility.007 — reversible, irreversible, reverse blocks - full_language.007 — comprehensive showcase of all features Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 0fc825c commit b2f7164

10 files changed

Lines changed: 3221 additions & 5 deletions

File tree

crates/oo7-cli/src/main.rs

Lines changed: 130 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,22 @@
44
// 007 Agent Meta-Language — CLI Evaluator
55
//
66
// Usage:
7-
// oo7 eval <file.007> Evaluate a .007 file and print trace
8-
// oo7 trace <file.007> Evaluate and output decision trace as JSON
9-
// oo7 demo Run a built-in demo that produces traces
7+
// oo7 parse <file.007> Parse a .007 file and print AST as JSON
8+
// oo7 check <file.007> Validate syntax (silent on success, errors on failure)
9+
// oo7 eval <file.007> Parse and evaluate (currently: parse only)
10+
// oo7 demo Run a built-in demo that produces traces
1011

1112
use oo7_core::ast::*;
1213
use oo7_core::eval::Evaluator;
14+
use oo7_core::parser;
1315

1416
fn main() {
1517
let args: Vec<String> = std::env::args().collect();
1618

1719
match args.get(1).map(|s| s.as_str()) {
20+
Some("parse") => cmd_parse(&args),
21+
Some("check") => cmd_check(&args),
22+
Some("eval") => cmd_eval(&args),
1823
Some("demo") => run_demo(),
1924
Some("--help") | Some("-h") | None => print_usage(),
2025
Some(cmd) => {
@@ -29,12 +34,132 @@ fn print_usage() {
2934
println!("007 Agent Meta-Language — Reference Evaluator");
3035
println!();
3136
println!("Usage:");
32-
println!(" oo7 demo Run built-in demo producing decision traces");
33-
println!(" oo7 --help Show this help message");
37+
println!(" oo7 parse <file.007> Parse a .007 file and print AST as JSON");
38+
println!(" oo7 check <file.007> Validate syntax (no output on success)");
39+
println!(" oo7 eval <file.007> Parse and evaluate (parse-only for now)");
40+
println!(" oo7 demo Run built-in demo producing decision traces");
41+
println!(" oo7 --help Show this help message");
3442
println!();
3543
println!("The telescope is on. We're looking.");
3644
}
3745

46+
/// Read a source file and return its contents, or exit with an error.
47+
fn read_source_file(args: &[String]) -> String {
48+
let path = match args.get(2) {
49+
Some(p) => p,
50+
None => {
51+
eprintln!("Error: missing file argument.");
52+
eprintln!("Usage: oo7 {} <file.007>", args.get(1).unwrap());
53+
std::process::exit(1);
54+
}
55+
};
56+
57+
match std::fs::read_to_string(path) {
58+
Ok(src) => src,
59+
Err(e) => {
60+
eprintln!("Error: could not read '{}': {}", path, e);
61+
std::process::exit(1);
62+
}
63+
}
64+
}
65+
66+
/// Format a parse error with nice line/column information.
67+
fn format_parse_error(path: &str, err: &parser::ParseError) -> String {
68+
// pest errors already include line/column, but we add the file path
69+
format!("Parse error in '{}':\n{}", path, err)
70+
}
71+
72+
/// `oo7 parse <file.007>` — parse and dump AST as JSON.
73+
fn cmd_parse(args: &[String]) {
74+
let source = read_source_file(args);
75+
let path = args.get(2).unwrap();
76+
77+
match parser::parse_program(&source) {
78+
Ok(program) => {
79+
let json = serde_json::to_string_pretty(&program).unwrap();
80+
println!("{}", json);
81+
}
82+
Err(e) => {
83+
eprintln!("{}", format_parse_error(path, &e));
84+
std::process::exit(1);
85+
}
86+
}
87+
}
88+
89+
/// `oo7 check <file.007>` — validate syntax, print declaration count on success.
90+
fn cmd_check(args: &[String]) {
91+
let source = read_source_file(args);
92+
let path = args.get(2).unwrap();
93+
94+
match parser::parse_program(&source) {
95+
Ok(program) => {
96+
println!("OK: {} declaration(s)", program.declarations.len());
97+
}
98+
Err(e) => {
99+
eprintln!("{}", format_parse_error(path, &e));
100+
std::process::exit(1);
101+
}
102+
}
103+
}
104+
105+
/// `oo7 eval <file.007>` — parse and evaluate.
106+
///
107+
/// Currently this only parses and reports the structure, since the
108+
/// evaluator does not yet handle all constructs (it was built for
109+
/// the branch/trace system). Full evaluation is future work.
110+
fn cmd_eval(args: &[String]) {
111+
let source = read_source_file(args);
112+
let path = args.get(2).unwrap();
113+
114+
match parser::parse_program(&source) {
115+
Ok(program) => {
116+
println!("007 Evaluator — Parsed '{}' successfully", path);
117+
println!(" {} top-level declaration(s)", program.declarations.len());
118+
for decl in &program.declarations {
119+
match decl {
120+
TopLevelDecl::DataBinding(d) => {
121+
println!(" - @total data {}", d.name);
122+
}
123+
TopLevelDecl::Agent(a) => {
124+
println!(" - agent {}", a.name);
125+
}
126+
TopLevelDecl::Supervisor(s) => {
127+
println!(" - supervisor {}", s.name);
128+
}
129+
TopLevelDecl::Protocol(p) => {
130+
println!(" - session protocol {}", p.name);
131+
}
132+
TopLevelDecl::Behaviour(b) => {
133+
println!(" - behaviour {}", b.name);
134+
}
135+
TopLevelDecl::Function(f) => {
136+
println!(" - fn {} ({:?})", f.name, f.purity);
137+
}
138+
TopLevelDecl::Choreography(c) => {
139+
println!(" - choreography {}", c.name);
140+
}
141+
TopLevelDecl::Locale(l) => {
142+
println!(" - locale {}", l.name);
143+
}
144+
TopLevelDecl::Import(i) => {
145+
println!(" - import {}", i.path.join("."));
146+
}
147+
TopLevelDecl::TypeDecl(t) => {
148+
println!(" - type {}", t.name);
149+
}
150+
}
151+
}
152+
println!();
153+
println!("Note: full evaluation not yet implemented for all constructs.");
154+
println!("The telescope is on. We're looking.");
155+
}
156+
Err(e) => {
157+
eprintln!("{}", format_parse_error(path, &e));
158+
std::process::exit(1);
159+
}
160+
}
161+
}
162+
38163
/// Run a built-in demo that exercises the branch/given/traced system
39164
/// and produces decision trace output.
40165
fn run_demo() {

crates/oo7-core/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ pub mod ast;
1515
pub mod eval;
1616
#[cfg(test)]
1717
mod eval_tests;
18+
pub mod parser;
19+
#[cfg(test)]
20+
mod parser_tests;
1821
pub mod trace;
1922

2023
// Re-export key types for convenience.
2124
pub use ast::*;
2225
pub use eval::{BranchStrategy, Evaluator, PredicateFirstStrategy, RtValue};
26+
pub use parser::{parse_program, ParseError};
2327
pub use trace::{DecisionTrace, TraceRecord};

0 commit comments

Comments
 (0)