]> gitweb.factorcode.org Git - factor.git/blob - extra/fuel/eval/eval.factor
FUEL: refactoring to eliminate the eval-result variable
[factor.git] / extra / fuel / eval / eval.factor
1 ! Copyright (C) 2009 Jose Antonio Ortega Ruiz.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays continuations debugger fry fuel.pprint io
4 io.streams.string kernel listener namespaces parser.notes
5 prettyprint.config sequences sets vocabs.parser ;
6 IN: fuel.eval
7
8 SYMBOL: restarts-stack
9 V{ } clone restarts-stack set-global
10
11 SYMBOL: eval-res-flag
12 t eval-res-flag set-global
13
14 : eval-restartable? ( -- ? )
15     eval-res-flag get-global ;
16
17 : push-status ( -- )
18     restarts get-global clone restarts-stack get push ;
19
20 : pop-restarts ( restarts -- )
21     eval-restartable? [ drop ] [ clone restarts set-global ] if ;
22
23 : pop-status ( -- )
24     restarts-stack get [ pop pop-restarts ] unless-empty ;
25
26 : send-retort ( error result output -- )
27     3array [ fuel-pprint ] without-limits flush nl
28     "<~FUEL~>" write nl flush ;
29
30 : begin-eval ( -- )
31     push-status ;
32
33 : end-eval ( result error/f output -- )
34     swapd send-retort pop-status ;
35
36 : eval ( lines -- result error/f )
37     '[ _ parse-lines-interactive call( -- x ) f ]
38     [ dup print-error f swap ] recover ;
39
40 : eval-usings ( usings -- )
41     [ [ use-vocab ] curry ignore-errors ] each ;
42
43 : eval-in ( in -- )
44     [ set-current-vocab ] when* ;
45
46 : eval-in-context ( lines in usings/f -- )
47     begin-eval
48     [
49         parser-quiet? on
50         [
51             ! The idea is that a correct usings list should always be
52             ! specified. But a lot of code in FUEL sends empty usings
53             ! lists so then we have to use the current manifests
54             ! vocabs instead.
55             manifest get search-vocab-names>> members
56         ] [
57             ! These vocabs are always needed in the manifest. syntax for
58             ! obvious reasons, fuel for FUEL stuff and debugger for the :N
59             ! words.
60             { "fuel" "syntax" "debugger" } prepend
61         ] if-empty
62         <manifest> manifest namespaces:set
63         [ eval-usings eval-in eval ] with-string-writer
64     ] with-scope end-eval ;