From 3350c34a74844e21ea69077e01efff3bae64bdcd Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Tue, 23 Mar 2021 17:31:08 -0700 Subject: . --- html/linux/factorial.mu.html | 123 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 html/linux/factorial.mu.html (limited to 'html/linux/factorial.mu.html') diff --git a/html/linux/factorial.mu.html b/html/linux/factorial.mu.html new file mode 100644 index 00000000..1d811ae7 --- /dev/null +++ b/html/linux/factorial.mu.html @@ -0,0 +1,123 @@ + + + + +Mu - linux/factorial.mu + + + + + + + + + + +https://github.com/akkartik/mu/blob/main/linux/factorial.mu +
+ 1 # compute the factorial of 5, and return the result in the exit code
+ 2 #
+ 3 # To run:
+ 4 #   $ ./translate 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 }
+
+ + + -- cgit 1.4.1-2-gfad0