Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format code using 'cargo fmt' #17

Merged
merged 1 commit into from
Jun 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
483 changes: 300 additions & 183 deletions src/alpha.rs

Large diffs are not rendered by default.

97 changes: 47 additions & 50 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

#![macro_use]

use util::mbe::EnvMBE;
use name::*;
use beta::{Beta, ExportBeta};
use std::iter;
use name::*;
use std::fmt;
use std::iter;
use util::mbe::EnvMBE;

// TODO: This really ought to be an `Rc` around an `enum`
custom_derive! {
Expand Down Expand Up @@ -45,24 +45,26 @@ pub use self::Ast::*;
impl fmt::Debug for Ast {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Trivial => { write!(f, "⨉") },
Atom(ref n) => { write!(f, "∘{:#?}∘", n) },
VariableReference(ref v) => { write!(f, "{:#?}", v) }
Trivial => write!(f, "⨉"),
Atom(ref n) => write!(f, "∘{:#?}∘", n),
VariableReference(ref v) => write!(f, "{:#?}", v),
Shape(ref v) => {
write!(f, "(")?;
let mut first = true;
for elt in v {
if !first { write!(f, " ")? }
if !first {
write!(f, " ")?
}
elt.fmt(f)?;
first = false;
}
write!(f, ")")
},
}
Node(ref form, ref body, ref export) => {
write!(f, "{{ ({}); {:#?}", form.name.sp(), body)?;
match *export {
::beta::ExportBeta::Nothing => {}
_ => write!(f, " ⇑{:#?}", export)?
_ => write!(f, " ⇑{:#?}", export)?,
}
write!(f, "}}")
}
Expand All @@ -73,15 +75,9 @@ impl fmt::Debug for Ast {
write!(f, "neg``{:#?}``", body)
}
}
QuoteLess(ref body, depth) => {
write!(f, ",,({}){:#?},,", depth, body)
}
IncompleteNode(ref body) => {
write!(f, "{{ INCOMPLETE; {:#?} }}", body)
}
ExtendEnv(ref body, ref beta) => {
write!(f, "{:#?}↓{:#?}", body, beta)
}
QuoteLess(ref body, depth) => write!(f, ",,({}){:#?},,", depth, body),
IncompleteNode(ref body) => write!(f, "{{ INCOMPLETE; {:#?} }}", body),
ExtendEnv(ref body, ref beta) => write!(f, "{:#?}↓{:#?}", body, beta),
}
}
}
Expand All @@ -90,20 +86,23 @@ impl fmt::Debug for Ast {
impl fmt::Display for Ast {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Atom(ref n) => { write!(f, "{}", n.print()) },
VariableReference(ref v) => { write!(f, "{}", v.print()) }
Atom(ref n) => write!(f, "{}", n.print()),
VariableReference(ref v) => write!(f, "{}", v.print()),
Node(ref form, ref body, _) => {
let s = ::unparse::unparse_mbe(
&form.grammar, self, body, &::core_forms::get_core_forms());
&form.grammar,
self,
body,
&::core_forms::get_core_forms(),
);
write!(f, "{}", s)
}
ExtendEnv(ref body, _) => { write!(f, "{}↓", body) }
_ => write!(f, "{:#?}", self)
ExtendEnv(ref body, _) => write!(f, "{}↓", body),
_ => write!(f, "{:#?}", self),
}
}
}


impl Ast {
// TODO: this ought to at least warn if we're losing anything other than `Shape`
pub fn flatten(&self) -> EnvMBE<Ast> {
Expand All @@ -116,22 +115,24 @@ impl Ast {
accum = accum.combine_overriding(&sub_a.flatten())
}
accum
},
IncompleteNode(ref env) => { env.clone() }
}
IncompleteNode(ref env) => env.clone(),
Node(ref _f, ref _body, ref _export) => {
// TODO: think about what should happen when
// `Scope` contains a `Scope` without an intervening `Named`
panic!("I don't know what to do here!")
},
}
QuoteMore(ref body, _) => body.flatten(),
QuoteLess(ref body, _) => body.flatten(),
ExtendEnv(ref body, _) => body.flatten()
ExtendEnv(ref body, _) => body.flatten(),
}
}

