about summary refs log tree commit diff stats
path: root/archive/1.vm/factorial.mu
blob: cf2284b29506260ac806afa590762d950aeffa93 (plain) (blame)
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
# example program: compute the factorial of 5

def main [
  local-scope
  x:num <- factorial 5
  $print [result: ], x, [ 
]
]

def factorial n:num -> result:num [
  local-scope
  load-inputs
  {
    # if n=0 return 1
    zero?:bool <- equal n, 0
    break-unless zero?
    return 1
  }
  # return n * factorial(n-1)
  x:num <- subtract n, 1
  subresult:num <- factorial x
  result <- multiply subresult, n
]

# unit test
scenario factorial-test [
  run [
    1:num <- factorial 5
  ]
  memory-should-contain [
    1 <- 120
  ]
]
8d/copy-address *(eax+4) 0/r32/eax # (copy-bytes %edx %eax %ecx) $stream-to-array:end: # . restore registers 5e/pop-to-esi 5a/pop-to-edx 59/pop-to-ecx 58/pop-to-eax # . epilogue 89/<- %esp 5/r32/ebp 5d/pop-to-ebp c3/return test-stream-to-array: # . prologue 55/push-ebp 89/<- %ebp 4/r32/esp # setup (clear-stream _test-input-stream) (write _test-input-stream "abc") # skip something (read-byte _test-input-stream) # => eax # var out/ecx: (handle array byte) 68/push 0/imm32 68/push 0/imm32 89/<- %ecx 4/r32/esp # (stream-to-array _test-input-stream %ecx) (lookup *ecx *(ecx+4)) # => eax (check-strings-equal %eax "bc") # . epilogue 89/<- %esp 5/r32/ebp 5d/pop-to-ebp c3/return # like stream-to-array but ignore surrounding quotes # we might do other stuff here later unquote-stream-to-array: # in: (addr stream _), out: (addr handle array _) # . prologue 55/push-ebp 89/<- %ebp 4/r32/esp # . save registers 50/push-eax 51/push-ecx 52/push-edx 56/push-esi # esi = s 8b/-> *(ebp+8) 6/r32/esi # var len/ecx: int = s->write - s->read - 2 8b/-> *esi 1/r32/ecx 2b/subtract *(esi+4) 1/r32/ecx 81 7/subop/compare %ecx 2/imm32 7c/jump-if-< $unquote-stream-to-array:end/disp8 81 5/subop/subtract %ecx 2/imm32 # allocate (allocate-array Heap %ecx *(ebp+0xc)) # var in/edx: (addr byte) = s->data + s->read + 1 8b/-> *(esi+4) 2/r32/edx 8d/copy-address *(esi+edx+0xd) 2/r32/edx # Stream-data + 1 # var dest/eax: (addr byte) = data for out 8b/-> *(ebp+0xc) 0/r32/eax (lookup *eax *(eax+4)) # => eax 8d/copy-address *(eax+4) 0/r32/eax # (copy-bytes %edx %eax %ecx) $unquote-stream-to-array:end: # . restore registers 5e/pop-to-esi 5a/pop-to-edx 59/pop-to-ecx 58/pop-to-eax # . epilogue 89/<- %esp 5/r32/ebp 5d/pop-to-ebp c3/return