From 10559b6df7347f6893549ef011eaf75a16b3ca76 Mon Sep 17 00:00:00 2001 From: imaqtkatt Date: Tue, 9 Apr 2024 20:24:25 -0300 Subject: [PATCH] Change examples test to read from examples directory --- examples/all_tree.hvm | 18 +++++++++++ examples/alloc_small_tree.hvm | 18 +++++++++++ examples/example.hvm | 11 ++++--- examples/fib.hvm | 11 +++++++ examples/gen_tree_kind2.hvm | 17 ++++++++++ examples/neg_fusion.hvm | 24 ++++++++++++++ tests/golden_tests.rs | 32 +++++++++++++++---- tests/snapshots/examples__callcc.hvm.snap | 5 +++ tests/snapshots/examples__example.hvm.snap | 5 +++ tests/snapshots/examples__fusing_add.hvm.snap | 5 +++ tests/snapshots/examples__fusing_not.hvm.snap | 5 +++ 11 files changed, 140 insertions(+), 11 deletions(-) create mode 100644 examples/all_tree.hvm create mode 100644 examples/alloc_small_tree.hvm create mode 100644 examples/fib.hvm create mode 100644 examples/gen_tree_kind2.hvm create mode 100644 examples/neg_fusion.hvm create mode 100644 tests/snapshots/examples__callcc.hvm.snap create mode 100644 tests/snapshots/examples__example.hvm.snap create mode 100644 tests/snapshots/examples__fusing_add.hvm.snap create mode 100644 tests/snapshots/examples__fusing_not.hvm.snap diff --git a/examples/all_tree.hvm b/examples/all_tree.hvm new file mode 100644 index 000000000..de7909f9a --- /dev/null +++ b/examples/all_tree.hvm @@ -0,0 +1,18 @@ +data Bool = True | False + +data Tree + = (Node lft rgt) + | (Leaf val) + +and True True = True +and _ _ = False + +all (Node lft rgt) = (and (all lft) (all rgt)) +all (Leaf val) = val + +main = (all (gen 8)) + +gen = λn switch n { + 0: (Leaf True) + _: let tree = (gen n-1); (Node tree tree) +} diff --git a/examples/alloc_small_tree.hvm b/examples/alloc_small_tree.hvm new file mode 100644 index 000000000..c3f473319 --- /dev/null +++ b/examples/alloc_small_tree.hvm @@ -0,0 +1,18 @@ +T = λt λf t +F = λt λf f +And = λpλq (p q F) + +Z = λs λz (z) +S = λn λs λz (s n) + +Add = λa λb (a (S b)) +Mul = λa λb λf (a (b f)) +Pow = λa λb (a (Mul b) (S Z)) + +Node = λa λb λn λl (n a b) +Leaf = λn λl l + +Alloc = λn (n (λp (Node (Alloc p) (Alloc p))) Leaf) +Destroy = λt (t (λaλb (And (Destroy a) (Destroy b))) T) + +Main = (Destroy (Alloc (Pow (S (S Z)) (S (S (S (S Z))))))) diff --git a/examples/example.hvm b/examples/example.hvm index 22ece4e99..165135284 100644 --- a/examples/example.hvm +++ b/examples/example.hvm @@ -19,8 +19,8 @@ // You can use 'switch' to pattern match native numbers // The `_` arm binds the `scrutinee`-1 variable to the the value of the number -1 -(Num.pred) = λn - switch n { +(Num.pred) = λn + switch n { 0: 0 _: n-1 } @@ -52,8 +52,9 @@ data Boxed = (Box val) (Box.map (Box val) f) = (Box (f val)) (Box.unbox) = λbox - let (Box val) = box - val + match box { + Box: box.val + } // Use tuples to store two values together without needing to create a new data type (Tuple.new fst snd) = @@ -71,7 +72,7 @@ data Boxed = (Box val) // You can execute a program in HVM with "cargo run -- --run " // Other options are "--check" (the default mode) to just see if the file is well formed // and "--compile" to output hvm-core code. -(main) = +(main) = let tup = (Tuple.new None (Num.pred 5)) let fst = (Tuple.fst tup) diff --git a/examples/fib.hvm b/examples/fib.hvm new file mode 100644 index 000000000..25264e13f --- /dev/null +++ b/examples/fib.hvm @@ -0,0 +1,11 @@ +add = λa λb (+ a b) + +fib = λx switch x { + 0: 1 + _: let p = x-1; switch p { + 0: 1 + _: (+ (fib p) (fib p-1)) + } +} + +main = (fib 30) diff --git a/examples/gen_tree_kind2.hvm b/examples/gen_tree_kind2.hvm new file mode 100644 index 000000000..8d78ccac0 --- /dev/null +++ b/examples/gen_tree_kind2.hvm @@ -0,0 +1,17 @@ +_Char = 0 +_List = λ_T 0 +_List.cons = λ_head λ_tail λ_P λ_cons λ_nil ((_cons _head) _tail) +_List.nil = λ_P λ_cons λ_nil _nil +_Nat = 0 +_Nat.succ = λ_n λ_P λ_succ λ_zero (_succ _n) +_Nat.zero = λ_P λ_succ λ_zero _zero +_String = (_List _Char) +_String.cons = λ_head λ_tail λ_P λ_cons λ_nil ((_cons _head) _tail) +_String.nil = λ_P λ_cons λ_nil _nil +_Tree = λ_A 0 +_Tree.gen = λ_n λ_x switch _n = _n { 0: _Tree.leaf _: let _n-1 = _n-1 (((_Tree.node _x) ((_Tree.gen _n-1) (+ (* _x 2) 1))) ((_Tree.gen _n-1) (+ (* _x 2) 2))) } +_Tree.leaf = λ_P λ_node λ_leaf _leaf +_Tree.node = λ_val λ_lft λ_rgt λ_P λ_node λ_leaf (((_node _val) _lft) _rgt) +_main = ((_Tree.gen 8) 2) + +main = _main diff --git a/examples/neg_fusion.hvm b/examples/neg_fusion.hvm new file mode 100644 index 000000000..b7cdf538e --- /dev/null +++ b/examples/neg_fusion.hvm @@ -0,0 +1,24 @@ +// size = 1 << 8 + +Mul = λn λm λs (n (m s)) + +// Church nat +C2 = λf λx (f (f x)) + +// Church powers of two +P2 = (Mul C2 C2) // 4 +P3 = (Mul C2 P2) // 8 +P4 = (Mul C2 P3) // 16 +P5 = (Mul C2 P4) // 32 +P6 = (Mul C2 P5) // 64 +P7 = (Mul C2 P6) // 128 +P8 = (Mul C2 P7) // 256 + +// Booleans +True = λt λf t +False = λt λf f +Not = λb ((b False) True) +Neg = λb λt λf (b f t) + +// Negates 'true' 256 times: 'neg' is faster than 'not' because it fuses +Main = (P8 Neg True) diff --git a/tests/golden_tests.rs b/tests/golden_tests.rs index f5628d2e2..d94f30586 100644 --- a/tests/golden_tests.rs +++ b/tests/golden_tests.rs @@ -420,14 +420,34 @@ fn io() { } #[test] -fn examples() { - run_golden_test_dir(function_name!(), &|code, path| { - let book = do_parse_book(code, path)?; +fn examples() -> Result<(), Diagnostics> { + let examples_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("examples"); + + for entry in WalkDir::new(examples_path) + .min_depth(1) + .into_iter() + .filter_map(|e| e.ok()) + .filter(|e| e.path().extension().map_or(false, |ext| ext == "hvm")) + { + let path = entry.path(); + eprintln!("Testing {}", path.display()); + let code = std::fs::read_to_string(path).map_err(|e| e.to_string())?; + + let book = do_parse_book(&code, path).unwrap(); let mut compile_opts = CompileOpts::default_strict(); compile_opts.linearize_matches = hvml::OptLevel::Extra; let diagnostics_cfg = DiagnosticsConfig::default_strict(); - let (res, info) = run_book(book, None, RunOpts::default(), compile_opts, diagnostics_cfg, None)?; + let (res, _) = run_book(book, None, RunOpts::default(), compile_opts, diagnostics_cfg, None)?; - Ok(format!("{}{}", info.diagnostics, res)) - }) + let mut settings = insta::Settings::clone_current(); + settings.set_prepend_module_to_snapshot(false); + settings.set_omit_expression(true); + settings.set_input_file(path); + + settings.bind(|| { + assert_snapshot!(format!("examples__{}", path.file_name().unwrap().to_str().unwrap()), res); + }); + } + + Ok(()) } diff --git a/tests/snapshots/examples__callcc.hvm.snap b/tests/snapshots/examples__callcc.hvm.snap new file mode 100644 index 000000000..ddccfe651 --- /dev/null +++ b/tests/snapshots/examples__callcc.hvm.snap @@ -0,0 +1,5 @@ +--- +source: tests/golden_tests.rs +input_file: examples/callcc.hvm +--- +52 diff --git a/tests/snapshots/examples__example.hvm.snap b/tests/snapshots/examples__example.hvm.snap new file mode 100644 index 000000000..de75fbc52 --- /dev/null +++ b/tests/snapshots/examples__example.hvm.snap @@ -0,0 +1,5 @@ +--- +source: tests/golden_tests.rs +input_file: examples/example.hvm +--- +8 diff --git a/tests/snapshots/examples__fusing_add.hvm.snap b/tests/snapshots/examples__fusing_add.hvm.snap new file mode 100644 index 000000000..10ce2e145 --- /dev/null +++ b/tests/snapshots/examples__fusing_add.hvm.snap @@ -0,0 +1,5 @@ +--- +source: tests/golden_tests.rs +input_file: examples/fusing_add.hvm +--- +λa λb λ* (b λc λ* (c a)) diff --git a/tests/snapshots/examples__fusing_not.hvm.snap b/tests/snapshots/examples__fusing_not.hvm.snap new file mode 100644 index 000000000..16c731613 --- /dev/null +++ b/tests/snapshots/examples__fusing_not.hvm.snap @@ -0,0 +1,5 @@ +--- +source: tests/golden_tests.rs +input_file: examples/fusing_not.hvm +--- +λa λb λc (a b c)