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