Skip to content

Commit 98ed304

Browse files
committed
Clean up the code some more. Closes #1051
1 parent 36fab9d commit 98ed304

File tree

1 file changed

+36
-31
lines changed

1 file changed

+36
-31
lines changed

lib/iex/lib/iex/server.ex

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,11 @@ defmodule IEx.Server do
1010

1111
defp do_loop(config) do
1212
counter = config.counter
13-
cache = config.cache
14-
code = cache ++ io_get(config)
15-
file = "iex"
13+
code = config.cache ++ io_get(config)
1614

1715
new_config =
1816
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)
4718
rescue
4819
exception ->
4920
print_stacktrace System.stacktrace, fn ->
@@ -61,6 +32,40 @@ defmodule IEx.Server do
6132
do_loop(new_config)
6233
end
6334

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+
6469
defp print_stacktrace(trace, callback) do
6570
try do
6671
io_error callback.()

0 commit comments

Comments
 (0)