forked from elixirscript/elixirscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelixir_script.ex
More file actions
214 lines (173 loc) · 5.65 KB
/
Copy pathelixir_script.ex
File metadata and controls
214 lines (173 loc) · 5.65 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
defmodule ElixirScript do
alias ElixirScript.Translator
alias ElixirScript.Translator.JSModule
alias ESTree.Tools.Builder
alias ESTree.Tools.Generator
require Logger
@moduledoc """
Translates Elixir into JavaScript.
All compile functions return a list of
transpiled javascript code or a tuple consisting of
the file name for the code and the transpiled javascript code.
All compile functions also take an optional opts parameter
that controls transpiler output.
Available options are:
* `:include_path` - a boolean controlling whether to return just the JavaScript code
or a tuple of the file name and the JavaScript code
* `:root` - a binary path prepended to the path of the standard lib imports if needed
* `:env` - a Macro.env struct to use. This is most useful when using macros. Make sure that the
given env has the macros required. Defaults to __ENV__.
"""
@doc """
Compiles the given Elixir code string
"""
@spec compile(binary, Dict.t) :: [binary | { binary, binary }]
def compile(elixir_code, opts \\ []) do
elixir_code
|> Code.string_to_quoted!
|> compile_quoted(opts)
end
@doc """
Compiles the given Elixir code in quoted form
"""
@spec compile_quoted(Macro.t, Dict.t) :: [binary | { binary, binary }]
def compile_quoted(quoted, opts \\ []) do
include_path = Dict.get(opts, :include_path, false)
root = Dict.get(opts, :root)
env = Dict.get(opts, :env, custom_env)
import_standard_libs? = Dict.get(opts, :import_standard_libs, true)
ElixirScript.State.start_link(root, env)
build_environment([quoted])
if Set.size(ElixirScript.State.get().modules) > 0 do
create_code(include_path, import_standard_libs?)
else
result = case Translator.translate(quoted, env) do
modules when is_list(modules) ->
List.flatten(modules)
|> Enum.map(fn(x) ->
convert_to_code(x, root, include_path, env, import_standard_libs?)
end)
module ->
List.wrap(
convert_to_code(module, root, include_path, env, import_standard_libs?)
)
end
ElixirScript.State.stop
result
end
end
def make_defmodule({:defmodule, _, _} = ast) do
ast
end
def make_defmodule(ast) do
{:defmodule, [], [{:__aliases__, [], [:Temp]}, [do: { :__block__, [], [ast] }]]}
end
@doc """
Compiles the elixir files found at the given path
"""
@spec compile_path(binary, Dict.t) :: [binary | { binary, binary }]
def compile_path(path, opts \\ []) do
include_path = Dict.get(opts, :include_path, false)
root = Dict.get(opts, :root)
env = Dict.get(opts, :env, custom_env)
ElixirScript.State.start_link(root, env)
path
|> Path.wildcard
|> Enum.map(fn(x) ->
File.read!(x)
|> Code.string_to_quoted!
end)
|> build_environment
create_code(include_path, true)
end
defp build_environment(code_list) do
code_list
|> ElixirScript.Preprocess.Modules.get_info
end
defp custom_env() do
require Logger
require ElixirScript.Lib.JS, as: JS
__ENV__
end
defp create_code(include_path, import_standard_libs?) do
state = ElixirScript.State.get()
result = Enum.map(state.modules, fn(x) ->
ElixirScript.Translator.Module.make_module(x.name, x.body, state.env)
end)
|> List.flatten
|> Enum.map(fn(x) ->
convert_to_code(x, state.root, include_path, state.env, import_standard_libs?)
end)
ElixirScript.State.stop
result
end
@doc """
Copies the javascript that makes up the ElixirScript standard libs
to the specified location
"""
def copy_standard_libs_to_destination(destination) do
File.cp_r!(operating_path <> "/dist", destination)
end
defp convert_to_code(js_ast, root, include_path, env, import_standard_libs \\ true) do
js_ast
|> process_module(root, env, import_standard_libs)
|> javascript_ast_to_code
|> process_include_path(include_path)
end
defp process_module(%JSModule{} = module, root, env, import_standard_libs) do
file_path = create_file_name(module)
standard_libs_import = if import_standard_libs do
ElixirScript.Translator.Import.create_standard_lib_imports(root, env)
else
[]
end
program = standard_libs_import ++ module.body
|> ESTree.Tools.Builder.program
{ file_path, program }
end
defp process_module(module, _root, _, _) do
{ "", module }
end
defp create_file_name(%JSModule{ name: module_list }) do
name = ElixirScript.Translator.Import.make_file_path(module_list)
"#{name}.js"
end
defp process_include_path({ _, _ } = pair, true) do
pair
end
defp process_include_path({ _, code }, false) do
code
end
@doc false
def javascript_ast_to_code({ path, js_ast }) do
js_code = javascript_ast_to_code(js_ast)
{ path, js_code }
end
@doc false
def javascript_ast_to_code(js_ast) do
prepare_js_ast(js_ast)
|> Generator.generate
end
defp prepare_js_ast(js_ast) do
case js_ast do
modules when is_list(modules) ->
Enum.reduce(modules, [], fn(x, list) -> list ++ x.body end)
|> Builder.program
%ElixirScript.Translator.Group{body: body} ->
Builder.program(body)
_ ->
js_ast
end
end
defp operating_path() do
try do
Mix.Project.build_path <> "/lib/elixir_script/priv/javascript"
rescue
UndefinedFunctionError ->
split_path = Path.split(Application.app_dir(:ex2js))
replaced_path = List.delete_at(split_path, length(split_path) - 1)
replaced_path = List.delete_at(replaced_path, length(replaced_path) - 1)
Path.join(replaced_path)
end
end
end