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