ir230@sdccs6.UUCP (05/03/84)
* Re: FORTH References * In learning FORTH one should not overlook the best teacher: FORTH itself. The people who find FORTH hardest to learn are those immersed in the philosophy of conventional languages. Conventionally, the implementation of a language or a system is concealed from the user. Ignorance is made a virtue ("You wouldn't WANT to understand that! We've arranged it so that you don't HAVE to. If you can point and grunt, you can use our system!"). Much of FORTH's power comes from the fact that it is comprehensible. All aspects of a FORTH system are accessible to the user. A simple "dump" word will allow the inspection of the dictionary (or any other desired area of memory). Even a beginner can write for him/herself tools to explore a FORTH system. One should experiment with short definitions and see what is actually compiled into the dictionary. Try different control structures and how they affect compilation. Follow the links to see how vocabularies are implemented. Examine the code produced when a "defining word" is created and when it is used. The word .S (non-destructructive stack print) will show what is in the stack without changing it. A definition for figFORTH is: : .S CR SP@ 2 - S0 @ 2 - OVER OVER < IF DO I @ . -2 +LOOP ELSE DROP DROP THEN ; A more sophisticated tool is a "decompiler". Most of FORTH is written in FORTH (even figFORTH, as a close look at the assembly language source will disclose!). A glossary provides a description of what a word does; the decompiler provides its definition. There are very fancy commercial decompilers. A simple one (3 screens long) is found in FORTH Dimensions III/2 p. 61. This decompiler shows what is actually in the dictionary rather than reconstructing the source that produced it. For learning purposes it is quite useful. I enclose a copy with this news article (even though it should probably go to net.sources) Experimentation can provide more insight than books. John J. Wavrik Math Dept C-012 Univ of Calif at San Diego La Jolla, CA 92093 ..ucbvax!sdcsvax!sdccsu3!sdccs6!ir230 Block: 1 0 ( DECOMPILER PART 1 FORTH DIMENSIONS VOL III # 2) 1 ( PAGE 61 ) 2 HEX 3 : PICK 2 * SP@ + @ ; 4 : TEST= 3 PICK = OR ; ( PFA1 f PFA2 -- PFA1 f' ) 5 : TEST.END DUP @ 2+ 0 ( @CFA -- @CFA f ) 6 ' ;S TEST= ( where f and f' are flags ) 7 ' (;CODE) TEST= 8 ' QUIT TEST= 9 SWAP DROP 0= ; 10 ' TEST= 2 - @ CONSTANT DOCOL --> 11 ( ------------------------------------------------------------ ) 12 @CFA marks the position in the definition being disassembled. 13 At this position is the CFA of a word. TEST.END determines if 14 the word should end the definition. 15 Block: 2 0 ( DECOMPILER PART 2 ) 1 : INCREMENT DUP 2 + SWAP @ 2 + 2 0 ' COMPILE TEST= IF DROP DUP @ 2 + NFA ID. 2 + SPACE 3 ELSE 0 ' (.") TEST= IF DROP DUP COUNT DUP ROT SWAP 4 TYPE + 1+ 22 EMIT SPACE 5 ELSE 0 ' LIT TEST= DUP IF HEX THEN 6 ' BRANCH TEST= 7 ' 0BRANCH TEST= 8 ' (+LOOP) TEST= 9 ' (LOOP) TEST= IF DROP DUP @ . SPACE 2 + ELSE DROP 10 THEN THEN DECIMAL THEN ; --> 11 ( ------------------------------------------------------------ ) 12 INCREMENT does more than increment: it tests if the current word 13 is one which requires special treatment. Certain words are 14 followed by arguments or parameters. Your system might have 15 more such words! Block: 3 0 ( DECOMPILER PART 3 ) 1 2 : DECOMP HEX DUP 4 .R DUP CFA @ 5 .R DECIMAL 3 DUP CFA @ DOCOL = 4 IF ." : " DUP NFA ID. SPACE DUP 5 BEGIN DUP @ 2 + NFA DUP C@ 40 AND 6 IF ." [COMPILE] " THEN ID. TEST.END 7 WHILE INCREMENT REPEAT 8 DROP DUP NFA @ 40 AND IF ." IMMEDIATE " THEN 9 ELSE ." " DUP NFA ID. 10 THEN LFA @ CR CR ; 11 12 : DECOMPILE CR DECOMP DROP ; DECIMAL ;S 13 ( ------------------------------------------------------------ ) 14 To Use: ' <word> DECOMPILE will decompile <word> 15