[Erlang 0110] Erlang Abstract Format , Part 1
Erlang Abstract Format并不难懂,只是枯燥一点罢了,如果把Abstract Format的文档翻译出来,其实就是Erlang教科书中语法入门的部分. Erlang Abstract Format实际上是用Erlang代码的AST,下面通过一些真切的实例代码了解一下它的一些细节.
首先,Erlang Abstract Format里面包含一些概念,我会在下面的描述中把涉及到的概念字体加粗.请注意概念之间的层次关系.Erlang代码本身使用非常扁平的module组织,每一个module是由一系列forms组成的.这些forms分成两大类:attributes和函数声明 见下图.

Attribute
attributes相对比较简单,我们先从一个只有attributes没有任何函数声明的module开始
-module(k).
-compile(export_all).
-compile({parse_transform,print_form}).
-export([test/]).
-url("http://cnblogs.com/me-sa/").
-record(student,{class,id}).
-record(player,{id=,name=[],level}).
[{attribute,1,file,{"k.erl",1}},
{attribute,1,module,k},
{attribute,2,compile,export_all},
{attribute,4,export,[{test,0}]},
{attribute,5,url,"http://cnblogs.com/me-sa/"},
{attribute,6,record,
{student,[{record_field,6,{atom,6,class}},
{record_field,6,{atom,6,id}}]}},
{attribute,7,record,
{player,[{record_field,7,{atom,7,id},{integer,7,0}},
{record_field,7,{atom,7,name},{nil,7}},
{record_field,7,{atom,7,level}}]}},
{eof,10}]

Function declarations
-module(a).
-compile(export_all).
-export([test/0]).
-record(student,{class,id}).
-record(player,{id=0,name=[],level}). test()->
"hello world!". test(a,[1,2]) ->
"a:[1,2]";
test(12.5,100)->
"test". test([]) ->
empty;
test(abc) ->
"atom test". foo(a)->
{b,100}. bar({1,2},12)->
[1,2,3,4,5,6]. k(Num) when Num >1000 ->
bigger_than_100;
k(Num) ->
whatever. call(1000)->
k(1000);
call(1002)->
erlang:now().
[{attribute,1,file,{"a.erl",1}},
{attribute,1,module,a},
{attribute,2,compile,export_all},
{attribute,4,export,[{test,0}]},
{attribute,6,record,
{student,
[{record_field,6,{atom,6,class}},{record_field,6,{atom,6,id}}]}},
{attribute,7,record,
{player,
[{record_field,7,{atom,7,id},{integer,7,0}},
{record_field,7,{atom,7,name},{nil,7}},
{record_field,7,{atom,7,level}}]}},
{function,10,test,0,[{clause,10,[],[],[{string,11,"hello world!"}]}]},
{function,13,test,2,
[{clause,13,
[{atom,13,a},
{cons,13,{integer,13,1},{cons,13,{integer,13,2},{nil,13}}}],
[],
[{string,14,"a:[1,2]"}]},
{clause,15,
[{float,15,12.5},{integer,15,100}],
[],
[{string,16,"test"}]}]},
{function,18,test,1,
[{clause,18,[{nil,18}],[],[{atom,19,empty}]},
{clause,20,[{atom,20,abc}],[],[{string,21,"atom test"}]}]},
{function,24,foo,1,
[{clause,24,
[{atom,24,a}],
[],
[{tuple,25,[{atom,25,b},{integer,25,100}]}]}]},
{function,27,bar,2,
[{clause,27,
[{tuple,27,[{integer,27,1},{integer,27,2}]},{integer,27,12}],
[],
[{cons,28,
{integer,28,1},
{cons,28,
{integer,28,2},
{cons,28,
{integer,28,3},
{cons,28,
{integer,28,4},
{cons,28,
{integer,28,5},
{cons,28,{integer,28,6},{nil,28}}}}}}}]}]},
{function,30,k,1,
[{clause,30,
[{var,30,'Num'}],
[[{op,30,'>',{var,30,'Num'},{integer,30,1000}}]],
[{atom,31,bigger_than_100}]},
{clause,32,[{var,32,'Num'}],[],[{atom,33,whatever}]}]},
{function,36,call,1,
[{clause,36,
[{integer,36,1000}],
[],
[{call,37,{atom,37,k},[{integer,37,1000}]}]},
{clause,38,
[{integer,38,1002}],
[],
[{call,39,{remote,39,{atom,39,erlang},{atom,39,now}},[]}]}]},
{eof,41}]
{function,18,test,1,
[{clause,18,[{nil,18}],[],[{atom,19,empty}]},
{clause,20,[{atom,20,abc}],[],[{string,21,"atom test"}]}]},
Clojure 1.4.0
user=> (defn make_a_set
([x] #{x})
([x,y] #{x,y}))
#'user/make_a_set
user=> (make_a_set 12)
#{12}
user=> (make_a_set 12 23)
#{12 23}
user=>
[[{op,30,'>',{var,30,'Num'},{integer,30,1000}}]].
7> E= fun(Code)-> {_,Tokens,_}=erl_scan:string(Code),rp(erl_parse:parse_exprs(Tokens)) end.
#Fun<erl_eval.6.80484245>
8> E(" A = lists:seq(1,10).").
{ok,[{match,1,
{var,1,'A'},
{call,1,
{remote,1,{atom,1,lists},{atom,1,seq}},
[{integer,1,1},{integer,1,10}]}}]}
ok
10>
Eshell V5.10.2 (abort with ^G)
1> E= fun(Code)-> {_,Tokens,_}=erl_scan:string(Code),rp(erl_parse:parse_exprs(Tokens)) end.
#Fun<erl_eval.6.80484245>
2> E("[Item || Item<- [1,2,3,4],Item>2 ].").
{ok,[{lc,1,
{var,1,'Item'},
[{generate,1,
{var,1,'Item'},
{cons,1,
{integer,1,1},
{cons,1,
{integer,1,2},
{cons,1,{integer,1,3},{cons,1,{integer,1,4},{nil,1}}}}}},
{op,1,'>',{var,1,'Item'},{integer,1,2}}]}]}
ok
3> 7> E= fun(Code)-> {_,Tokens,_}=erl_scan:string(Code),rp(erl_parse:parse_exprs(Tokens)) end.
#Fun<erl_eval.6.80484245>
8> E("<<A:8,B/binary>> = <<1,2,3,4>>.").
{ok,[{match,1,
{bin,1,
[{bin_element,1,{var,1,'A'},{integer,1,8},default},
{bin_element,1,{var,1,'B'},default,[binary]}]},
{bin,1,
[{bin_element,1,{integer,1,1},default,default},
{bin_element,1,{integer,1,2},default,default},
{bin_element,1,{integer,1,3},default,default},
{bin_element,1,{integer,1,4},default,default}]}}]}
ok
4> E("[a,b,c,d].").
{ok,[{cons,1,
{atom,1,a},
{cons,1,
{atom,1,b},
{cons,1,{atom,1,c},{cons,1,{atom,1,d},{nil,1}}}}}]}
ok

Run! Run!
> {ok, MTs, _} = erl_scan:string("-module(t).").
{ok,[{'-',},
{atom,,module},
{'(',},
{atom,,t},
{')',},
{dot,}],
}
> {ok, ETs, _} = erl_scan:string("-export([say/0]).").
{ok,[{'-',},
{atom,,export},
{'(',},
{'[',},
{atom,,say},
{'/',},
{integer,,},
{']',},
{')',},
{dot,}],
}
> {ok, FTs, _} = erl_scan:string("say() -> \"hello_world!!\".").
{ok,[{atom,,say},
{'(',},
{')',},
{'->',},
{string,,"hello_world!!"},
{dot,}],
}
> Forms= [begin {ok,R}=erl_parse:parse_form(Item),R end || Item<-[MTs,ETs,FTs]].
[{attribute,,module,t},
{attribute,,export,[{say,}]},
{function,,say,,
[{clause,,[],[],[{string,,"hello_world!!"}]}]}]
> {ok, t, Bin} = compile:forms(Forms).
{ok,t,
<<,,,,,,,,,,,,,,,,,,,
,,,,,,,...>>}
> code:load_binary(t,"nofile",Bin).
{module,t}
> t:say().
"hello_world!!"
>

[Erlang 0110] Erlang Abstract Format , Part 1的更多相关文章
- [Erlang 0111] Erlang Abstract Format , Part 2
上回书,我们说到飞天玉虎蒋伯芳来到蜈蚣岭,不是,重来,上回咱们说到可以在Erlang Shell里面手工构造,加载并调用一个模块.在那个demo里面,我把多个Form单独生成出来,最后放在一起做 ...
- [Erlang 0129] Erlang 杂记 VI
把之前阅读资料的时候记下的东西,整理了一下. Adding special-purpose processor support to the Erlang VM P23 简单介绍了Erlang C ...
- [Erlang 0124] Erlang Unicode 两三事 - 补遗
最近看了Erlang User Conference 2013上patrik分享的BRING UNICODE TO ERLANG!视频,这个分享很好的梳理了Erlang Unicode相关的问题,基本 ...
- [Erlang 0105] Erlang Resources 小站 2013年1月~6月资讯合集
很多事情要做,一件一件来; Erlang Resources 小站 2013年1月~6月资讯合集,方便检索. 小站地址: http://site.douban.com/204209/ ...
- [Erlang 0122] Erlang Resources 2014年1月~6月资讯合集
虽然忙,有些事还是要抽时间做; Erlang Resources 小站 2014年1月~6月资讯合集,方便检索. 小站地址: http://site.douban.com/204209/ ...
- Erlang 103 Erlang分布式编程
Outline 笔记系列 Erlang环境和顺序编程Erlang并发编程Erlang分布式编程YawsErlang/OTP 日期 变更说明 2014-11-23 A Outl ...
- [Erlang 0057] Erlang 排错利器: Erlang Crash Dump Viewer
http://www.cnblogs.com/me-sa/archive/2012/04/28/2475556.html Erlang Crash Dump Viewer真的是排错的天兵神器,还记得我 ...
- [Erlang 0118] Erlang 杂记 V
我在知乎回答问题不多,这个问题: "对你职业生涯帮助最大的习惯是什么?它是如何帮助你的?",我还是主动回答了一下. 做笔记 一开始笔记软件做的不好的时候就发邮件给自己, ...
- [Erlang 0107] Erlang实现文本截断
抽时间处理一下之前积压的一些笔记.前段时间有网友 @稻草人 问字符串截断的问题"各位大侠 erlang截取字符串一般用哪个函数啊",有人支招用string:substr/3, ...
随机推荐
- Fedora 22中的RPM软件包管理工具
Introduction The RPM Package Manager (RPM) is an open packaging system that runs on Fedora as well a ...
- 【Java并发编程实战】-----“J.U.C”:ReentrantReadWriteLock
ReentrantLock实现了标准的互斥操作,也就是说在某一时刻只有有一个线程持有锁.ReentrantLock采用这种独占的保守锁直接,在一定程度上减低了吞吐量.在这种情况下任何的"读/ ...
- CSharpGL(19)用glReadPixels把渲染的内容保存为PNG图片(C#)
CSharpGL(19)用glReadPixels把渲染的内容保存为PNG图片(C#) 效果图 本文解决了将OpenGL渲染出来的内容保存到PNG图片的方法. 下载 CSharpGL已在GitHub开 ...
- CSharpGL(15)用GLSL渲染2种类型的文字
CSharpGL(15)用GLSL渲染2种类型的文字 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharpGL源码中包含10多个独立的Demo,更适合 ...
- ABP框架 - 动态Web Api层
文档目录 本节内容: 创建动态Web Api控制器 ForAll 方法 重写 ForAll ForMethods Http 动词 WithVerb 方法 HTTP 特性 命名约定 Api 浏览器 Re ...
- C++ std::deque
std::deque template < class T, class Alloc = allocator > class deque; Double ended queue deque ...
- Javascript之变量作用域
分析: 无论是强类型语言c#.c++.java等语言,还是弱类型语言如Javascript,所有变量可以抽象为两种类型,即局部变量和全局变量. 全局变量:整个作用域可见. 局部变量:局部可见,退出作用 ...
- (实例篇)PHP实现HTTP断点续传的方法
PHP实现HTTP断点续传的方法. <?php /** * PHP-HTTP断点续传实现 * @param string $path: 文件所在路径 * @param string $file: ...
- 一起学微软Power BI系列-使用技巧(2)连接Excel数据源错误解决方法
上一篇文章一起学微软Power BI系列-使用技巧(1)连接Oracle与Mysql数据库中,我们介绍了Power BI Desktop中连接Oracle和Mysql的方法,其实说到底还是驱动的问题, ...
- Java开发中的23种设计模式详解(转)
设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...