Skip to content

Commit 25adee9

Browse files
committed
Inlining erlang operators to javascript ones
1 parent 33fe54e commit 25adee9

File tree

4 files changed

+230
-3
lines changed

4 files changed

+230
-3
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule Example do
22
def start(_, _) do
3-
Base.encode16("Hello")
3+
1 + 1
44
end
55

66
end

lib/elixir_script/next/passes/translate/form.ex

Lines changed: 188 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@ defmodule ElixirScript.Translate.Form do
55
alias ElixirScript.Translator.Identifier
66
alias ElixirScript.Translate.Clause
77

8+
@erlang_modules [
9+
:erlang,
10+
:maps,
11+
:lists,
12+
:gen,
13+
:elixir_errors,
14+
:supervisor,
15+
:application,
16+
:code,
17+
:elixir_utils,
18+
:file
19+
]
20+
821
@moduledoc """
922
Handles translation of all forms that are not functions or clauses
1023
"""
@@ -63,7 +76,7 @@ defmodule ElixirScript.Translate.Form do
6376
Bitstring.compile(bitstring, state)
6477
end
6578

66-
def compile({:=, _, [left, right]} = match, state) do
79+
def compile({:=, _, [_, _]} = match, state) do
6780
Match.compile(match, state)
6881
end
6982

@@ -139,7 +152,180 @@ defmodule ElixirScript.Translate.Form do
139152
)
140153
end
141154

