https://github.com/akkartik/mu/blob/master/apps/factorial.mu
 1 # compute the factorial of 5, and return the result in the exit code
 2 #
 3 # To run:
 4 #   $ ./translate_mu apps/factorial.mu
 5 #   $ ./a.elf
 6 #   $ echo $?
 7 #   120
 8 #
 9 # You can also run the automated test suite:
10 #   $ ./a.elf test
11 #
12 # Compare apps/factorial4.subx
13 
14 fn factorial n: int -> result/eax: int {
15   compare n 1
16   {
17     break-if->
18     result <- copy 1
19   }
20   {
21     break-if-<=
22     var tmp/ecx: int <- copy n
23     tmp <- decrement
24     result <- factorial tmp
25     result <- multiply n
26   }
27 }
28 
29 fn test-factorial {
30   var result/eax: int <- factorial 5
31   check-ints-equal result 0x78 "F - test-factorial"
32 }
33 
34 fn main args-on-stack: (addr array (addr array byte)) -> exit-status/ebx: int {
35   var args/eax: (addr array (addr array byte)) <- copy args-on-stack
36   var tmp/ecx: int <- length args
37   $main-body: {
38     # if (len(args) <= 1) factorial(5)
39     compare tmp, 1
40     {
41       break-if->
42       var tmp/eax: int <- factorial 5
43       exit-status <- copy tmp
44       break $main-body
45     }
46     # if (args[1] == "test") run-tests()
47     var tmp2/ecx: (addr addr array byte) <- index args, 1
48     var tmp3/eax: boolean <- string-equal? *tmp2, "test"
49     compare tmp3, 0
50     {
51       break-if-=
52       run-tests
53       exit-status <- copy 0  # TODO: get at Num-test-failures somehow
54     }
55   }
56 }