May 22nd, 2009

Forth school

I went to Forth school with Mitch Bradley yesterday. Here are a few cool new things I learned:
  • many: The word many repeats execution of the current line until a key is pressed.
    ok ." luke rules" cr  many
    luke rules
    luke rules
    luke rules
    
    ok see many
    : many   
       key? 0= if    
          0 >in ! 
       then  
    ; 
    
  • quine: Since we can introspect the input stream it's easy to write a program that prints itself to stdout.
    ok ( this is a quine ) source type
    ( this is a quine ) source type
    
  • patch: The word patch is a simple way to make binary patches to Forth words.
    ok : foo 1 + ;           
    ok see foo
    : foo   
       1 + 
    ; 
    ok 41 foo .
    42 
    
    ok patch - + foo
    ok see foo
    : foo   
       1 - 
    ; 
    ok 43 foo .
    42 
    
    ok patch 5 1 foo
    ok see foo
    : foo   
       5 - 
    ; 
    ok 47 foo .
    42 
    ok 
    
I love Forth.