Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting ASCII code with Gforth

Tags:

char

forth

gforth

When entering 'a' in Gforth, the ASCII number of the character (the same number which would be put onto the stack by using the key word and pressing a) is put onto the stack.

This does not work for example with ' ' (space). Instead:

' '  ok
.s <1> 34384939008  ok

The number “should” be 32. What explains this behavior? And what can be done about it – aside from manually putting the ASCII number corresponding to ' ' (space) on the stack?

like image 218
viuser Avatar asked Dec 23 '22 20:12

viuser


1 Answers

This 'a' syntax is quite new to Forth. It's added as an extension on top of the traditional syntax which parses everything into whitespace-delimited tokens. So 'a' is one atomic token, which is then parsed as a character literal.

Now, ' ' isn't an atomic token, since it contains a space character. Rather, it's parsed as two ' tokens. It's actually perfectly valid Forth code, because ' is a Forth word (called "tick"). In your example, the first tick operates on the second. The result, 34384939008, is the xt for '.

What to do instead? The traditional words for getting the ASCII code of a character is CHAR or [CHAR]. The first works in interpreted mode, and the second in compiled mode. BUT they don't work for the particular case of the space character, because again, all whitespace is parsed away.

However, there is another word which pushes the ASCII code space character: BL.

like image 67
Lars Brinkhoff Avatar answered Jan 23 '23 07:01

Lars Brinkhoff