Published using Google Docs
( INFIX.fth )
Updated automatically every 5 minutes

{                                *** INFIX DEMONSTRATION ***

Simple demonstration of handling infix notation in Forth

Operators are redefined so that they push the code address of the function onto an operations stack

numbers are processed normally and left on the stack.

At the end of the line the op stack is processed giving the appearance of processing infix fashion

Since the Forth parser is being used then all commands and parameters must be separated by at least one space.

To make this more flexible it would probably be necessary to flag each number on the stack with perhaps another number to indicate whether it is a literal or a variable or a string pointer etc.

The default search order needs to be changed as well so that the kernel words will be ignored without having to manually disable them as at present.

<webdoc>

}

FORGET INFIX.fth

: INFIX.fth                ." Simple demonstration of infix notation in Forth " ;

8 WORDS opstk

: +OP                opstk DUP 2+ #14 <CMOVE opstk W! ;

: -OP                opstk 2+ opstk #14 CMOVE ;

: ?OP                BEGIN opstk W@ ?DUP WHILE CALL -OP REPEAT ;

: INFIX                ' ?OP prompt W! opstk 16 ERASE DECIMAL ;

: &PRINT                 

         DUP 1- C@ $8D = OVER ' COLD names W@ WITHIN AND IF @ THEN

         DUP HERE DUP 16 + WITHIN IF PRINT$ ELSE $400A .NUM THEN

CR

;

: PRINT                 ' &PRINT +OP ;

( Define the := assignment operator )

: =!           SWAP ! ;

: :=          ' =! +OP ;

: &*                * ;

BL NFA' * 1+ C!  ( disable the original name as TF will try to search kernel words first )

: *           ' &* +OP ;

: &/                / ;

BL NFA' / 1+ C!

: /           ' &/ +OP ;

: &+                + ;

BL NFA' + 1+ C!

: +           ' &+ +OP ;

: &-                - ;

BL NFA' - 1+ C!

: -           ' &- +OP ;

: &+=           SWAP +! ;

: +=                ' &+= +OP ;

INFIX

{

create stack for action codes

If compiling then compile action stack

\ try this

DECIMAL

LONG pbj

PRINT pbj

pbj := 1234

PRINT pbj

pbj  := pbj @ * 100

PRINT pbj

PRINT 15 + 23 * 10 - 2

PRINT 15 + DUP

PRINT " THIS IS A STRING"

}