wpohl@hvrunix.UUCP (Walter E. Pohl) (08/11/88)
I've come up with a small set of words for defining infix notation in Forth. The words are simple and fairly Forth-like. They are basically syntactical sugar that give the "illusion" of infix without any of infix notation's speed costs at run time. Define ( in Forth-83 Standard): : ) IF EXECUTE ELSE , THEN ; IMMEDIATE : INFIX CREATE ' , IMMEDIATE DOES> @ 0 ; : IMFIX CREATE ' , IMMEDIATE DOES> @ -1 ; INFIX +( + INFIX -( - INFIX *( * INFIX /( / IMFIX IF( IF IMFIX THEN( THEN The three colon definitions define all the primitives necessary for infix notation. INFIX and IMFIX are defining words which create actual infix words. For example, with the above words defined with INFIX and IMFIX, you can now type: : FOO IF( 5 +( 5 ) 10 = ) THEN( ." YOU FOOL" ) ; instead of: : FOO 5 5 + 10 = IF ." YOU FOOL" ; or you can mix the two: : FOO IF( 5 5 + 10 = ) THEN( ." YOU FOOL" ) ; INFIX creates an infix form of a non-immediate word, and IMFIX creates an infix form of an immediate word. By convention, each infix word ends with an '('. An infix word tells the compiler to wait until the ) before compiling its code, so 5 +( 3 ) is equivalent to 5 3 +. The use of the right parenthesis in that fashion allows nesting of infix words, like 5 +( 3 -( 2 ) ) instead of 5 3 2 - +. Note that ) is an actual word in this case, and not just a delimiter. Thus it must be preceded and followed by a space. By making ) a word, and not just a delimiter, it allows infix usage to spread over more than one line, such as: : FOO IF( 5 3 = ) THEN( ." You fool" ) ; Also, all of these words are immediate, and thus can only be used in colon-definitions. They really work thus: : FOO 5 +( \ put the address of + on the stack. 3 ) \ compile the address of + ; : FOO IF( \ put the address if IF on the stack. 5 3 = ) \ Execute IF. THEN( \ put the address of THEN on the stack. ." You fool" ) \ execute THEN. I hope this was comprehensible. My brain was fried while I typed it. If you have any questions, send me mail, and maybe I'll be able to explain it better. I hope people find this useful. -- Walt Pohl (Haverford College) UUCP: {rutgers, uunet} !cbmvax!vu-vlsi!hvrunix!wpohl BITNET: w_pohl@hvrford, or walt@brynmawr