Skip to content

Commit b5ab795

Browse files
author
José Valim
committed
Clean up conversion to list
1 parent 7f9e95e commit b5ab795

File tree

4 files changed

+26
-63
lines changed

4 files changed

+26
-63
lines changed

lib/elixir/src/elixir_literal.erl

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-module(elixir_literal).
22
-export([translate/2]).
33
-import(elixir_translator, [translate_each/2, translate_args/2]).
4-
-import(elixir_scope, [umergev/2, umergec/2]).
4+
-import(elixir_scope, [umergec/2]).
55
-include("elixir.hrl").
66
-compile({parse_transform, elixir_transform}).
77

@@ -29,33 +29,23 @@ translate({ '{}', Meta, Args } = Original, S) when is_list(Args) ->
2929
translate({ '[]', _Meta, [] }, S) ->
3030
{ { nil, 0 }, S };
3131

32-
translate({ '[]', _Meta, Args } = Original, S) when is_list(Args) ->
32+
translate({ '[]', Meta, Args } = Original, S) when is_list(Args) ->
3333
case elixir_partials:handle(Original, S, allow_tail) of
3434
error ->
3535
[RTail|RArgs] = lists:reverse(Args),
3636

3737
case RTail of
3838
{'|',_,[Left,Right]} ->
39-
Exprs = [Left|RArgs],
40-
{ Tail, ST } = translate_each(Right, S),
41-
ListS = umergec(S, ST);
39+
RExprs = [Left|RArgs],
40+
TailFun = fun(ST) -> translate_each(Right, ST) end;
4241
_ ->
43-
Exprs = [RTail|RArgs],
44-
Tail = { nil, 0 },
45-
ST = S,
46-
ListS = S
42+
RExprs = [RTail|RArgs],
43+
TailFun = fun(ST) -> { { nil, ?line(Meta) }, ST } end
4744
end,
4845

49-
{ FExprs, FS } = case S#elixir_scope.context of
50-
match ->
51-
elixir_tree_helpers:build_reverse_list(fun elixir_translator:translate_each/2, Exprs, 0, ListS, Tail);
52-
_ ->
53-
{ TArgs, { SC, SV } } =
54-
elixir_tree_helpers:build_reverse_list(fun elixir_translator:translate_arg/2, Exprs, 0, { ListS, ListS }, Tail),
55-
{ TArgs, umergec(SV, SC) }
56-
end,
57-
58-
{ FExprs, umergev(ST, FS) };
46+
{ Exprs, SE } = translate_args(lists:reverse(RExprs), S),
47+
{ Tail, ST } = TailFun(SE),
48+
{ elixir_tree_helpers:list_to_cons(?line(Meta), Exprs, Tail), ST };
5949
Else -> Else
6050
end;
6151

lib/elixir/src/elixir_quote.erl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,13 @@ from_buffer_to_acc([], _Q, Acc, S) ->
200200
{ Acc, S };
201201

202202
from_buffer_to_acc(Buffer, Q, Acc, S) ->
203-
{ New, NewS } = elixir_tree_helpers:build_reverse_list(
204-
fun(X, AccS) -> do_quote(X, Q, AccS) end, Buffer, line(Q), S),
203+
Line = line(Q),
204+
205+
{ New, NewS } = lists:foldl(fun(X, { AccX, AccS }) ->
206+
{ FX, FS } = do_quote(X, Q, AccS),
207+
{ { cons, Line, FX, AccX }, FS }
208+
end, { { nil, Line }, S }, Buffer),
209+
205210
{ [New|Acc], NewS }.
206211

207212
meta(Meta, #elixir_quote{line=keep}) ->

lib/elixir/src/elixir_translator.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ translate_each({ '__aliases__', Meta, _ } = Alias, S) ->
229229
Atoms = [Atom || { atom, _, Atom } <- TAliases],
230230
{ { atom, ?line(Meta), elixir_aliases:concat(Atoms) }, SA };
231231
false ->
232-
Args = [elixir_tree_helpers:build_simple_list(?line(Meta), TAliases)],
232+
Args = [elixir_tree_helpers:list_to_cons(?line(Meta), TAliases)],
233233
{ ?wrap_call(?line(Meta), elixir_aliases, concat, Args), SA }
234234
end
235235
end;

lib/elixir/src/elixir_tree_helpers.erl

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
-module(elixir_tree_helpers).
66
-compile({parse_transform, elixir_transform}).
77
-export([abstract_syntax/1, split_last/1, cons_to_list/1,
8-
convert_to_boolean/5, returns_boolean/1, get_line/1,
9-
build_list/4, build_list/5, build_simple_list/2,
10-
build_reverse_list/4, build_reverse_list/5, build_simple_reverse_list/2]).
8+
list_to_cons/2, list_to_cons/3,
9+
convert_to_boolean/5, returns_boolean/1, get_line/1]).
1110
-include("elixir.hrl").
1211

1312
get_line(Opts) ->
@@ -50,44 +49,13 @@ cons_to_list({ cons, _, Left, { nil, _ } }) ->
5049
cons_to_list({ cons, _, Left, Right }) ->
5150
[Left|cons_to_list(Right)].
5251

53-
% Build a list translating each expression and accumulating
54-
% vars in one pass. It uses tail-recursive form.
55-
%
56-
% It receives a function to translate each expression given
57-
% in Exprs, a Line used to build the List and the variables
58-
% scope V is passed down item by item.
59-
%
60-
% The function needs to return a tuple where the first element
61-
% is an erlang abstract form and the second is the new variables
62-
% list.
63-
build_list(Fun, Exprs, Line, S) ->
64-
build_list(Fun, Exprs, Line, S, {nil, Line}).
65-
66-
build_list(Fun, Exprs, Line, S, Tail) when is_integer(Line) ->
67-
build_list_each(Fun, lists:reverse(Exprs), Line, S, Tail).
68-
69-
% Same as build_list, but the list given is in reverse other.
70-
build_reverse_list(Fun, Exprs, Line, S) ->
71-
build_list_each(Fun, Exprs, Line, S, {nil,Line}).
72-
73-
build_reverse_list(Fun, Exprs, Line, S, Tail) when is_integer(Line) ->
74-
build_list_each(Fun, Exprs, Line, S, Tail).
75-
76-
% Builds a simple list, without translatation, just by generating the cons-cell.
77-
build_simple_list(Line, Args) ->
78-
{ List, [] } = build_list(fun(X,Y) -> {X,Y} end, Args, Line, []),
79-
List.
80-
81-
build_simple_reverse_list(Line, Args) ->
82-
{ List, [] } = build_reverse_list(fun(X,Y) -> {X,Y} end, Args, Line, []),
83-
List.
84-
85-
build_list_each(_Fun, [], _Line, S, Acc) ->
86-
{ Acc, S };
87-
88-
build_list_each(Fun, [H|T], Line, S, Acc) ->
89-
{ Expr, NS } = Fun(H, S),
90-
build_list_each(Fun, T, Line, NS, { cons, Line, Expr, Acc }).
52+
list_to_cons(Line, List) ->
53+
list_to_cons(Line, List, { nil, Line }).
54+
55+
list_to_cons(Line, List, Tail) ->
56+
lists:foldr(fun(X, Acc) ->
57+
{ cons, Line, X, Acc }
58+
end, Tail, List).
9159

9260
%% Others
9361

0 commit comments

Comments
 (0)