1
1
use budvm::{ir::Module, VirtualMachine};
2

            
3
1
fn main() {
4
1
    let mut vm = VirtualMachine::empty();
5
1
    let module = Module::from_asm(
6
1
        r#"
7
1
            // Any loose assembly before function calls will be gathered into
8
1
            // the module's init function, which will be executed when the
9
1
            // module is loaded into a virtual machine.
10
1
            //
11
1
            // The init function in this case calls the fibonacci function with
12
1
            // 1 argument: 10.
13
1
            push 10
14
1
            call fibonacci 1 $
15
1

            
16
1
            // Define the recursive fibonacci function with one parameter, @n
17
1
            function fibonacci @n
18
1
            // If @n is less than 2 or equal to 2, return 1
19
1
            lte @n 2 jump #if_greater_than_two
20
1
            return 1
21
1

            
22
1
            // Otherwise, recurse twice, passing in n-1 and n-2, and then sum
23
1
            // the results.
24
1
            #if_greater_than_two
25
1
            sub @n 1 $
26
1
            recurse 1 $fib_n_minus_1
27
1
            sub @n 2 $
28
1
            recurse 1 $fib_n_minus_2
29
1
            add $fib_n_minus_1 $fib_n_minus_2 $$
30
1
        "#,
31
1
    )
32
1
    .unwrap();
33
1
    let result: i64 = module.load_into(&mut vm).unwrap();
34
1
    assert_eq!(result, 55);
35
1
}
36

            
37
1
#[test]
38
1
fn runs() {
39
1
    main()
40
1
}