Created
August 24, 2012 15:15
-
-
Save uho/3451894 to your computer and use it in GitHub Desktop.
Simple Forth example showing the handling of string and number input/output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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