Skip to content

Commit

Permalink
Merge #775
Browse files Browse the repository at this point in the history
775: feat(repl): add a --no-std option to the standalone interpreter / REPL r=Marwes a=Etherian

## Description
Adds a command-line switch `--no-std` to the standalone interpreter / REPL which causes it to ignore its internal standard library modules when searching for modules to import.

This should make less awkward to use the REPL to test changes to the standard library.

resolves #751 

Co-authored-by: Etherian <etherain@gmail.com>
  • Loading branch information
bors[bot] and Etherian committed Aug 12, 2019
2 parents 4f1146e + f2c1819 commit ba66854
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
28 changes: 18 additions & 10 deletions repl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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'"),
})
}
}
Expand Down Expand Up @@ -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<String>,

Expand All @@ -155,8 +161,8 @@ fn init_env_logger() {
#[cfg(not(feature = "env_logger"))]
fn init_env_logger() {}

fn format(file: &str, file_map: Arc<codespan::FileMap>) -> Result<String> {
let mut compiler = Compiler::new();
fn format(file: &str, file_map: Arc<codespan::FileMap>, opt: &Opt) -> Result<String> {
let mut compiler = Compiler::new().use_standard_lib(!opt.no_std);
let thread = new_vm();

Ok(compiler.format_expr(
Expand All @@ -167,7 +173,7 @@ fn format(file: &str, file_map: Arc<codespan::FileMap>) -> Result<String> {
)?)
}

fn fmt_file(name: &Path) -> Result<()> {
fn fmt_file(name: &Path, opt: &Opt) -> Result<()> {
use std::fs::File;
use std::io::Read;

Expand All @@ -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 {
Expand All @@ -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();
Expand All @@ -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(())
}
Expand Down Expand Up @@ -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)) => {
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 7 additions & 5 deletions src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ impl<I> Import<I> {

fn get_unloaded_module(
&self,
compiler: &mut Compiler,
vm: &Thread,
module: &str,
filename: &str,
Expand All @@ -180,10 +181,11 @@ impl<I> Import<I> {
// 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 => {
Expand Down Expand Up @@ -309,7 +311,7 @@ impl<I> Import<I> {
// 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 {
Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ struct Settings {
emit_debug_info: bool,
run_io: bool,
full_metadata: bool,
use_standard_lib: bool,
}

impl Default for Settings {
Expand All @@ -265,6 +266,7 @@ impl Default for Settings {
emit_debug_info: true,
run_io: false,
full_metadata: false,
use_standard_lib: true,
}
}
}
Expand Down Expand Up @@ -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<State> {
self.state.lock().unwrap()
}
Expand Down

0 comments on commit ba66854

Please sign in to comment.