长生不老药转变为Erlang [英] Elixir into Erlang transformation
问题描述
我想看看当长生不老药转化为射线文件时会发生什么。有什么方法可以在控制台或文件中进行打印?我想知道这个模块在erlang中是什么样子。
I want to see what happens when elixir gets transformed into beam files. Is there any way to print in console or in a file how it is translated? I want to know what would this module look like in erlang.
我在考虑是否有elixir调试模式,该调试模式会输出上述任何内容。
I was thinking if there is a debug mode of elixir, which would output any of the above.
更具体地说,我有这个示例:
More specifically I have this example:
defmodule Test do
def t1(a), do: a
def t1(a, b \\ 2), do: a + b
end
上面的代码会引发警告,考虑到我所做的事情,这是非常重要的。基本上,我想了解更多情况。
The above code raises a warning, which is understanable considering what I've done. Basically I want to understand a bit more what's happening.
推荐答案
首先,您需要将Elixir模块编译为.beam文件。 :
First, you need to compile the Elixir module to a .beam file:
$ cat test.ex
defmodule Test do
def t1(a), do: a
def t1(a, b \\ 2), do: a + b
end
$ elixirc test.ex
warning: this clause cannot match because a previous clause at line 2 always matches
test.ex:3
这将生成 Elixir.Test.beam
。然后,您可以使用以下escript将此.beam反编译为Erlang源代码(我之前在Stackoverflow上的某个答案中复制了此代码。不幸的是,我似乎无法找到确切的源代码,但是此代码在很多答案中都包括< a href = https://stackoverflow.com/a/1069185/320615>这。):
This will generate Elixir.Test.beam
. Then, you can decompile this .beam to Erlang source using the following escript (I copied this from some answer here on Stackoverflow a while ago. Unfortunately I can't seem to locate the exact source but this code is in many answers here including this one.):
$ cat decompile.erl
#!/usr/bin/env escript
main([BeamFile]) ->
{ok,{_,[{abstract_code,{_,AC}}]}} = beam_lib:chunks(BeamFile,[abstract_code]),
io:fwrite("~s~n", [erl_prettypr:format(erl_syntax:form_list(AC))]).
然后运行它:
$ escript decompile.erl Elixir.Test.beam
-compile(no_auto_import).
-file("test.ex", 1).
-module('Elixir.Test').
-export(['__info__'/1, t1/1, t1/2]).
-spec '__info__'(attributes | compile | exports |
functions | macros | md5 | module |
native_addresses) -> atom() |
[{atom(), any()} |
{atom(), byte(), integer()}].
'__info__'(functions) -> [{t1, 1}, {t1, 2}];
'__info__'(macros) -> [];
'__info__'(info) ->
erlang:get_module_info('Elixir.Test', info).
t1(a@1) -> a@1;
t1(x0@1) -> t1(x0@1, 2).
t1(a@1, b@1) -> a@1 + b@1.
这篇关于长生不老药转变为Erlang的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!