Skip to content

Commit 62306b7

Browse files
committed
Updated pattern matching library and erlang types library
1 parent 5050077 commit 62306b7

File tree

9 files changed

+452
-408
lines changed

9 files changed

+452
-408
lines changed

.tool-versions

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
erlang ref-OTP-20.0-rc2
1+
erlang 20.0
22
elixir ref-d19a92b
33
nodejs 8.1.0

lib/elixir_script/next/passes/translate/forms/pattern.ex

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ defmodule ElixirScript.Translate.Forms.Pattern do
5959
end
6060

6161
defp process_pattern({:_, _, _}, _) do
62-
{ [PM.wildcard()], [J.identifier(:_)] }
62+
{ [PM.parameter()], [J.identifier(:_)] }
6363
end
6464

6565
defp process_pattern({a, b}, state) do
@@ -110,20 +110,40 @@ defmodule ElixirScript.Translate.Forms.Pattern do
110110
end
111111

112112
defp process_pattern({:%, _, [module, {:%{}, _, props}]}, state) do
113-
process_pattern({:%{}, [], [__struct__: module] ++ props}, state)
113+
process_pattern({:%{}, [], [__module__struct__: module] ++ props}, state)
114114
end
115115

116116
defp process_pattern({:%{}, _, props}, state) do
117-
properties = Enum.map(props, fn({key, value}) ->
118-
{pattern, params} = process_pattern(value, state)
119-
property = case key do
120-
{:^, _, [the_key]} ->
121-
J.property(Form.compile!(the_key, state), hd(List.wrap(pattern)), :init, false, false, true)
122-
_ ->
123-
ElixirScript.Translate.Forms.Map.make_property(Form.compile!(key, state), hd(List.wrap(pattern)))
124-
end
125-
126-
{ property, params }
117+
properties = Enum.map(props, fn
118+
{:__module__struct__, module} ->
119+
a = J.object_expression([%ESTree.Property{
120+
key: J.identifier("__MODULE__"),
121+
value: J.call_expression(
122+
J.member_expression(
123+
J.identifier("Symbol"),
124+
J.identifier("for")
125+
),
126+
[J.literal(to_string(module))]
127+
)
128+
}])
129+
130+
property = ElixirScript.Translate.Forms.Map.make_property(
131+
Form.compile!(:__struct__, state),
132+
a
133+
)
134+
135+
{ property, [] }
136+
137+
{key, value} ->
138+
{pattern, params} = process_pattern(value, state)
139+
property = case key do
140+
{:^, _, [the_key]} ->
141+
J.property(Form.compile!(the_key, state), hd(List.wrap(pattern)), :init, false, false, true)
142+
_ ->
143+
ElixirScript.Translate.Forms.Map.make_property(Form.compile!(key, state), hd(List.wrap(pattern)))
144+
end
145+
146+
{ property, params }
127147
end)
128148

129149
{props, params} = Enum.reduce(properties, {[], []}, fn({prop, param}, {props, params}) ->

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ defmodule ElixirScript.Translate.Function do
3737
arg_matches_declaration,
3838
clauses,
3939
J.throw_statement(
40-
J.call_expression(
40+
J.new_expression(
4141
J.member_expression(
4242
patterns_ast(),
4343
J.identifier("MatchError")
@@ -70,7 +70,7 @@ defmodule ElixirScript.Translate.Function do
7070
arg_matches_declaration,
7171
clauses,
7272
J.throw_statement(
73-
J.call_expression(
73+
J.new_expression(
7474
J.member_expression(
7575
patterns_ast(),
7676
J.identifier("MatchError")

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ defmodule ElixirScript.Translate.Module do
5252

5353
#we combine our function arities
5454
combined_defs = combine_defs(used_defs)
55-
exports = make_exports(combined_defs)
55+
exports = make_exports(module, combined_defs)
5656

5757
# Don't skip compilation and output of modules that don't have
5858
# any public functions
@@ -85,7 +85,7 @@ defmodule ElixirScript.Translate.Module do
8585
end)
8686
end
8787

88-
defp make_exports(reachable_defs) do
88+
defp make_exports(module, reachable_defs) do
8989
exports = Enum.reduce(reachable_defs, [], fn
9090
{{name, arity}, :def, _, _}, list ->
9191
function_name = ElixirScript.Translate.Identifier.make_identifier(name)
@@ -96,10 +96,16 @@ defmodule ElixirScript.Translate.Module do
9696

9797
# Add an attribute to use to determine if this is a module
9898
# Will be used by the is_atom implementation
99-
exports ++ [ElixirScript.Translate.Forms.Map.make_property(
100-
Form.compile!("__MODULE__", %{}),
101-
Form.compile!(true, %{})
102-
)]
99+
exports = exports ++ [%ESTree.Property{
100+
key: J.identifier("__MODULE__"),
101+
value: J.call_expression(
102+
J.member_expression(
103+
J.identifier("Symbol"),
104+
J.identifier("for")
105+
),
106+
[J.literal(to_string(module))]
107+
)
108+
}]
103109

104110
J.object_expression(exports)
105111
end

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
"author": "",
2323
"license": "MIT",
2424
"dependencies": {
25-
"erlang-types": "^1.0.0",
26-
"tailored": "2.4.5"
25+
"erlang-types": "^1.0.1",
26+
"tailored": "^2.4.6"
2727
},
2828
"devDependencies": {
2929
"ava": "^0.19.1",
@@ -48,4 +48,4 @@
4848
"babelrc": true
4949
}
5050
}
51-
}
51+
}

rollup.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default {
1010
babel({
1111
babelrc: false,
1212
}),
13-
babili({}),
13+
//babili({}),
1414
],
1515
targets: [{ dest: 'priv/build/iife/Elixir.Bootstrap.js', format: 'iife' }],
1616
};

src/javascript/lib/core/protocol.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ class Protocol {
1111
const thing = args[0];
1212
let fun = null;
1313

14-
if (Number.isInteger(thing) && this.hasImplementation(Core.Integer)) {
14+
if (thing === null && this.hasImplementation(Symbol('null'))) {
15+
fun = this.registry.get(Symbol)[funName];
16+
} else if (
17+
Number.isInteger(thing) &&
18+
this.hasImplementation(Core.Integer)
19+
) {
1520
fun = this.registry.get(Core.Integer)[funName];
1621
} else if (
1722
typeof thing === 'number' &&
@@ -20,14 +25,19 @@ class Protocol {
2025
) {
2126
fun = this.registry.get(Core.Float)[funName];
2227
} else if (
23-
typeof thing === 'string' && this.hasImplementation(Core.BitString)
28+
typeof thing === 'string' &&
29+
this.hasImplementation(Core.BitString)
2430
) {
2531
fun = this.registry.get(Core.BitString)[funName];
2632
} else if (
27-
thing[Symbol.for('__struct__')] && this.hasImplementation(thing)
33+
thing &&
34+
thing[Symbol.for('__struct__')] &&
35+
this.hasImplementation(thing)
2836
) {
29-
fun = this.registry.get(thing[Symbol.for('__struct__')])[funName];
30-
} else if (this.hasImplementation(thing)) {
37+
fun = this.registry.get(thing[Symbol.for('__struct__')].__MODULE__)[
38+
funName
39+
];
40+
} else if (thing !== null && this.hasImplementation(thing)) {
3141
fun = this.registry.get(thing.constructor)[funName];
3242
} else if (this.fallback) {
3343
fun = this.fallback[funName];
@@ -57,11 +67,13 @@ class Protocol {
5767

5868
hasImplementation(thing) {
5969
if (
60-
thing === Core.Integer || thing === Core.Float || thing === Core.BitString
70+
thing === Core.Integer ||
71+
thing === Core.Float ||
72+
thing === Core.BitString
6173
) {
6274
return this.registry.has(thing);
63-
} else if (thing[Symbol.for('__struct__')]) {
64-
return this.registry.has(thing[Symbol.for('__struct__')]);
75+
} else if (thing && thing[Symbol.for('__struct__')]) {
76+
return this.registry.has(thing[Symbol.for('__struct__')].__MODULE__);
6577
}
6678

6779
return this.registry.has(thing.constructor);

test/support/main.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule Main do
22
def start(:normal, [callback]) do
33
callback.("started")
4+
Enum.each(1..5, fn(x) -> JS.console.log(x) end)
45
end
56
end

0 commit comments

Comments
 (0)