Permalink
Browse files

Assert that later keys have higher preference.

  • Loading branch information...
1 parent ac9b615 commit f8814dc8d284d79eb8dc8aa9c2be4e20c6904586 @josevalim josevalim committed Dec 24, 2011
Showing with 30 additions and 18 deletions.
  1. +9 −8 include/elixir.hrl
  2. +2 −2 rebar.config
  3. +5 −2 src/elixir_macro.erl
  4. +2 −2 src/elixir_parser.yrl
  5. +3 −0 src/elixir_translator.erl
  6. +9 −4 test/erlang/match_test.erl
View
@@ -12,13 +12,14 @@
% 2) when true, we are inside a guard
% 3) when true, don't resolve sub references
% 4) when true, don't add new names. used by try.
-% 5) the current method
-% 6) the current namespace
-% 7) a dict of defined variables and their alias
-% 8) a list of all variables defined in a particular assign
-% 9) a dict of all variables defined in a particular clause
-% 10) a counter with the variables defined
-% 11) the current scope filename
+% 5) the current macro being transformed
+% 6) the current method
+% 7) the current namespace
+% 8) a dict of defined variables and their alias
+% 9) a list of all variables defined in a particular assign
+% 10) a dict of all variables defined in a particular clause
+% 11) a counter with the variables defined
+% 12) the current scope filename
%
--record(elixir_scope, {assign=false, guard=false, noref=false, noname=false, method=[],
+-record(elixir_scope, {assign=false, guard=false, noref=false, noname=false, macro=[], method=[],
namespace=[], vars=dict:new(), temp_vars=[], clause_vars=dict:new(), counter=0, filename="nofile"}).
View
@@ -6,7 +6,7 @@
warn_unused_vars,
warn_export_all,
warn_shadow_vars,
- %% warn_unused_import,
+ warn_unused_import,
warn_unused_function,
warn_bif_clash,
warn_unused_record,
@@ -16,7 +16,7 @@
warn_export_vars,
warn_exported_vars,
%% warn_missing_spec,
- warn_untyped_record,
+ %% warn_untyped_record,
%% warnings_as_errors,
debug_info
]}.
View
@@ -6,11 +6,14 @@ dispatch_one(Receiver, Name, Args, S, Callback) ->
case is_bootstrap(S#elixir_scope.namespace) of
true -> Callback();
false ->
+ Arity = length(Args),
try
- case lists:member({Name, length(Args)}, Receiver:'__macros__'()) of
+ case lists:member({Name, Arity}, Receiver:'__macros__'()) of
true ->
Tree = apply(Receiver, Name, Args),
- elixir_translator:translate_each(Tree, S);
+ NewS = S#elixir_scope{macro={Name,Arity}},
+ { TTree, TS } = elixir_translator:translate_each(Tree, S),
+ { TTree, TS#elixir_scope{macro=[]} };
false -> Callback()
end
catch
View
@@ -295,10 +295,10 @@ kv_eol -> kv_identifier : '$1'.
kv_eol -> kv_identifier eol : '$1'.
kv_comma -> kv_eol expr : [{?exprs('$1'),'$2'}].
-kv_comma -> kv_eol expr comma_separator kv_comma : [{?exprs('$1'), '$2'}|'$4'].
+kv_comma -> kv_eol expr comma_separator kv_comma : orddict:update(?exprs('$1'), fun(X) -> X end, '$2', '$4').
matched_kv_comma -> kv_eol matched_expr : [{?exprs('$1'),'$2'}].
-matched_kv_comma -> kv_eol matched_expr comma_separator kv_comma : [{?exprs('$1'), '$2'}|'$4'].
+matched_kv_comma -> kv_eol matched_expr comma_separator kv_comma : orddict:update(?exprs('$1'), fun(X) -> X end, '$2', '$4').
base_orddict -> kv_comma : { '[:]', ?line(hd('$1')), '$1' }.
matched_base_orddict -> matched_kv_comma : { '[:]', ?line(hd('$1')), '$1' }.
@@ -85,6 +85,9 @@ translate_each({ block, Line, Args }, S) when is_list(Args) ->
{ TArgs, NS } = translate(Args, S),
{ { block, Line, TArgs }, NS };
+% TODO: Add tests that kv_block not handled by macros raises
+% a meaningful exception
+
%% Bit strings
translate_each({ bitstr, Line, Args }, S) when is_list(Args) ->
View
@@ -43,9 +43,8 @@ multiple_assignment_with_expression_test() ->
multiple_assignment_with_binding_expression_test() ->
{3, [{a, 3}, {b, 1}]} = elixir:eval("a = (2 + b)", [{b, 1}]).
-% underscore_assignment_test() ->
-% {1, []} = elixir:eval("_ = 1"),
-% {_, [{a,1},{c,3}]} = elixir:eval("[a,_,c] = [1,2,3]\n[\~a,_,\~c] = [1,4,3]").
+underscore_assignment_test() ->
+ {1, []} = elixir:eval("_ = 1").
% Tuples match
simple_tuple_test() ->
@@ -76,4 +75,10 @@ head_and_tail_test() ->
{_,[{t,[3]}]} = elixir:eval("[1,2|t] = [1,2,3]"),
{_,[{h,1}]} = elixir:eval("[h|[2,3]] = [1,2,3]"),
{_,[{t,[2,3]}]} = elixir:eval("[+1|t] = [1,2,3]"),
- ?assertError({badmatch, _}, elixir:eval("[2,h|t] = [1,2,3]")).
+ ?assertError({badmatch, _}, elixir:eval("[2,h|t] = [1,2,3]")).
+
+% Orddict match
+
+orrdict_match_test() ->
+ {[{a,1},{b,2}], _} = elixir:eval("a = [a: 1, b: 2]"),
+ {[{a,3},{b,2}], _} = elixir:eval("a = [a: 1, b: 2, a: 3]").

0 comments on commit f8814dc

Please sign in to comment.