Skip to content

Instantly share code, notes, and snippets.

@UA3MQJ
Last active September 25, 2022 19:56
Show Gist options
  • Save UA3MQJ/e3d14e1525b5e2620985eb82e52a4a67 to your computer and use it in GitHub Desktop.
Save UA3MQJ/e3d14e1525b5e2620985eb82e52a4a67 to your computer and use it in GitHub Desktop.
Elixir compare COND/CASE/IF
$ cat test.ex
defmodule Test do
def test_case(foo) do
case foo do
:bar -> true
:buz -> false
end
end
def test_cond(foo) do
cond do
foo == :bar -> true
foo == :buz -> false
end
end
def test_if(foo) do
if foo == :bar do
true
else if foo == :buz do
false
end
end
end
end
$ ERL_COMPILER_OPTIONS="['E']" elixirc test.ex
...
$ cat test.ex.E
-file("test.ex", 1).
-module('Elixir.Test').
-compile(no_auto_import).
-export(['info'/1,test_case/1,test_cond/1,test_if/1]).
-spec 'info'(attributes |
compile |
functions |
macros |
md5 |
module |
deprecated) ->
any().
'info'(module) ->
'Elixir.Test';
'info'(functions) ->
[{test_case,1},{test_cond,1},{test_if,1}];
'info'(macros) ->
[];
'info'(attributes) ->
erlang:get_module_info('Elixir.Test', attributes);
'info'(compile) ->
erlang:get_module_info('Elixir.Test', compile);
'info'(md5) ->
erlang:get_module_info('Elixir.Test', md5);
'info'(deprecated) ->
[].
test_case(Vfoo@1) ->
case Vfoo@1 of
bar ->
true;
buz ->
false
end.
test_cond(Vfoo@1) ->
case Vfoo@1 == bar of
true ->
true;
false ->
case Vfoo@1 == buz of
true ->
false;
false ->
error(cond_clause)
end
end.
test_if(Vfoo@1) ->
case Vfoo@1 == bar of
false ->
case Vfoo@1 == buz of
false ->
nil;
true ->
false
end;
true ->
true
end.
$ ERL_COMPILER_OPTIONS="['S']" elixirc test.ex
...
$ cat test.ex.S
{module, 'Elixir.Test'}. %% version = 0
{exports, [{'info',1},
{module_info,0},
{module_info,1},
{test_case,1},
{test_cond,1},
{test_if,1}]}.
{attributes, []}.
{labels, 28}.
{function, 'info', 1, 2}.
{label,1}.
{line,[]}.
{func_info,{atom,'Elixir.Test'},{atom,'info'},1}.
{label,2}.
{test,is_atom,{f,1},[{x,0}]}.
{select_val,{x,0},
{f,1},
{list,[{atom,functions},
{f,3},
{atom,md5},
{f,4},
{atom,compile},
{f,5},
{atom,attributes},
{f,6},
{atom,module},
{f,7},
{atom,macros},
{f,8},
{atom,deprecated},
{f,8}]}}.
{label,3}.
{move,{literal,[{test_case,1},{test_cond,1},{test_if,1}]},{x,0}}.
return.
{label,4}.
{move,{atom,md5},{x,1}}.
{move,{atom,'Elixir.Test'},{x,0}}.
{line,[]}.
{call_ext_only,2,{extfunc,erlang,get_module_info,2}}.
{label,5}.
{move,{atom,compile},{x,1}}.
{move,{atom,'Elixir.Test'},{x,0}}.
{line,[]}.
{call_ext_only,2,{extfunc,erlang,get_module_info,2}}.
{label,6}.
{move,{atom,attributes},{x,1}}.
{move,{atom,'Elixir.Test'},{x,0}}.
{line,[]}.
{call_ext_only,2,{extfunc,erlang,get_module_info,2}}.
{label,7}.
{move,{atom,'Elixir.Test'},{x,0}}.
return.
{label,8}.
{move,nil,{x,0}}.
return.
{function, test_case, 1, 10}.
{label,9}.
{line,[{location,"test.ex",2}]}.
{func_info,{atom,'Elixir.Test'},{atom,test_case},1}.
{label,10}.
{test,is_atom,{f,13},[{x,0}]}.
{select_val,{x,0},{f,13},{list,[{atom,bar},{f,11},{atom,buz},{f,12}]}}.
{label,11}.
{move,{atom,true},{x,0}}.
return.
{label,12}.
{move,{atom,false},{x,0}}.
return.
{label,13}.
{line,[{location,"test.ex",3}]}.
{case_end,{x,0}}.
{function, test_cond, 1, 15}.
{label,14}.
{line,[{location,"test.ex",9}]}.
{func_info,{atom,'Elixir.Test'},{atom,test_cond},1}.
{label,15}.
{select_val,{x,0},{f,18},{list,[{atom,bar},{f,16},{atom,buz},{f,17}]}}.
{label,16}.
{move,{atom,true},{x,0}}.
return.
{label,17}.
{move,{atom,false},{x,0}}.
return.
{label,18}.
{move,{atom,cond_clause},{x,0}}.
{line,[]}.
{call_ext,1,{extfunc,erlang,error,1}}.
{function, test_if, 1, 20}.
{label,19}.
{line,[{location,"test.ex",16}]}.
{func_info,{atom,'Elixir.Test'},{atom,test_if},1}.
{label,20}.
{select_val,{x,0},{f,23},{list,[{atom,bar},{f,21},{atom,buz},{f,22}]}}.
{label,21}.
{move,{atom,true},{x,0}}.
return.
{label,22}.
{move,{atom,false},{x,0}}.
return.
{label,23}.
{move,{atom,nil},{x,0}}.
return.
{function, module_info, 0, 25}.
{label,24}.
{line,[]}.
{func_info,{atom,'Elixir.Test'},{atom,module_info},0}.
{label,25}.
{move,{atom,'Elixir.Test'},{x,0}}.
{line,[]}.
{call_ext_only,1,{extfunc,erlang,get_module_info,1}}.
{function, module_info, 1, 27}.
{label,26}.
{line,[]}.
{func_info,{atom,'Elixir.Test'},{atom,module_info},1}.
{label,27}.
{move,{x,0},{x,1}}.
{move,{atom,'Elixir.Test'},{x,0}}.
{line,[]}.
{call_ext_only,2,{extfunc,erlang,get_module_info,2}}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment