factor "parse-word" out of "scan-word"

db4
Joe Groff 2009-10-16 12:26:31 -05:00
parent 98a2f5152c
commit ce807b0fbb
2 changed files with 14 additions and 5 deletions

View File

@ -140,12 +140,20 @@ HELP: no-word
{ $values { "name" string } { "newword" word } }
{ $description "Throws a " { $link no-word-error } "." } ;
HELP: parse-word
{ $values { "string" string } { "word/number" "a word or number" } }
{ $description "If " { $snippet "string" } " is a valid number literal, it is converted to a number, otherwise the current vocabulary search path is searched for a word named by the string." }
{ $errors "Throws an error if the token does not name a word, and does not parse as a number." }
{ $notes "This word is used to implement " { $link scan-word } "." } ;
HELP: scan-word
{ $values { "word/number/f" "a word, number or " { $link f } } }
{ $description "Reads the next token from parser input. If the token is a valid number literal, it is converted to a number, otherwise the dictionary is searched for a word named by the token. Outputs " { $link f } " if the end of the input has been reached." }
{ $errors "Throws an error if the token does not name a word, and does not parse as a number." }
$parsing-note ;
{ scan-word parse-word } related-words
HELP: parse-step
{ $values { "accum" vector } { "end" word } { "?" "a boolean" } }
{ $description "Parses a token. If the token is a number or an ordinary word, it is added to the accumulator. If it is a parsing word, calls the parsing word with the accumulator on the stack. Outputs " { $link f } " if " { $snippet "end" } " is encountered, " { $link t } " otherwise." }

View File

@ -40,12 +40,13 @@ SYMBOL: auto-use?
[ <no-word-error> throw-restarts no-word-restarted ]
if ;
: parse-word ( string -- word/number )
dup search [ ] [
dup string>number [ ] [ no-word ] ?if
] ?if ;
: scan-word ( -- word/number/f )
scan dup [
dup search [ ] [
dup string>number [ ] [ no-word ] ?if
] ?if
] when ;
scan dup [ parse-word ] when ;
ERROR: staging-violation word ;