Search code examples
functionfibonacciforth

How to read user input of integer number and give to function in Forth?


I've written a piece of code in Forth which calculates the 9th Fibonacci number:

." Fibonacci numbers"
: INP 9 0 ;
: FIB_0 1 0 1 ;
: FIB FIB_0 INP DO  + SWAP OVER LOOP SWAP . ;

Now I want to read the integer number N from user input and give it to INP instead of 9 so that I could calculate an arbitrary integer Fibonacci number. I'm using Gforth on Windows.


Solution

  • You can use accept to grab user input and then s>number? to attempt to convert it to a number. accept expects a memory address and a max length. 's>number?` leaves a double and a flag.

    So you could do something like

    : fetch-input ( - n f ) 
         pad 40 accept   
         pad swap s>number? 
         >r d>s r> ;
    

    If s>number? can't convert the string to a number it will leave 0 0 under the flag on the stack.

    So after s>number? you have three entries on the stack: the two parts of the double precision fixed point number and a flag--stack comment would be: ( - n n f ). But I'm assuming you want a single precision number given the code in your question.

    d>s will take a double precision number and convert it to single precision. We need to move the flag out of the way so we can get to the number. A standard trick for temporarily moving something out of the way is to move it from the data stack to the return stack, do whatever it is you need to do, then move it back. Thus, >r and r>.