about summary refs log tree commit diff stats
path: root/arc/edit.mu
Commit message (Expand)AuthorAgeFilesLines
* 1276 - make C++ version the defaultKartik K. Agaram2015-05-051-0/+18
m <vc@akkartik.com> 2021-03-04 00:11:23 -0800 committer Kartik K. Agaram <vc@akkartik.com> 2021-03-04 00:11:23 -0800 7846' href='/akkartik/mu/commit/linux/factorial.mu?h=main&id=2d306e2a989386b41eb9626324f6702a64e87b77'>2d306e2a ^
6db05611 ^





480fd995 ^




9428990b ^
16f2bd11 ^
189d34dc ^
17623a62 ^
7dd5a41e ^
17623a62 ^
189d34dc ^

17623a62 ^
189d34dc ^
17623a62 ^





189d34dc ^



7dd5a41e ^
189d34dc ^

0102aa37 ^
e8ffaf29 ^
9635163b ^

17623a62 ^














8eec3eae ^
17623a62 ^
ea62afb1 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60


                                                                    
                              





                                            




                                                                            
 
                         
 
                                   
              
                        

              
            
   





                                      



                                    
                                                     

 
                                                                   
                                                                  

                                 














                                                       
   
          
 
# compute the factorial of 5, and return the result in the exit code
#
# To run:
#   $ ./translate factorial.mu
#   $ ./a.elf
#   $ echo $?
#   120
#
# You can also run the automated test suite:
#   $ ./a.elf test
# Expected output:
#   ........
# Every '.' indicates a passing test. Failing tests get a 'F'.
# There's only one test in this file, but you'll also see tests running from
# Mu's standard library.
#
# Compare factorial4.subx

fn factorial n: int -> _/eax: int {
  compare n, 1
  # if (n <= 1) return 1
  {
    break-if->
    return 1
  }
  # n > 1; return n * factorial(n-1)
  var tmp/ecx: int <- copy n
  tmp <- decrement
  var result/eax: int <- factorial tmp
  result <- multiply n
  return result
}

fn test-factorial {
  var result/eax: int <- factorial 5
  check-ints-equal result, 0x78, "F - test-factorial"
}

fn main args-on-stack: (addr array addr array byte) -> _/ebx: int {
  var args/eax: (addr array addr array byte) <- copy args-on-stack
  # len = length(args)
  var len/ecx: int <- length args
  # if (len <= 1) return factorial(5)
  compare len, 1
  {
    break-if->
    var exit-status/eax: int <- factorial 5
    return exit-status
  }
  # if (args[1] == "test") run-tests()
  var tmp2/ecx: (addr addr array byte) <- index args, 1
  var tmp3/eax: boolean <- string-equal? *tmp2, "test"
  compare tmp3, 0
  {
    break-if-=
    run-tests
    # TODO: get at Num-test-failures somehow
  }
  return 0
}