Skip to content

Commit 36ead77

Browse files
committed
Got tests running again
1 parent b93ac76 commit 36ead77

File tree

18 files changed

+124
-63
lines changed

18 files changed

+124
-63
lines changed

lib/elixir_script.ex

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,54 @@ defmodule ElixirScript do
6363

6464
opts = build_compiler_options(opts)
6565

66-
result = %{ data: [%{ast: quoted}] }
66+
data = quoted
67+
|> get_modules_from_quoted
68+
|> Enum.map(fn(x) -> %{ast: x, app: :app} end)
69+
70+
result = %{ data: data }
71+
|> ElixirScript.Passes.Init.execute(opts)
6772
|> ElixirScript.Passes.FindModules.execute(opts)
6873
|> ElixirScript.Passes.FindDeps.execute(opts)
69-
|> ElixirScript.Passes.RemoveUnused.execute(opts)
70-
|> ElixirScript.Passes.LoadModules.execute(opts)
71-
|> ElixirScript.Passes.FindChangedFiles.execute(opts)
7274
|> ElixirScript.Passes.FindFunctions.execute(opts)
75+
|> ElixirScript.Passes.AddStdLib.execute(opts)
7376
|> ElixirScript.Passes.JavaScriptAST.execute(opts)
7477
|> ElixirScript.Passes.ConsolidateProtocols.execute(opts)
7578
|> ElixirScript.Passes.JavaScriptCode.execute(opts)
7679
|> ElixirScript.Passes.JavaScriptName.execute(opts)
7780
|> ElixirScript.Passes.HandleOutput.execute(opts)
7881

79-
80-
{ code, _ } = do_compile(opts, [quoted], get_stdlib_state, [])
81-
result = Output.out(quoted, code, build_compiler_options(opts))
82-
ElixirScript.Translator.State.stop
8382
result
8483
end
8584

85+
defp get_modules_from_quoted(quoted) do
86+
results = case quoted do
87+
{ :__block__, _, list } ->
88+
{modules, not_modules} = Enum.partition(list,
89+
fn
90+
{type, _, _ } when type in [:defprotocol, :defimpl, :defmodule] ->
91+
true
92+
_ ->
93+
false
94+
end)
95+
96+
temp_module = case not_modules do
97+
[] ->
98+
[]
99+
_ ->
100+
[{:defmodule, [], [{:__aliases__, [], [:ElixirScript, :Temp]}, [do: { :__block__, [], not_modules }]]}]
101+
end
102+
103+
modules ++ temp_module
104+
105+
{type, _, _ } = x when type in [:defprotocol, :defimpl, :defmodule] ->
106+
x
107+
x ->
108+
{:defmodule, [], [{:__aliases__, [], [:ElixirScript, :Temp]}, [do: { :__block__, [], [x] }]]}
109+
end
110+
111+
List.wrap(results)
112+
end
113+
86114
@doc """
87115
Compiles the elixir files found at the given path
88116
"""
@@ -92,6 +120,7 @@ defmodule ElixirScript do
92120
opts = build_compiler_options(opts)
93121

94122
result = %{ path: path }
123+
|> ElixirScript.Passes.Init.execute(opts)
95124
|> ElixirScript.Passes.DepsPaths.execute(opts)
96125
|> ElixirScript.Passes.ASTFromFile.execute(opts)
97126
|> ElixirScript.Passes.FindModules.execute(opts)
@@ -106,6 +135,8 @@ defmodule ElixirScript do
106135
|> ElixirScript.Passes.JavaScriptName.execute(opts)
107136
|> ElixirScript.Passes.WriteCache.execute(opts)
108137
|> ElixirScript.Passes.HandleOutput.execute(opts)
138+
139+
result
109140
end
110141

111142
defp process_path(path) do
@@ -189,13 +220,10 @@ defmodule ElixirScript do
189220
libs_path = Path.join([__DIR__, "elixir_script", "prelude"])
190221

