[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文件的编译过程,书接上文,从 ...
随机推荐
- 更有效率的使用Visual Studio(二)
没想到上一篇文章有这么多人喜欢,多谢大家支持.继续- 很多比较通用的快捷键的默认设置其实是有一些缩写在里面的,这个估计也是MS帮助我们记忆.比如说注释代码的快捷键是Ctrl + E + C,我们如果知 ...
- CSharpGL(37)创建和使用VBO的最佳方式
CSharpGL(37)创建和使用VBO的最佳方式 开始 近日在OpenGL红宝书上看到这样的讲解. 其核心意思是,在创建VBO时用 glBufferData(GL_ARRAY_BUFFER, len ...
- 给WinForm窗体添加快捷键 响应回车键、空格键
1.设置窗体KeyPreview属性为True 2.添加窗体的KeyDown事件 /// <summary> /// 窗体的keydown事件响应 添加快捷键 /// </summa ...
- [Java 缓存] Java Cache之 Guava Cache的简单应用.
前言 今天第一次使用MarkDown的形式发博客. 准备记录一下自己对Guava Cache的认识及项目中的实际使用经验. 一: 什么是Guava Guava工程包含了若干被Google的 Java项 ...
- Laravel - 安装与配置
有多重途径可以安装Laravel,下面是通过composer安装laravel的方法.Composer 是 PHP 的一个依赖管理工具.它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们.c ...
- JavaScript的同步与异步
1.手绘一张图说明. 2.为什么JavaScript是单线程(这里引用阮一峰老师的话) JavaScript的单线程,与它的用途有关. 作为浏览器脚本语言,JavaScript的主要用途是与用户互动, ...
- 总结一下CSS中的定位 Position 属性
在CSS中,Position 属性经常会用到,主要是绝对定位和相对定位,简单的使用都没有问题,尤其嵌套起来,就会有些混乱,今记录总结一下,防止久而忘之. CSS position 属性值: absol ...
- 三个不常用的HTML元素:<details>、<summary>、<dialog>
前面的话 HTML5不仅新增了语义型区块级元素及表单类元素,也新增了一些其他的功能性元素,这些元素由于浏览器支持等各种原因,并没有被广泛使用 文档描述 <details>主要用于描述文档或 ...
- .NET平台开源项目速览(1)SharpConfig配置文件读写组件
在.NET平台日常开发中,读取配置文件是一个很常见的需求.以前都是使用System.Configuration.ConfigurationSettings来操作,这个说实话,搞起来比较费劲.不知道大家 ...
- 计算机程序的思维逻辑 (48) - 剖析ArrayDeque
前面我们介绍了队列Queue的两个实现类LinkedList和PriorityQueue,LinkedList还实现了双端队列接口Deque,Java容器类中还有一个双端队列的实现类ArrayDequ ...