diff --git a/repl/src/main.rs b/repl/src/main.rs index 0560e5986b..52e1fec837 100644 --- a/repl/src/main.rs +++ b/repl/src/main.rs @@ -79,7 +79,7 @@ impl ::std::str::FromStr for Color { "always" => Always, "always-ansi" => AlwaysAnsi, "never" => Never, - _ => return Err("Expected on of auto, always, always-ansi, never"), + _ => return Err("Expected one of 'auto', 'always', 'always-ansi', 'never'"), }) } } @@ -129,6 +129,12 @@ pub struct Opt { )] debug_level: base::DebugLevel, + #[structopt( + long = "no-std", + help = "Skip searching the internal standard library for requested modules." + )] + no_std: bool, + #[structopt(name = "FILE", help = "Executes each file as a gluon program")] input: Vec, @@ -155,8 +161,8 @@ fn init_env_logger() { #[cfg(not(feature = "env_logger"))] fn init_env_logger() {} -fn format(file: &str, file_map: Arc) -> Result { - let mut compiler = Compiler::new(); +fn format(file: &str, file_map: Arc, opt: &Opt) -> Result { + let mut compiler = Compiler::new().use_standard_lib(!opt.no_std); let thread = new_vm(); Ok(compiler.format_expr( @@ -167,7 +173,7 @@ fn format(file: &str, file_map: Arc) -> Result { )?) } -fn fmt_file(name: &Path) -> Result<()> { +fn fmt_file(name: &Path, opt: &Opt) -> Result<()> { use std::fs::File; use std::io::Read; @@ -180,7 +186,7 @@ fn fmt_file(name: &Path) -> Result<()> { let module_name = filename_to_module(&name.display().to_string()); let mut code_map = codespan::CodeMap::new(); let file_map = code_map.add_filemap(module_name.clone().into(), buffer); - let formatted = format(&module_name, file_map.clone())?; + let formatted = format(&module_name, file_map.clone(), opt)?; // Avoid touching the .glu file if it did not change if file_map.src() != formatted { @@ -196,7 +202,7 @@ fn fmt_file(name: &Path) -> Result<()> { Ok(()) } -fn fmt_stdio() -> Result<()> { +fn fmt_stdio(opt: &Opt) -> Result<()> { use std::io::{stdin, stdout, Read}; let mut buffer = String::new(); @@ -205,7 +211,7 @@ fn fmt_stdio() -> Result<()> { let mut code_map = codespan::CodeMap::new(); let file_map = code_map.add_filemap("STDIN".into(), buffer); - let formatted = format("STDIN", file_map)?; + let formatted = format("STDIN", file_map, opt)?; stdout().write_all(formatted.as_bytes())?; Ok(()) } @@ -241,10 +247,10 @@ fn run( gluon_files.dedup(); for file in gluon_files { - fmt_file(&file)?; + fmt_file(&file, opt)?; } } else { - fmt_stdio()?; + fmt_stdio(opt)?; } } Some(SubOpt::Doc(ref doc_opt)) => { @@ -275,7 +281,9 @@ fn main() { let opt = Opt::from_args(); - let mut compiler = Compiler::new().run_io(true); + let mut compiler = Compiler::new() + .run_io(true) + .use_standard_lib(!opt.no_std); let vm = new_vm(); if let Err(err) = run(&opt, &mut compiler, opt.color, &vm) { diff --git a/src/import.rs b/src/import.rs index d6feadfb75..e918b97c62 100644 --- a/src/import.rs +++ b/src/import.rs @@ -171,6 +171,7 @@ impl Import { fn get_unloaded_module( &self, + compiler: &mut Compiler, vm: &Thread, module: &str, filename: &str, @@ -180,10 +181,11 @@ impl Import { // Retrieve the source, first looking in the standard library included in the // binary - let std_file = STD_LIBS.iter().find(|tup| tup.0 == module); - if let Some(tup) = std_file { - return Ok(UnloadedModule::Source(Cow::Borrowed(tup.1))); - } + let std_file = if compiler.settings.use_standard_lib { + STD_LIBS.iter().find(|tup| tup.0 == module) + } else { + None + }; Ok(match std_file { Some(tup) => UnloadedModule::Source(Cow::Borrowed(tup.1)), None => { @@ -309,7 +311,7 @@ impl Import { // Retrieve the source, first looking in the standard library included in the // binary let unloaded_module = self - .get_unloaded_module(vm, &modulename, &filename) + .get_unloaded_module(compiler, vm, &modulename, &filename) .map_err(|err| (None, err.into()))?; match unloaded_module { diff --git a/src/lib.rs b/src/lib.rs index 7334160eb7..01d0ee5b74 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -256,6 +256,7 @@ struct Settings { emit_debug_info: bool, run_io: bool, full_metadata: bool, + use_standard_lib: bool, } impl Default for Settings { @@ -265,6 +266,7 @@ impl Default for Settings { emit_debug_info: true, run_io: false, full_metadata: false, + use_standard_lib: true, } } } @@ -345,6 +347,12 @@ impl Compiler { full_metadata set_full_metadata: bool } + option_settings! { + /// Sets whether internal standard library is searched for requested modules + /// (default: true) + use_standard_lib set_use_standard_lib: bool + } + fn state(&self) -> MutexGuard { self.state.lock().unwrap() }