about summary refs log blame commit diff stats
path: root/linux/factorial.mu
blob: 7eccc1ec5958d4de5e9e282415be4a620341a497 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
pre { line-height: 125%; } td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; } td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; } .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .hi
# 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
}