191222
result = %{ path: libs_path }
223+
|> ElixirScript.Passes.Init.execute(opts)
192224
|> ElixirScript.Passes.DepsPaths.execute(opts)
193225
|> ElixirScript.Passes.ASTFromFile.execute(opts)
194226
|> ElixirScript.Passes.FindModules.execute(opts)
195-
#|> ElixirScript.Passes.FindDeps.execute(opts)
196-
#|> ElixirScript.Passes.RemoveUnused.execute(opts)
197-
#|> ElixirScript.Passes.LoadModules.execute(opts)
198-
#|> ElixirScript.Passes.FindChangedFiles.execute(opts)
199227
|> ElixirScript.Passes.FindFunctions.execute(opts)
200228
|> ElixirScript.Passes.JavaScriptAST.execute(opts)
201229
|> ElixirScript.Passes.ConsolidateProtocols.execute(opts)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule ElixirScript.Passes.AddStdLib do
2+
alias ElixirScript.Translator.State
3+
4+
def execute(compiler_data, opts) do
5+
State.deserialize(ElixirScript.get_stdlib_state, [])
6+
compiler_data
7+
end
8+
9+
end

lib/elixir_script/passes/consolidate_protocols.ex

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,29 @@ defmodule ElixirScript.Passes.ConsolidateProtocols do
2525
Enum.reduce(data, %{}, fn({module_name, module_data} = dat, state) ->
2626
if module_data.type == :protocol do
2727
existing = Map.get(state, module_name, %{})
28-
Map.put(existing, :protocol, dat)
28+
existing = Map.put(existing, :protocol, dat)
2929
Map.put(state, module_name, existing)
3030
else
3131
existing = Map.get(state, module_data.implements, %{})
3232
existing_protocol_data = Map.get(existing, :impls, [])
3333
existing_protocol_data = existing_protocol_data ++ [dat]
34-
Map.put(existing, :impls, existing_protocol_data)
35-
Map.put(state, module_name.implements, existing)
34+
existing = Map.put(existing, :impls, existing_protocol_data)
35+
Map.put(state, module_data.implements, existing)
3636
end
3737
end)
3838
end
3939

