@@ -10,40 +10,11 @@ defmodule IEx.Server do
10
10
11
11
defp do_loop ( config ) do
12
12
counter = config . counter
13
- cache = config . cache
14
- code = cache ++ io_get ( config )
15
- file = "iex"
13
+ code = config . cache ++ io_get ( config )
16
14
17
15
new_config =
18
16
try do
19
- # Instead of doing just `eval`, we first parse the expression to see if
20
- # it's well formed. If parsing succeeds, we evaluate the AST as usual.
21
- #
22
- # If parsing fails, this might be a TokenMissingError which we treat in
23
- # a special way (to allow for continuation of an expression on the next
24
- # line in IEx). In case of any other error, we let :elixir_translator
25
- # to re-raise it.
26
- case :elixir_translator . forms ( code , counter , file , [ ] ) do
27
- { :ok , forms } ->
28
- { result , new_binding , scope } =
29
- :elixir . eval_forms ( forms , config . binding , config . scope )
30
-
31
- io_put result
32
-
33
- config = config . result ( result )
34
- update_history ( config . cache ( code ) . scope ( nil ) )
35
- config . update_counter ( & 1 + 1 ) . cache ( '' ) . binding ( new_binding ) . scope ( scope )
36
-
37
- { :error , { line , error , token } } ->
38
- if token == [ ] do
39
- # Update config.cache so that IEx continues to add new input to
40
- # the unfinished expression in `code`
41
- config . cache ( code )
42
- else
43
- # Encountered malformed expression
44
- :elixir_translator . parse_error ( line , file , error , token )
45
- end
46
- end
17
+ eval ( code , counter , config )
47
18
rescue
48
19
exception ->
49
20
print_stacktrace System . stacktrace , fn ->
@@ -61,6 +32,40 @@ defmodule IEx.Server do
61
32
do_loop ( new_config )
62
33
end
63
34
35
+ # Instead of doing just `:elixir.eval`, we first parse the expression to see
36
+ # if it's well formed. If parsing succeeds, we evaluate the AST as usual.
37
+ #
38
+ # If parsing fails, this might be a TokenMissingError which we treat in
39
+ # a special way (to allow for continuation of an expression on the next
40
+ # line in IEx). In case of any other error, we let :elixir_translator
41
+ # to re-raise it.
42
+ #
43
+ # Returns updated config.
44
+ defp eval ( code , line , config ) do
45
+ file = "iex"
46
+ case :elixir_translator . forms ( code , line , file , [ ] ) do
47
+ { :ok , forms } ->
48
+ { result , new_binding , scope } =
49
+ :elixir . eval_forms ( forms , config . binding , config . scope )
50
+
51
+ io_put result
52
+
53
+ config = config . result ( result )
54
+ update_history ( config . cache ( code ) . scope ( nil ) )
55
+ config . update_counter ( & 1 + 1 ) . cache ( '' ) . binding ( new_binding ) . scope ( scope )
56
+
57
+ { :error , { line , error , token } } ->
58
+ if token == [ ] do
59
+ # Update config.cache so that IEx continues to add new input to
60
+ # the unfinished expression in `code`
61
+ config . cache ( code )
62
+ else
63
+ # Encountered malformed expression
64
+ :elixir_errors . parse_error ( line , file , error , token )
65
+ end
66
+ end
67
+ end
68
+
64
69
defp print_stacktrace ( trace , callback ) do
65
70
try do
66
71
io_error callback . ( )
0 commit comments