X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?p=forth.jl.git;a=blobdiff_plain;f=src%2Flib_6_strings.4th;fp=src%2Flib_6_strings.4th;h=753e756a12a8b4ba222b9b4eee607132241e44da;hp=aec678b5d4be35953fb2b4da779155b2b7235137;hb=353259d7b95b1783b1fb0f572df4b1bb43cca9c4;hpb=b258f43857f94e2eb35fd2a8c3bc41451772666f diff --git a/src/lib_6_strings.4th b/src/lib_6_strings.4th index aec678b..753e756 100644 --- a/src/lib_6_strings.4th +++ b/src/lib_6_strings.4th @@ -37,12 +37,19 @@ DROP ; -( Compile-mode word which compiles everything until the next - double quote as a litstring. ) -: S" IMMEDIATE ( -- addr len ) - ['] LITSTRING , ( compile LITSTRING ) - HERE ( save the address of the length word on the stack ) - 0 , ( dummy length - we don't know what it is yet ) +: COMPILING? STATE @ 0<> ; + +( In compile mode, word compiles everything until the next + double quote as a litstring. Otherwise, dynamically allocates + memory and stores string there, returning address and length. ) +: S" IMMEDIATE ( -- addr len ) + COMPILING? IF + ['] LITSTRING , ( compile LITSTRING ) + HERE ( save the address of the length word on the stack ) + 0 , ( dummy length - we don't know what it is yet ) + ELSE + HERE ( save the starting address on the stack ) + THEN BEGIN >IN @ #IB @ >= IF \ End of IB? @@ -53,13 +60,18 @@ DUP [CHAR] " <> WHILE - C, ( copy character ) + , ( copy character ) REPEAT DROP ( drop the double quote character at the end ) - DUP ( get the saved address of the length word ) - HERE SWAP - ( calculate the length ) - 1- ( subtract 1 (because we measured from the start of the length word) ) - SWAP ! ( and back-fill the length location ) + + COMPILING? IF + DUP ( get the saved address of the length word ) + HERE SWAP - ( calculate the length ) + 1- ( subtract 1 (because we measured from the start of the length word) ) + SWAP ! ( and back-fill the length location ) + ELSE + DUP HERE SWAP - + THEN ; ( Compile-mode word which compiles everything until the