6.1.0895 CHAR char CORE

( "<spaces>name" -- char )

Skip leading space delimiters. Parse name delimited by a space. Put the value of its first character onto the stack.



Typical use: ... CHAR A CONSTANT "A" ...


T{ CHAR X     -> 58 }T
T{ CHAR HELLO -> 48 }T


BerndPaysanavatar of BerndPaysan [3] Non-XCHAR implementationSuggested reference implementation2015-11-27 20:45:54

: CHAR ( "char" -- c ) BL WORD CHAR+ C@ ;

Reply New Version

mcondronavatar of mcondron [78] Behavior when no text foundComment2019-05-19 13:57:46

When no text is found (i.e. CHAR followed by nothing but spaces until the end of the line) should this be an ambiguous condition, or should CHAR place 0 on the stack?

AntonErtlavatar of AntonErtl

I don't see this defined in the standard at all (not even as ambiguous condition). I guess we should fix this in the standard.

I think a system should react to this condition by reporting an error.

BerndPaysanavatar of BerndPaysan

There's not even a suitable standard throw code available.

Gforth's current behavior is to return whatever character is found after the end of the input buffer, which is a classical read outside boundary problem.

AntonErtlavatar of AntonErtl

I have just fixed this in Gforth, which now produces an error. I have used -16 "Attempt to use zero-length string as a name" as throw code, but it does not quite fit.

mcondronavatar of mcondron

Yes, seems like an error to me, too. Thanks.

JohanKotlinskiavatar of JohanKotlinski

My Forth does REFILL until it finds a char... does that make sense, or is it better to simply ABORT?

ruvavatar of ruv

The section 3.4.1 Parsing says: "Parsing may change the contents of >IN, but shall not affect the contents of the input buffer". It means, CHAR, doing parsing, shall not refill the input buffer.

On the other hand, 4.1.2 Ambiguous conditions says that an ambiguous condition can occur due to "unexpected end of input buffer, resulting in an attempt to use a zero-length string as a name". NB: "name" is not necessary a word name. It's a special parsed text abbreviation, which means "a token delimited by space" (actually a lexeme).

Due to this ambiguous condition, CHAR may do anything if name is empty string, including refilling the input buffer. Probably a good choice is just to throw an exception.

Reply New Version

PopovMPavatar of PopovMP [270] Describe Compile time and Run time behaviorComment2022-11-19 09:25:07

I had troubles to make my .f files working in popular distributions until I found CHAR is not supported in Compile / Run time.


Compilation semantics for this word are undefined.


Run-time semantics for this word are undefined.

AntonErtlavatar of AntonErtl

CHAR has default compilation semantics, and that may be confusing. Forth-94 supports [CHAR] for compiling a literal character into a definition. In Forth-2012 there is also the option to use the syntax 'A', which pushes the ASCII code of A at interpretation/run-time.

Reply New Version