As you can see {test,is_tuple,... , {test,test_arity,... , {get_tuple_element,... and {test,is_eq_exact,... are instruction how this match is performed in beam and it's transformed directly to byte-code of beam. Erlang compiler is implemented in Erlang itself and you can look at each phase of compilation in source code of compile module and details in depend modules.
4条答案
按热度按时间kmbjn2e31#
在"The implementation of functional programming languages" by Simon Peyton Jones中给出了编译模式匹配的一个很好的描述。这是一本有点旧但非常好的书。它还包含了编译列表解析的描述。
Erlang编译器使用书中的这两种算法。
v6ylcynt2#
You can see what happen if compile some code
When you want see how looks like core
or
result is
If you want see asm code of beam you can do
or
and result
As you can see
{test,is_tuple,...
,{test,test_arity,...
,{get_tuple_element,...
and{test,is_eq_exact,...
are instruction how this match is performed in beam and it's transformed directly to byte-code of beam.Erlang compiler is implemented in Erlang itself and you can look at each phase of compilation in source code of compile module and details in depend modules.
7uzetpgm3#
如果你想构建你自己的模式匹配器,有一个paper by Scott and Ramsey和一个paper by Luc Maranget,它们都描述了如何将模式编译成有效的决策树(也就是嵌套的switch语句)。
dwthyt8l4#
我能建议的最好的事情是编译一些测试函数并查看生成的代码。
生成可读性很好的test. S。
为了回答这个问题,模式匹配是从更原始的操作中以一种有效的方式建立起来的。|T ]}的时间。