Change examples test to read from examples directory

This commit is contained in:
imaqtkatt 2024-04-09 20:24:25 -03:00
parent 60e4cc6eb4
commit 10559b6df7
11 changed files with 140 additions and 11 deletions

18
examples/all_tree.hvm Normal file
View File

@ -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)
}

View File

@ -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)))))))

View File

@ -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 <path to file>"
// 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)

11
examples/fib.hvm Normal file
View File

@ -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)

View File

@ -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

24
examples/neg_fusion.hvm Normal file
View File

@ -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)

View File

@ -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(())
}

View File

@ -0,0 +1,5 @@
---
source: tests/golden_tests.rs
input_file: examples/callcc.hvm
---
52

View File

@ -0,0 +1,5 @@
---
source: tests/golden_tests.rs
input_file: examples/example.hvm
---
8

View File

@ -0,0 +1,5 @@
---
source: tests/golden_tests.rs
input_file: examples/fusing_add.hvm
---
λa λb λ* (b λc λ* (c a))

View File

@ -0,0 +1,5 @@
---
source: tests/golden_tests.rs
input_file: examples/fusing_not.hvm
---
λa λb λc (a b c)