Why does Forth use IF THEN ... instead of ENDIF?

Why does Forth use IF THEN ... instead of ENDIF?

I am implementing a (inappropriate) Forth compiler. Basically, the Forth syntax looks very intuitive to me regarding IF statements.

IF ."Statement is true" ELSE ."Statement is not true" THEN ."Printed no matter what; 

Why is the final instruction a THEN ? This makes the language very strange to me. For my compiler, I am considering changing it to something like ENDIF , which reads more naturally. But what was the suggestion that IF-THEN statements should be in the first place?

+6
source share
1 answer

Think of it as: " IF , what if, do it, ELSE do it ... and THEN continue with ..."

Or it’s even better to use quotes (as in Factor , RetroForth , ...), in which case it is completely postfix without special compile-time words; just regular words that accept addresses from the stack: [ do this ] [ do that ] if or [ do this ] when or [ do that ] unless . I personally prefer that.

Beyond RE: Quotes

Here's how quotes are compiled in RetroForth . In my own Forth (which compiles to my own virtual machine) I just added a QUOTE statement that pops the next address onto the stack and jumps through n-bytes. It is expected that n-bytes will be completed with the RETURN command, and the words IF , when , unless will use the predicate along with the address (s) left by previous quotes; calling if necessary. Very simple, and quotes usually open the door to all kinds of beautiful abstractions from the thought of the stack .

+6
source

Source: https://habr.com/ru/post/925966/


All Articles