Many times those who want to try Forth are frustrated by the absence of simple features that they took for granted in BASIC.
One of those things in my experience was the BASIC word INPUT. It allows us to get user input very easily.
Forth was created by a person who started from the premis that he never knew what a program was going to require in precise terms so he assumed very little with his language but gave himself the tools to build what he needed rather than make it in advance. Charles Moore is something of an enigma in programming because he is happy to start from scratch every time. For us normal humans it's handy to have some things "in the can".
The following code adds INPUT to Camel Forth. (it will require tweaking for other flavours of Forth)
The important difference is that there must be two "INPUTs" for Forth. One for strings and one for numbers. BASIC knows what kind of variable you use with INPUT so it can run the right code. Forth has no knowledge like that so you have to use $INPUT or #INPUT as needed. The good news is that #INPUT uses $INPUT to gather the number string and then converts it.
I have made #INPUT with a much smaller error message that TI BASIC.
I could not justify all the space for
INPUT ERROR IN 100
But it's simple to change if you want it.
\ INPUT.FTH creates input like BASIC \ *Difference* there is a separate input for numbers and strings DECIMAL : "?" BEEP CR ." ? " ; \ we can reuse this gem : $ACCEPT ( $addr -- ) DUP 1+ 80 ACCEPT SWAP C! ; : $INPUT ( $addr -- ) "?" $ACCEPT ; \ "?" to look like TI-BASIC : #INPUT ( variable -- ) \ made to look like TI-BASIC BEGIN PAD $INPUT \ $ACCEPT text into temp buffer PAD PAD ?NUMBER 0= \ convert the number in PAD WHILE \ while the conversion is bad we do this CR HONK ." input error " CR DROP REPEAT SWAP ! ; \ store the number in the variable on the stack) \ Usage: \ \ VARIABLE A$ 100 ALLOT \ string variables need more space \ VARIABLE X \ \ A$ $INPUT \ \ X #INPUT
Edited by TheBF, Mon Aug 7, 2017 1:45 PM.