@@ -4,6 +4,7 @@ defmodule ElixirScript.Translate.Form do
44 # Handles translation of all forms that are not functions or clauses
55
66 alias ESTree.Tools.Builder , as: J
7+ alias ElixirScript.Translate.Helpers
78 alias ElixirScript.Translate.Forms . { Bitstring , Match , Try , For , Receive , Remote , Pattern , With }
89 alias ElixirScript.Translate.Clause
910 require Logger
@@ -23,7 +24,7 @@ defmodule ElixirScript.Translate.Form do
2324 end
2425
2526 def compile ( [ { :| , _ , [ head , tail ] } ] , state ) do
26- ast = J . call_expression (
27+ ast = Helpers . call (
2728 J . member_expression (
2829 J . array_expression ( [ compile! ( head , state ) ] ) ,
2930 J . identifier ( "concat" )
@@ -35,7 +36,7 @@ defmodule ElixirScript.Translate.Form do
3536 end
3637
3738 def compile ( { :| , _ , [ head , tail ] } , state ) do
38- ast = J . call_expression (
39+ ast = Helpers . call (
3940 J . member_expression (
4041 J . array_expression ( [ compile! ( head , state ) ] ) ,
4142 J . identifier ( "concat" )
@@ -58,13 +59,7 @@ defmodule ElixirScript.Translate.Form do
5859 ast = if ElixirScript.Translate.Module . is_elixir_module ( form ) do
5960 Remote . process_module_name ( form , state )
6061 else
61- J . call_expression (
62- J . member_expression (
63- J . identifier ( "Symbol" ) ,
64- J . identifier ( "for" )
65- ) ,
66- [ J . literal ( form ) ]
67- )
62+ Helpers . symbol ( form )
6863 end
6964
7065 { ast , state }
@@ -75,14 +70,8 @@ defmodule ElixirScript.Translate.Form do
7570 end
7671
7772 def compile ( { :{} , _ , elements } , state ) do
78- ast = J . new_expression (
79- J . member_expression (
80- J . member_expression (
81- J . identifier ( "ElixirScript" ) ,
82- J . identifier ( "Core" )
83- ) ,
84- J . identifier ( "Tuple" )
85- ) ,
73+ ast = Helpers . new (
74+ Helpers . tuple ( ) ,
8675 Enum . map ( elements , & compile! ( & 1 , state ) ) |> List . flatten
8776 )
8877
@@ -130,7 +119,7 @@ defmodule ElixirScript.Translate.Form do
130119 end
131120
132121 def compile ( { :% , _ , [ module , params ] } , state ) do
133- ast = J . call_expression (
122+ ast = Helpers . call (
134123 J . member_expression (
135124 Remote . process_module_name ( module , state ) ,
136125 J . identifier ( "__struct__" )
@@ -155,15 +144,15 @@ defmodule ElixirScript.Translate.Form do
155144 end
156145
157146 def compile ( { :case , _ , [ condition , [ do: clauses ] ] } , state ) do
158- func = J . call_expression (
147+ func = Helpers . call (
159148 J . member_expression (
160- ElixirScript.Translate.Function . patterns_ast ( ) ,
149+ Helpers . patterns ( ) ,
161150 J . identifier ( "defmatch" )
162151 ) ,
163152 Enum . map ( clauses , fn x -> Clause . compile ( x , state ) |> elem ( 0 ) end ) |> List . flatten
164153 )
165154
166- ast = J . call_expression (
155+ ast = Helpers . call (
167156 J . member_expression ( func , J . identifier ( "call" ) ) ,
168157 [ J . identifier ( :this ) , compile! ( condition , state ) ]
169158 )
@@ -178,7 +167,7 @@ defmodule ElixirScript.Translate.Form do
178167 translated_body = translated_body
179168 |> Clause . return_last_statement
180169
181- translated_body = J . arrow_function_expression ( [ ] , [ ] , J . block_statement ( translated_body ) )
170+ translated_body = Helpers . arrow_function ( [ ] , J . block_statement ( translated_body ) )
182171
183172 { translated_clause , _ } = compile ( hd ( clause ) , state )
184173
@@ -188,17 +177,11 @@ defmodule ElixirScript.Translate.Form do
188177
189178
190179 cond_function = J . member_expression (
191- J . member_expression (
192- J . identifier ( "ElixirScript" ) ,
193- J . member_expression (
194- J . identifier ( "Core" ) ,
195- J . identifier ( "SpecialForms" )
196- )
197- ) ,
180+ Helpers . special_forms ( ) ,
198181 J . identifier ( "cond" )
199182 )
200183
201- ast = J . call_expression (
184+ ast = Helpers . call (
202185 cond_function ,
203186 processed_clauses
204187 )
@@ -308,7 +291,7 @@ defmodule ElixirScript.Translate.Form do
308291 end
309292
310293 def compile ( { { :. , _ , [ { _ , _ , nil } = var , func_or_prop ] } , _ , [ ] } , state ) do
311- ast = J . call_expression (
294+ ast = Helpers . call (
312295 ElixirScript.Translate.Forms.JS . call_property ( ) ,
313296 [ compile! ( var , state ) , J . literal ( to_string ( func_or_prop ) ) ]
314297 )
@@ -328,7 +311,7 @@ defmodule ElixirScript.Translate.Form do
328311 { function_name , _ } = Map . get ( state , :function )
329312 { var_decs , params } = compile_params ( params , state )
330313
331- ast = J . call_expression (
314+ ast = Helpers . call (
332315 ElixirScript.Translate.Identifier . make_function_name ( function_name ) ,
333316 params
334317 )
@@ -348,7 +331,7 @@ defmodule ElixirScript.Translate.Form do
348331 def compile ( { var , _ , params } , state ) when is_list ( params ) and is_atom ( var ) do
349332 { var_decs , params } = compile_params ( params , state )
350333
351- ast = J . call_expression (
334+ ast = Helpers . call (
352335 ElixirScript.Translate.Identifier . make_function_name ( var ) ,
353336 params
354337 )
@@ -362,7 +345,7 @@ defmodule ElixirScript.Translate.Form do
362345 end
363346
364347 def compile ( { function , _ , [ ] } , state ) do
365- ast = J . call_expression (
348+ ast = Helpers . call (
366349 ElixirScript.Translate.Forms.JS . call_property ( ) ,
367350 [ compile! ( function , state ) ]
368351 )
@@ -373,7 +356,7 @@ defmodule ElixirScript.Translate.Form do
373356 def compile ( { function , _ , params } , state ) when is_list ( params ) do
374357 { var_decs , params } = compile_params ( params , state )
375358
376- ast = J . call_expression (
359+ ast = Helpers . call (
377360 compile! ( function , state ) ,
378361 params
379362 )
0 commit comments