Commit 0d6dd42c authored by Jeff Smits's avatar Jeff Smits

cargo fmt with new 0.9 version

parent 5b283330
......@@ -28,7 +28,8 @@ fn gen_primitives() {
("libstrc", r#"("libstrc/libstrc.ctree", None)"#),//Some(&STRC))"#)
];
let ssl = [// INT
let ssl = [
// INT
"is_int",
"int",
"iori",
......@@ -153,20 +154,23 @@ fn gen_primitives() {
"getPlaceholder",
"preserve_annotations_attachments",
// STACKTRACE
"stacktrace_get_all_frame_names"];
"stacktrace_get_all_frame_names",
];
// let jsglr = [];
// let jsglr = [];
// let strc = [];
// let strc = [];
writeln!(&mut file,
"pub type Primitives = phf::Map<&'static str, (Libs, usize)>;")
.unwrap();
writeln!(
&mut file,
"pub type Primitives = phf::Map<&'static str, (Libs, usize)>;"
).unwrap();
writeln!(&mut file,
writeln!(
&mut file,
"pub static LIBS: phf::Map<&'static str, \
(&'static str, Option<&'static Primitives>)> =")
.unwrap();
(&'static str, Option<&'static Primitives>)> ="
).unwrap();
let mut builder = phf_codegen::Map::new();
for &(name, pair) in &libs {
builder.entry(name, pair);
......@@ -179,17 +183,21 @@ fn gen_primitives() {
.unwrap();
}
fn write_primitive_libs(file: &mut BufWriter<File>,
libs: &[(&'static str, &[&'static str])])
-> ::std::io::Result<()> {
writeln!(file,
"#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum Libs {{")?;
fn write_primitive_libs(
file: &mut BufWriter<File>,
libs: &[(&'static str, &[&'static str])],
) -> ::std::io::Result<()> {
writeln!(
file,
"#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum Libs {{"
)?;
for &(name, _) in libs {
write!(file, "{},", name.to_ascii_uppercase())?;
}
writeln!(file, "}}")?;
writeln!(file,
writeln!(
file,
"\
pub fn eval_prim_ref<'a>(prim_ref: (Libs, usize),
context: &MutContext<'a>,
......@@ -199,20 +207,25 @@ pub fn eval_prim_ref<'a>(prim_ref: (Libs, usize),
-> Result<ATermRef>
{{
match prim_ref.0 {{\
")?;
"
)?;
for &(name, _) in libs {
write!(file,
write!(
file,
"Libs::{} => {}(prim_ref.1, context, sargs, targs, current),",
name.to_ascii_uppercase(),
name)?;
name
)?;
}
writeln!(file,
writeln!(
file,
"\
}}
}}
\
")?;
"
)?;
for &(name, lib) in libs {
write_primitives(file, lib, name)?;
......@@ -221,13 +234,16 @@ pub fn eval_prim_ref<'a>(prim_ref: (Libs, usize),
Ok(())
}
fn write_primitives(file: &mut BufWriter<File>,
fn write_primitives(
file: &mut BufWriter<File>,
names: &[&'static str],
lib_name: &'static str)
-> ::std::io::Result<()> {
write!(file,
lib_name: &'static str,
) -> ::std::io::Result<()> {
write!(
file,
"static {}: Primitives =\n",
lib_name.to_ascii_uppercase())?;
lib_name.to_ascii_uppercase()
)?;
{
let true_names: Vec<String> = names
.iter()
......@@ -237,14 +253,17 @@ fn write_primitives(file: &mut BufWriter<File>,
let mut builder = phf_codegen::Map::<&str>::new();
for (offset, true_name) in true_names.iter().enumerate() {
builder.entry(true_name.as_ref(),
&format!("(Libs::{}, {})", lib_name.to_ascii_uppercase(), offset));
builder.entry(
true_name.as_ref(),
&format!("(Libs::{}, {})", lib_name.to_ascii_uppercase(), offset),
);
}
builder.build(file).unwrap();
}
writeln!(file, ";")?;
write!(file,
write!(
file,
"\
#[inline]
fn {}<'a>(offset: usize,
......@@ -257,20 +276,25 @@ fn {}<'a>(offset: usize,
match offset {{
\
",
lib_name)?;
lib_name
)?;
for (offset, &name) in names.iter().enumerate() {
writeln!(file,
writeln!(
file,
" {} => {}::{}(context, sargs, targs, current),",
offset,
lib_name,
name)?;
name
)?;
}
write!(file,
write!(
file,
r#" _ => unreachable!("Don't call {} with an self chosen offset!")
}}
}}
"#,
lib_name)
lib_name
)
}
fn gen_strc_tests() {
......@@ -283,13 +307,13 @@ fn gen_strc_tests() {
let path = entry.unwrap().path();
if path.is_file() && Some("ctree".as_ref()) == path.extension() {
// TODO: sanitise file_stem so it's likely to be a valid test name?
let test_name = path.file_stem()
.unwrap()
.to_str()
.unwrap()
.replace('-', "_");
let test_name = path.file_stem().unwrap().to_str().unwrap().replace(
'-',
"_",
);
let path_string = path.to_str().unwrap();
write!(file,
write!(
file,
r#"
#[test]
fn {}() {{
......@@ -322,8 +346,8 @@ fn {}() {{
}}
"#,
test_name,
path_string)
.unwrap();
path_string
).unwrap();
}
}
}
......@@ -22,10 +22,11 @@ pub struct MutContext<'a> {
}
impl<'a> MutContext<'a> {
pub fn new(factory: &'a ATermFactory,
pub fn new(
factory: &'a ATermFactory,
scopes: Vec<Scope<ATermRef>>,
primitives: Vec<&'static Primitives>)
-> MutContext<'a> {
primitives: Vec<&'static Primitives>,
) -> MutContext<'a> {
MutContext {
stack_tracer: RefCell::new(StackTracer::default()),
factory: factory,
......@@ -35,21 +36,20 @@ impl<'a> MutContext<'a> {
}
}
pub fn call_primitive(&self,
pub fn call_primitive(
&self,
prim_name: &str,
sargs: Vec<StrategyDef>,
targs: Vec<ATermRef>,
current: ATermRef)
-> Result<ATermRef> {
current: ATermRef,
) -> Result<ATermRef> {
let prim_ref = self.primitives
.iter()
.flat_map(|prims| prims.get(prim_name))
.cloned() // ATermRef should be very cheap to clone
.next()
.ok_or_else(|| Error::UndefinedPrimitive(prim_name.to_owned()))?;
self.stack_tracer
.borrow_mut()
.push(prim_name.to_owned());
self.stack_tracer.borrow_mut().push(prim_name.to_owned());
let result = eval_prim_ref(prim_ref, self, sargs, targs, current);
if result.is_ok() {
self.stack_tracer.borrow_mut().pop_on_success();
......@@ -76,8 +76,9 @@ impl<'a> MutContext<'a> {
}
pub fn get_term(&self, term_name: &str) -> Result<ATermRef> {
self.get_term_option(term_name)
.and_then(|o| o.ok_or(Error::StrategyFailed))
self.get_term_option(term_name).and_then(|o| {
o.ok_or(Error::StrategyFailed)
})
}
pub fn match_term(&self, term_name: &str, current: &ATermRef) -> Result<()> {
......@@ -99,33 +100,56 @@ impl<'a> MutContext<'a> {
}
fn _apply_overlay(&self, scope: Scope<ATermRef>) {
debug_assert!(scope.strategy.is_empty(), "_apply_overlay: Interpreter bug, overlay is leaking a strategy definition");
debug_assert!(
scope.strategy.is_empty(),
"_apply_overlay: Interpreter bug, overlay is leaking a strategy definition"
);
for (name, value) in scope.term {
if let Some(value) = value {
let result = self.match_term(&name, &value);
debug_assert!(result.is_ok(), "_apply_overlay: Interpreter bug, overlay has names that were bound underneath");
debug_assert!(
result.is_ok(),
"_apply_overlay: Interpreter bug, overlay has names that were bound underneath"
);
} else {
unreachable!("_apply_overlay: Interpreter bug, who put an uninitialised variable in \
my overlay? >:(")
unreachable!(
"_apply_overlay: Interpreter bug, who put an uninitialised variable in \
my overlay? >:("
)
}
}
}
pub fn apply_overlay(&self) {
let scope = self.scopes.borrow_mut().pop().expect("apply_overlay: Interpreter bug, unexpected end of stack");
debug_assert!(scope.is_overlay, "apply_overlay: Interpreter bug, unexpected normal scope");
let scope = self.scopes.borrow_mut().pop().expect(
"apply_overlay: Interpreter bug, unexpected end of stack",
);
debug_assert!(
scope.is_overlay,
"apply_overlay: Interpreter bug, unexpected normal scope"
);
self._apply_overlay(scope);
}
pub fn drop_overlay(&self) {
let popped_scope = self.scopes.borrow_mut().pop().expect("drop_overlay: Interpreter bug, unexpected end of stack");
debug_assert!(popped_scope.is_overlay, "drop_overlay: Interpreter bug, unexpected normal scope");
let popped_scope = self.scopes.borrow_mut().pop().expect(
"drop_overlay: Interpreter bug, unexpected end of stack",
);
debug_assert!(
popped_scope.is_overlay,
"drop_overlay: Interpreter bug, unexpected normal scope"
);
}
pub fn pop_scope(&self) {
let scope = self.scopes.borrow_mut().pop().expect("pop_scope: Interpreter bug, unexpected end of stack");
debug_assert!(!scope.is_overlay, "pop_scope: Interpreter bug, unexpected overlay");
let scope = self.scopes.borrow_mut().pop().expect(
"pop_scope: Interpreter bug, unexpected end of stack",
);
debug_assert!(
!scope.is_overlay,
"pop_scope: Interpreter bug, unexpected overlay"
);
}
}
......@@ -178,8 +202,9 @@ pub struct Scope<ATerm> {
impl<ATerm> Scope<ATerm> {
pub fn new<IA, IS>(terms: IA, defs: IS) -> Scope<ATerm>
where IA: IntoIterator<Item = (String, ATerm)>,
IS: IntoIterator<Item = (String, StrategyDef)>
where
IA: IntoIterator<Item = (String, ATerm)>,
IS: IntoIterator<Item = (String, StrategyDef)>,
{
Scope {
term: terms.into_iter().map(|(n, a)| (n, Some(a))).collect(),
......@@ -189,7 +214,8 @@ impl<ATerm> Scope<ATerm> {
}
pub fn from_defs<I>(defs: I) -> Scope<ATerm>
where I: IntoIterator<Item = StrategyDef>
where
I: IntoIterator<Item = StrategyDef>,
{
Scope {
term: HashMap::default(),
......@@ -201,19 +227,23 @@ impl<ATerm> Scope<ATerm> {
}
pub fn from_let_defs<I>(no_of_scopes: usize, defs: I) -> Scope<ATerm>
where I: IntoIterator<Item = StrategyDef>
where
I: IntoIterator<Item = StrategyDef>,
{
Scope {
term: HashMap::default(),
strategy: defs.into_iter()
.map(|sdeft| (sdeft.name(), sdeft.set_scope_on_let_sdef(no_of_scopes)))
.map(|sdeft| {
(sdeft.name(), sdeft.set_scope_on_let_sdef(no_of_scopes))
})
.collect(),
is_overlay: false,
}
}
pub fn from_fresh_variables<I>(fresh_vars: I) -> Scope<ATerm>
where I: IntoIterator<Item = String>
where
I: IntoIterator<Item = String>,
{
Scope {
term: {
......@@ -235,7 +265,10 @@ impl<ATerm> Scope<ATerm> {
pub mod Scopes {
use super::*;
pub fn get_term_option(vec: &Vec<Scope<ATermRef>>, term_name: &str) -> Result<Option<ATermRef>> {
pub fn get_term_option(
vec: &Vec<Scope<ATermRef>>,
term_name: &str,
) -> Result<Option<ATermRef>> {
vec.iter()
.rev()
.flat_map(|scope| scope.term.get(term_name))
......@@ -244,7 +277,11 @@ pub mod Scopes {
.ok_or_else(|| Error::UndefinedVariable(term_name.to_owned()))
}
pub fn match_term(scopes: &mut Vec<Scope<ATermRef>>, term_name: &str, current: &ATermRef) -> Result<()> {
pub fn match_term(
scopes: &mut Vec<Scope<ATermRef>>,
term_name: &str,
current: &ATermRef,
) -> Result<()> {
let term = get_term_option(scopes, term_name)?;
match term {
Some(term) => {
......@@ -257,13 +294,17 @@ pub mod Scopes {
None => {
for mut scope in scopes.iter_mut().rev() {
if scope.is_overlay || scope.term.contains_key(term_name) {
if let Some(Some(t)) = scope
.term
.insert(term_name.to_owned(), Some(current.clone())) {
unreachable!(format!("match_term: No scope had {}, but we just \
if let Some(Some(t)) = scope.term.insert(
term_name.to_owned(),
Some(current.clone()),
)
{
unreachable!(format!(
"match_term: No scope had {}, but we just \
replaced {} when we added it?!",
term_name,
t))
t
))
} else {
return Ok(());
}
......@@ -271,8 +312,10 @@ pub mod Scopes {
}
}
}
unreachable!(format!("match_term: First we could find {} unbound, then we couldn't \
unreachable!(format!(
"match_term: First we could find {} unbound, then we couldn't \
anymore?!",
term_name))
term_name
))
}
}
......@@ -27,7 +27,8 @@ pub enum Module {
}
impl<'a, A> TryFrom<&'a A> for Module
where A: ATerm
where
A: ATerm,
{
type Err = Error;
......@@ -61,7 +62,8 @@ pub enum Decl {
}
impl<'a, A> TryFrom<&'a A> for Decl
where A: ATerm
where
A: ATerm,
{
type Err = Error;
......@@ -97,7 +99,8 @@ pub enum ImportModName {
}
impl<'a, A> TryFrom<&'a A> for ImportModName
where A: ATerm
where
A: ATerm,
{
type Err = Error;
......@@ -128,7 +131,8 @@ pub enum SDecl {
}
impl<'a, A> TryFrom<&'a A> for SDecl
where A: ATerm
where
A: ATerm,
{
type Err = Error;
......@@ -160,7 +164,8 @@ pub enum Sort {
}
impl<'a, A> TryFrom<&'a A> for Sort
where A: ATerm
where
A: ATerm,
{
type Err = Error;
......@@ -201,7 +206,8 @@ pub enum OpDecl {
}
impl<'a, A> TryFrom<&'a A> for OpDecl
where A: ATerm
where
A: ATerm,
{
type Err = Error;
......@@ -253,7 +259,8 @@ impl<'a, A> TryFrom<&'a A> for OpDecl
pub struct ConstType(pub Sort);
impl<'a, A> TryFrom<&'a A> for ConstType
where A: ATerm
where
A: ATerm,
{
type Err = Error;
......@@ -279,7 +286,8 @@ pub enum Type {
}
impl<'a, A> TryFrom<&'a A> for Type
where A: ATerm
where
A: ATerm,
{
type Err = Error;
......@@ -313,7 +321,8 @@ pub enum Def {
}
impl<'a, A> TryFrom<&'a A> for Def
where A: ATerm
where
A: ATerm,
{
type Err = Error;
......@@ -369,7 +378,8 @@ impl<'a, A> TryFrom<&'a A> for Def
pub struct SVar(pub Id);
impl<'a, A> TryFrom<&'a A> for SVar
where A: ATerm
where
A: ATerm,
{
type Err = Error;
......@@ -394,7 +404,8 @@ pub enum StrategyDef {
}
impl<'a, A> TryFrom<&'a A> for StrategyDef
where A: ATerm
where
A: ATerm,
{
type Err = Error;
......@@ -422,18 +433,14 @@ impl<'a, A> TryFrom<&'a A> for StrategyDef
2 => {
let tids1 = match_list(&r[1])?;
return match_list(&r[2]).map(|tids2| {
StrategyDef::ExtSDef(None,
tids1,
tids2)
StrategyDef::ExtSDef(None, tids1, tids2)
});
}
3 => {
let str = match_string(&r[0])?;
let tids1 = match_list(&r[1])?;
return match_list(&r[2]).map(|tids2| {
StrategyDef::ExtSDef(Some(str),
tids1,
tids2)
StrategyDef::ExtSDef(Some(str), tids1, tids2)
});
}
_ => {}
......@@ -466,7 +473,8 @@ pub enum Anno {
}
impl<'a, A> TryFrom<&'a A> for Anno
where A: ATerm
where
A: ATerm,
{
type Err = Error;
......@@ -500,7 +508,8 @@ impl<'a, A> TryFrom<&'a A> for Anno
pub struct VarDec(pub Id, pub Type);
impl<'a, A> TryFrom<&'a A> for VarDec
where A: ATerm
where
A: ATerm,
{
type Err = Error;
......@@ -539,7 +548,8 @@ pub enum Strategy {
}
impl<'a, A> TryFrom<&'a A> for Strategy
where A: ATerm
where
A: ATerm,
{
type Err = Error;
......@@ -566,9 +576,7 @@ impl<'a, A> TryFrom<&'a A> for Strategy
let term = match_(&r[0])?;
let strategies = match_list(&r[1])?;
return match_list(&r[2]).map(|terms| {
Strategy::CallDynamic(term,
strategies,
terms)
Strategy::CallDynamic(term, strategies, terms)
});
}
}
......@@ -615,8 +623,7 @@ impl<'a, A> TryFrom<&'a A> for Strategy
if r.len() == 2 {
let strat1 = match_(&r[0])?;
return match_(&r[1]).map(|strat2| {
Strategy::Seq(Box::new(strat1),
Box::new(strat2))
Strategy::Seq(Box::new(strat1), Box::new(strat2))
});
}
}
......@@ -625,9 +632,11 @@ impl<'a, A> TryFrom<&'a A> for Strategy
let strat1 = match_(&r[0])?;
let strat2 = match_(&r[1])?;
return match_(&r[2]).map(|strat3| {
Strategy::GuardedLChoice(Box::new(strat1),
Strategy::GuardedLChoice(
Box::new(strat1),
Box::new(strat2),
Box::new(strat3))
Box::new(strat3),
)
});
}
}
......@@ -669,7 +678,8 @@ impl<'a, A> TryFrom<&'a A> for Strategy
pub struct Var(pub Id);
impl<'a, A> TryFrom<&'a A> for Var
where A: ATerm
where
A: ATerm,
{
type Err = Error;
......@@ -695,7 +705,8 @@ pub enum Term {
}
impl<'a, A> TryFrom<&'a A> for Term
where A: ATerm
where
A: ATerm,
{
type Err = Error;
......@@ -746,7 +757,8 @@ pub enum PreTerm {
}
impl<'a, A> TryFrom<&'a A> for PreTerm
where A: ATerm
where
A: ATerm,
{
type Err = Error;
......@@ -767,9 +779,9 @@ impl<'a, A> TryFrom<&'a A> for PreTerm
"Int" => {
if r.len() == 1 {
let str = match_string(&r[0])?;
return str.parse()
.map(PreTerm::Int)
.map_err(|_| Error::CTreeParse("int"));
return str.parse().map(PreTerm::Int).map_err(
|_| Error::CTreeParse("int"),
);
}
}
"Real" => {
......@@ -817,8 +829,9 @@ impl<'a, A> TryFrom<&'a A> for PreTerm
}
fn match_string<A, R>(r: &R) -> Result<String, Error>
where A: ATerm<Rec = R>,
R: Borrow<A>
where
A: ATerm<Rec = R>,
R: Borrow<A>,
{
if let a::Term::Application(ref str, ref r) = r.borrow().as_inner().term {
if r.is_empty() {
......@@ -829,8 +842,9 @@ fn match_string<A, R>(r: &R) -> Result<String, Error>
}
fn match_strings<A, R>(r: &R) -> Result<Vec<String>, Error>
where A: ATerm<Rec = R>,
R: Borrow<A>
where
A: ATerm<Rec = R>,
R: Borrow<A>,
{
if let a::Term::List(ref r) = r.borrow().as_inner().term {
return r.into_iter()
......@@ -841,9 +855,10 @@ fn match_strings<A, R>(r: &R) -> Result<Vec<String>, Error>
}
fn match_list<'a, A, R, T>(r: &'a R) -> Result<Vec<T>, Error>
where T: TryFrom<&'a A, Err = Error>,
where
T: TryFrom<&'a A, Err = Error>,
A: 'a + ATerm<Rec = R>,
R: Borrow<A>
R: Borrow<A>,
{
if let a::Term::List(ref r) = r.borrow().as_inner().term {
return r.iter()
......@@ -854,9 +869,10 @@ fn match_list<'a, A, R, T>(r: &'a R) -> Result<Vec<T>, Error>
}
fn match_<'a, A, R, T>(r: &'a R) -> Result<T, Error>
where T: TryFrom<&'a A, Err = Error>,
where
T: TryFrom<&'a A, Err = Error>,
A: 'a + ATerm<Rec = R>,
R: Borrow<A>
R: Borrow<A>,
{
r.borrow().try_into()
}
......
......@@ -29,8 +29,9 @@ impl IndexedSet {
}
pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&u16>
where ATermRef: Borrow<Q>,
Q: Eq + Hash
where
ATermRef: Borrow<Q>,
Q: Eq + Hash,
{
self.0.get(key)
}
......@@ -42,8 +43,9 @@ impl IndexedSet {
}
pub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<u16>
where ATermRef: Borrow<Q>,
Q: Eq + Hash
where
ATermRef: Borrow<Q>,
Q: Eq + Hash,
{
self.0.remove(key)
}
......
This diff is collapsed.
......@@ -27,10 +27,11 @@ pub fn read_aterm<P: AsRef<Path>>(f: &ATermFactory, path: &P) -> Result<ATermRef
Ok(aterm)
}
pub fn read_lib(f: &ATermFactory,
pub fn read_lib(
f: &ATermFactory,
str_path: &Path,
path: &str)
-> Result<(ATermRef, Option<&'static Primitives>)> {
path: &str,
) -> Result<(ATermRef, Option<&'static Primitives>)> {
if let Some(&(path, prim)) = primitives::LIBS.get(path) {
Ok((read_aterm(f, &Path::new(&str_path).join(path))?, prim))
} else {
......