]> gitweb.factorcode.org Git - factor.git/blob - extra/fuel/eval/eval.factor
9d45e63f66941b3813cdb28d658667388f347f20
[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 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-result
12 f eval-result set-global
13
14 SYMBOL: eval-res-flag
15 t eval-res-flag set-global
16
17 : eval-restartable? ( -- ? )
18     eval-res-flag get-global ;
19
20 : push-status ( -- )
21     restarts get-global clone restarts-stack get push ;
22
23 : pop-restarts ( restarts -- )
24     eval-restartable? [ drop ] [ clone restarts set-global ] if ;
25
26 : pop-status ( -- )
27     restarts-stack get [ pop pop-restarts ] unless-empty ;
28
29 : send-retort ( error result output -- )
30     3array [ fuel-pprint ] without-limits flush nl
31     "<~FUEL~>" write nl flush ;
32
33 : begin-eval ( -- )
34     f eval-result set-global push-status ;
35
36 : end-eval ( error/f output -- )
37     eval-result get-global swap send-retort pop-status ;
38
39 : eval ( lines -- error/f )
40     [ parse-lines-interactive call( -- ) f ] curry
41     [ dup print-error ] recover ;
42
43 : eval-usings ( usings -- )
44     [ [ use-vocab ] curry ignore-errors ] each ;
45
46 : eval-in ( in -- )
47     [ set-current-vocab ] when* ;
48
49 : eval-in-context ( lines in usings/f -- )
50     begin-eval
51     [
52         parser-quiet? on
53         [
54             ! The idea is that a correct usings list should always be
55             ! specified. But a lot of code in FUEL sends empty usings
56             ! lists so then we have to use the current manifests
57             ! vocabs instead.
58             manifest get search-vocab-names>> members
59         ] [
60             ! These vocabs are always needed in the manifest. syntax for
61             ! obvious reasons, fuel for FUEL stuff and debugger for the :N
62             ! words.
63             { "fuel" "syntax" "debugger" } prepend
64         ] if-empty
65         <manifest> manifest namespaces:set
66         [ eval-usings eval-in eval ] with-string-writer
67     ] with-scope end-eval ;