vextoly/cryn

[# word counter: reads stdin, prints "lines words chars" #]
make int64 lines
make int64 words
make int64 chars
make int64 inword
make string "\n" nl

define newline result nothing
  address nl
  output text
  set nothing
  return
end

define bump address int64 cell result nothing
  move cell
  read
  move at
  read
  add 1
  write
  set nothing
  return
end

define doline result nothing
  address lines
  push
  call bump
  set nothing
  return
end

define startword result nothing
  set 1
  move inword
  write
  address words
  push
  call bump
  set nothing
  return
end

define put int64 v result nothing
  move v
  read
  output
  set ' '
  output
  set nothing
  return
end

define main
  set 0
  move lines
  write
  set 0
  move words
  write
  set 0
  move chars
  write
  set 0
  move inword
  write
  set 0
  home

  mark next
  input
  compare -1
  jump done when equal
  push

  address chars
  push
  call bump

  pop
  compare 10
  jump newline when equal
  compare 32
  jump space when equal
  compare 9
  jump space when equal
  jump word

  mark newline
  call doline

  mark space
  set 0
  move inword
  write
  set 0
  home
  jump next

  mark word
  move inword
  read
  compare 0
  jump cont when different
  call startword

  mark cont
  set 0
  home
  jump next

  mark done
  move lines
  read
  push
  call put
  move words
  read
  push
  call put
  move chars
  read
  push
  call put
  call newline
  return
end