X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=src%2Flib.4th;h=4e732068323e3ab7c710e42c69b722be08101072;hb=366db53c2e77d243c744c11f68b8bdeb575c86e6;hp=955cadfe236e34a1c286d4e98c9f5659b5245676;hpb=d2b2b3e5b33f882c18c9e7cf8c6623f4e863c2dd;p=forth.jl.git diff --git a/src/lib.4th b/src/lib.4th index 955cadf..4e73206 100644 --- a/src/lib.4th +++ b/src/lib.4th @@ -1,6 +1,5 @@ : \ IMMEDIATE - KEY - 10 = 0BRANCH [ -5 , ] + #TIB @ >IN ! ; \ We can now comment! \ BASIC DEFINITIONS ---------------------------------------------------------------------- @@ -33,6 +32,9 @@ : LITERAL IMMEDIATE ['] LIT , , ; +: ' BL WORD FIND >CFA ; + +: CHAR BL WORD 1+ @ ; : [CHAR] IMMEDIATE CHAR ['] LIT , , @@ -42,7 +44,7 @@ : SPACE BL emit ; : [COMPILE] IMMEDIATE - WORD \ get the next word + BL WORD \ get the next word FIND \ find it in the dictionary >CFA \ get its codeword , \ and compile that @@ -54,9 +56,6 @@ , \ compile it ; -: DEBUGON TRUE DEBUG ! ; -: DEBUGOFF FALSE DEBUG ! ; - \ CONTROL STRUCTURES ---------------------------------------------------------------------- : IF IMMEDIATE @@ -149,11 +148,16 @@ ; : +LOOP IMMEDIATE + + trace + ['] DUP , \ Store copy of increment ['] R> , ['] SWAP , ['] R> , ['] SWAP , ['] R> , ['] SWAP , ['] + , ['] 2DUP , ['] - , ['] SWAP , ['] >R , ['] SWAP , ['] >R , ['] SWAP , ['] >R , + trace + \ Condition differently depending on sign of increment ['] SWAP , ['] 0>= , [COMPILE] IF ['] 0<= , @@ -161,12 +165,16 @@ ['] 0> , [COMPILE] THEN + trace + \ Branch back to begining of loop kernel ['] 0BRANCH , HERE @ - , \ Clean up ['] RDROP , ['] RDROP , ['] RDROP , + trace + \ Record address of loop end for any LEAVEs to use HERE @ SWAP ! @@ -209,13 +217,18 @@ REPEAT ; +xx \ COMMENTS ---------------------------------------------------------------------- : ( IMMEDIATE 1 \ allowed nested parens by keeping track of depth BEGIN - KEY \ read next character + >IN @ #TIB @ >= IF \ End of TIB? + QUERY \ Get next line + THEN + + TIB >IN @ + @ 1 >IN +! DUP [CHAR] ( = IF \ open paren? DROP \ drop the open paren 1+ \ depth increases @@ -454,7 +467,7 @@ : ." IMMEDIATE ( -- ) [COMPILE] S" ( read the string, and compile LITSTRING, etc. ) - ['] TELL , ( compile the final TELL ) + ['] TYPE , ( compile the final TYPE ) ; : .( @@ -469,11 +482,16 @@ AGAIN ; +( Converts address of counted string into address of + start of string and length of string. ) +: COUNT ( addr1 -- addr2 n ) + DUP 1+ SWAP @ ; + ( CONSTANTS AND VARIABLES ------------------------------------------------------ ) : CONSTANT - WORD HEADER ( make dictionary entry (the name follows CONSTANT) ) + BL WORD HEADER ( make dictionary entry (the name follows CONSTANT) ) DOCOL , ( append DOCOL (the codeword field of this word) ) ['] LIT , ( append the codeword LIT ) , ( append the value on the top of the stack ) @@ -491,7 +509,7 @@ : VALUE ( n -- ) - WORD HEADER ( make the dictionary entry (the name follows VALUE) ) + BL WORD HEADER ( make the dictionary entry (the name follows VALUE) ) DOCOL , ( append DOCOL ) ['] LIT , ( append the codeword LIT ) , ( append the initial value ) @@ -499,9 +517,9 @@ ; : TO IMMEDIATE ( n -- ) - WORD ( get the name of the value ) + BL WORD ( get the name of the value ) FIND ( look it up in the dictionary ) - >DFA ( get a pointer to the first data field (the 'LIT') ) + >PFA ( get a pointer to the first data field (the 'LIT') ) 1+ ( increment to point at the value ) STATE @ IF ( compiling? ) ['] LIT , ( compile LIT ) @@ -514,9 +532,9 @@ ( x +TO VAL adds x to VAL ) : +TO IMMEDIATE - WORD ( get the name of the value ) + BL WORD ( get the name of the value ) FIND ( look it up in the dictionary ) - >DFA ( get a pointer to the first data field (the 'LIT') ) + >PFA ( get a pointer to the first data field (the 'LIT') ) 1+ ( increment to point at the value ) STATE @ IF ( compiling? ) ['] LIT , ( compile LIT ) @@ -592,7 +610,7 @@ ( FORGET ---------------------------------------------------------------------- ) : FORGET - WORD FIND ( find the word, gets the dictionary entry address ) + BL WORD FIND ( find the word, gets the dictionary entry address ) DUP @ LATEST ! ( set LATEST to point to the previous word ) HERE ! ( and store HERE with the dictionary address ) ; @@ -621,7 +639,14 @@ ; : SEE - WORD FIND ( find the dictionary entry to decompile ) + BL WORD 2DUP FIND ( find the dictionary entry to decompile ) + + ?DUP 0= IF + ." Word '" TYPE ." ' not found in dictionary." + EXIT + THEN + + -ROT 2DROP ( Now we search again, looking for the next word in the dictionary. This gives us the length of the word that we will be decompiling. (Well, mostly it does). ) @@ -667,7 +692,7 @@ 4 SPACES - >DFA ( get the data address, ie. points after DOCOL | end-of-word start-of-data ) + >PFA ( get the data address, ie. points after DOCOL | end-of-word start-of-data ) ( now we start decompiling until we hit the end of the word ) BEGIN ( end start ) @@ -684,7 +709,7 @@ [CHAR] S EMIT [CHAR] " EMIT SPACE ( print S" ) 1+ DUP @ ( get the length word ) SWAP 1+ SWAP ( end start+1 length ) - 2DUP TELL ( print the string ) + 2DUP TYPE ( print the string ) [CHAR] " EMIT SPACE ( finish the string with a final quote ) + ( end start+1+len, aligned ) 1- ( because we're about to add 4 below )