Refactor listener so that it infers

db4
Slava Pestov 2009-04-13 17:19:20 -05:00
parent bfe0787454
commit 2fc05aa44c
2 changed files with 32 additions and 39 deletions

View File

@ -1,4 +1,4 @@
USING: help.markup help.syntax kernel io system prettyprint ; USING: help.markup help.syntax kernel io system prettyprint continuations ;
IN: listener IN: listener
ARTICLE: "listener-watch" "Watching variables in the listener" ARTICLE: "listener-watch" "Watching variables in the listener"
@ -41,32 +41,18 @@ $nl
{ $example "{ 1 2 3 } [\n .\n] each" "1\n2\n3" } { $example "{ 1 2 3 } [\n .\n] each" "1\n2\n3" }
"The listener knows when to expect more input by looking at the height of the stack. Parsing words such as " { $link POSTPONE: { } " leave elements on the parser stack, and corresponding words such as " { $link POSTPONE: } } " pop them." "The listener knows when to expect more input by looking at the height of the stack. Parsing words such as " { $link POSTPONE: { } " leave elements on the parser stack, and corresponding words such as " { $link POSTPONE: } } " pop them."
{ $subsection "listener-watch" } { $subsection "listener-watch" }
"You can start a nested listener or exit a listener using the following words:" "To start a nested listener:"
{ $subsection listener } { $subsection listener }
{ $subsection bye } "To exit the listener, invoke the " { $link return } " word."
"Finally, the multi-line expression reading word can be used independently of the rest of the listener:" $nl
"Multi-line quotations can be read independently of the rest of the listener:"
{ $subsection read-quot } ; { $subsection read-quot } ;
ABOUT: "listener" ABOUT: "listener"
<PRIVATE
HELP: quit-flag
{ $var-description "Variable set to true by " { $link bye } " word; it forces the next iteration of the " { $link listener } " loop to end." } ;
PRIVATE>
HELP: read-quot HELP: read-quot
{ $values { "quot/f" "a parsed quotation, or " { $link f } " indicating end of file" } } { $values { "quot/f" "a parsed quotation, or " { $link f } " indicating end of file" } }
{ $description "Reads a Factor expression which possibly spans more than one line from " { $link input-stream } ". Additional lines of input are read while the parser stack height is greater than one. Since structural parsing words push partial quotations on the stack, this will keep on reading input until all delimited parsing words are terminated." } ; { $description "Reads a Factor expression which possibly spans more than one line from " { $link input-stream } ". Additional lines of input are read while the parser stack height is greater than one. Since structural parsing words push partial quotations on the stack, this will keep on reading input until all delimited parsing words are terminated." } ;
HELP: listen
{ $description "Prompts for an expression on " { $link input-stream } " and evaluates it. On end of file, " { $link quit-flag } " is set to terminate the listener loop." }
{ $errors "If the expression input by the user throws an error, the error is printed to " { $link output-stream } " and the word returns normally." } ;
HELP: listener HELP: listener
{ $description "Prompts for expressions on " { $link input-stream } " and evaluates them until end of file is reached." } ; { $description "Prompts for expressions on " { $link input-stream } " and evaluates them until end of file is reached." } ;
HELP: bye
{ $description "Exits the current listener." }
{ $notes "This word is for interactive use only. To exit the Factor runtime, use " { $link exit } "." } ;

View File

@ -4,7 +4,7 @@ USING: arrays hashtables io kernel math math.parser memory
namespaces parser lexer sequences strings io.styles namespaces parser lexer sequences strings io.styles
vectors words generic system combinators continuations debugger vectors words generic system combinators continuations debugger
definitions compiler.units accessors colors prettyprint fry definitions compiler.units accessors colors prettyprint fry
sets vocabs.parser source-files.errors ; sets vocabs.parser source-files.errors locals ;
IN: listener IN: listener
GENERIC: stream-read-quot ( stream -- quot/f ) GENERIC: stream-read-quot ( stream -- quot/f )
@ -32,14 +32,6 @@ M: object stream-read-quot
: read-quot ( -- quot/f ) input-stream get stream-read-quot ; : read-quot ( -- quot/f ) input-stream get stream-read-quot ;
<PRIVATE
SYMBOL: quit-flag
PRIVATE>
: bye ( -- ) quit-flag on ;
SYMBOL: visible-vars SYMBOL: visible-vars
: show-var ( var -- ) visible-vars [ swap suffix ] change ; : show-var ( var -- ) visible-vars [ swap suffix ] change ;
@ -98,28 +90,43 @@ SYMBOL: error-summary-hook
] dip ] dip
] when stack. ; ] when stack. ;
: stacks. ( -- ) : datastack. ( datastack -- )
display-stacks? get [ display-stacks? get [
datastack [ nl "--- Data stack:" title. trimmed-stack. ] unless-empty [ nl "--- Data stack:" title. trimmed-stack. ] unless-empty
] when ; ] [ drop ] if ;
: prompt. ( -- ) : prompt. ( -- )
"( " in get auto-use? get [ " - auto" append ] when " )" 3append in get auto-use? get [ " - auto" append ] when "( " " )" surround
H{ { background T{ rgba f 1 0.7 0.7 1 } } } format bl flush ; H{ { background T{ rgba f 1 0.7 0.7 1 } } } format bl flush ;
[ error-summary ] error-summary-hook set-global [ error-summary ] error-summary-hook set-global
: listen ( -- ) : call-error-summary-hook ( -- )
error-summary-hook get call( -- ) visible-vars. stacks. prompt. error-summary-hook get call( -- ) ;
[ read-quot [ [ call-error-hook ] recover ] [ bye ] if* ]
[ dup lexer-error? [ call-error-hook ] [ rethrow ] if ] recover ;
: until-quit ( -- ) :: (listener) ( datastack -- )
quit-flag get [ quit-flag off ] [ listen until-quit ] if ; call-error-summary-hook
visible-vars.
datastack datastack.
prompt.
[
read-quot [
'[ datastack _ with-datastack ]
[ call-error-hook datastack ]
recover
(listener)
] when*
] [
dup lexer-error?
[ call-error-hook datastack (listener) ]
[ rethrow ]
if
] recover ;
PRIVATE> PRIVATE>
: listener ( -- ) : listener ( -- )
[ until-quit ] with-interactive-vocabs ; [ [ { } (listener) ] with-interactive-vocabs ] with-return ;
MAIN: listener MAIN: listener