]> gitweb.factorcode.org Git - factor.git/blob - basis/listener/listener.factor
Move call( and execute( to core
[factor.git] / basis / listener / listener.factor
1 ! Copyright (C) 2003, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays hashtables io kernel math math.parser memory
4 namespaces parser lexer sequences strings io.styles
5 vectors words generic system combinators continuations debugger
6 definitions compiler.units accessors colors prettyprint fry
7 sets vocabs.parser ;
8 IN: listener
9
10 GENERIC: stream-read-quot ( stream -- quot/f )
11
12 : parse-lines-interactive ( lines -- quot/f )
13     [ parse-lines in get ] with-compilation-unit in set ;
14
15 : read-quot-step ( lines -- quot/f )
16     [ parse-lines-interactive ] [
17         dup error>> unexpected-eof?
18         [ 2drop f ] [ rethrow ] if
19     ] recover ;
20
21 : read-quot-loop ( stream accum -- quot/f )
22     over stream-readln dup [
23         over push
24         dup read-quot-step dup
25         [ 2nip ] [ drop read-quot-loop ] if
26     ] [
27         3drop f
28     ] if ;
29
30 M: object stream-read-quot
31     V{ } clone read-quot-loop ;
32
33 : read-quot ( -- quot/f ) input-stream get stream-read-quot ;
34
35 <PRIVATE
36
37 SYMBOL: quit-flag
38
39 PRIVATE>
40
41 : bye ( -- ) quit-flag on ;
42
43 SYMBOL: visible-vars
44
45 : show-var ( var -- ) visible-vars  [ swap suffix ] change ;
46
47 : show-vars ( seq -- ) visible-vars [ swap union ] change ;
48
49 : hide-var ( var -- ) visible-vars [ remove ] change ;
50
51 : hide-vars ( seq -- ) visible-vars [ swap diff ] change ;
52
53 : hide-all-vars ( -- ) visible-vars off ;
54
55 SYMBOL: error-hook
56
57 : call-error-hook ( error -- )
58     error-continuation get error-hook get
59     call( error continuation -- ) ;
60
61 [ drop print-error-and-restarts ] error-hook set-global
62
63 SYMBOL: display-stacks?
64
65 t display-stacks? set-global
66
67 SYMBOL: max-stack-items
68
69 10 max-stack-items set-global
70
71 <PRIVATE
72
73 : title. ( string -- )
74     H{ { foreground T{ rgba f 0.3 0.3 0.3 1 } } } format nl ;
75
76 : visible-vars. ( -- )
77     visible-vars get [
78         nl "--- Watched variables:" title.
79         standard-table-style [
80             [
81                 [
82                     [ [ short. ] with-cell ]
83                     [ [ get short. ] with-cell ]
84                     bi
85                 ] with-row
86             ] each
87         ] tabular-output nl
88     ] unless-empty ;
89     
90 : trimmed-stack. ( seq -- )
91     dup length max-stack-items get > [
92         max-stack-items get cut*
93         [
94             [ length number>string "(" " more items)" surround ] keep
95             write-object nl
96         ] dip
97     ] when stack. ;
98
99 : stacks. ( -- )
100     display-stacks? get [
101         datastack [ nl "--- Data stack:" title. trimmed-stack. ] unless-empty
102     ] when ;
103
104 : prompt. ( -- )
105     "( " in get auto-use? get [ " - auto" append ] when " )" 3append
106     H{ { background T{ rgba f 1 0.7 0.7 1 } } } format bl flush ;
107
108 : listen ( -- )
109     visible-vars. stacks. prompt.
110     [ read-quot [ [ call-error-hook ] recover ] [ bye ] if* ]
111     [ dup lexer-error? [ call-error-hook ] [ rethrow ] if ] recover ;
112
113 : until-quit ( -- )
114     quit-flag get [ quit-flag off ] [ listen until-quit ] if ;
115
116 PRIVATE>
117
118 : listener ( -- )
119     [ until-quit ] with-interactive-vocabs ;
120
121 MAIN: listener