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     # n <= 1; return 1
19     result <- copy 1
20   }
21   {
22     break-if-<=
23     # n > 1; return n * factorial(n-1)
24     var tmp/ecx: int <- copy n
25     tmp <- decrement
26     result <- factorial tmp
27     result <- multiply n
28   }
29 }
30 
31 fn test-factorial {
32   var result/eax: int <- factorial 5
33   check-ints-equal result, 0x78, "F - test-factorial"
34 }
35 
36 fn main args-on-stack: (addr array (addr array byte)) -> exit-status/ebx: int {
37   var args/eax: (addr array addr array byte) <- copy args-on-stack
38   # len = length(args)
39   var len/ecx: int <- length args
40   $main-body: {
41     # if (len <= 1) return factorial(5)
42     compare len, 1
43     {
44       break-if->
45       var tmp/eax: int <- factorial 5
46       exit-status <- copy tmp
47       break $main-body
48     }
49     # if (args[1] == "test") run-tests()
50     var tmp2/ecx: (addr addr array byte) <- index args, 1
51     var tmp3/eax: boolean <- string-equal? *tmp2, "test"
52     compare tmp3, 0
53     {
54       break-if-=
55       run-tests
56       exit-status <- copy 0  # TODO: get at Num-test-failures somehow
57     }
58   }
59 }