4040
defp update_protocols(grouped_protocol_data, opts) do
41-
Enum.map(grouped_protocol_data, fn({ protocol_name, %{ protocol: protocol, impls: impls } }) ->
42-
make_defimpl(protocol_name, protocol, Enum.uniq(impls), opts)
41+
Enum.map(grouped_protocol_data, fn
42+
({ protocol_name, %{ protocol: protocol, impls: impls } }) ->
43+
make_defimpl(protocol_name, protocol, Enum.uniq(impls), opts)
44+
45+
({ protocol_name, %{ protocol: protocol } }) ->
46+
make_defimpl(protocol_name, protocol, [], opts)
4347
end)
4448
end
4549

46-
defp make_defimpl(name, protocol, implementations, compiler_opts) do
50+
defp make_defimpl(name, { protocol_name, protocol }, implementations, compiler_opts) do
4751
imports = [ModuleSystems.import_module(:Elixir, Utils.make_local_file_path(:elixir, compiler_opts.core_path, compiler_opts.root))]
4852

4953
declarator = JS.variable_declarator(

lib/elixir_script/passes/deps_paths.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
defmodule ElixirScript.Passes.DepsPaths do
22
@pass 1
3-
alias ElixirScript.Translator.State
3+
44

55
def execute(compiler_data, opts) do
6-
State.start_link(opts, [])
76

87
data = cond do
98
opts.std_lib ->

lib/elixir_script/passes/find_functions.ex

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@ defmodule ElixirScript.Passes.FindFunctions do
55
def execute(data, opts) do
66
new_data = Enum.map(data.data, fn { module_name, module_data } ->
77

8-
functions = case module_data.ast do
8+
%{def: functions, defp: private_functions, defgen: generators, defgenp: private_generators } = case module_data.ast do
99
{:defmodule, _, [_, [do: body]]} ->
1010
get_functions_from_module(body)
1111
{:defprotocol, _, [_, [do: {:__block__, _, _} = block]]} ->
1212
get_functions_from_module(block)
1313
{:defprotocol, _, [{:__aliases__, _, _} = the_alias, [do: spec]]} ->
1414
get_functions_from_module({:__block__, [], [spec]})
1515
_ ->
16-
%{}
16+
%{ def: Keyword.new, defp: Keyword.new, defgen: Keyword.new, defgenp: Keyword.new }
1717
end
1818

1919

20-
module_data = Map.put(module_data, :functions, functions)
20+
module_data = Map.put(module_data, :functions, functions ++ generators)
21+
|> Map.put(:private_functions, private_functions ++ private_generators)
22+
2123
{module_name, module_data}
2224
end)
2325

lib/elixir_script/passes/find_modules.ex

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,30 @@ defmodule ElixirScript.Passes.FindModules do
88
quoted = update_quoted(data.ast)
99
{ _, modules } = Macro.postwalk(quoted, [], &get_defmodules(&1, &2, opts))
1010

11-
modules = Enum.map(modules, fn(x) -> { x.module, Map.merge(data, x) } end)
11+
modules = Enum.map(modules, fn(x) -> { x.name, Map.merge(data, x) } end)
1212
list ++ modules
1313
end)
1414

1515
Map.put(compiler_data, :data, data)
1616
end
1717

1818
defp get_defmodules({:defprotocol, _, [{:__aliases__, _, _} = the_alias, _]} = ast, state, _) do
19-
s = %{ module: Utils.quoted_to_name(the_alias), type: :protocol, ast: ast }
19+
s = %{ name: Utils.quoted_to_name(the_alias), type: :protocol, ast: ast }
2020
{ ast, state ++ [s] }
2121
end
2222

2323
defp get_defmodules({:defimpl, _, [ {:__aliases__, _, name} = the_alias, [for: {:__aliases__, _, type_name} = type], _ ]} = ast, state, _) do
2424
name = name ++ [DefImpl] ++ type_name
25-
s = %{module: Utils.quoted_to_name({:__aliases__, [], name}), type: :impl, for: Utils.quoted_to_name(type), ast: ast, implements: Utils.quoted_to_name(the_alias) }
25+
s = %{name: Utils.quoted_to_name({:__aliases__, [], name}), type: :impl, for: type, ast: ast, implements: Utils.quoted_to_name(the_alias) }
2626
{ ast, state ++ [s] }
2727
end
2828

29+
defp get_defmodules({:defmodule, _, [{:__aliases__, _, [:ElixirScript, :Temp]}, [do: body]]} = ast, state, opts) do
30+
s = %{name: ElixirScript.Temp, type: :module, ast: body }
31+
{ ast, state ++ [s] }
32+
end
33+
34+
2935
defp get_defmodules({:defmodule, _, [{:__aliases__, _, _}, [do: _]]} = ast, state, opts) do
3036
{ ast, do_module_processing(ast, state, opts) }
3137
end
@@ -46,7 +52,7 @@ defmodule ElixirScript.Passes.FindModules do
4652
({:defmodule, context1, [{:__aliases__, context2, inner_module_name}, [do: inner_module_body]]}, state) ->
4753

4854
module_name = Utils.quoted_to_name({:__aliases__, [], tl(name) ++ inner_module_name})
49-
state = Enum.reject(state, fn(x) -> x.module == module_name end)
55+
state = Enum.reject(state, fn(x) -> x.name == module_name end)
5056

5157
this_module_aliases = aliases -- [{ :alias, [], [{:__aliases__, [alias: false], name ++ inner_module_name}, [as: {:__aliases__, [alias: false], inner_module_name }] ] }]
5258

@@ -85,7 +91,7 @@ defmodule ElixirScript.Passes.FindModules do
8591
end
8692

8793

88-
[%{module: Utils.quoted_to_name(the_alias), type: :module, ast: {:defmodule, context1, [the_alias, [do: body]]} }] ++ state
94+
[%{name: Utils.quoted_to_name(the_alias), type: :module, ast: body }] ++ state
8995
end
9096

9197
defp add_aliases_to_body(body, aliases) do

lib/elixir_script/passes/handle_output.ex

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule ElixirScript.Passes.HandleOutput do
33
alias ElixirScript.Translator.State
44

55
def execute(compiler_data, opts) do
6-
if opts.std_lib do
6+
if Map.get(opts, :std_lib, false) do
77
State.set_module_data(compiler_data.data)
88
new_std_state = State.serialize()
99
stdlib_state_path = Path.join([File.cwd!(), "lib", "elixir_script", "translator", "stdlib_state.bin"])
@@ -29,14 +29,16 @@ defmodule ElixirScript.Passes.HandleOutput do
2929
end
3030

3131
defp out(compiler_output, %{output: output_path, core_path: core_path} = compiler_opts) do
32-
Enum.each(compiler_output, fn(x) ->
32+
Enum.each(compiler_output.data, fn({_, x}) ->
3333
write_to_file(x, output_path)
3434
end)
3535

36-
ElixirScript.copy_stdlib_to_destination(output_path)
36+
if Map.get(compiler_opts, :std_lib, false) == false do
37+
ElixirScript.copy_stdlib_to_destination(output_path)
38+
end
3739
end
3840

39-
defp write_to_file({_, module_data}, destination) do
41+
defp write_to_file(module_data, destination) do
4042
file_name = Path.join([destination, to_string(module_data.app), module_data.javascript_name])
4143

4244
if !File.exists?(Path.dirname(file_name)) do
@@ -47,7 +49,7 @@ defmodule ElixirScript.Passes.HandleOutput do
4749
end
4850

4951
defp process_include_path(compiler_output, compiler_opts) do
50-
Enum.map(compiler_output, fn
52+
Enum.map(compiler_output.data, fn
5153
{module_name, module_data} ->
5254
case compiler_opts.include_path do
5355
true ->

lib/elixir_script/passes/init.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule ElixirScript.Passes.Init do
2+
alias ElixirScript.Translator.State
3+
4+
def execute(compiler_data, opts) do
5+
State.start_link(opts, [])
6+
compiler_data
7+
end
8+
9+
end

lib/elixir_script/passes/java_script_ast.ex

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
defmodule ElixirScript.Passes.JavaScriptAST do
22
@pass 7
33
alias ElixirScript.Translator.Utils
4+
alias ElixirScript.Translator.State
45

56
def execute(compiler_data, opts) do
7+
State.set_module_data(compiler_data.data)
68
data = Enum.map(compiler_data.data, fn({module_name, module_data}) ->
79
module_data = compile(module_data, opts)
810
{module_name, module_data}
@@ -13,15 +15,15 @@ defmodule ElixirScript.Passes.JavaScriptAST do
1315

1416

1517
defp compile(module_data, opts) do
16-
env = ElixirScript.Translator.LexicalScope.module_scope(module_data.module, Utils.name_to_js_file_name(module_data.module) <> ".js", opts.env, module_data)
18+
env = ElixirScript.Translator.LexicalScope.module_scope(module_data.name, Utils.name_to_js_file_name(module_data.name) <> ".js", opts.env)
1719

1820
module = case module_data.type do
1921
:module ->
20-
ElixirScript.Translator.Defmodule.make_module(module_data.module, module_data.ast, env)
22+
ElixirScript.Translator.Defmodule.make_module(module_data.name, module_data.ast, env)
2123
:protocol ->
22-
ElixirScript.Translator.Defprotocol.make(module_data.module, module_data.functions, env)
23-
:protocol_implementation ->
24-
ElixirScript.Translator.Defimpl.make(module_data.module, module_data.for, module_data.ast, env)
24+
ElixirScript.Translator.Defprotocol.make(module_data.name, module_data.functions, env)
25+
:impl ->
26+
ElixirScript.Translator.Defimpl.make(module_data.name, module_data.for, module_data.ast, env)
2527
end
2628

2729
Map.put(module_data, :javascript_ast, module.body)

lib/elixir_script/passes/load_modules.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ defmodule ElixirScript.Passes.LoadModules do
22
@pass 5
33

44
def execute(compiler_data, opts) do
5-
ex_files = Enum.map(compiler_data.data, fn { module, %{path: path} } -> path end)
5+
ex_files = Enum.filter(compiler_data.data, fn
6+
{ module, %{path: path} } -> path end)
67
|> Enum.filter(fn path -> Path.extname(path) == ".ex" || Path.extname(path) == ".exs" end)
78

89
loaded_modules = Kernel.ParallelRequire.files(ex_files)

0 commit comments

Comments
 (0)