compilation - How to define a wildcard pattern using cerl:c_clause -
i'm trying compile personal language erlang. want create function pattern matching on clauses.
this data :
data = [ {a, <a_body> } , {b, <b_body> } , {c, <c_body> } ].
this want :
foo(a) -> <a_body>; foo(b) -> <b_body>; foo(c) -> <c_body>; foo(_) -> undefined. %% <-
i @ moment :
mkcaseclause = fun({pattern,body}) -> cerl:c_clause([cerl:c_atom(pattern)], deep_literal(body)) end, wildcardclause = cerl:c_clause([ ??? ], cerl:c_atom(undefined)), caseclauses = [mkcaseclause(s) || s <- data] ++ [wildcardclause],
so please me define wildcardclause
. saw if call compiled function neither nor b nor c results in ** exception error: no true branch found when evaluating if expression in function ...
.
when print core erlang code :
'myfuncname'/1 = fun (key) -> case key of <'a'> when 'true' -> ... <'b'> when 'true' -> ... <'c'> when 'true' -> ... end
so okay, case
translated if
when core compiled. need specify true
clause in if
expression pure wildcard. don't know how it, since matching true
in if
expression , in case
1 different semantics. in case, true
not wildcard.
and if match expressions wildcards inside {sometag,_,_,thing} -> {ok, thing}
.
thank you
i've found way
... wildcardvar = cerl:c_var('_any'), wildcardclause = cerl:c_clause([wildcardvar], cerl:c_atom(undefined)), ...
it should work inner wildcards too, 1 has careful give different variable names each _
wildcard since multiple _
not match each other, variables do.
f(x,_, _ ) %% matches f(a,b,c) f(x,_x,_x) %% doesn't
Comments
Post a Comment