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

cargo fmt with new 0.9 version

parent 5b283330
......@@ -28,145 +28,149 @@ fn gen_primitives() {
("libstrc", r#"("libstrc/libstrc.ctree", None)"#),//Some(&STRC))"#)
];
let ssl = [// INT
"is_int",
"int",
"iori",
"xori",
"andi",
"shli",
"shri",
"addi",
"divi",
"gti",
"muli",
"modi",
"subti",
"int_to_string",
"string_to_int",
"rand",
// REAL
"is_real",
"real",
"addr",
"divr",
"gtr",
"mulr",
"modr",
"subtr",
"real_to_string",
"real_to_string_precision",
"string_to_real",
"cos",
"sin",
"atan2",
"sqrt",
// STRING
"explode_string",
"is_string",
"strcat",
"implode_string",
"strlen",
"concat_strings",
"write_term_to_string",
"read_term_from_string",
"new",
"newname",
// (FILE) SYSTEM
"chdir",
"mkdir",
"P_tmpdir",
"mkstemp",
"mkdtemp",
"filemode",
"S_ISDIR",
"fputs",
"fputc",
"access",
"getcwd",
"readdir",
"modification_time",
"fileno",
"STDIN_FILENO",
"STDOUT_FILENO",
"stdout_stream",
"STDERR_FILENO",
"close",
"perror",
"fopen",
"write_term_to_stream_baf",
"write_term_to_stream_saf",
"write_term_to_stream_taf",
"fclose",
"fgetc",
"fflush",
"remove",
"copy",
"filesize",
"rmdir",
"getenv",
"printnl",
"stderr_stream",
"write_term_to_stream_text",
"stdin_stream",
"EXDEV",
"read_term_from_stream",
"exit",
"times",
"TicksToSeconds",
"cputime",
// INDEXEDSET
"indexedSet_create",
"indexedSet_destroy",
"indexedSet_put",
"indexedSet_getIndex",
"indexedSet_elements",
"indexedSet_remove",
"indexedSet_reset",
// HASHTABLE
"dynamic_rules_hashtable",
"table_hashtable",
"hashtable_get",
"hashtable_create",
"hashtable_put",
"hashtable_reset",
"hashtable_destroy",
"hashtable_remove",
"hashtable_keys",
"table_fold",
"table_keys_fold",
"table_values_fold",
// LIST
"get_list_length",
"list_loop",
"list_fold",
// TERM
"get_constructor",
"mkterm",
"get_arguments",
"explode_term",
"get_appl_arguments_map",
"address",
"checksum",
"isPlaceholder",
"makePlaceholder",
"getPlaceholder",
"preserve_annotations_attachments",
// STACKTRACE
"stacktrace_get_all_frame_names"];
let ssl = [
// INT
"is_int",
"int",
"iori",
"xori",
"andi",
"shli",
"shri",
"addi",
"divi",
"gti",
"muli",
"modi",
"subti",
"int_to_string",
"string_to_int",
"rand",
// REAL
"is_real",
"real",
"addr",
"divr",
"gtr",
"mulr",
"modr",
"subtr",
"real_to_string",
"real_to_string_precision",
"string_to_real",
"cos",
"sin",
"atan2",
"sqrt",
// STRING
"explode_string",
"is_string",
"strcat",
"implode_string",
"strlen",
"concat_strings",
"write_term_to_string",
"read_term_from_string",
"new",
"newname",
// (FILE) SYSTEM
"chdir",
"mkdir",
"P_tmpdir",
"mkstemp",
"mkdtemp",
"filemode",
"S_ISDIR",
"fputs",
"fputc",
"access",
"getcwd",
"readdir",
"modification_time",
"fileno",
"STDIN_FILENO",
"STDOUT_FILENO",
"stdout_stream",
"STDERR_FILENO",
"close",
"perror",
"fopen",
"write_term_to_stream_baf",
"write_term_to_stream_saf",
"write_term_to_stream_taf",
"fclose",
"fgetc",
"fflush",
"remove",
"copy",
"filesize",
"rmdir",
"getenv",
"printnl",
"stderr_stream",
"write_term_to_stream_text",
"stdin_stream",
"EXDEV",
"read_term_from_stream",
"exit",
"times",
"TicksToSeconds",
"cputime",
// INDEXEDSET
"indexedSet_create",
"indexedSet_destroy",
"indexedSet_put",
"indexedSet_getIndex",
"indexedSet_elements",
"indexedSet_remove",
"indexedSet_reset",
// HASHTABLE
"dynamic_rules_hashtable",
"table_hashtable",
"hashtable_get",
"hashtable_create",
"hashtable_put",
"hashtable_reset",
"hashtable_destroy",
"hashtable_remove",
"hashtable_keys",
"table_fold",
"table_keys_fold",
"table_values_fold",
// LIST
"get_list_length",
"list_loop",
"list_fold",
// TERM
"get_constructor",
"mkterm",
"get_arguments",
"explode_term",
"get_appl_arguments_map",
"address",
"checksum",
"isPlaceholder",
"makePlaceholder",
"getPlaceholder",
"preserve_annotations_attachments",
// STACKTRACE
"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,
"pub static LIBS: phf::Map<&'static str, \
(&'static str, Option<&'static Primitives>)> =")
.unwrap();
writeln!(
&mut file,
"pub static LIBS: phf::Map<&'static str, \
(&'static str, Option<&'static Primitives>)> ="
).unwrap();
let mut builder = phf_codegen::Map::new();
for &(name, pair) in &libs {
builder.entry(name, pair);
......@@ -179,18 +183,22 @@ 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>,
sargs: Vec<StrategyDef>,
......@@ -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,
"Libs::{} => {}(prim_ref.1, context, sargs, targs, current),",
name.to_ascii_uppercase(),
name)?;
write!(
file,
"Libs::{} => {}(prim_ref.1, context, sargs, targs, current),",
name.to_ascii_uppercase(),
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>,
names: &[&'static str],
lib_name: &'static str)
-> ::std::io::Result<()> {
write!(file,
"static {}: Primitives =\n",
lib_name.to_ascii_uppercase())?;
fn write_primitives(
file: &mut BufWriter<File>,
names: &[&'static str],
lib_name: &'static str,
) -> ::std::io::Result<()> {
write!(
file,
"static {}: Primitives =\n",
lib_name.to_ascii_uppercase()
)?;
{
let true_names: Vec<String> = names
.iter()
......@@ -237,15 +253,18 @@ 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,
context: &MutContext<'a>,
......@@ -257,20 +276,25 @@ fn {}<'a>(offset: usize,
match offset {{
\
",
lib_name)?;
lib_name
)?;
for (offset, &name) in names.iter().enumerate() {
writeln!(file,
" {} => {}::{}(context, sargs, targs, current),",
offset,
lib_name,
name)?;
writeln!(
file,
" {} => {}::{}(context, sargs, targs, current),",
offset,
lib_name,
name
)?;
}
write!(file,
r#" _ => unreachable!("Don't call {} with an self chosen offset!")
write!(
file,
r#" _ => unreachable!("Don't call {} with an self chosen offset!")
}}
}}
"#,
lib_name)
lib_name
)
}
fn gen_strc_tests() {
......@@ -283,14 +307,14 @@ 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,
r#"
write!(
file,
r#"
#[test]
fn {}() {{
let str_path = io::find_str_path(None);
......@@ -321,9 +345,9 @@ fn {}() {{
assert!(result.is_ok());
}}
"#,
test_name,
path_string)
.unwrap();
test_name,
path_string
).unwrap();
}
}
}
......@@ -22,10 +22,11 @@ pub struct MutContext<'a> {
}
impl<'a> MutContext<'a> {
pub fn new(factory: &'a ATermFactory,
scopes: Vec<Scope<ATermRef>>,
primitives: Vec<&'static Primitives>)
-> MutContext<'a> {
pub fn new(
factory: &'a ATermFactory,
scopes: Vec<Scope<ATermRef>>,
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,
prim_name: &str,
sargs: Vec<StrategyDef>,
targs: Vec<ATermRef>,
current: ATermRef)
-> Result<ATermRef> {
pub fn call_primitive(
&self,
prim_name: &str,
sargs: Vec<StrategyDef>,
targs: Vec<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>