https://github.com/akkartik/mu/blob/master/apps/factorial.mu
 1 # usage:
 2 #   ./translate_mu apps/factorial.mu
 3 #   ./a.elf test  # to run tests
 4 #   ./a.elf       # to run factorial(5)
 5 fn main args: (addr array kernel-string) -> exit-status/ebx: int {
 6   var a/eax: (addr array kernel-string) <- copy args
 7   var tmp/ecx: int <- length a
 8   $main-body: {
 9     compare tmp, 1
10     # if (len(args) == 1) factorial(5)
11     {
12       break-if-!=
13       var tmp/eax: int <- factorial 5
14       exit-status <- copy tmp
15       break $main-body
16     }
17     # if (args[1] == "test") run-tests()
18     var tmp2/ecx: int <- copy 1  # we need this just because we don't yet support `index` on literals; requires some translation-time computation
19     var tmp3/ecx: (addr kernel-string) <- index a, tmp2
20     var tmp4/eax: boolean <- kernel-string-equal? *tmp3, "test"
21     compare tmp4, 0
22     {
23       break-if-=
24       run-tests
25       exit-status <- copy 0  # TODO: get at Num-test-failures somehow
26     }
27   }
28 }
29 
30 fn factorial n: int -> result/eax: int {
31   compare n 1
32   {
33     break-if->
34     result <- copy 1
35   }
36   {
37     break-if-<=
38     var tmp/ecx: int <- copy n
39     tmp <- decrement
40     result <- factorial tmp
41     result <- multiply n
42   }
43 }
44 
45 fn test-factorial {
46   var result/eax: int <- factorial 5
47   check-ints-equal result 0x78 "F - test-factorial"
48 }