MAINT: cargo fmt

This commit is contained in:
Jeremy Wall 2019-11-03 14:20:25 -06:00
parent db9b7aef43
commit 34636a1743
11 changed files with 108 additions and 91 deletions

View File

@ -578,7 +578,12 @@ impl ModuleDef {
let rewrite_import = |e: &mut Expression| { let rewrite_import = |e: &mut Expression| {
let main_separator = format!("{}", std::path::MAIN_SEPARATOR); let main_separator = format!("{}", std::path::MAIN_SEPARATOR);
if let Expression::Include(ref mut def) = e { if let Expression::Include(ref mut def) = e {
let path = PathBuf::from(&def.path.fragment.replace("/", &main_separator).replace("\\", &main_separator)); let path = PathBuf::from(
&def.path
.fragment
.replace("/", &main_separator)
.replace("\\", &main_separator),
);
if path.is_relative() { if path.is_relative() {
def.path.fragment = base def.path.fragment = base
.join(path) .join(path)
@ -589,7 +594,12 @@ impl ModuleDef {
} }
} }
if let Expression::Import(ref mut def) = e { if let Expression::Import(ref mut def) = e {
let path = PathBuf::from(&def.path.fragment.replace("/", &main_separator).replace("\\", &main_separator)); let path = PathBuf::from(
&def.path
.fragment
.replace("/", &main_separator)
.replace("\\", &main_separator),
);
// std/ paths are special and do not get made into absolute paths. // std/ paths are special and do not get made into absolute paths.
if path.starts_with(format!("std{}", main_separator)) { if path.starts_with(format!("std{}", main_separator)) {
return; return;

View File

@ -575,4 +575,4 @@ fn test_bad_import_path_compile_failure() {
Regex::new(r"line: 1 column: 18").unwrap(), Regex::new(r"line: 1 column: 18").unwrap(),
], ],
) )
} }

View File

@ -23,19 +23,19 @@ use std::io::Read;
use std::path::PathBuf; use std::path::PathBuf;
use std::rc::Rc; use std::rc::Rc;
use simple_error;
use rustyline; use rustyline;
use simple_error;
use crate::ast::*; use crate::ast::*;
use crate::error; use crate::error;
use crate::iter::OffsetStrIter; use crate::iter::OffsetStrIter;
use crate::parse::parse; use crate::parse::parse;
use crate::build::opcode::pointer::OpPointer;
use crate::build::opcode::translate; use crate::build::opcode::translate;
use crate::build::opcode::translate::PositionMap;
use crate::build::opcode::Environment; use crate::build::opcode::Environment;
use crate::build::opcode::VM; use crate::build::opcode::VM;
use crate::build::opcode::pointer::OpPointer;
use crate::build::opcode::translate::PositionMap;
pub mod assets; pub mod assets;
pub mod format; pub mod format;
@ -207,7 +207,11 @@ where
println!("Type '#help' for help."); println!("Type '#help' for help.");
println!(""); println!("");
// Initialize VM with an empty OpPointer // Initialize VM with an empty OpPointer
let mut vm = VM::new(Rc::new(PositionMap::new()), self.environment.clone(), &self.working_dir); let mut vm = VM::new(
Rc::new(PositionMap::new()),
self.environment.clone(),
&self.working_dir,
);
loop { loop {
// print prompt // print prompt
let line = editor.readline(&format!("{}> ", lines.next_line()))?; let line = editor.readline(&format!("{}> ", lines.next_line()))?;
@ -255,8 +259,7 @@ where
println!("{}", val); println!("{}", val);
vm.last = None; vm.last = None;
} }
None => { None => {}
}
} }
editor.history_mut().add(stmt); editor.history_mut().add(stmt);
editor.save_history(&config_home)?; editor.save_history(&config_home)?;

View File

@ -46,9 +46,7 @@ impl<'a> Entry<'a> {
path: P, path: P,
) -> Result<OpPointer, Error> { ) -> Result<OpPointer, Error> {
let cached = match self.0 { let cached = match self.0 {
btree_map::Entry::Occupied(e) => { btree_map::Entry::Occupied(e) => e.get().clone(),
e.get().clone()
}
btree_map::Entry::Vacant(e) => { btree_map::Entry::Vacant(e) => {
let v = Rc::new(f()?); let v = Rc::new(f()?);
e.insert(v.clone()); e.insert(v.clone());

View File

@ -78,7 +78,9 @@ impl From<regex::Error> for Error {
impl From<std::io::Error> for Error { impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self { fn from(e: std::io::Error) -> Self {
let msg = match dbg!(e.kind()) { let msg = match dbg!(e.kind()) {
io::ErrorKind::NotFound | io::ErrorKind::Other => format!("OSError: Path not found: {}", e), io::ErrorKind::NotFound | io::ErrorKind::Other => {
format!("OSError: Path not found: {}", e)
}
_ => format!("{}", e), _ => format!("{}", e),
}; };
Error { Error {

View File

@ -367,4 +367,4 @@ impl From<&Val> for Value {
} }
} }
} }
} }

View File

@ -12,11 +12,11 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use std::cell::RefCell; use std::cell::RefCell;
use std::fmt::Debug;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::rc::Rc; use std::rc::Rc;
use std::fmt::Debug;
use regex::Regex; use regex::Regex;
@ -123,11 +123,11 @@ impl Builtins {
// The canonicalize method on windows is not what we want so we'll // The canonicalize method on windows is not what we want so we'll
// do something a little different on windows that we would do on // do something a little different on windows that we would do on
// other Operating Systems. // other Operating Systems.
#[cfg(target_os="windows")] #[cfg(target_os = "windows")]
{ {
Ok(dbg!(normalized)) Ok(dbg!(normalized))
} }
#[cfg(not(target_os="windows"))] #[cfg(not(target_os = "windows"))]
{ {
Ok(dbg!(normalized.canonicalize()?)) Ok(dbg!(normalized.canonicalize()?))
} }
@ -156,7 +156,12 @@ impl Builtins {
} }
} }
fn get_file_as_string<P: Into<PathBuf>>(&self, base_path: P, path: &str, pos: Position) -> Result<String, Error> { fn get_file_as_string<P: Into<PathBuf>>(
&self,
base_path: P,
path: &str,
pos: Position,
) -> Result<String, Error> {
let sep = format!("{}", std::path::MAIN_SEPARATOR); let sep = format!("{}", std::path::MAIN_SEPARATOR);
let raw_path = path.replace("/", &sep); let raw_path = path.replace("/", &sep);
// FIXME(jwall): import paths? // FIXME(jwall): import paths?
@ -180,40 +185,37 @@ impl Builtins {
where where
O: std::io::Write + Clone, O: std::io::Write + Clone,
E: std::io::Write + Clone, E: std::io::Write + Clone,
P: Into<PathBuf> + Clone + Debug, P: Into<PathBuf> + Clone + Debug,
{ {
let path = stack.pop(); let path = stack.pop();
if let Some((val, path_pos)) = path { if let Some((val, path_pos)) = path {
if let &Value::P(Str(ref path)) = val.as_ref() { if let &Value::P(Str(ref path)) = val.as_ref() {
// TODO(jwall): A bit hacky we should probably change import stacks to be pathbufs. // TODO(jwall): A bit hacky we should probably change import stacks to be pathbufs.
let normalized = decorate_error!(path_pos => self.normalize_path(base_path, false, dbg!(path)))?; let normalized =
decorate_error!(path_pos => self.normalize_path(base_path, false, dbg!(path)))?;
// first we chack the cache // first we chack the cache
let path = dbg!(normalized.to_string_lossy().to_string()); let path = dbg!(normalized.to_string_lossy().to_string());
if let Some(val) = env.borrow().get_cached_path_val(&path) { if let Some(val) = env.borrow().get_cached_path_val(&path) {
stack.push((val, path_pos)); stack.push((val, path_pos));
return Ok(()); return Ok(());
} }
if import_stack if import_stack.iter().find(|p| *p == &path).is_some() {
.iter() return Err(Error::new(
.find(|p| *p == &path) format!("Import cycle detected: {} in {:?}", path, import_stack),
.is_some() { pos,
return Err(Error::new( ));
format!("Import cycle detected: {} in {:?}", path, import_stack),
pos));
} }
let val = { let val = { env.borrow_mut().get_cached_path_val(&path) };
env.borrow_mut().get_cached_path_val(&path)
};
match val { match val {
Some(v) => { Some(v) => {
stack.push((v, path_pos)); stack.push((v, path_pos));
} }
None => { None => {
let op_pointer = let op_pointer = decorate_error!(path_pos => env.borrow_mut().get_ops_for_path(&normalized))?;
decorate_error!(path_pos => env.borrow_mut().get_ops_for_path(&normalized))?;
// TODO(jwall): What if we don't have a base path? // TODO(jwall): What if we don't have a base path?
let mut vm = VM::with_pointer(op_pointer, env.clone(), normalized.parent().unwrap()) let mut vm =
.with_import_stack(import_stack.clone()); VM::with_pointer(op_pointer, env.clone(), normalized.parent().unwrap())
.with_import_stack(import_stack.clone());
vm.run()?; vm.run()?;
let result = Rc::new(vm.symbols_to_tuple(true)); let result = Rc::new(vm.symbols_to_tuple(true));
env.borrow_mut().update_path_val(&path, result.clone()); env.borrow_mut().update_path_val(&path, result.clone());
@ -265,7 +267,11 @@ impl Builtins {
}; };
if typ == "str" { if typ == "str" {
stack.push(( stack.push((
Rc::new(P(Str(self.get_file_as_string(base_path, &path, pos.clone())?))), Rc::new(P(Str(self.get_file_as_string(
base_path,
&path,
pos.clone(),
)?))),
pos.clone(), pos.clone(),
)); ));
} else { } else {
@ -368,7 +374,8 @@ impl Builtins {
if env.borrow().get_out_lock_for_path(&path) { if env.borrow().get_out_lock_for_path(&path) {
return Err(Error::new( return Err(Error::new(
format!("You can only have one output per file"), format!("You can only have one output per file"),
pos)); pos,
));
} }
env.borrow_mut().set_out_lock_for_path(path.as_ref()); env.borrow_mut().set_out_lock_for_path(path.as_ref());
Some(write_path) Some(write_path)
@ -376,7 +383,8 @@ impl Builtins {
if env.borrow().get_out_lock_for_path("/dev/stdout") { if env.borrow().get_out_lock_for_path("/dev/stdout") {
return Err(Error::new( return Err(Error::new(
format!("You can only have one output per file"), format!("You can only have one output per file"),
pos)); pos,
));
} }
env.borrow_mut().set_out_lock_for_path("/dev/stdout"); env.borrow_mut().set_out_lock_for_path("/dev/stdout");
None None
@ -393,7 +401,7 @@ impl Builtins {
Some(p) => { Some(p) => {
let p = p.with_extension(c.file_ext()); let p = p.with_extension(c.file_ext());
Box::new(File::create(&p)?) Box::new(File::create(&p)?)
}, }
None => Box::new(stdout), None => Box::new(stdout),
}; };
if let Err(e) = c.convert(Rc::new(val), &mut writer) { if let Err(e) = c.convert(Rc::new(val), &mut writer) {
@ -625,7 +633,7 @@ impl Builtins {
_ => { _ => {
result_elems.push(e.clone()); result_elems.push(e.clone());
pos_elems.push(e_pos); pos_elems.push(e_pos);
}, }
} }
} }
stack.push((Rc::new(C(List(result_elems, pos_elems))), pos)); stack.push((Rc::new(C(List(result_elems, pos_elems))), pos));
@ -651,7 +659,7 @@ impl Builtins {
_ => { _ => {
new_fields.push((name.clone(), val.clone())); new_fields.push((name.clone(), val.clone()));
new_flds_pos_list.push((name_pos, val_pos)); new_flds_pos_list.push((name_pos, val_pos));
}, }
} }
} }
stack.push((Rc::new(C(Tuple(new_fields, new_flds_pos_list))), pos)); stack.push((Rc::new(C(Tuple(new_fields, new_flds_pos_list))), pos));

View File

@ -140,7 +140,7 @@ where
} }
return C(Tuple(flds, pos_list)); return C(Tuple(flds, pos_list));
} }
pub fn remove_symbol(&mut self, sym: &str) -> Option<(Rc<Value>, Position)> { pub fn remove_symbol(&mut self, sym: &str) -> Option<(Rc<Value>, Position)> {
self.symbols.remove_symbol(sym) self.symbols.remove_symbol(sym)
} }

View File

@ -165,11 +165,7 @@ impl Scope {
} }
} }
} }
Val::Boolean(_) Val::Boolean(_) | Val::Empty | Val::Float(_) | Val::Int(_) | Val::Str(_) => {
| Val::Empty
| Val::Float(_)
| Val::Int(_)
| Val::Str(_) => {
// noop // noop
} }
}; };

View File

@ -28,7 +28,7 @@ use std::process;
use std::rc::Rc; use std::rc::Rc;
use ucglib::build; use ucglib::build;
use ucglib::build::assets::{MemoryCache}; use ucglib::build::assets::MemoryCache;
use ucglib::convert::{ConverterRegistry, ImporterRegistry}; use ucglib::convert::{ConverterRegistry, ImporterRegistry};
use ucglib::iter::OffsetStrIter; use ucglib::iter::OffsetStrIter;
use ucglib::parse::parse; use ucglib::parse::parse;

View File

@ -472,50 +472,50 @@ make_fn!(
); );
//fn select_expression(input: SliceIter<Token>) -> Result<SliceIter<Token>, Expression> { //fn select_expression(input: SliceIter<Token>) -> Result<SliceIter<Token>, Expression> {
//let parsed = do_each!(input, //let parsed = do_each!(input,
//_ => word!("select"), //_ => word!("select"),
//val => do_each!( //val => do_each!(
//expr => wrap_err!( //expr => wrap_err!(
//trace_parse!(must!(expression)), //trace_parse!(must!(expression)),
//"Did you forget a comma after your selector value?"), //"Did you forget a comma after your selector value?"),
//_ => must!(punct!(",")), //_ => must!(punct!(",")),
//(expr) //(expr)
//), //),
//default_and_map => either!( //default_and_map => either!(
//do_each!( //do_each!(
//default => do_each!( //default => do_each!(
//expr => trace_parse!(expression), //expr => trace_parse!(expression),
//_ => punct!(","), //_ => punct!(","),
//(expr) //(expr)
//), //),
//map => trace_parse!(must!(tuple)), //map => trace_parse!(must!(tuple)),
//(Some(default), map) //(Some(default), map)
//), //),
//do_each!( //do_each!(
//map => wrap_err!( //map => wrap_err!(
//trace_parse!(must!(tuple)), //trace_parse!(must!(tuple)),
//"Did you forget a comma after your default value?" //"Did you forget a comma after your default value?"
//), //),
//(None, map) //(None, map)
//) //)
//), //),
//(val, default_and_map.0, default_and_map.1) //(val, default_and_map.0, default_and_map.1)
//); //);
//match parsed { //match parsed {
//Result::Abort(e) => Result::Abort(e), //Result::Abort(e) => Result::Abort(e),
//Result::Fail(e) => Result::Fail(e), //Result::Fail(e) => Result::Fail(e),
//Result::Incomplete(offset) => Result::Incomplete(offset), //Result::Incomplete(offset) => Result::Incomplete(offset),
//Result::Complete(rest, (val, default, map)) => { //Result::Complete(rest, (val, default, map)) => {
//match tuple_to_select(input.clone(), val, default, map) { //match tuple_to_select(input.clone(), val, default, map) {
//Ok(expr) => Result::Complete(rest, expr), //Ok(expr) => Result::Complete(rest, expr),
//Err(e) => Result::Fail(Error::caused_by( //Err(e) => Result::Fail(Error::caused_by(
//"Invalid Select Expression", //"Invalid Select Expression",
//Box::new(e), //Box::new(e),
//Box::new(rest.clone()), //Box::new(rest.clone()),
//)), //)),
//} //}
//} //}
//} //}
//} //}
make_fn!( make_fn!(