EMIT

( x -- )

If x is a graphic character in the implementation-defined character set, display x. The effect of EMIT for all other values of x is implementation-defined.

When passed a character 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. Each EMIT deals with only one character.

See:

Testing:

: OUTPUT-TEST
   ." YOU SHOULD SEE THE STANDARD GRAPHIC CHARACTERS:" CR
   41 BL DO I EMIT LOOP CR
   61 41 DO I EMIT LOOP CR
   7F 61 DO I EMIT LOOP CR
   ." YOU SHOULD SEE 0-9 SEPARATED BY A SPACE:" CR
   9 1+ 0 DO I . LOOP CR
   ." YOU SHOULD SEE 0-9 (WITH NO SPACES):" CR
   [CHAR] 9 1+ [CHAR] 0 DO I 0 SPACES EMIT LOOP CR
   ." YOU SHOULD SEE A-G SEPARATED BY A SPACE:" CR
   [CHAR] G 1+ [CHAR] A DO I EMIT SPACE LOOP CR
   ." YOU SHOULD SEE 0-5 SEPARATED BY TWO SPACES:" CR
   5 1+ 0 DO I [CHAR] 0 + EMIT 2 SPACES LOOP CR
   ." YOU SHOULD SEE TWO SEPARATE LINES:" CR
   S" LINE 1" TYPE CR S" LINE 2" TYPE CR
   ." YOU SHOULD SEE THE NUMBER RANGES OF SIGNED AND UNSIGNED NUMBERS:" CR
   ." SIGNED: " MIN-INT . MAX-INT . CR
   ." UNSIGNED: " 0 U. MAX-UINT U. CR
;

T{ OUTPUT-TEST -> }T

ContributeContributions

enochavatar of enoch emit fortified :-)Comment2015-12-13 19:01:24

Each EMIT deals with only one character -- and this creates a problem in multi-tasking / interrupt-service Forth implementations where words are allowed to (or need to) emit character strings to a common terminal. These words concurrent operation may result in UTF-8 and VT100 escape strings being broken, logging messages getting mangled, etc.

In other language environments one usually restricts terminal output rights to a single task (0), but that is, IMHO, not really Forth-like (aka KISS-like) solution as it requires creating message queues or other complex locking mechanisms.

In amforth-shadow I decided to enhance the kernel as follows and I hope that one of the Forth gurus here would carry it up to the standard in this form or another.


\ The mite.frt module protects .{ enclosed text .} output from being broken
\ by like output from other soft ISRs, etc. Install on start-up by: {mite}
\ and the standard emit would be replaced by this "mite proof" version.

: .{  STX emit  ;
: .}  ETX emit  ;

\ A test simulating concurrent output from three words.
\ :  test  .{ ." 12" .{ ." abcd " .} ." 34" .{ ." ABCD " .} ." 56" .}  ;
\ test
\ abcd ABCD 123456 ok

Since all the people here are so experienced are bore you not with the implementation details (but you are certainly welcome to click here to visit).

Thanks, Enoch.

Reply