Skip to content

Instantly share code, notes, and snippets.

@uho
Created August 24, 2012 15:15
Show Gist options
  • Save uho/3451894 to your computer and use it in GitHub Desktop.
Save uho/3451894 to your computer and use it in GitHub Desktop.
Simple Forth example showing the handling of string and number input/output
20 chars Constant namestring-len
Create namestring namestring-len allot
: input ( buf-addr buf-len prompt-addr promt-len -- buf-add len )
\ prompt user for string
cr type over swap accept ;
: get-age ( -- n )
0.
BEGIN ( d )
here 3 chars s" How old are you? " input ( d addr len )
>number 2drop
2dup or
UNTIL
drop ;
: get-name ( -- addr len )
namestring namestring-len s" Who are you? " input ;
: .hello ( user user-len age -- )
cr ." Nice to meet you " . ." year old " type ." ." cr ;
: ask ( -- ) get-name get-age .hello ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment