The Abstract Format
博客专区 > hncscwc 的博客 > 博客详情
The Abstract Format
hncscwc 发表于4年前
The Abstract Format
  • 发表于 4年前
  • 阅读 63
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

摘要: 备忘

1. 模块声明及属性

(1). 模块的申明

-module(test).
{attribute, LINE, module, test}.
(2). 函数的导出
-export([start/0, start/1]).
{attribute, LINE, export, [{start,1}, {start,2}]}.
(3). 函数的导入
-export(io, [format/2]).
{attribute, LINE, import, {io, [{format, 2}]}}.

(4). 记录定义
-reocrd(state, {sock, recv_len}).
{attribute, LINE, record, {state, [{record_field, LINE, {atom, LINE, socket}},
                                   {record_field, LINE, {atom, LINE, recv_len}}]}}.
(5). 其他属性地定义
-author("hncscwc").
{attribute, LINE, author, "hncscwc"}.
2. 模式
(1). 变量
V
{var, LINE, 'V'}
(2). 元组
{A, B, C}
{tuple, LINE, [Rep(A), Rep(B), Rep(C)]}
例如:
{ok, "info", Data}
{tuple, Line, [{atom, Line, ok}, {string, LINE, "info"}, {var, LINE, 'Data'}]}
(3). 空列表
[]
{nil, LINE}
(4). 列表
[A|B]
{cons, LINE, Rep(A), Rep(B)}
同理
[A,B,C] == [A|[B,C]] == [A|[B|[C]]] == [A|[B|[C|[]]]]
{cons, LINE, Rep(A),
             {cons, LINE, Rep(B),
                          {cons, LINE, Rep(C),
                                       {nil, LINE}}}}
例如:
[ok, "info", Info]
{cons, Line, {atom, Line, ok},
             {cons, Line, {string, LINE, "info"},
                          {cons, Line, {var, LINE, 'Info'},
                                       {nil, LINE}}}}

(5). binary

<<P1:Size1/TSL1, ..., Pk:Sizek/TSLk>>
{bin, LINE, [{bin_element, LINE, Rep(P1), Rep(Size1), Rep(TSL1)},
             ...
             {bin_element, LINE, Rep(Pk), Rep(Sizek), Rep(TSLk)}]}
注意: 缺省用default替代, 另外Rep(TSL) 形式为 [TSL]
例如:
<<156:32/big, 1, Info/binary>>
{bin, LINE, [{bin_element, LINE, {integer, LINE, 156}, {integer,LINE,32}, [big]},
             {bin_element, LINE, {integer, LINE, 1},default, default,},
             {bin_element, LINE, {var, LINE, 'Info'}, default, [binary]}]}
(6). record赋值
#Name{Field1=P1, ..., Fieldk=Pk}
{record, LINE, Name, [{record_field, LINE, Rep(Field1), Rep(P1)},
                      ...,
                      {record_field, LINE, Rep(Fieldk), Rep(Pk)}]}
例如:
#state{sock = Socket, recv_len = 4}
{record, LINE, state, [{record_field, LINE, {atom, LINE, sock}, {var, LINE, 'Socket'}},
                       {record_field, LINE, {aotm, LINE, recv_len}, {integer, LINE, 4}]}
(7). record取值
#Name.Field
{record_index, LINE, Name, Rep(Field)}
例如:
#state.sock
{record_index, LINE, state, {atom, LINE, sock}}
(8). record
Val#Name.Field
{record_field, LINE, Rep(Val), Name, Rep(Field)}
例如:
State#state.sock
{record_field, LINE, {var, LINE, 'State'}, state, {atom, LINE, sock}}
(9). 函数
1) Name/Arity
{'fun', LINE, {function, Name, Arity}}.
例如:
F = fun start/2
{match, LINE, {var, LINE, 'F'}, {'fun', LINE, {function, start, 2}}}


2) Module:Name/Arity
{'fun', LINE, {function, Rep(Module), Rep(Name), Rep(Arity)}}
例如:
F = lists:nth/2
{match, LINE, {var, LINE, 'F'},{'fun', LINE, {function, {atom, LINE, lists}, {atom, LINE, nth}, {integer, LINE, 2}}}}

3) 匿名
{'fun', LINE, {clauses, [Rep(F1),Rep(F2),...,Rep(Fk)]}}
例如:
F = fun() -> ok end.
{match, LINE, {var, LINE, 'F'}, {'fun', LINE, {clauses, [{clause, LINE, [],[], [{atom, LINE, ok}]}]}}}
3. 表达式

(1) = 操作

A = V
{match, LINE, Rep(P), Rep(V)}
例如:
Info = "hello world"
{match, LINE, {var, LINE, 'Info'}, {string, LINE, "hello world"}}
(2) P1 Op P2
{op, LINE, Op, Rep(P1), Rep(P2)}
例如: Var >= 10
{op, LINE, '>=', {var, LINE, 'Var'}, {integer, LINE, 10}}
(3) FA
Function(A1, A2, ... Ak)
{call, LINE, Rep(Function), [Rep(A1), ..., Rep(Ak)]}
例如:
format("~p~n", [Info]).
{call, Line, {atom, LINE, format}, [{string, LINE, "~p~n"}, {cons, LINE, {var, LINE, 'Info'}, {nil, LINE}}]}
(4)  MFA
Module:Function(A1, A2,... Ak).
{call, LINE, {remote, LINE, Rep(Module), Rep(Function)}, [Rep(A1), Rep(A2)]}.
例如:
lager:error("~p~n", [Info]).
{call, LINE, {remote, LINE, {atom, LINE, lager}, {atom, LINE, error}},
             [{string, LINE, "~p~n"}, {cons, LINE, {var, LINE, 'Info'}, {nil, LINE}}]}
(5) catch
catch V
{'catch', LINE, Rep(V)}
例如:
catch Info = "hello world"
{'catch', LINE, {match, LINE, {var, LINE, 'Info'}, {string, LINE, "hello world"}}}
(6) case
case Express of
    C1;
    C2;
    ...
    Ck
end
{'case', LINE, Rep(Express), [Rep(C1), Rep(C2),... Rep(Ck)]}
4. 语句
(1) P -> B   P表示一个模式, B为实际执行语句 常见于case/if语句
{clause, LINE, [Rep(P)], [], [Rep(B)]}
例如:
debug ->
    lager:debug("~p~n", [Info])

{clause, LINE, [{atom, LINE, debug}], [], 
               [{call, LINE, {remote, LINE, {atom, LINE, lager}, {atom, LINE, error}},
                             [{string, LINE, "~p~n"}, {cons, LINE, {var, LINE, 'Info'}, {nil, LINE}}]}]}
(2) P when G -> B   P表示一个模式匹配, G表示一个断言, B为执行语句
{clause, LINE, [Rep(P)], [Rep(G)], [Rep(B)]}
(3). (P1, P2, ..., Pk) -> B   Pk表示一个模式, B为实际执行语句, 常见于函数
{clause, LINE, [Rep(P1),Rep(P2),..., Rep(Pk)], [], [Rep(B)]}
例如
(Level, Info) ->
    lager:Level("~p~n", [Info])

{clause, LINE, [{var, LINE, 'Level'}, {var, LINE, 'Info'}], [], 
               [{call, LINE, {remote, LINE, {atom, LINE, lager}, {var, LINE, 'Level'}},
                             [{string, LINE, "~p~n"}, {cons, LINE, {var, LINE, 'Info'}, {nil, LINE}}]}]}
(4). (P1, P2, ..., Pk) when G1,G2,...,Gk -> B  Pk表示一个模式, G表示一个断言 B为实际执行语句, 常见于函数
{clause, LINE, [Rep(P1), Rep(P2), ..., Rep(Pk)], [[Rep(G1), Rep(G2), ... Rep(G3)]], [Rep(B)]}

(Level, Info) when Level >= 1, Level =< 3 ->
    lager:info("~p~n", [Info])

{clause, LINE, [{var, LINE, 'Var'}, {var, LINE, 'Info'}],
               [[{op, LINE, '>=', {var, LINE, 'Level'}, {integer, LINE, 1}},
                 {op, LINE, '=<', {var, LINE, 'Level'}, {integer, LINE, 3}}]],
               [{call, LINE, {remote, LINE, {atom, LINE, lager}, {atom, LINE, info}},
                             [{string, LINE, "~p~n"}, {cons, LINE, {var, LINE, 'Info'}, {nil, LINE}}]}]}
(5). (P1, P2, ..., Pk) when G1;G2;...;Gk -> B
{clause, LINE, [Rep(P1), Rep(P2), ..., Rep(Pk)], [[Rep(G1)], [Rep(G2)], ... [Rep(G3)]], [Rep(B)]}



共有 人打赏支持
粉丝 64
博文 64
码字总数 75100
×
hncscwc
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: