forked from elixirscript/elixirscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.ex
More file actions
244 lines (213 loc) · 6.6 KB
/
Copy pathfunction.ex
File metadata and controls
244 lines (213 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
defmodule ElixirScript.Translator.Function do
@moduledoc false
alias ESTree.Tools.Builder, as: JS
alias ElixirScript.Translator
alias ElixirScript.Translator.Utils
alias ElixirScript.PatternMatching.Match
alias ElixirScript.Preprocess.Variables
alias ElixirScript.Translator.Map
def process_function(name, functions, env) do
result = make_anonymous_function(functions, env)
declarator = JS.variable_declarator(
JS.identifier(name),
result
)
JS.variable_declaration([declarator], :let)
end
def make_anonymous_function(functions, env) do
clauses = functions
|> Stream.map(fn(x) -> Variables.process(x) end)
|> Stream.map(fn
{:->, _, [ [{:when, _, [params | guards]}], body ]} ->
{ patterns, params } = Match.build_match(List.wrap(params), env)
params = make_params(params)
body = make_function_body(body, env)
guard_body = make_guards(guards, env)
do_make_function_clause(patterns, params, body, guard_body)
({:->, _, [params, body]}) ->
{ patterns, params } = Match.build_match(params, env)
params = make_params(params)
body = make_function_body(body, env)
do_make_function_clause(patterns, params, body)
({_, _, [{:when, _, [{_, _, params} | guards] }, [do: body]]}) ->
{ patterns, params } = Match.build_match(params, env)
params = make_params(params)
body = make_function_body(body, env)
guard_body = make_guards(guards, env)
do_make_function_clause(patterns, params, body, guard_body)
({_, _, [{_, _, params}, [do: body]]}) ->
{ patterns, params } = Match.build_match(params, env)
params = make_params(params)
body = make_function_body(body, env)
do_make_function_clause(patterns, params, body)
end)
|> Enum.to_list
JS.call_expression(
JS.member_expression(
JS.identifier("Patterns"),
JS.identifier("defmatch")
),
clauses
)
end
def make_function_body(body, env) do
body
|> prepare_function_body(env)
|> JS.block_statement
end
defp make_guards(guards, env) do
hd(List.wrap(guards))
|> prepare_function_body(env)
|> JS.block_statement
end
defp make_params(params) do
Enum.filter(params, fn
(%ESTree.Identifier{name: :undefined}) -> false
(_) -> true
end)
end
defp do_make_function_clause(patterns, params, body, guard_body) do
JS.call_expression(
JS.member_expression(
JS.identifier("Patterns"),
JS.identifier("make_case")
),
[
JS.array_expression(patterns),
JS.function_expression(params, [], body),
JS.function_expression(params, [], guard_body)
]
)
end
defp do_make_function_clause(patterns, params, body) do
JS.call_expression(
JS.member_expression(
JS.identifier("Patterns"),
JS.identifier("make_case")
),
[
JS.array_expression(patterns),
JS.function_expression(params, [], body)
]
)
end
def make_function_or_property_call(module_name, function_name, env) do
the_name = case module_name do
{:__aliases__, _, name} ->
name
{name, _, _} when is_atom(name) ->
name
{{:., _, [_module_name, _function_name]}, _, _params } = ast ->
ast
name ->
case to_string(name) do
"Elixir." <> actual_name ->
actual_name
_ ->
name
end
end
JS.call_expression(
JS.member_expression(
JS.identifier("JS"),
JS.identifier("get_property_or_call_function")
),
[
Utils.make_module_expression_tree(the_name, false, env),
Translator.translate(to_string(function_name), env)
]
)
end
def make_function_call(function_name, params, env) do
Utils.make_call_expression(Utils.filter_name(function_name), params, env)
end
def make_function_call(module_name, function_name, params, env) do
the_name = case module_name do
{:__aliases__, _, name} ->
name
{name, _, _} when is_atom(name) ->
name
{{:., _, [_, _]}, _, _ } = ast ->
ast
{{:., _, [{:__aliases__, _, _}]}, _, _} = ast ->
ast
name ->
case to_string(name) do
"Elixir." <> actual_name ->
actual_name
_ ->
name
end
end
Utils.make_call_expression(the_name, Utils.filter_name(function_name), params, env)
end
def prepare_function_body(body, env) do
case body do
nil ->
[]
list when is_list(list) ->
Enum.map(list, &Translator.translate(&1, env))
{:__block__, _, list} ->
Enum.map(list, &Translator.translate(&1, env))
_ ->
[Translator.translate(body, env)]
end
|> Utils.inflate_groups
|> return_last_expression
end
def return_last_expression(nil) do
nil
end
def return_last_expression([]) do
[JS.return_statement(JS.literal(nil))]
end
def return_last_expression(%ESTree.BlockStatement{} = block) do
%ESTree.BlockStatement{ block | body: return_last_expression(block.body) }
end
def return_last_expression(list) when is_list(list) do
last_item = List.last(list)
last_item = case last_item do
%ESTree.Literal{} ->
JS.return_statement(last_item)
%ESTree.Identifier{} ->
JS.return_statement(last_item)
%ESTree.VariableDeclaration{} ->
declaration = hd(last_item.declarations).id
return_statement = case declaration do
%ESTree.ArrayPattern{elements: elements} ->
if(length(elements) == 1) do
JS.return_statement(hd(declaration.elements))
else
JS.return_statement(JS.array_expression(declaration.elements))
end
_ ->
JS.return_statement(declaration)
end
[last_item, return_statement]
%ESTree.BlockStatement{} ->
last_item = %ESTree.BlockStatement{ last_item | body: return_last_expression(last_item.body) }
_ ->
if String.contains?(last_item.type, "Expression") do
JS.return_statement(last_item)
else
[last_item, JS.return_statement(JS.literal(nil))]
end
end
list = Enum.take(list, length(list)-1)
|> Enum.map(fn(x) ->
case x do
%ESTree.MemberExpression{} ->
JS.expression_statement(x)
%ESTree.CallExpression{} ->
JS.expression_statement(x)
_ ->
x
end
end)
if is_list(last_item) do
list ++ last_item
else
list ++ [last_item]
end
end
end