[Erlang 0109] From Elixir to Erlang Code

iex> defmodule Math do
...> def sum(a, b) do
...> a + b
...> end
...> end iex> Math.sum(1, 2)
3
[root@nimbus elixir]# elixirc m.ex
[root@nimbus elixir]# ls
Elixir.Math.beam m.ex [root@nimbus elixir]# erl
Erlang R16B01 (erts-5.10.2) [source] [64-bit] [smp:2:2] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V5.10.2 (abort with ^G)
1> 'Elixir.Math':sum(12,23).
35
2>
> {ok,{_,[{abstract_code,{_,AC}}]}} = beam_lib:chunks("Elixir.Math",[abstract_code]).
{ok,{'Elixir.Math',
[{abstract_code,
{raw_abstract_v1,
[{attribute,,compile, {no_auto_import,[{bitsize,},{apply,},
{spawn,}, {spawn_link,}, {spawn_monitor,}, {spawn_opt,},
{spawn_opt,}, {spawn,},
{spawn_link,}, {spawn_opt,},
{spawn_opt,}, {nodes,...},
{...}|...]}},
{attribute,,file,{"/data2/elixir/m.ex",}},
{attribute,,module,'Elixir.Math'},
{attribute,,export,[{'__info__',},{sum,}]},
{function,,'__info__',,
[{clause,,
[{atom,,functions}],
[],
[{cons,,{...},...}]},
{clause,,[{atom,,macros}],[],[{nil,}]},
{clause,,[{atom,,docs}],[],[{cons,...}]},
{clause,,[{atom,,...}],[],[{...}]},
{clause,,[{atom,...}],[],[...]},
{clause,,[{...}],[],...}]},
{function,,sum,,
[{clause,,
[{var,,a},{var,,b}],
[],
[{op,,...}]}]}]}}]}}
> io:fwrite("~s~n", [erl_prettypr:format(erl_syntax:form_list(AC))]).
-compile({no_auto_import,
[{bitsize, }, {apply, }, {spawn, }, {spawn_link, },
{spawn_monitor, }, {spawn_opt, }, {spawn_opt, },
{spawn, }, {spawn_link, }, {spawn_opt, },
{spawn_opt, }, {nodes, }, {disconnect_node, },
{integer_to_list, }, {integer_to_binary, }, {max, },
{min, }, {port_control, }, {port_connect, },
{port_command, }, {port_command, }, {port_close, },
{spawn_monitor, }, {spawn, }, {load_module, },
{spawn_link, }, {binary_to_float, },
{float_to_binary, }, {float_to_binary, },
{list_to_integer, }, {integer_to_binary, },
{binary_to_integer, }, {binary_to_integer, },
{check_old_code, }, {binary_part, }, {binary_part, },
{binary_to_term, }, {binary_to_existing_atom, },
{binary_to_atom, }, {atom_to_binary, },
{bitstring_to_list, }, {list_to_bitstring, },
{bit_size, }, {byte_size, }, {tuple_size, },
{is_bitstring, }, {list_to_existing_atom, },
{iolist_to_binary, }, {iolist_size, },
{is_boolean, }, {is_record, }, {is_record, },
{is_function, }, {is_function, }, {is_binary, },
{is_reference, }, {is_port, }, {is_pid, },
{is_number, }, {is_integer, }, {is_float, },
{is_tuple, }, {is_list, }, {is_atom, }, {error, },
{error, }, {is_process_alive, }, {demonitor, },
{demonitor, }, {monitor, }, {whereis, },
{unregister, }, {unlink, }, {tuple_to_list, },
{trunc, }, {tl, }, {time, }, {throw, },
{term_to_binary, }, {term_to_binary, },
{statistics, }, {split_binary, }, {spawn_link, },
{spawn, }, {size, }, {setelement, }, {self, },
{round, }, {registered, }, {register, }, {put, },
{purge_module, }, {processes, }, {process_info, },
{process_info, }, {process_flag, }, {process_flag, },
{pre_loaded, }, {pid_to_list, }, {open_port, },
{now, }, {nodes, }, {node, }, {node, },
{monitor_node, }, {module_loaded, }, {make_ref, },
{list_to_tuple, }, {list_to_pid, },
{list_to_integer, }, {list_to_float, },
{list_to_binary, }, {list_to_atom, }, {link, },
{length, }, {is_alive, }, {integer_to_list, },
{hd, }, {halt, }, {halt, }, {halt, },
{group_leader, }, {group_leader, }, {get_keys, },
{get, }, {get, }, {garbage_collect, },
{garbage_collect, }, {float_to_list, },
{float_to_list, }, {float, }, {exit, }, {exit, },
{erase, }, {erase, }, {element, },
{delete_module, }, {date, }, {check_process_code, },
{binary_to_term, }, {binary_to_list, },
{binary_to_list, }, {atom_to_list, }, {apply, },
{abs, }]}).
-file("/data2/elixir/m.ex", ).
-module('Elixir.Math').
-export(['__info__'/, sum/]).
'__info__'(functions) -> [{sum, }];
'__info__'(macros) -> [];
'__info__'(docs) ->
[{{sum, }, , def,
[{a, [{line, }], nil}, {b, [{line, }], nil}], nil}];
'__info__'(moduledoc) -> {, nil};
'__info__'(module) -> 'Elixir.Math';
'__info__'(atom) -> module_info(atom).
sum(a, b) -> a + b.
ok
>
如何编译的?
exec "$SCRIPT_PATH"/elixir +compile "$@"
erl -pa "$SCRIPT_PATH"/../lib/*/ebin -noshell -s elixir start_cli -extra +compile
% erl +W w -sname arnie +R -s my_init -extra +bertie
(arnie@host)> init:get_argument(sname).
{ok,[["arnie"]]}
(arnie@host)> init:get_plain_arguments().
["+bertie"]
Here +W w and +R 9 are emulator flags. -s my_init is an init flag, interpreted by init. -sname arnie is a user flag, stored by init. It is read by Kernel and will cause the Erlang runtime system to become distributed. Finally, everything after -extra (that is, +bertie) is considered as plain arguments.
% erl -myflag
> init:get_argument(myflag).
{ok,[[""]]}
> init:get_plain_arguments().
[]
Here the user flag -myflag 1 is passed to and stored by the init process. It is a user defined flag, presumably used by some user defined application.
%% Boot and process given options. Invoked by Elixir's script.
start_cli() ->
application:start(?MODULE), 'Elixir.Kernel.CLI':main(init:get_plain_arguments()).
[root@nimbus ebin]# erl
Erlang R16B01 (erts-5.10.) [source] [-bit] [smp::] [async-threads:] [hipe] [kernel-poll:false] Eshell V5.10.2 (abort with ^G)
> application:start(elixir).
ok > 'Elixir.Kernel.CLI':main(["+compile","m.ex"]).
@doc """
This is the API invoked by Elixir boot process.
"""
def main(argv) do
argv = lc arg inlist argv, do: String.from_char_list!(arg) { config, argv } = process_argv(argv, Kernel.CLI.Config.new)
System.argv(argv) run fn ->
command_results = Enum.map(Enum.reverse(config.commands), &process_command(&1, config))
command_errors = lc { :error, msg } inlist command_results, do: msg
errors = Enum.reverse(config.errors) ++ command_errors if errors != [] do
Enum.each(errors, &IO.puts(:stderr, &1))
System.halt(1)
end
end, config.halt
end
defp process_argv(["+compile"|t], config) do
process_compiler t, config
end

[Erlang 0109] From Elixir to Erlang Code的更多相关文章
- elixir 调用erlang 代码
备注: 项目比较简单,主要是elixir 混合erlang 代码,elixir 调用erlang 模块方法 1. 初始化项目 mix new erlangelixirdemo 项目结构如 ...
- [Erlang 0116] 当我们谈论Erlang Maps时,我们谈论什么 Part 1
Erlang 增加 Maps数据类型并不是很突然,因为这个提议已经进行了2~3年之久,只不过Joe Armstrong老爷子最近一篇文章Big changes to Erlang掀起不小了风 ...
- [Erlang 0117] 当我们谈论Erlang Maps时,我们谈论什么 Part 2
声明:本文讨论的Erlang Maps是基于17.0-rc2,时间2014-3-4.后续Maps可能会出现语法或函数API上的有所调整,特此说明. 前情提要: [Erlang 0116] 当我们谈论E ...
- Erlang基础 -- 介绍 -- 历史及Erlang并发
前言 最近在总结一些Erlang编程语言的基础知识,拟系统的介绍Erlang编程语言,从基础到进阶,然后再做Erlang编程语言有意思的库的分析. 其实,还是希望越来越多的人关注Erlang,使用Er ...
- [Erlang 0125] Know a little Erlang opcode
Erlang源代码编译为beam文件,代码要经过一系列的过程(见下面的简图),Core Erlang之前已经简单介绍过了Core Erlang,代码转换为Core Erlang,就容易拨开一些语法糖的 ...
- [Erlang 0128] Term sharing in Erlang/OTP 下篇
继续昨天的话题,昨天提到io:format对数据共享的间接影响,如果是下面两种情况恐怕更容易成为"坑", 呃,恰好我都遇到过; 如果是测试代码是下面这样,得到的结果会是怎样?猜! ...
- [Erlang 0127] Term sharing in Erlang/OTP 上篇
之前,在 [Erlang 0126] 我们读过的Erlang论文 提到过下面这篇论文: On Preserving Term Sharing in the Erlang Virtual Machine ...
- [Erlang 0121] 当我们谈论Erlang Maps时,我们谈论什么 Part 3
Erlang/OTP 17.0 has been released http://www.erlang.org/download/otp_src_17.0.readme Erlang/OTP ...
- [Erlang 0113] Elixir 编译流程梳理
注意:目前Elixir版本还不稳定,代码调整较大,本文随时失效 之前简单演示过如何从elixir ex代码生成并运行Erlang代码,下面仔细梳理一遍elixir文件的编译过程,书接上文,从 ...
随机推荐
- JAAS 是个什么梗
参考资料 该文中的内容来源于 Oracle 的官方文档.Oracle 在 Java 方面的文档是非常完善的.对 Java 8 感兴趣的朋友,可以从这个总入口 Java SE 8 Documentati ...
- 浅谈WebService的版本兼容性设计
在现在大型的项目或者软件开发中,一般都会有很多种终端, PC端比如Winform.WebForm,移动端,比如各种Native客户端(iOS, Android, WP),Html5等,我们要满足以上所 ...
- docker创建私有仓库
由于网速和大中华局域网效果,使得我们在DockerHub下载镜像的速度很慢,甚至一些国内的镜像仓库,也感觉速度不是很好.所以,很有必要在本地或者一个我们访问很快速的地方(自己的云服务器)搭建一套镜像仓 ...
- 原生ajax实现登录(一部分代码)
<script type="text/javascript"> function ajax() { //先声明一个异步请求对 ...
- WCF学习之旅—基于Fault Contract 的异常处理(十八)
WCF学习之旅—WCF中传统的异常处理(十六) WCF学习之旅—基于ServiceDebug的异常处理(十七) 三.基于Fault Contract 的异常处理 第二个示例是通过定制Servic ...
- Nginx代理功能与负载均衡详解
序言 Nginx的代理功能与负载均衡功能是最常被用到的,关于nginx的基本语法常识与配置已在上篇文章中有说明,这篇就开门见山,先描述一些关于代理功能的配置,再说明负载均衡详细. Nginx代理服务的 ...
- nhibernate连接11g数据库
我框架的数据映射用 nhibernate连接多数据库,这次又增加了oracle11g,负责开发的同事始终连接不上,悲催的sharepoint调试是在不方便... 下面描述下问题的解决,细节问题有3个: ...
- jeffy-vim-v3.0
jeffy-vim-v3.0 修改了配色.
- Deep learning:五十一(CNN的反向求导及练习)
前言: CNN作为DL中最成功的模型之一,有必要对其更进一步研究它.虽然在前面的博文Stacked CNN简单介绍中有大概介绍过CNN的使用,不过那是有个前提的:CNN中的参数必须已提前学习好.而本文 ...
- JavaScript知识点总结(命名规范,变量的作用域)
命名规范 有人说JavaScript的宽容性是这个语言最糟糕的方面之一.比如说想把2个数字加在一起,JavaScript会把其中一个数字解析成字符串,那么就会得到一个奇怪的字符串,而不是2个数字的和. ...