\ -*- mode: forth -*- \ $Id: wc.bigforth,v 1.1 2001/06/24 22:27:35 doug Exp $ \ http://www.bagley.org/~doug/shootout/ variable nn 0 nn ! \ number of newlines variable nw 0 nw ! \ number of words variable nc 0 nc ! \ number of chars variable in_word 0 in_word ! \ flag: "in word" 10 constant nl_ch 9 constant tab_ch 32 constant space_ch 4096 constant MAXREAD create buff MAXREAD allot \ scan the buffer and count lines, words, chars : scanbuff dup nc +! \ update nc with amount of chars in buffer buff + buff \ from start of buff to buff + n do i c@ case nl_ch of 0 in_word ! 1 nn +! endof tab_ch of 0 in_word ! endof space_ch of 0 in_word ! endof \ otherwise: in_word @ 0= if 1 in_word ! 1 nw +! then endcase loop ; : wc buff begin buff MAXREAD stdin read-file throw dup while scanbuff repeat ; wc nn @ . nw @ . nc @ 1 u.r cr bye \ th-th-that's all folks!