diff --git a/src/builtins/mod.rs b/src/builtins/mod.rs index 12052e976..81bf00d15 100644 --- a/src/builtins/mod.rs +++ b/src/builtins/mod.rs @@ -8,7 +8,7 @@ use hvmc::{ast, host::Host, stdlib::LogDef}; use crate::{ readback_hvmc, term::{ - builtins::{SCONS, SNIL}, + builtins::{RESULT_ERR, RESULT_OK, SCONS, SNIL}, term_to_net::Labels, AdtEncoding, Book, Term, }, @@ -27,7 +27,8 @@ pub mod util; pub const CORE_BUILTINS: [&str; 7] = ["HVM.log", "HVM.black_box", "HVM.print", "HVM.query", "HVM.store", "HVM.load", "HVM.exit"]; /// List of definition names used by the core builtins -pub const CORE_BUILTINS_USES: [&[&str]; 7] = [&[], &[], &[], &[SCONS, SNIL], &[], &[], &[]]; +pub const CORE_BUILTINS_USES: [&[&str]; 7] = + [&[], &[], &[], &[SCONS, SNIL], &[RESULT_OK, RESULT_ERR], &[RESULT_OK, RESULT_ERR], &[]]; /// Creates a host with the hvm-core primitive definitions built-in. /// This needs the book as an Arc because the closure that logs diff --git a/src/lib.rs b/src/lib.rs index c962e9f53..c87b31e2f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ #![feature(box_patterns)] #![feature(let_chains)] -use builtins::create_host; +use builtins::{create_host, CORE_BUILTINS_USES}; use diagnostics::{DiagnosticOrigin, Diagnostics, DiagnosticsConfig, Severity}; use hvmc::{ ast::Net, @@ -63,7 +63,10 @@ pub fn compile_book( diagnostics.fatal(())?; } if opts.prune { - core_book.prune(&[book.hvmc_entrypoint().to_string()]); + let mut prune_entrypoints = vec![book.hvmc_entrypoint().to_string()]; + let mut builtin_uses = CORE_BUILTINS_USES.concat().iter().map(|x| x.to_string()).collect::>(); + prune_entrypoints.append(&mut builtin_uses); + core_book.prune(&prune_entrypoints); } mutual_recursion::check_cycles(&core_book, &mut diagnostics)?; diff --git a/tests/golden_tests.rs b/tests/golden_tests.rs index 2d8196c2b..0e538dfba 100644 --- a/tests/golden_tests.rs +++ b/tests/golden_tests.rs @@ -357,3 +357,27 @@ fn mutual_recursion() { Ok(format!("{}{}", res.diagnostics, res.core_book)) }) } + +#[test] +fn io() { + run_golden_test_dir_multiple(function_name!(), &[ + (&|code, path| { + let book = do_parse_book(code, path)?; + + let mut desugar_opts = CompileOpts::light(); + desugar_opts.lazy_mode(); + + // 1 million nodes for the test runtime. Smaller doesn't seem to make it any faster + let (res, info) = + run_book(book, None, RunOpts::lazy(), desugar_opts, DiagnosticsConfig::default(), None)?; + Ok(format!("Lazy mode:\n{}{}", info.diagnostics, res)) + }), + (&|code, path| { + let book = do_parse_book(code, path)?; + + let (res, info) = + run_book(book, None, RunOpts::default(), CompileOpts::light(), DiagnosticsConfig::default(), None)?; + Ok(format!("Strict mode:\n{}{}", info.diagnostics, res)) + }), + ]) +} diff --git a/tests/golden_tests/io/load.hvm b/tests/golden_tests/io/load.hvm new file mode 100644 index 000000000..2deeb3931 --- /dev/null +++ b/tests/golden_tests/io/load.hvm @@ -0,0 +1,6 @@ +(Main) = + use path = "tests/golden_tests/io/load.txt" + (HVM.load path @result match result { + Result.ok: result.val; + Result.err: result.val; + }) diff --git a/tests/golden_tests/io/load.txt b/tests/golden_tests/io/load.txt new file mode 100644 index 000000000..dfe437bde --- /dev/null +++ b/tests/golden_tests/io/load.txt @@ -0,0 +1 @@ +Contents diff --git a/tests/golden_tests/io/load_fail.hvm b/tests/golden_tests/io/load_fail.hvm new file mode 100644 index 000000000..734831453 --- /dev/null +++ b/tests/golden_tests/io/load_fail.hvm @@ -0,0 +1,6 @@ +(Main) = + use path = "tests/golden_tests/io/load_fail.txt" + (HVM.load path @result match result { + Result.ok: result.val; + Result.err: result.val; + }) diff --git a/tests/golden_tests/io/store.hvm b/tests/golden_tests/io/store.hvm new file mode 100644 index 000000000..1b6b47174 --- /dev/null +++ b/tests/golden_tests/io/store.hvm @@ -0,0 +1,6 @@ +(Main) = + use path = "tests/golden_tests/io/store.txt" + (HVM.store path "(Main) = 0" @result match result { + Result.ok: result.val; + Result.err: result.val; + }) diff --git a/tests/golden_tests/io/store.txt b/tests/golden_tests/io/store.txt new file mode 100644 index 000000000..475ff38f2 --- /dev/null +++ b/tests/golden_tests/io/store.txt @@ -0,0 +1 @@ +(Main) = 0 \ No newline at end of file diff --git a/tests/golden_tests/io/store_fail.hvm b/tests/golden_tests/io/store_fail.hvm new file mode 100644 index 000000000..b6cc8abb8 --- /dev/null +++ b/tests/golden_tests/io/store_fail.hvm @@ -0,0 +1,6 @@ +(Main) = + use path = "tests/golden_tests/io/missing_dir/store_fail.txt" + (HVM.store path "(Main) = 0" @result match result { + Result.ok: result.val; + Result.err: result.val; + }) diff --git a/tests/snapshots/io__load.hvm.snap b/tests/snapshots/io__load.hvm.snap new file mode 100644 index 000000000..5db3c9ff7 --- /dev/null +++ b/tests/snapshots/io__load.hvm.snap @@ -0,0 +1,8 @@ +--- +source: tests/golden_tests.rs +input_file: tests/golden_tests/io/load.hvm +--- +Lazy mode: +"Contents\n" +Strict mode: +"Contents\n" diff --git a/tests/snapshots/io__load_fail.hvm.snap b/tests/snapshots/io__load_fail.hvm.snap new file mode 100644 index 000000000..f8bedf75d --- /dev/null +++ b/tests/snapshots/io__load_fail.hvm.snap @@ -0,0 +1,8 @@ +--- +source: tests/golden_tests.rs +input_file: tests/golden_tests/io/load_fail.hvm +--- +Lazy mode: +"Filesystem error: No such file or directory (os error 2)" +Strict mode: +"Filesystem error: No such file or directory (os error 2)" diff --git a/tests/snapshots/io__store.hvm.snap b/tests/snapshots/io__store.hvm.snap new file mode 100644 index 000000000..69efd2406 --- /dev/null +++ b/tests/snapshots/io__store.hvm.snap @@ -0,0 +1,8 @@ +--- +source: tests/golden_tests.rs +input_file: tests/golden_tests/io/store.hvm +--- +Lazy mode: +* +Strict mode: +* diff --git a/tests/snapshots/io__store_fail.hvm.snap b/tests/snapshots/io__store_fail.hvm.snap new file mode 100644 index 000000000..5d2288990 --- /dev/null +++ b/tests/snapshots/io__store_fail.hvm.snap @@ -0,0 +1,8 @@ +--- +source: tests/golden_tests.rs +input_file: tests/golden_tests/io/store_fail.hvm +--- +Lazy mode: +"Filesystem error: No such file or directory (os error 2)" +Strict mode: +"Filesystem error: No such file or directory (os error 2)"