Se puede ver lo que sucedería si compilar un código
-module(match).
-export([match/1]).
match(X) -> {a,Y} = X.
Cuando se quiere ver cómo se parece a core
> c(match, to_core).
o
$ erlc +to_core match.erl
resultado es
module 'match' ['match'/1,
'module_info'/0,
'module_info'/1]
attributes []
'match'/1 =
%% Line 3
fun (_cor0) ->
case _cor0 of
<{'a',Y}> when 'true' ->
_cor0
(<_cor1> when 'true' ->
primop 'match_fail'
({'badmatch',_cor1})
-| ['compiler_generated'])
end
'module_info'/0 =
fun() ->
call 'erlang':'get_module_info'
('match')
'module_info'/1 =
fun (_cor0) ->
call 'erlang':'get_module_info'
('match', _cor0)
Si desea ver el código asm de la viga que puede hacer
> c(match, 'S').
o
$ erlc -S match.erl
y el resultado
{module, match}. %% version = 0
{exports, [{match,1},{module_info,0},{module_info,1}]}.
{attributes, []}.
{labels, 8}.
{function, match, 1, 2}.
{label,1}.
{func_info,{atom,match},{atom,match},1}.
{label,2}.
{test,is_tuple,{f,3},[{x,0}]}.
{test,test_arity,{f,3},[{x,0},2]}.
{get_tuple_element,{x,0},0,{x,1}}.
{test,is_eq_exact,{f,3},[{x,1},{atom,a}]}.
return.
{label,3}.
{badmatch,{x,0}}.
{function, module_info, 0, 5}.
{label,4}.
{func_info,{atom,match},{atom,module_info},0}.
{label,5}.
{move,{atom,match},{x,0}}.
{call_ext_only,1,{extfunc,erlang,get_module_info,1}}.
{function, module_info, 1, 7}.
{label,6}.
{func_info,{atom,match},{atom,module_info},1}.
{label,7}.
{move,{x,0},{x,1}}.
{move,{atom,match},{x,0}}.
{call_ext_only,2,{extfunc,erlang,get_module_info,2}}.
Como se puede ver {test,is_tuple,...
, {test,test_arity,...
, {get_tuple_element,...
y {test,is_eq_exact,...
son instrucciones de cómo se realiza esta coincidencia en haz y se transforma directamente en código de byte de haz.
El compilador Erlang se implementa en Erlang y se puede ver cada fase de compilación en el código fuente del módulo compile y los detalles en los módulos dependientes.
maravillosa respuesta, mucha información excelente aquí (especialmente las directivas de compilación). gracias – deepblue
+1 por una excelente respuesta. –