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 # Expected output:
12 #   ........
13 # Every '.' indicates a passing test. Failing tests get a 'F'.
14 # There's only one test in this file, but you'll also see tests running from
15 # Mu's standard library.
16 #
17 # Compare apps/factorial4.subx
18 
19 fn factorial n: int -> _/eax: int {
20   compare n, 1
21   # if (n <= 1) return 1
22   {
23     break-if->
24     return 1
25   }
26   # n > 1; return n * factorial(n-1)
27   var tmp/ecx: int <- copy n
28   tmp <- decrement
29   var result/eax: int <- factorial tmp
30   result <- multiply n
31   return result
32 }
33 
34 fn test-factorial {
35   var result/eax: int <- factorial 5
36   check-ints-equal result, 0x78, "F - test-factorial"
37 }
38 
39 fn main args-on-stack: (addr array (addr array byte)) -> _/ebx: int {
40   var args/eax: (addr array addr array byte) <- copy args-on-stack
41   # len = length(args)
42   var len/ecx: int <- length args
43   # if (len <= 1) return factorial(5)
44   compare len, 1
45   {
46     break-if->
47     var exit-status/eax: int <- factorial 5
48     return exit-status
49   }
50   # if (args[1] == "test") run-tests()
51   var tmp2/ecx: (addr addr array byte) <- index args, 1
52   var tmp3/eax: boolean <- string-equal? *tmp2, "test"
53   compare tmp3, 0
54   {
55     break-if-=
56     run-tests
57     # TODO: get at Num-test-failures somehow
58   }
59   return 0
60 }