// TODO: use this more
pub fn destructure(&self, expd_form: ::std::rc::Rc<::form::Form>)
-> Option<::util::mbe::EnvMBE<Ast>> {
pub fn destructure(
&self,
expd_form: ::std::rc::Rc<::form::Form>,
) -> Option<::util::mbe::EnvMBE<Ast>> {
match self {
Node(ref f, ref parts, _) => {
if f == &expd_form {
Expand All @@ -141,35 +142,32 @@ impl Ast {
_ => {}
}
None

}


// TODO: I think we have a lot of places where we ought to use this function:
pub fn node_parts(&self) -> &EnvMBE<Ast> {
match *self {
Node(_, ref body, _) => body,
_ => panic!("ICE")
_ => panic!("ICE"),
}
}
pub fn node_form(&self) -> &::form::Form {
match *self {
Node(ref form, _, _) => form,
_ => panic!("ICE")
_ => panic!("ICE"),
}
}
}

// This is used by combine::many, which is used by the Star parser
impl iter::FromIterator<Ast> for Ast {
fn from_iter<I: IntoIterator<Item=Ast>>(i: I) -> Self {
IncompleteNode(
EnvMBE::new_from_anon_repeat(
i.into_iter().map(|a| a.flatten()).collect()))
fn from_iter<I: IntoIterator<Item = Ast>>(i: I) -> Self {
IncompleteNode(EnvMBE::new_from_anon_repeat(
i.into_iter().map(|a| a.flatten()).collect(),
))
}
}


/*
* This is also sort of a test of MBE, since we need `Ast`s to make them with the macros
*
Expand All @@ -183,38 +181,37 @@ impl iter::FromIterator<Ast> for Ast {
fn combine_from_kleene_star() {
use std::iter::FromIterator;

let parse_parts = vec![ast!({ - "b" => "8.0"}),
ast!({ - "a" => ["1", "2"], "b" => "8.1"}),
ast!({ - "a" => ["1", "2", "3"], "b" => "8.2"})];
let parse_parts = vec![
ast!({ - "b" => "8.0"}),
ast!({ - "a" => ["1", "2"], "b" => "8.1"}),
ast!({ - "a" => ["1", "2", "3"], "b" => "8.2"}),
];
let parsed = Ast::from_iter(parse_parts);

let mut expected_mbe =
mbe!("a" => [@"triple" [], ["1", "2"], ["1", "2", "3"]],
let mut expected_mbe = mbe!("a" => [@"triple" [], ["1", "2"], ["1", "2", "3"]],
"b" => [@"triple" "8.0", "8.1", "8.2"]);
expected_mbe.anonimize_repeat(n("triple"));

assert_eq!(parsed, IncompleteNode(expected_mbe));
}


#[test]
fn star_construction() {
let env = mbe!( "a" => ["1", "2"]);

assert_eq!(
ast!( { - "x" => [* env =>("a") env : (, env.get_leaf_or_panic(&n("a")).clone())]} ),
ast!( { - "x" => ["1", "2"] }));


ast!( { - "x" => ["1", "2"] })
);

let env = mbe!( "a" => [@"duo" "1", "2"], "b" => [@"duo" "11", "22"]);

assert_eq!(
ast!( { - "x" => [* env =>("a", "b") env :
((, env.get_leaf_or_panic(&n("b")).clone())
(, env.get_leaf_or_panic(&n("a")).clone()))]} ),
ast!( { - "x" => [("11" "1"), ("22" "2")] }));

ast!( { - "x" => [("11" "1"), ("22" "2")] })
);
}

#[test]
Expand Down
Loading