Inline strings

license
$ -> counted strings
S -> string described by two stack items
z -> zero terminated, not implemented in kernel.
,string ( addr count --)

Take a string pointed to by addr and count and place it in the dictionary.

 
	: ,string ( addr count--)
		2DUP              \ addr count addr  count (--
		NIP HERE $count!
		#$count_length ALLOT    \ addr count (--
		TUCK                    \ count addr count(--
		HERE SWAP CMOVE         \ count(--
		ALLOT
		ALIGN
    ;
	 
S string run time code

Place the address of the string on the stack and adjust things so that the string is missed.

 
	\ Run time code
	: _S"_do ( -- addr count )
		R>				 \ Next execution on return
		COUNT            \ addr u (--
		2DUP             \ addr u addr u (--  
		+                \ addr u next1
		ALIGNED          \ addr count next2
		>R
	;
	 
," ( --)

Take a string delimited by " from the input stream and place it in the dictionary.

 
	: ," ( --)
		[CHAR] " PARSE ,string 
	;
	 
6.1.0190 ."

dot-quote CORE

Interpretation: Interpretation semantics for this word are undefined.

Compilation: ( "ccc"" -- )

Parse ccc delimited by " (double-quote). Append the run-time semantics given below to the current definition.

Run-time: ( -- )

Display ccc.

Typical use: : X ... ." ccc" ... ;

An implementation may define interpretation semantics for ." if desired. In one plausible implementation, interpreting ." would display the delimited message. In another plausible implementation, interpreting ." would compile code to display the message later. In still another plausible implementation, interpreting ." would be treated as an exception. Given this variation a Standard Program may not use ." while interpreting. Similarly, a Standard Program may not compile POSTPONE ." inside a new word, and then use that word while interpreting.

 	 
    : ."   
    	COMPILE dot" ," 
    ;  IMMEDIATE 
	 
17.6.1.2212 SLITERAL

STRING

Interpretation: Interpretation semantics for this word are undefined.

Compilation: ( c-addr1 u -- )

Append the run-time semantics given below to the current definition.

Run-time: ( -- c-addr2 u )

Return c-addr2 u describing a string consisting of the characters specified by c-addr1 u during compilation. A program shall not alter the returned string.

 
	: SLITERAL \ compile time ( addr n--)
	           \ runtime ( --add n)
		COMPILE _S"_do ,string ; IMMEDIATE

	 
$ string run time code

Place the address of the string on the stack and adjust things so that the string is missed.

 
	\ Run time code
	: _$"_do ( -- addr)
		R> 
		DUP
		COUNT +    \ addr addr1 (--
		ALIGNED    \ addr addr2 (--
		>R
	;
	 
$"

ANS standard defines this word as C", however COLDFORTH has a word set that is based on calling a counted string $, therefor the $" definition should remain.

 
	: $" 
		COMPILE _$"_do ," 
	; IMMEDIATE
    
	forth : $" 
		HOST
		COMPILE _$"_do ," 
	; TARGET
	 
ANS 6.2.0855 C"

CORE EXT

Interpretation: Interpretation semantics for this word are undefined.

Compilation: ( "ccc< quote >" -- )

Parse ccc delimited by " (double-quote) and append the run-time semantics given below to the current definition.

Run-time: ( -- c-addr )

Return c-addr, a counted string consisting of the characters ccc. A program shall not alter the returned string.

 
	: C" COMPILE _$"_do ," ; IMMEDIATE
   
    forth : C" 
		HOST
		COMPILE _$"_do ," 
	; TARGET
	 
ANS 6.1.2165 S"
ANS 11.6.1.2165 S"

CORE and FILE

Interpretation: ( "ccc" -- c-addr u )

Parse ccc delimited by " (double quote). Store the resulting string c-addr u at a temporary location. The maximum length of the temporary buffer is implementation-dependent but shall be no less than 80 characters. Subsequent uses of S" may overwrite the temporary buffer. At least one such buffer shall be provided.

Compilation: ( "ccc< quote >" -- )

Parse ccc delimited by " (double-quote). Append the run-time semantics given below to the current definition.

Run-time: ( -- c-addr u )

Return c-addr and u describing a string consisting of the characters ccc. A program shall not alter the returned string.

 
		Typical use: : X ... S" ccc" ... ;
 

This word is found in many systems under the name " (quote). However, current practice is almost evenly divided on the use of ", with many systems using the execution semantics given here, while others return the address of a counted string. We attempt here to satisfy both camps by providing two words, S" and the Core Extension word C" so that users may have whichever behavior they expect with a simple renaming operation.

 


	#$buffer ubuffer _stemp_buffer

	: S"    \ interpret ( "string" -- c_addr n )
			\ compile   ( "string" -- )
			\ runtime   ( -- c_addr n )
		STATE @ IF
			[CHAR] " PARSE  [COMPILE] SLITERAL 
	    ELSE  \ interpret action
			\ This is required to meet 11.6.1.2165
			\ The standard requires this to be valid until
			\ S" is used in interpretive mode again, so,
			\ I suppose we had better have a special buffer for
			\ this one word, another 4 bytes of user space zip
			\ out the window.
			[CHAR] " WORD
			_stemp_buffer OVER C@ 1+ MOVE
            _stemp_buffer COUNT
		THEN 
	; IMMEDIATE

	 
 
    forth : S" 
		HOST
		COMPILE _S"_do ," 
	; TARGET
	 

So an application can panic

 
	: panic" 
		COMPILE _panic"_do ," 
	; IMMEDIATE