In article <871plws0m7.fsf@nightsong.com>,   
   Paul Rubin wrote:   
   >I'm playing with the idea of writing a Roff-like text formatter in   
   >Forth. The input is lines of text "blah blech, and this that the   
   >other...". The text lines can be arbitrarily long so I don't want to   
   >read the entire line into a memory buffer using something like REFILL.   
   >   
   >Let's say I don't have to worry about individual words overflowing   
   >memory though (segfault is not allowed, but it's ok to panic and quit).   
   >So the main loop will be to copy an input word to the output buffer and   
   >maybe flush the output buffer. The output buffer can be of fixed size.   
   >   
   >Also, some input lines will be formatting commands like ".i\n" (change   
   >font to italic). Those lines should be given to the Forth text   
   >interpreter.   
   >   
   >I guess I could use the FILE word set to write something like getc()   
   >with its own buffering, but that seems messy. I'm wondering if this is   
   >a common situation and there's an idiomatic solution.   
      
   Going character by character gets you little speed. Mostly   
   GET-FILE ("slurp-file") is the way to go. Then can you make the file   
   current input stream.   
   SAVE RESTORE SET-SRC and parsing like PARSE-NAME PARSE are your friend.   
      
   EXECUTE-PARSING is similar (bit 10 years after SAVE SET-SRC RESTORE.):   
   : EXECUTE-PARSING ROT ROT SAVE SET-SRC CATCH RESTORE THROW ;   
      
   \ ----------------------   
   #!/usr/bin/lina -s   
   \ Copyright 2015 (c): Albert van der Horst, Dutch Forth Worksshop by GPL   
      
   \ wc , using all tricks ciforth has to offer, in script style.   
   \ Usage: wc.script    
      
   ARGC 1 ?DO   
    1 ARG[] 2DUP TYPE SPACE   
    GET-FILE   
    2DUP 0 >R BEGIN ^J $/ 2DROP OVER WHILE R> 1+ >R REPEAT R> . 2DROP   
    2DUP SAVE SET-SRC 0 BEGIN NAME NIP WHILE 1+ REPEAT RESTORE .   
    2DUP . DROP   
    2DROP CR   
    SHIFT-ARGS   
   LOOP   
   \ ----------------------   
   (The -s option automatically loads argument handling and control   
   structures interpretation.)   
      
   Example :   
      
   albert@sinas2:~/PROJECT/ciriscv$ wc.script [a-h]*.frt   
   aap.frt 3 10 55   
   blocks.frt 3712 20819 109460   
   doit.frt 6 21 106   
   hello.frt 0 3 19   
   hellow.frt 1 8 40   
      
   See also advance/lispl.frt   
      
    https://github.com/albertvanderhorst/forthlisp   
      
   The definition of TOKEN that replaces NAME that is my WORD.   
      
   The transformation generates a lisp turnkey.   
      
   Groetjes Albert   
   --   
   The Chinese government is satisfied with its military superiority over USA.   
   The next 5 year plan has as primary goal to advance life expectancy   
   over 80 years, like Western Europe.   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|