🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

5 total messages Started by Robert Kaires Wed, 06 Feb 1985 22:36
redefine "+" ??
#160
Author: Robert Kaires
Date: Wed, 06 Feb 1985 22:36
5 lines
764 bytes
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
Re: redefine "+" ??
#159
Author: James Van Ornum
Date: Fri, 08 Feb 1985 01:14
59 lines
2255 bytes
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
Re: redefine "+" ??
#162
Author: matheus
Date: Fri, 08 Feb 1985 19:38
5 lines
688 bytes



                  "impossible" is not a defined FORTH word!
Re: redefine "+" ??
#161
Author: Thomas M. Breuel
Date: Sat, 09 Feb 1985 19:10
21 lines
1456 bytes
> 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.
Re: redefine "+" ??
#164
Author: David Angier
Date: Thu, 14 Feb 1985 16:09
12 lines
1200 bytes
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)
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads