Skip to content

Commit 6d4d625

Browse files
committed
Some fixes for pattern compilation
1 parent 26d9dcf commit 6d4d625

File tree

7 files changed

+93
-18
lines changed

7 files changed

+93
-18
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ defmodule ElixirScript.Translate.Form do
6565
end
6666

6767
def compile({:{}, _, elements}, state) do
68-
ast = J.call_expression(
68+
ast = J.new_expression(
6969
J.member_expression(
7070
J.member_expression(
7171
J.identifier("Bootstrap"),
@@ -79,6 +79,15 @@ defmodule ElixirScript.Translate.Form do
7979
{ast, state}
8080
end
8181

82+
def compile({:&, _, [{:/, _, [{{:., _, [_module, _function]} = ast, [], []}, _]}]}, state) do
83+
Remote.compile(ast, state)
84+
end
85+
86+
def compile({:&, _, [{:/, _, [{var, _, _}, _]}]}, state) do
87+
ast = ElixirScript.Translate.Identifier.make_function_name(var)
88+
{ast, state}
89+
end
90+
8291
def compile({:%{}, _, _} = map, state) do
8392
ElixirScript.Translate.Forms.Map.compile(map, state)
8493
end
@@ -213,6 +222,15 @@ defmodule ElixirScript.Translate.Form do
213222
{ast, state}
214223
end
215224

225+
def compile({function, _, []}, state) do
226+
ast = J.call_expression(
227+
ElixirScript.Translate.Forms.JS.call_property(),
228+
[compile!(function, state)]
229+
)
230+
231+
{ast, state}
232+
end
233+
216234
def compile({function, _, params}, state) when is_list(params) do
217235
ast = J.call_expression(
218236
compile!(function, state),

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

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,26 @@ defmodule ElixirScript.Translate.Forms.JS do
33
alias ESTree.Tools.Builder, as: J
44
alias ElixirScript.Translate.Form
55

6+
def call_property() do
7+
J.member_expression(
8+
J.member_expression(
9+
J.identifier("Bootstrap"),
10+
J.member_expression(
11+
J.identifier("Core"),
12+
J.identifier("Functions")
13+
)
14+
),
15+
J.identifier("call_property")
16+
)
17+
end
18+
619
def global() do
7-
Builder.member_expression(
8-
Builder.member_expression(
9-
Builder.identifier("Bootstrap"),
10-
Builder.identifier("Core")
20+
J.member_expression(
21+
J.member_expression(
22+
J.identifier("Bootstrap"),
23+
J.identifier("Core")
1124
),
12-
Builder.identifier("global")
25+
J.identifier("global")
1326
)
1427
end
1528

@@ -41,15 +54,15 @@ defmodule ElixirScript.Translate.Forms.JS do
4154
{ast, state}
4255
end
4356

44-
defp do_translate({{:., _, [JS, :throw]}, _, [term]}, state) do
57+
def compile({{:., _, [JS, :throw]}, _, [term]}, state) do
4558
ast = J.throw_statement(
4659
Form.compile!(term, state)
4760
)
4861

4962
{ast, state}
5063
end
5164

52-
defp do_translate({{:., _, [JS, :import]}, _, [term]}, state) do
65+
def compile({{:., _, [JS, :import]}, _, [term]}, state) do
5366
ast = J.call_expression(
5467
J.identifier("import"),
5568
[Form.compile!(term, state)]
@@ -58,7 +71,19 @@ defmodule ElixirScript.Translate.Forms.JS do
5871
{ast, state}
5972
end
6073

61-
defp do_translate({{:., _, [JS, function]}, _, params}, state) do
74+
def compile({{:., _, [JS, function]}, _, []}, state) do
75+
ast = J.call_expression(
76+
call_property(),
77+
[
78+
global(),
79+
Form.compile!(to_string(function), state)
80+
]
81+
)
82+
83+
{ast, state}
84+
end
85+
86+
def compile({{:., _, [JS, function]}, _, params}, state) do
6287
ast = J.call_expression(
6388
J.identifier(function),
6489
Enum.map(params, &Form.compile!(&1, state))

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ defmodule ElixirScript.Translate.Forms.Pattern do
109109
process_pattern({:|, context, [head, tail]}, state)
110110
end
111111

112+
defp process_pattern({:%, _, [module, {:%{}, _, props}]}, state) do
113+
process_pattern({:%{}, [], [__struct__: module] ++ props}, state)
114+
end
115+
112116
defp process_pattern({:%{}, _, props}, state) do
113117
properties = Enum.map(props, fn({key, value}) ->
114118
{pattern, params} = process_pattern(value, state)
@@ -152,11 +156,11 @@ defmodule ElixirScript.Translate.Forms.Pattern do
152156
{ [PM.starts_with(prefix)], [Form.compile!(value, state)] }
153157
end
154158

155-
defp process_pattern({:=, _, [{name, _, _}, right]}, state) do
159+
defp process_pattern({:=, _, [{name, _, _}, right]}, state) when not name in [:%, :{}, :^, :%{}, :<<>>] do
156160
unify(name, right, state)
157161
end
158162

159-
defp process_pattern({:=, _, [left, {name, _, _}]}, state) do
163+
defp process_pattern({:=, _, [left, {name, _, _}]}, state) when not name in [:%, :{}, :^, :%{}, :<<>>] do
160164
unify(name, left, state)
161165
end
162166

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule ElixirScript.Translate.Forms.Try do
22
@moduledoc false
33
alias ESTree.Tools.Builder, as: JS
44
alias ElixirScript.Translate.Clause
5-
alias ElixirScript.Translate.Form
5+
alias ElixirScript.Translate.{Form, Function}
66

77
def compile(blocks, state) do
88
try_block = Keyword.get(blocks, :do)
@@ -92,12 +92,8 @@ defmodule ElixirScript.Translate.Forms.Try do
9292
end
9393

9494
defp prepare_function_body(body, state) do
95-
{ast, state} = body
96-
|> List.wrap
97-
|> Enum.map_reduce(state, &Form.compile(&1, &2))
95+
{ast, state} = Function.compile_block(body, state)
9896

99-
ast
100-
|> List.flatten
101-
|> Clause.return_last_statement
97+
Clause.return_last_statement(ast)
10298
end
10399
end

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,21 @@ defmodule ElixirScript.Translate.Function do
128128
defp compile_clause({:->, _, [params, body]}, state) do
129129
compile_clause({[], params, [], body}, state)
130130
end
131+
132+
def compile_block(block, state) do
133+
ast = case block do
134+
nil ->
135+
J.identifier("null")
136+
{:__block__, _, block_body} ->
137+
{list, _} = Enum.map_reduce(block_body, state, &Form.compile(&1, &2))
138+
List.flatten(list)
139+
b when is_list(b) ->
140+
{list, _} = Enum.map_reduce(b, state, &Form.compile(&1, &2))
141+
List.flatten(list)
142+
_ ->
143+
Form.compile!(block, state)
144+
end
145+
146+
{ast, state}
147+
end
131148
end

src/javascript/lib/core/functions.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ function iterator_to_reducer(iterable, acc, fun) {
2323
}
2424

2525
function call_property(item, property) {
26+
if (!property) {
27+
if (item instanceof Function) {
28+
return item();
29+
}
30+
31+
return item;
32+
}
33+
2634
let prop = null;
2735

2836
if (

test/support/main.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
defmodule Main do
22
def start(:normal, [callback]) do
33
callback.("started")
4+
user = %User{}
5+
user.first
6+
draw()
7+
end
8+
9+
defp draw() do
10+
JS.console.log("Here")
411
end
512
end

0 commit comments

Comments
 (0)