Skip to content

Commit

Permalink
Have the REPL debug-print or pretty-print separately.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulstansifer committed Dec 26, 2019
1 parent 2a83235 commit 986ef9a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
12 changes: 6 additions & 6 deletions src/grammar.rs
Expand Up @@ -340,12 +340,12 @@ fn advanced_parsing() {
parse(
&form_pat!((call "Expr")),
&assoc_n!(
"other_1" => Rc::new(Scope(simple_form("o", form_pat!((lit_aat "other"))),
crate::beta::ExportBeta::Nothing)),
"Expr" => Rc::new(Scope(pair_form.clone(), crate::beta::ExportBeta::Nothing)),
"other_2" =>
Rc::new(Scope(simple_form("o", form_pat!((lit_aat "otherother"))),
crate::beta::ExportBeta::Nothing))),
"other_1" => Rc::new(Scope(simple_form("o", form_pat!((lit_aat "other"))),
crate::beta::ExportBeta::Nothing)),
"Expr" => Rc::new(Scope(pair_form.clone(), crate::beta::ExportBeta::Nothing)),
"other_2" =>
Rc::new(Scope(simple_form("o", form_pat!((lit_aat "otherother"))),
crate::beta::ExportBeta::Nothing))),
crate::earley::empty__code_envs(),
&toks_a_b
)
Expand Down
17 changes: 12 additions & 5 deletions src/main.rs
Expand Up @@ -20,7 +20,6 @@ extern crate custom_derive;
#[macro_use]
extern crate quote;


use std::{fs::File, io::Read, path::Path};

mod macros;
Expand Down Expand Up @@ -144,6 +143,7 @@ fn main() {
rl.set_helper(Some(LineHelper::new()));

let just_parse = regex::Regex::new("^:p (.*)$").unwrap();
let just_parse_debug_print = regex::Regex::new("^:pd (.*)$").unwrap();

let just_type = regex::Regex::new("^:t (.*)$").unwrap();
let just_eval = regex::Regex::new("^:e (.*)$").unwrap();
Expand All @@ -166,7 +166,8 @@ fn main() {
println!(" `<name> t= <type>` to bind a type for this session.");
println!(" `:s <name> := <expr>` to save a binding to the prelude for the future.");
println!(" `:s <name> t= <expr>` to save a type binding to the prelude.");
println!(" `:p <expr>` to parse `<expr>` and print its debug AST output.");
println!(" `:p <expr>` to parse `<expr>` and pretty-print its AST output.");
println!(" `:pd <expr>` to parse `<expr>` and debug-print its AST output.");
println!(" Command history is saved over sessions.");
println!(" Tab-completion works on variables, and lots of Bash-isms work.");
println!();
Expand Down Expand Up @@ -196,7 +197,9 @@ fn main() {
rl.add_history_entry(line.clone());

let result_display = if let Some(caps) = just_parse.captures(&line) {
parse_unseemly_program(&caps[1])
parse_unseemly_program(&caps[1], true)
} else if let Some(caps) = just_parse_debug_print.captures(&line) {
parse_unseemly_program(&caps[1], false)
} else if let Some(caps) = just_type.captures(&line) {
type_unseemly_program(&caps[1]).map(|x| format!("{}", x))
} else if let Some(caps) = just_eval.captures(&line) {
Expand Down Expand Up @@ -316,7 +319,7 @@ fn canonicalize_type(t: &str) -> Result<ty::Ty, String> {
ty_env.with(|tys| ty::synth_type(&ast, tys.borrow().clone()).map_err(|e| format!("{:#?}", e)))
}

fn parse_unseemly_program(program: &str) -> Result<String, String> {
fn parse_unseemly_program(program: &str, pretty: bool) -> Result<String, String> {
let ast = grammar::parse(
&core_forms::outermost_form(),
&core_forms::get_core_forms(),
Expand All @@ -325,7 +328,11 @@ fn parse_unseemly_program(program: &str) -> Result<String, String> {
)
.map_err(|e| e.msg)?;

Ok(format!("▵ {:#?}\n∴ {}\n", ast, ast))
if pretty {
Ok(format!("{}", ast))
} else {
Ok(format!("{:#?}", ast))
}
}

fn type_unseemly_program(program: &str) -> Result<ty::Ty, String> {
Expand Down
10 changes: 2 additions & 8 deletions src/unparse.rs
Expand Up @@ -146,17 +146,11 @@ pub fn unparse_mbe(pat: &FormPat, actl: &Ast, context: &EnvMBE<Ast>, s: &SynEnv)
unparse_mbe(&*body, &*actl_body, context, s)
}
(&QuoteEscape(_, _), _) => format!("[Missing ql]{:#?}", actl),
(&SynImport(ref _fp, ref _n, ref _se), &Node(_, ref _body, _)) => {
// TODO: I think we need to store the LHS in the AST somehow for this to work.
// (*se.0)(se, )
// format!("{} {}",
// unparse_mbe(fp, ????, context, s))
// unparse_mbe(pat: &FormPat, actl: &Ast, context: &EnvMBE<Ast>, s: &SynEnv)
(&SynImport(ref _lhs_grammar, ref rhs, _), &Node(_, ref body, _)) => {
// TODO: I think we need to store the LHS or the new SynEnv to make this pretty.
format!("?synax import? {:#?} ?si?", actl)
}
(&SynImport(_, _, _), _) => "".to_string(),
(&Reserved(ref body, _), _) => unparse_mbe(body, actl, context, s),
}
}

// pub fn unparse_mbe(pat: &FormPat, actl: &Ast, context: &EnvMBE<Ast>, s: &SynEnv) -> String {

0 comments on commit 986ef9a

Please sign in to comment.