TYPE

( c-addr u -- )

If u is greater than zero, display the character string specified by c-addr and u.

When passed a character in a character string whose character-defining bits have a value between hex 20 and 7E inclusive, the corresponding standard character, specified by 3.1.2.1 Graphic characters, is displayed. Because different output devices can respond differently to control characters, programs that use control characters to perform specific functions have an environmental dependency.

See:

Testing:

ContributeContributions

NieDzejkobavatar of NieDzejkob Suggested reference implementationSuggested reference implementation2018-01-06 18:15:17

: TYPE 0 MAX 0 ?DO DUP C@ EMIT 1+ LOOP DROP ;

NieDzejkobavatar of NieDzejkobNew Version

Hide differences

: TYPE 0 MAX 0 ?DO DUP C@ EMIT 1+ LOOP DROP ;

: TYPE ( c-addr u -- ) 0 MAX 0 ?DO DUP C@ EMIT CHAR+ LOOP DROP ;

Alternatively, without ?DO or LOOP:

: TYPE ( c-addr u -- ) BEGIN DUP 0> WHILE OVER C@ EMIT [ -1 CHARS ] LITERAL - SWAP CHAR+ SWAP REPEAT 2DROP ;

alextangentavatar of alextangent

: TYPE 0 MAX 0 ?DO DUP C@ EMIT 1+ LOOP DROP ;

should read

: TYPE ( c-addr u -- ) 0 MAX 0 ?DO DUP C@ EMIT CHAR+ LOOP DROP ;

There are several problems with (even the corrected version) as a reference implementation. The biggest issue is that it doesn't permit using the XCHAR wordset. The primitive for output there is XEMIT, and there is no equivalent XTYPE. The extension says "All words dealing with strings shall handle xchars when the xchar word set is present." It may be that there is no reference implementation in terms of standard words possible.

alextangentavatar of alextangent

Addendum; and the use of 0 MAX is not correct, as the length of the buffer is unsigned.

JimPetersonavatar of JimPeterson

What about this:

: TYPE ( c-addr u -- ) 0 ?DO COUNT EMIT LOOP DROP ;

It's kind of crazy, and still no XCHAR support, but COUNT is very (overly?) specific about what it does.

AntonErtlavatar of AntonErtl

Actually implementing TYPE using EMIT is fine even for strings containing xchars. Such an implementation of TYPE emits the individual chars (bytes) of a string, one byte at a time, which fits nicely with C@ and CHAR+ (1+). That's the magic the string representation of xchars; Strings still are arrays of chars (bytes). If you don't need individual xchars (and you rarely do), you can just treat them as such, no need for xchar-specific words. That's why we have no XTYPE.

Reply New Version