11defmodule ElixirScript.PatternMatching.Match do
2+ @ moduledoc false
3+
24 alias ESTree.Tools.Builder , as: JS
35 alias ElixirScript.Translator
46 alias ElixirScript.Translator.Utils
@@ -88,30 +90,30 @@ defmodule ElixirScript.PatternMatching.Match do
8890 )
8991 end
9092
91- def build_match ( params ) do
92- Enum . map ( params , & do_build_match ( & 1 ) )
93+ def build_match ( params , env ) do
94+ Enum . map ( params , & do_build_match ( & 1 , env ) )
9395 |> reduce_patterns
9496 end
9597
96- defp do_build_match ( { :^ , _ , [ value ] } ) do
97- { [ bound ( Translator . translate ( value ) ) ] , [ nil ] }
98+ defp do_build_match ( { :^ , _ , [ value ] } , env ) do
99+ { [ bound ( Translator . translate ( value , env ) ) ] , [ nil ] }
98100 end
99101
100- defp do_build_match ( { :_ , _ , _ } ) do
102+ defp do_build_match ( { :_ , _ , _ } , env ) do
101103 { [ @ wildcard ] , [ JS . identifier ( :undefined ) ] }
102104 end
103105
104- defp do_build_match ( [ { :| , _ , [ head , tail ] } ] ) do
105- { [ @ head_tail ] , [ Translator . translate ( head ) , Translator . translate ( tail ) ] }
106+ defp do_build_match ( [ { :| , _ , [ head , tail ] } ] , env ) do
107+ { [ @ head_tail ] , [ Translator . translate ( head , env ) , Translator . translate ( tail , env ) ] }
106108 end
107109
108- defp do_build_match ( { :<> , _ , [ prefix , value ] } ) do
109- { [ startsWith ( prefix ) ] , [ Translator . translate ( value ) ] }
110+ defp do_build_match ( { :<> , _ , [ prefix , value ] } , env ) do
111+ { [ startsWith ( prefix ) ] , [ Translator . translate ( value , env ) ] }
110112 end
111113
112- defp do_build_match ( { :%{} , _ , props } ) do
114+ defp do_build_match ( { :%{} , _ , props } , env ) do
113115 properties = Enum . map ( props , fn ( { key , value } ) ->
114- { pattern , params } = do_build_match ( value )
116+ { pattern , params } = do_build_match ( value , env )
115117 { JS . property ( JS . literal ( key ) , hd ( List . wrap ( pattern ) ) ) , params }
116118 end )
117119
@@ -122,44 +124,44 @@ defmodule ElixirScript.PatternMatching.Match do
122124 { JS . object_expression ( List . wrap ( props ) ) , params }
123125 end
124126
125- defp do_build_match ( { :% , _ , [ { :__aliases__ , _ , name } , { :%{} , meta , props } ] } ) do
127+ defp do_build_match ( { :% , _ , [ { :__aliases__ , _ , name } , { :%{} , meta , props } ] } , env ) do
126128 props = [ { "__struct__" , List . last ( name ) } ] ++ props
127- do_build_match ( { :%{} , meta , props } )
129+ do_build_match ( { :%{} , meta , props } , env )
128130 end
129131
130- defp do_build_match ( { := , _ , [ { name , _ , _ } , right ] } ) when not name in [ :% , :{} , :__aliases__ , :^ ] do
131- unify ( name , right )
132+ defp do_build_match ( { := , _ , [ { name , _ , _ } , right ] } , env ) when not name in [ :% , :{} , :__aliases__ , :^ ] do
133+ unify ( name , right , env )
132134 end
133135
134- defp do_build_match ( { := , _ , [ left , { name , _ , _ } ] } ) when not name in [ :% , :{} , :__aliases__ , :^ ] do
135- unify ( name , left )
136+ defp do_build_match ( { := , _ , [ left , { name , _ , _ } ] } , env ) when not name in [ :% , :{} , :__aliases__ , :^ ] do
137+ unify ( name , left , env )
136138 end
137139
138- defp do_build_match ( list ) when is_list ( list ) do
140+ defp do_build_match ( list , env ) when is_list ( list ) do
139141 { patterns , params } = list
140- |> Enum . map ( & build_match ( [ & 1 ] ) )
142+ |> Enum . map ( & build_match ( [ & 1 ] , env ) )
141143 |> reduce_patterns
142144
143145 { [ make_list ( patterns ) ] , params }
144146 end
145147
146- defp do_build_match ( term ) when is_number ( term ) or is_binary ( term ) or is_boolean ( term ) or is_atom ( term ) or is_nil ( term ) do
147- { [ Translator . translate ( term ) ] , [ ] }
148+ defp do_build_match ( term , env ) when is_number ( term ) or is_binary ( term ) or is_boolean ( term ) or is_atom ( term ) or is_nil ( term ) do
149+ { [ Translator . translate ( term , env ) ] , [ ] }
148150 end
149151
150- defp do_build_match ( { one , two } ) do
151- do_build_match ( { :{} , [ ] , [ one , two ] } )
152+ defp do_build_match ( { one , two } , env ) do
153+ do_build_match ( { :{} , [ ] , [ one , two ] } , env )
152154 end
153155
154- defp do_build_match ( { :{} , _ , list } ) do
156+ defp do_build_match ( { :{} , _ , list } , env ) do
155157 { patterns , params } = list
156- |> Enum . map ( & build_match ( [ & 1 ] ) )
158+ |> Enum . map ( & build_match ( [ & 1 ] , env ) )
157159 |> reduce_patterns
158160
159161 { [ make_tuple ( patterns ) ] , params }
160162 end
161163
162- defp do_build_match ( { name , _ , _ } ) do
164+ defp do_build_match ( { name , _ , _ } , env ) do
163165 name = Utils . filter_name ( name )
164166 { [ @ parameter ] , [ JS . identifier ( name ) ] }
165167 end
@@ -171,8 +173,8 @@ defmodule ElixirScript.PatternMatching.Match do
171173 end )
172174 end
173175
174- defp unify ( target , source ) do
175- { patterns , params } = build_match ( [ source ] )
176+ defp unify ( target , source , env ) do
177+ { patterns , params } = build_match ( [ source ] , env )
176178 { [ capture ( hd ( patterns ) ) ] , params ++ [ JS . identifier ( Utils . filter_name ( target ) ) ] }
177179 end
178180
0 commit comments