Skip to content
28 changes: 24 additions & 4 deletions lib/iex/lib/iex/evaluator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,31 @@ defmodule IEx.Evaluator do

result =
with {:ok, tokens} <- :elixir.string_to_tokens(input, line, column, file, opts),
{:ok, adjusted_tokens} <- adjust_operator(tokens, line, column, file, opts, last_op),
{:ok, adjusted_tokens, adjusted_op} <-
adjust_operator(tokens, line, column, file, opts, last_op),
{:ok, forms} <- :elixir.tokens_to_quoted(adjusted_tokens, file, opts) do
last_op =
case forms do
{:=, _, [_, _]} -> :match
_ -> :other
end

forms =
if adjusted_op != nil do
quote do
if Process.get(:iex_error?) == true do
reraise RuntimeError.exception(
"skipping evaluation of expression because pipeline has failed"
),
[]
else
unquote(forms)
end
end
else
forms
end

{:ok, forms, last_op}
end

Expand Down Expand Up @@ -129,10 +146,10 @@ defmodule IEx.Evaluator do
defp adjust_operator([{op_type, _, _} | _] = tokens, line, column, file, opts, _last_op)
when op_type in @op_tokens do
{:ok, prefix} = :elixir.string_to_tokens(~c"v(-1)", line, column, file, opts)
{:ok, prefix ++ tokens}
{:ok, prefix ++ tokens, op_type}
end

defp adjust_operator(tokens, _line, _column, _file, _opts, _last_op), do: {:ok, tokens}
defp adjust_operator(tokens, _line, _column, _file, _opts, _last_op), do: {:ok, tokens, nil}

@doc """
Gets a value out of the binding, using the provided
Expand Down Expand Up @@ -293,9 +310,12 @@ defmodule IEx.Evaluator do
defp safe_eval_and_inspect(forms, counter, state) do
put_history(state)
put_whereami(state)
{:ok, eval_and_inspect(forms, counter, state)}
result = eval_and_inspect(forms, counter, state)
Process.delete(:iex_error?)
{:ok, result}
catch
kind, error ->
Process.put(:iex_error?, true)
print_error(kind, error, __STACKTRACE__)
{:error, state}
after
Expand Down