|
| 1 | +defmodule ElixirScript.Experimental.Form do |
| 2 | + alias ESTree.Tools.Builder, as: J |
| 3 | + alias ElixirScript.Experimental.Forms.{Map, Bitstring, Match, Call, Try, For, Struct} |
| 4 | + alias ElixirScript.Experimental.Functions.{Erlang, Lists, Maps} |
| 5 | + alias ElixirScript.Translator.Identifier |
| 6 | + alias ElixirScript.Experimental.Clause |
| 7 | + |
| 8 | + @moduledoc """ |
| 9 | + Handles translation of all forms that are not functions or clauses |
| 10 | + """ |
| 11 | + |
| 12 | + def compile(nil, _) do |
| 13 | + J.identifier("null") |
| 14 | + end |
| 15 | + |
| 16 | + def compile(form, _) when is_boolean(form) when is_integer(form) when is_float(form) when is_binary(form) do |
| 17 | + J.literal(form) |
| 18 | + end |
| 19 | + |
| 20 | + def compile(form, state) when is_list(form) do |
| 21 | + J.array_expression( |
| 22 | + Enum.map(form, &compile(&1, state)) |
| 23 | + ) |
| 24 | + end |
| 25 | + |
| 26 | + def compile(form, state) when is_atom(form) do |
| 27 | + if ElixirScript.Experimental.Module.is_elixir_module(form) do |
| 28 | + members = ["Elixir"] ++ Module.split(form) |
| 29 | + J.identifier(Enum.join(members, "_")) |
| 30 | + else |
| 31 | + J.call_expression( |
| 32 | + J.member_expression( |
| 33 | + J.identifier("Symbol"), |
| 34 | + J.identifier("for") |
| 35 | + ), |
| 36 | + [J.literal(form)] |
| 37 | + ) |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + def compile({a, b}, state) do |
| 42 | + compile({:{}, [], [a, b]}, state) |
| 43 | + end |
| 44 | + |
| 45 | + def compile({:{}, _, elements}, state) do |
| 46 | + J.call_expression( |
| 47 | + J.member_expression( |
| 48 | + J.member_expression( |
| 49 | + J.identifier("Bootstrap"), |
| 50 | + J.identifier("Core") |
| 51 | + ), |
| 52 | + J.identifier("Tuple") |
| 53 | + ), |
| 54 | + Enum.map(elements, &compile(&1, state)) |
| 55 | + ) |
| 56 | + end |
| 57 | + |
| 58 | + def compile({:%{}, _, _} = map, state) do |
| 59 | + Map.compile(map, state) |
| 60 | + end |
| 61 | + |
| 62 | + def compile({:<<>>, _, _} = bitstring, state) do |
| 63 | + Bitstring.compile(bitstring, state) |
| 64 | + end |
| 65 | + |
| 66 | + def compile({:=, _, [left, right]} = match, state) do |
| 67 | + Match.compile(match, state) |
| 68 | + end |
| 69 | + |
| 70 | + def compile({:%, _, [_, _]} = ast, state) do |
| 71 | + Struct.compile(ast, state) |
| 72 | + end |
| 73 | + |
| 74 | + def compile({:for, _, _} = ast, state) do |
| 75 | + For.compile(ast, state) |
| 76 | + end |
| 77 | + |
| 78 | + def compile({:case, _, [condition, [do: clauses]]}, state) do |
| 79 | + func = J.call_expression( |
| 80 | + J.member_expression( |
| 81 | + ElixirScript.Experimental.Function.patterns_ast(), |
| 82 | + J.identifier("defmatch") |
| 83 | + ), |
| 84 | + Enum.map(clauses, &Clause.compile(&1, state)) |
| 85 | + ) |
| 86 | + |
| 87 | + J.call_expression( |
| 88 | + J.member_expression( func, J.identifier("call")), |
| 89 | + [J.identifier(:this), compile(condition, state)] |
| 90 | + ) |
| 91 | + end |
| 92 | + |
| 93 | + def compile({:cond, _, [[do: clauses]]}, state) do |
| 94 | + processed_clauses = Enum.map(clauses, fn({:->, _, [clause, clause_body]}) -> |
| 95 | + translated_body = Enum.map(List.wrap(clause_body), &compile(&1, state)) |
| 96 | + |> Clause.return_last_statement |
| 97 | + translated_body = J.function_expression([], [], J.block_statement(translated_body)) |
| 98 | + |
| 99 | + translated_clause = compile(hd(clause), state) |
| 100 | + |
| 101 | + |
| 102 | + J.array_expression([translated_clause, translated_body]) |
| 103 | + end) |
| 104 | + |
| 105 | + |
| 106 | + cond_function = J.member_expression( |
| 107 | + J.member_expression( |
| 108 | + J.identifier("Bootstrap"), |
| 109 | + J.member_expression( |
| 110 | + J.identifier("Core"), |
| 111 | + J.identifier("SpecialForms") |
| 112 | + ) |
| 113 | + ), |
| 114 | + J.identifier("cond") |
| 115 | + ) |
| 116 | + |
| 117 | + J.call_expression( |
| 118 | + cond_function, |
| 119 | + processed_clauses |
| 120 | + ) |
| 121 | + end |
| 122 | + |
| 123 | + def compile({:receive, context, _}, state) do |
| 124 | + line = Keyword.get(context, :line, 1) |
| 125 | + raise ElixirScriptCompileError, message: "Line: #{line} receive not supported" |
| 126 | + end |
| 127 | + |
| 128 | + def compile({:try, _, [blocks]}, state) do |
| 129 | + Try.compile(blocks, state) |
| 130 | + end |
| 131 | + |
| 132 | + def compile({:fn, _, clauses}, state) do |
| 133 | + J.call_expression( |
| 134 | + J.member_expression( |
| 135 | + ElixirScript.Experimental.Function.patterns_ast(), |
| 136 | + J.identifier("defmatch") |
| 137 | + ), |
| 138 | + Enum.map(clauses, &Clause.compile(&1, state)) |
| 139 | + ) |
| 140 | + end |
| 141 | + |
| 142 | + def compile({{:., _, [:erlang, _]}, _, _} = ast, state) do |
| 143 | + Erlang.rewrite(ast, state) |
| 144 | + end |
| 145 | + |
| 146 | + def compile({{:., _, [:lists, _]}, _, _} = ast, state) do |
| 147 | + Lists.rewrite(ast, state) |
| 148 | + end |
| 149 | + |
| 150 | + def compile({{:., _, [:maps, _]}, _, _} = ast, state) do |
| 151 | + Maps.rewrite(ast, state) |
| 152 | + end |
| 153 | + |
| 154 | + def compile({{:., _, [_, _]}, _, _} = ast, state) do |
| 155 | + Call.compile(ast, state) |
| 156 | + end |
| 157 | + |
| 158 | + def compile({:super, context, params}, state) do |
| 159 | + {function_name, _} = Keyword.fetch!(context, :function) |
| 160 | + IO.inspect {"HERE!!!!", function_name} |
| 161 | + |
| 162 | + J.call_expression( |
| 163 | + ElixirScript.Translator.Identifier.make_function_name(function_name, length(params)), |
| 164 | + Enum.map(params, &compile(&1, state)) |
| 165 | + ) |
| 166 | + end |
| 167 | + |
| 168 | + def compile({function_name, _, params}, state) when is_list(params) do |
| 169 | + case function_name do |
| 170 | + a when is_atom(a) -> |
| 171 | + J.call_expression( |
| 172 | + ElixirScript.Translator.Identifier.make_function_name(function_name, length(params)), |
| 173 | + Enum.map(params, &compile(&1, state)) |
| 174 | + ) |
| 175 | + _ -> |
| 176 | + J.call_expression( |
| 177 | + compile(function_name, state), |
| 178 | + Enum.map(params, &compile(&1, state)) |
| 179 | + ) |
| 180 | + end |
| 181 | + end |
| 182 | + |
| 183 | + def compile({var, _, _}, state) do |
| 184 | + ElixirScript.Translator.Identifier.make_identifier(var) |
| 185 | + end |
| 186 | + |
| 187 | +end |
0 commit comments