about summary refs log blame commit diff stats
path: root/archive/0.vm.arc/tangle.mu
blob: 3e73dd891f766cc2306acc7211c3c7e04e8d67c5 (plain) (tree)
1
2
3
4
5
6
7
8




                                                                             
                     
                                                               
                           





                

                   


                                              



                        
; To demonstrate tangle directives, we'll construct a factorial function with
; separate base and recursive cases. Compare factorial.mu.
; This isn't a very realistic example, just a simple demonstration of
; possibilities.

(function factorial [
  (default-space:space-address <- new space:literal 30:literal)
  (n:integer <- next-input)
  { begin
    base-case
  }
  recursive-case
])

(after base-case [
  ; if n=0 return 1
  (zero?:boolean <- equal n:integer 0:literal)
  (break-unless zero?:boolean)
  (reply 1:literal)
])

(after recursive-case [
  ; return n*factorial(n-1)
  (x:integer <- subtract n:integer 1:literal)
  (subresult:integer <- factorial x:integer)
  (result:integer <- multiply subresult:integer n:integer)
  (reply result:integer)
])

(function main [
  (1:integer <- factorial 5:literal)
  ($print (("result: " literal)))
  (print-integer nil:literal/terminal 1:integer)
  (print-character nil:literal/terminal ((#\newline literal)))
])
. . . 4/imm32 # add to esp # . clear-stream($_test-buffered-file->buffer) # . . push args 68/push $_test-buffered-file->buffer/imm32 # . . call e8/call clear-stream/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp # read-byte-buffered(_test-buffered-file) # . . push args 68/push _test-buffered-file/imm32 # . . call e8/call read-byte-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp # check-ints-equal(eax, 0xffffffff, msg) # . . push args 68/push "F - test-read-byte-buffered-end-of-file"/imm32 68/push 0xffffffff/imm32/Eof 50/push-eax # . . call e8/call check-ints-equal/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp # . end c3/return test-read-byte-buffered-refills-buffer: # - consume buffered-file's buffer, check that next read-byte-buffered still works # setup # . clear-stream(_test-stream) # . . push args 68/push _test-stream/imm32 # . . call e8/call clear-stream/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp # . clear-stream($_test-buffered-file->buffer) # . . push args 68/push $_test-buffered-file->buffer/imm32 # . . call e8/call clear-stream/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp # . write(_test-stream, "Abcdefgh") # . . push args 68/push "Abcdefgh"/imm32 68/push _test-stream/imm32 # . . call e8/call write/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp # pretend buffer is full # . _test-buffered-file->read = 6 # >= _test-buffered-file->size b8/copy-to-eax _test-buffered-file/imm32 c7 0/subop/copy 1/mod/*+disp8 0/rm32/eax . . . . 8/disp8 6/imm32 # copy to *(eax+8) # read-byte-buffered(_test-buffered-file) # . . push args 68/push _test-buffered-file/imm32 # . . call e8/call read-byte-buffered/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp # check-ints-equal(eax, 'A', msg) # . . push args 68/push "F - test-read-byte-buffered-refills-buffer"/imm32 68/push 0x41/imm32 50/push-eax # . . call e8/call check-ints-equal/disp32 # . . discard args 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp # . end c3/return == data # a test buffered file for _test-stream _test-buffered-file: # buffered-file # file descriptor or (addr stream byte) _test-stream/imm32 $_test-buffered-file->buffer: # current write index 0/imm32 # current read index 0/imm32 # size 6/imm32 # data 00 00 00 00 00 00 # 6 bytes _test-input-stream: # (stream byte) # current write index 0/imm32 # current read index 0/imm32 # size 0x100/imm32 # 256 bytes # data (16 lines x 16 bytes/line) 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 # a test buffered file for _test-input-stream _test-input-buffered-file: # buffered-file # file descriptor or (addr stream byte) _test-input-stream/imm32 $_test-input-buffered-file->buffer: # current write index 0/imm32 # current read index 0/imm32 # size 6/imm32 # data 00 00 00 00 00 00 # 6 bytes # . . vim:nowrap:textwidth=0