142-
def compile({{:., _, [module, function]}, _, params}, state) when module in [:erlang, :lists, :maps] do
155+
def compile({{:., _, [:erlang, function]}, _, [first]}, state) when function in [:+, :-] do
156+
J.unary_expression(
157+
function,
158+
compile(first, state),
159+
true
160+
)
161+
end
162+
163+
def compile({{:., _, [:erlang, :not]}, _, [first]}, state) do
164+
J.unary_expression(
165+
:!,
166+
compile(first, state),
167+
true
168+
)
169+
end
170+
171+
def compile({{:., _, [:erlang, :bnot]}, _, [first]}, state) do
172+
J.unary_expression(
173+
:"~",
174+
compile(first, state),
175+
true
176+
)
177+
end
178+
179+
def compile({{:., _, [:erlang, :=]}, _, [_, _] = match}, state) do
180+
Match.compile(match, state)
181+
end
182+
183+
def compile({{:., _, [:erlang, function]}, _, [first, second]}, state) when function in [:+, :-, :*, :/, :==, :>=] do
184+
J.binary_expression(
185+
function,
186+
compile(first, state),
187+
compile(second, state)
188+
)
189+
end
190+
191+
def compile({{:., _, [:erlang, :"/="]}, _, [first, second]}, state) do
192+
J.binary_expression(
193+
:!=,
194+
compile(first, state),
195+
compile(second, state)
196+
)
197+
end
198+
199+
def compile({{:., _, [:erlang, :"=<"]}, _, [first, second]}, state) do
200+
J.binary_expression(
201+
:<=,
202+
compile(first, state),
203+
compile(second, state)
204+
)
205+
end
206+
207+
def compile({{:., _, [:erlang, :"=:="]}, _, [first, second]}, state) do
208+
J.binary_expression(
209+
:===,
210+
compile(first, state),
211+
compile(second, state)
212+
)
213+
end
214+
215+
def compile({{:., _, [:erlang, :"=/="]}, _, [first, second]}, state) do
216+
J.binary_expression(
217+
:!==,
218+
compile(first, state),
219+
compile(second, state)
220+
)
221+
end
222+
223+
def compile({{:., _, [:erlang, function]}, _, [first, second]}, state) when function in [:andalso, :and] do
224+
J.binary_expression(
225+
:&&,
226+
compile(first, state),
227+
compile(second, state)
228+
)
229+
end
230+
231+
def compile({{:., _, [:erlang, function]}, _, [first, second]}, state) when function in [:orelse, :or] do
232+
J.binary_expression(
233+
:||,
234+
compile(first, state),
235+
compile(second, state)
236+
)
237+
end
238+
239+
def compile({{:., _, [:erlang, :div]}, _, [first, second]}, state) do
240+
J.binary_expression(
241+
:/,
242+
compile(first, state),
243+
compile(second, state)
244+
)
245+
end
246+
247+
def compile({{:., _, [:erlang, :rem]}, _, [first, second]}, state) do
248+
J.binary_expression(
249+
:mod,
250+
compile(first, state),
251+
compile(second, state)
252+
)
253+
end
254+
255+
def compile({{:., _, [:erlang, :band]}, _, [first, second]}, state) do
256+
J.binary_expression(
257+
:&,
258+
compile(first, state),
259+
compile(second, state)
260+
)
261+
end
262+
263+
def compile({{:., _, [:erlang, :bor]}, _, [first, second]}, state) do
264+
J.binary_expression(
265+
:|,
266+
compile(first, state),
267+
compile(second, state)
268+
)
269+
end
270+
271+
def compile({{:., _, [:erlang, :bsl]}, _, [first, second]}, state) do
272+
J.binary_expression(
273+
:"<<",
274+
compile(first, state),
275+
compile(second, state)
276+
)
277+
end
278+
279+
def compile({{:., _, [:erlang, :bsl]}, _, [first, second]}, state) do
280+
J.binary_expression(
281+
:">>",
282+
compile(first, state),
283+
compile(second, state)
284+
)
285+
end
286+
287+
def compile({{:., _, [:erlang, :bxor]}, _, [first, second]}, state) do
288+
J.binary_expression(
289+
:">>",
290+
compile(first, state),
291+
compile(second, state)
292+
)
293+
end
294+
295+
def compile({{:., _, [:erlang, :++]}, _, [_, _] = params}, state) do
296+
J.call_expression(
297+
J.member_expression(
298+
J.member_expression(
299+
J.member_expression(
300+
J.identifier("Bootstrap"),
301+
J.identifier("Core")
302+
),
303+
J.identifier(:erlang)
304+
),
305+
J.identifier("list_concatenation2")
306+
),
307+
Enum.map(params, &compile(&1, state))
308+
)
309+
end
310+
311+
def compile({{:., _, [:erlang, :--]}, _, [_, _] = params}, state) do
312+
J.call_expression(
313+
J.member_expression(
314+
J.member_expression(
315+
J.member_expression(
316+
J.identifier("Bootstrap"),
317+
J.identifier("Core")
318+
),
319+
J.identifier(:erlang)
320+
),
321+
J.identifier("list_substraction2")
322+
),
323+
Enum.map(params, &compile(&1, state))
324+
)
325+
end
326+
327+
328+
def compile({{:., _, [module, function]}, _, params}, state) when module in @erlang_modules do
143329
J.call_expression(
144330
J.member_expression(
145331
J.member_expression(

src/javascript/lib/core/erlang_compat/erlang.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,26 @@ function atom_to_binary2(atom, encoding = Symbol.for('utf8')) {
88
return Symbol.keyFor(atom);
99
}
1010

11+
function list_concatenation2(list1, list2) {
12+
return list1.concat(list2);
13+
}
14+
15+
function list_subtraction2(list1, list2) {
16+
const list = [...list1];
17+
18+
for (const item of list2) {
19+
const index = list.indexOf(item);
20+
21+
if (index > -1) {
22+
list.splice(index, 1);
23+
}
24+
}
25+
26+
return list;
27+
}
28+
1129
export default {
1230
atom_to_binary2,
31+
list_concatenation2,
32+
list_subtraction2,
1333
};

src/javascript/tests/core/erlang_compat/erlang_spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,24 @@ test('atom_to_binary2', t => {
1313
Error
1414
);
1515
});
16+
17+
test('list_concatenation2', t => {
18+
t.deepEqual(Core.erlang.list_concatenation2([], []), []);
19+
t.deepEqual(Core.erlang.list_concatenation2([1], []), [1]);
20+
t.deepEqual(Core.erlang.list_concatenation2([1, 2, 3], [4, 5, 6]), [
21+
1,
22+
2,
23+
3,
24+
4,
25+
5,
26+
6,
27+
]);
28+
});
29+
30+
test('list_subtraction2', t => {
31+
t.deepEqual(Core.erlang.list_subtraction2([], []), []);
32+
t.deepEqual(Core.erlang.list_subtraction2([1], []), [1]);
33+
t.deepEqual(Core.erlang.list_subtraction2([1, 2, 3], [4, 5, 6]), [1, 2, 3]);
34+
t.deepEqual(Core.erlang.list_subtraction2([1, 2, 3], [1, 2, 3]), []);
35+
t.deepEqual(Core.erlang.list_subtraction2([1, 2, 3], [1, 2]), [3]);
36+
});

0 commit comments

Comments
 (0)