【发布时间】:2013-07-17 13:07:27
【问题描述】:
我正在尝试将一些个人语言编译为 erlang。我想创建一个对子句进行模式匹配的函数。
这是我的数据:
Data =
[ {a, <a_body> }
, {b, <b_body> }
, {c, <c_body> }
].
这就是我想要的:
foo(a) -> <a_body>;
foo(b) -> <b_body>;
foo(c) -> <c_body>;
foo(_) -> undefined. %% <- this
我现在这样做:
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],
所以请帮我定义WildCardClause。我看到如果我用既不 a 也不 b 也不 c 调用我的编译函数,它会导致 ** exception error: no true branch found when evaluating an if expression in function ...。
当我打印我的 Core Erlang 代码时,我得到了这个:
'myfuncname'/1 =
fun (Key) ->
case Key of
<'a'> when 'true' -> ...
<'b'> when 'true' -> ...
<'c'> when 'true' -> ...
end
好吧,当内核编译时,case 被翻译成if。所以我需要在if 表达式中指定true 子句以获得纯通配符。我不知道该怎么做,因为在 if 表达式和 case 中匹配 true 是不同的语义。在某种情况下,true 不是通配符。
如果我想匹配带有通配符的表达式,比如{sometag,_,_,Thing} -> {ok, Thing}。
谢谢
【问题讨论】:
标签: compilation programming-languages erlang