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

cargo fmt with new 0.9 version

parent 5b283330
This diff is collapsed.
......@@ -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>
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))
term_name,
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
))
}
}
This diff is collapsed.
......@@ -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,
str_path: &Path,
path: &str)
-> Result<(ATermRef, Option<&'static Primitives>)> {
pub fn read_lib(
f: &ATermFactory,
str_path: &Path,
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 {
......
......@@ -34,21 +34,21 @@ struct Opt {
// #[structopt(short = "d", long = "debug", help = "Activate debug mode")]
// debug: bool,
#[structopt(long = "strpath",
help = "Path to Stratego/XT installation with the libstratego-* directories. If none is \
given the environment variable STRDIR is tried. If that is not set, we try to call \
`strategoxt-path` and go one directory up from the output. If that doesn't work, \
well.. I guess we'll just take the current directory. ")]
help = "Path to Stratego/XT installation with the libstratego-* directories. If \
none is given the environment variable STRDIR is tried. If that is not set, we try \
to call `strategoxt-path` and go one directory up from the output. If that doesn't \
work, well.. I guess we'll just take the current directory. ")]
str_path: Option<String>,
#[structopt(short = "l", long = "libs",
help = "Library files (CTree). Use the old libstratego-* names to load those libraries \
from --strpath along with the necessary primitives")]
help = "Library files (CTree). Use the old libstratego-* names to load those \
libraries from --strpath along with the necessary primitives")]
libraries: Vec<String>,
#[structopt(short = "p", long = "prims",
help = "Primitives to be loaded (usually done implicitly with standard \
libraries in the --libs option")]
help = "Primitives to be loaded (usually done implicitly with standard libraries \
in the --libs option")]
primitives: Vec<String>,
#[structopt(short = "s", long = "stack-space", default_value = "64",
help = "The amount of stack space in mibibytes to be used")]
help = "The amount of stack space in mibibytes to be used")]
stack_space: usize,
// TODO: add an option to explicitly load strategy performance overrides
#[structopt(help = "Program file (CTree)")]
......@@ -75,11 +75,12 @@ fn main() {
}
}
fn exec(program_path: String,
str_path: PathBuf,
library_paths: Vec<String>,
stack_space: usize)
-> result::Result<String, TracedError> {
fn exec(
program_path: String,
str_path: PathBuf,
library_paths: Vec<String>,
stack_space: usize,
) -> result::Result<String, TracedError> {
use std::thread;
thread::Builder::new()
.name("Main execution thread".into())
......
This diff is collapsed.
This diff is collapsed.
......@@ -11,37 +11,43 @@ use std::cell::RefMut;
use std::rc::Rc;
use std::borrow::Borrow;
pub fn indexedSet_create<'a>(context: &MutContext<'a>,
sargs: Vec<StrategyDef>,
targs: Vec<ATermRef>,
current: ATermRef)
-> Result<ATermRef> {
pub fn indexedSet_create<'a>(
context: &MutContext<'a>,
sargs: Vec<StrategyDef>,
targs: Vec<ATermRef>,
current: ATermRef,
) -> Result<ATermRef> {
if targs.len() < 2 {
return Err(Error::UnknownBehaviour("Primitive indexedSet_create called with wrong argument \
count"));
return Err(Error::UnknownBehaviour(
"Primitive indexedSet_create called with wrong argument count",
));
}
let capacity = targs[0].get_int().ok_or(Error::StrategyFailed)?;
// We don't have a concept of load_factor, so we check the type and then ignore it
let _load_factor = targs[1].get_int().ok_or(Error::StrategyFailed)?;
if capacity < 0 || _load_factor < 0 {
Err(Error::UnknownBehaviour("indexedSet_create (SSL) received negative number(s)"))
Err(Error::UnknownBehaviour(
"indexedSet_create (SSL) received negative number(s)",
))
} else {
Ok(context
.factory
.blob(IndexedSet::with_capacity(capacity as usize).into()))
Ok(context.factory.blob(
IndexedSet::with_capacity(capacity as usize).into(),
))
}
}
pub fn indexedSet_reset<'a>(context: &MutContext<'a>,
sargs: Vec<StrategyDef>,
targs: Vec<ATermRef>,
current: ATermRef)
-> Result<ATermRef> {
pub fn indexedSet_reset<'a>(
context: &MutContext<'a>,
sargs: Vec<StrategyDef>,
targs: Vec<ATermRef>,
current: ATermRef,
) -> Result<ATermRef> {
use try_from::TryInto;
if targs.len() < 1 {
return Err(Error::UnknownBehaviour("Primitive indexedSet_destroy called with wrong \
argument count"));
return Err(Error::UnknownBehaviour(
"Primitive indexedSet_destroy called with wrong argument count",
));
}
let set: Rc<RefCell<IndexedSet>> = targs[0]
.get_blob()
......@@ -56,23 +62,26 @@ pub fn indexedSet_reset<'a>(context: &MutContext<'a>,
Ok(current)
}
pub fn indexedSet_destroy<'a>(context: &MutContext<'a>,
sargs: Vec<StrategyDef>,
targs: Vec<ATermRef>,
current: ATermRef)
-> Result<ATermRef> {
pub fn indexedSet_destroy<'a>(
context: &MutContext<'a>,
sargs: Vec<StrategyDef>,
targs: Vec<ATermRef>,
current: ATermRef,
) -> Result<ATermRef> {
indexedSet_reset(context, sargs, targs, current)
}
pub fn indexedSet_put<'a>(context: &MutContext<'a>,
sargs: Vec<StrategyDef>,
targs: Vec<ATermRef>,
current: ATermRef)
-> Result<ATermRef> {
pub fn indexedSet_put<'a>(
context: &MutContext<'a>,
sargs: Vec<StrategyDef>,
targs: Vec<ATermRef>,
current: ATermRef,
) -> Result<ATermRef> {
use try_from::TryInto;
if sargs.len() < 1 || targs.len() < 2 {
return Err(Error::UnknownBehaviour("Primitive indexedSet_put called with wrong argument \
count"));
return Err(Error::UnknownBehaviour(
"Primitive indexedSet_put called with wrong argument count",
));
}
let set: Rc<RefCell<IndexedSet>> = targs[0]
.get_blob()
......@@ -86,24 +95,28 @@ pub fn indexedSet_put<'a>(context: &MutContext<'a>,
let mut set: RefMut<IndexedSet> = set.borrow_mut();
match set.get(key) {
Some(&index) => {
sargs[0].eval(context,
Vec::new(),
Vec::new(),
context.factory.int(index as i32))
sargs[0].eval(
context,
Vec::new(),
Vec::new(),
context.factory.int(index as i32),
)
}
None => Ok(context.factory.int(set.insert(key.clone()) as i32)),
}
}
pub fn indexedSet_remove<'a>(context: &MutContext<'a>,
sargs: Vec<StrategyDef>,
targs: Vec<ATermRef>,
current: ATermRef)
-> Result<ATermRef> {
pub fn indexedSet_remove<'a>(
context: &MutContext<'a>,
sargs: Vec<StrategyDef>,
targs: Vec<ATermRef>,
current: ATermRef,
) -> Result<ATermRef> {
use try_from::TryInto;
if targs.len() < 2 {
return Err(Error::UnknownBehaviour("Primitive indexedSet_remove called with wrong argument \
count"));
return Err(Error::UnknownBehaviour(
"Primitive indexedSet_remove called with wrong argument count",
));
}
let set: Rc<RefCell<IndexedSet>> = targs[0]
.get_blob()
......@@ -117,15 +130,17 @@ pub fn indexedSet_remove<'a>(context: &MutContext<'a>,
Ok(targs[0].clone())
}
pub fn indexedSet_getIndex<'a>(context: &MutContext<'a>,
sargs: Vec<StrategyDef>,
targs: Vec<ATermRef>,
current: ATermRef)
-> Result<ATermRef> {
pub fn indexedSet_getIndex<'a>(
context: &MutContext<'a>,
sargs: Vec<StrategyDef>,
targs: Vec<ATermRef>,
current: ATermRef,
) -> Result<ATermRef> {
use try_from::TryInto;
if targs.len() < 2 {
return Err(Error::UnknownBehaviour("Primitive indexedSet_getIndex called with wrong \
argument count"));
return Err(Error::UnknownBehaviour(
"Primitive indexedSet_getIndex called with wrong argument count",
));
}
let set: Rc<RefCell<IndexedSet>> = targs[0]
.get_blob()
......@@ -135,24 +150,25 @@ pub fn indexedSet_getIndex<'a>(context: &MutContext<'a>,
.map_err(|_| Error::StrategyFailed)?;
let set: &RefCell<IndexedSet> = set.borrow();
let result = set.borrow()
.get(&targs[1])
.cloned()
.ok_or(Error::StrategyFailed)?;
let result = set.borrow().get(&targs[1]).cloned().ok_or(
Error::StrategyFailed,
)?;
Ok(context.factory.int(result as i32))
}
// TODO: libstratego-lib uses a primitive SSL_indexedSet_getElem, which does not exist in the Java implementation!
pub fn indexedSet_elements<'a>(context: &MutContext<'a>,
sargs: Vec<StrategyDef>,
targs: Vec<ATermRef>,
current: ATermRef)
-> Result<ATermRef> {
pub fn indexedSet_elements<'a>(
context: &MutContext<'a>,
sargs: Vec<StrategyDef>,
targs: Vec<ATermRef>,
current: ATermRef,
) -> Result<ATermRef> {
use try_from::TryInto;
if targs.len() < 1 {
return Err(Error::UnknownBehaviour("Primitive indexedSet_elements called with wrong \
argument count"));
return Err(Error::UnknownBehaviour(
"Primitive indexedSet_elements called with wrong argument count",
));
}
let set: Rc<RefCell<IndexedSet>> = targs[0]
.get_blob()
......
This diff is collapsed.
......@@ -6,28 +6,34 @@ use preprocess::StrategyDef;
use aterm::ATermFactory as ATF;
pub fn get_list_length<'a>(context: &MutContext<'a>,
sargs: Vec<StrategyDef>,
targs: Vec<ATermRef>,
current: ATermRef)
-> Result<ATermRef> {
pub fn get_list_length<'a>(
context: &MutContext<'a>,
sargs: Vec<StrategyDef>,
targs: Vec<ATermRef>,
current: ATermRef,
) -> Result<ATermRef> {
if targs.len() < 1 {
return Err(Error::UnknownBehaviour("Primitive get_list_length was called with wrong \
argument count"));
return Err(Error::UnknownBehaviour(
"Primitive get_list_length was called with wrong \
argument count",
));
}
// Not cheap :(
let list = from_cons_nil_list(targs[0].clone())?;
Ok(context.factory.int(list.len() as i32))
}
pub fn list_loop<'a>(context: &MutContext<'a>,
sargs: Vec<StrategyDef>,
targs: Vec<ATermRef>,
current: ATermRef)
-> Result<ATermRef> {
pub fn list_loop<'a>(
context: &MutContext<'a>,
sargs: Vec<StrategyDef>,
targs: Vec<ATermRef>,
current: ATermRef,
) -> Result<ATermRef> {
if sargs.len() < 1 || targs.len() < 1 {
return Err(Error::UnknownBehaviour("Primitive get_list_length was called with wrong \
argument count"));
return Err(Error::UnknownBehaviour(
"Primitive get_list_length was called with wrong \
argument count",
));
}
// Allocates more, but gives a type error earlier
......@@ -40,14 +46,17 @@ pub fn list_loop<'a>(context: &MutContext<'a>,
Ok(current)
}
pub fn list_fold<'a>(context: &MutContext<'a>,
sargs: Vec<StrategyDef>,
targs: Vec<ATermRef>,
current: ATermRef)
-> Result<ATermRef> {
pub fn list_fold<'a>(
context: &MutContext<'a>,
sargs: Vec<StrategyDef>,
targs: Vec<ATermRef>,
current: ATermRef,
) -> Result<ATermRef> {
if sargs.len() < 1 || targs.len() < 2 {
return Err(Error::UnknownBehaviour("Primitive get_list_length was called with wrong \
argument count"));
return Err(Error::UnknownBehaviour(
"Primitive get_list_length was called with wrong \
argument count",
));
}
// Allocates more, but gives a type error earlier
......
This diff is collapsed.
......@@ -6,15 +6,16 @@ use preprocess::StrategyDef;
use aterm::ATermFactory as ATF;
pub fn stacktrace_get_all_frame_names<'a>(context: &MutContext<'a>,
sargs: Vec<StrategyDef>,
targs: Vec<ATermRef>,
current: ATermRef)
-> Result<ATermRef> {
Ok(to_cons_nil_list(context.factory,
context
.stack_tracer
.borrow()
.stack()
.map(|s| context.factory.string(s.clone()))))
pub fn stacktrace_get_all_frame_names<'a>(
context: &MutContext<'a>,
sargs: Vec<StrategyDef>,
targs: Vec<ATermRef>,
current: ATermRef,
) -> Result<ATermRef> {
Ok(to_cons_nil_list(
context.factory,
context.stack_tracer.borrow().stack().map(|s| {
context.factory.string(s.clone())
}),
))
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment