[net.lang.forth] redefine "+" ??

robertk@tekig.UUCP (Robert Kaires) (02/07/85)

Do any of you FORTH experts out there know the answer to this:
How do you redefine the "+" function (or any other operator) such that
entering  "2 + 2" yields "4" instead of typing "2 2 +". Is this 
impossible in FORTH ?
thanks

jhv@houxu.UUCP (James Van Ornum) (02/08/85)

Michael Stolowitz presented the paper "Algebraic Expression Evaluation in
FORTH" at a Computer Faire (I don't know which year - just that it was 1982
or later) with the following screens to convert infix notation to
postfix for evaluation:

	0 ( ALGEBRAIC )
	1 CREATE OP  44 ALLOT
	2
	3 : ?INTERP ( pfa -- ) CFA STATE @ IF , ELSE EXECUTE THEN ;
	4
	5 : OPP@    ( -- addr ) OP DUP @ + ;
	6
	7 : >OP     ( pfa lev -- ) 4 OP +! OPP@ 21 ;
	8
	9 : OP>     ( -- ) OPP@ 2@ -4 OP +! DROP ?INTERP ;
       10
       11 : LEV?    ( -- lev ) OPP@ @ ;
       12
       13 : ]A      BEGIN LEV? WHILE OP> REPEAT
       14           [COMPILE] FORTH ; IMMEDIATE
       15 -->

	0 ( ALGEBRAIC )
	1 : INFIX ( lev -- )  ( old rpn op   new infix op )
	2    '  CREATE  SWAP  ,  ,  IMMEDIATE
	3       DOES> 2@ BEGIN DUP LEV? > NOT WHILE
	4          >R >R   OP>  R> R>  REPEAT  >OP ;
	5
	6 VOCABULARY ALGEBRAIC IMMEDIATE ALGEBRAIC DEFINITIONS
	7
	8 7 INFIX * *    7 INFIX / /
	9 6 INFIX + +    6 INFIX - -
       10 5 INFIX > >    5 INFIX < <    5 INFIX = =
       11 4 INFIX NOT NOT
       12 3 INFIX AND AND
       13 2 INFIX OR  OR
       14
       15 -->

	0 ( ALGEBRAIC )
	1 : (  ['] CR  1  >OP ; IMMEDIATE
	2
	3 : )  FORTH  BEGIN  1 LEV? < WHILE OP> REPEAT
	4      1  LEV? = IF -4 OP +!
	5                ELSE  1 ABORT" Missing (" THEN ; IMMEDIATE
	6
	7 FORTH DEFINITIONS
	8
	9 : A[   0 OP !  [COMPILE] ALGEBRAIC ; IMMEDIATE        EXIT
       10
       11 Examples:      A[  A  +  B  -  C  *  (  D  /  A  )  ]A
       12
       13   or    : EXPR A[  A  +  B  -  C  *  (  D  /  A  )  ]A ;
       14
       15


-----------------------
	James Van Ornum, AT&T Bell Laboratories, inhp4!houxu!jhv

matheus@uiucdcsb.UUCP (02/08/85)

                  "impossible" is not a defined FORTH word!

breuel@harvard.ARPA (Thomas M. Breuel) (02/09/85)

> Do any of you FORTH experts out there know the answer to this:
> How do you redefine the "+" function (or any other operator) such that
> entering  "2 + 2" yields "4" instead of typing "2 2 +". Is this 
> impossible in FORTH ?
> thanks

To make it work interpreted, you have to build some kind of interpreter
for BASIC like expressions. I don't think that that's worth it.

To make it work compiled, you have to write a parser which translates
infix to postfix notation. The easiest way of doing that would probably
be to have a compiler word, say 'let', and have it do the parsing, e.g.

: ComputeIt let x = a * 9 + 3 ; ;

I believe, though, that this is hardly useful and very confusing. In
particular, if you don't have a mechanism for accessing local variables
by name, then computations involving data on the stack are very hard
to do.

							Thomas.

dgary@ecsvax.UUCP (D Gary Grady) (02/11/85)

> Do any of you FORTH experts out there know the answer to this:
> How do you redefine the "+" function (or any other operator) such that
> entering  "2 + 2" yields "4" instead of typing "2 2 +". Is this 
> impossible in FORTH ?

You can do something like this:

: +  32 WORD  NUMBER + ;

This defines plus to be a word that reads in the following word,
converts it to a number on the stack, then adds that to the number
previously put there.  This is almost identical to a similar word
defined on page 277 of Brodie's _Starting Forth_.

A similar idea is used with almost all text-oriented operations in
Forth.  Although Forth uses postfix notation for numbers, for text it
(of practical necessity) reverts to prefix or infix.
-- 
D Gary Grady
Duke U Comp Center, Durham, NC  27706
(919) 684-3695
USENET:  {seismo,decvax,ihnp4,akgua,etc.}!mcnc!ecsvax!dgary

dpa@snow.UUCP (David Angier) (02/14/85)

For interpretive infix use of '+' you could use WORD to get
the next number from the input stream and then use NUMBER to
push the number on the stack, then add them together using the
old version of '+'.
Since you can check whether you are compiling or not you can
make this safe, also you could make it use WORD to get the
next word then NUMBER and finally LITERAL to compile it in
RPN, afterwards use COMPILE + to put the old + in.
I don't think it is worth it, but it can be done.  I once
saw a Pascal compiler that was writeen in Forth (UGH).

			Dave (Maths @ University of Warwick,UK)