[Erlang 0111] Erlang Abstract Format , Part 2
erl_syntax
Eshell V5.10.2 (abort with ^G)
1> Code = "-module(t).\n-export([say/0]).\n\nsay() -> \"Hello world!!\". ".
"-module(t).\n-export([say/0]).\n\nsay() -> \"Hello world!!\". "
2> {ok,Tokens,_} =erl_scan:string(Code).
{ok,[{'-',1},
{atom,1,module},
{'(',1},
{atom,1,t},
{')',1},
{dot,1},
{'-',2},
{atom,2,export},
{'(',2},
{'[',2},
{atom,2,say},
{'/',2},
{integer,2,0},
{']',2},
{')',2},
{dot,2},
{atom,4,say},
{'(',4},
{')',4},
{'->',4},
{string,4,"Hello world!!"},
{dot,4}],
4}
4> erl_scan:tokens([],Code,1).
{done,{ok,[{'-',1},
{atom,1,module},
{'(',1},
{atom,1,t},
{')',1},
{dot,1}],
2},
"-export([say/0]).\n\nsay() -> \"Hello world!!\". "}
Eshell V5.10.2 (abort with ^G)
1> Code = "-module(t).\n-export([say/0]).\n\nsay() -> \"Hello world!!\". ".
"-module(t).\n-export([say/0]).\n\nsay() -> \"Hello world!!\". "
2> dynamic_compile:load_from_string(Code).
{module,t}
3> t:say().
"Hello world!!"
4>
Eshell V5.10.2 (abort with ^G)
1> M = erl_syntax:attribute(erl_syntax:atom(module),[erl_syntax:atom(t)]).
{tree,attribute,
{attr,0,[],none},
{attribute,{tree,atom,{attr,0,[],none},module},
[{tree,atom,{attr,0,[],none},t}]}}
2> MF = erl_syntax:revert(M).
{attribute,0,module,t}
3> E = erl_syntax:attribute(erl_syntax:atom(export),[erl_syntax:list([erl_syntax:arity_qualifier(erl_syntax:atom(say),erl_syntax:integer(0))])]).
{tree,attribute,
{attr,0,[],none},
{attribute,
{tree,atom,{attr,0,[],none},export},
[{tree,list,
{attr,0,[],none},
{list,
[{tree,arity_qualifier,
{attr,0,[],none},
{arity_qualifier,
{tree,atom,{attr,0,[],none},say},
{tree,integer,{attr,0,[],none},0}}}],
none}}]}}
4> ExportForm = erl_syntax:revert(E).
{attribute,0,export,[{say,0}]}
5>
5>
5> Clause= erl_syntax:clause([],[],[erl_syntax:atom(hello_world)]).
{tree,clause,
{attr,0,[],none},
{clause,[],none,[{tree,atom,{attr,0,[],none},hello_world}]}}
6>
6> Function = erl_syntax:function(erl_syntax:atom(say),[Clause]).
{tree,function,
{attr,0,[],none},
{func,{tree,atom,{attr,0,[],none},say},
[{tree,clause,
{attr,0,[],none},
{clause,[],none,
[{tree,atom,{attr,0,[],none},hello_world}]}}]}}
7> FunctionForm = erl_syntax:revert(Function).
{function,0,say,0,[{clause,0,[],[],[{atom,0,hello_world}]}]}
8> {ok, Mod, Bin1} = compile:forms([MF,ExportForm, FunctionForm]).
{ok,t,
<<70,79,82,49,0,0,1,208,66,69,65,77,65,116,111,109,0,0,0,
57,0,0,0,6,1,116,...>>}
9> t:say().
** exception error: undefined function t:say/0
10> code:load_binary(Mod, [], Bin1).
{module,t}
11> t:say().
hello_world
12>
test_smerl() ->
M1 = smerl:new(foo),
{ok, M2} = smerl:add_func(M1, "bar() -> 1 + 1."),
smerl:compile(M2),
foo:bar(), % returns 2``
smerl:has_func(M2, bar, 0). % returns true
parse_transform
Causes the parse transformation function Module:parse_transform/2 to be applied to the parsed code before the code is checked for errors.
-module(print_form).
-export([parse_transform/2]). parse_transform(Forms, _Options) ->
io:format("forms: ~p~n", [Forms]),
Forms.
[root@nimbus demo]# cat a.erl
-module(a).
-compile({parse_transform,print_form}).
-export([test/0]). test()->
"hello world!". [root@nimbus demo]# erlc -o . -pa . a.erl
forms: [{attribute,1,file,{"a.erl",1}},
{attribute,1,module,a},
{attribute,3,export,[{test,0}]},
{function,5,test,0,[{clause,5,[],[],[{string,6,"hello world!"}]}]},
{eof,10}]

[Erlang 0111] Erlang Abstract Format , Part 2的更多相关文章
- [Erlang 0110] Erlang Abstract Format , Part 1
Erlang Abstract Format并不难懂,只是枯燥一点罢了,如果把Abstract Format的文档翻译出来,其实就是Erlang教科书中语法入门的部分. Erlang Abstract ...
- [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, ...
随机推荐
- stanford corenlp自定义切词类
stanford corenlp的中文切词有时不尽如意,那我们就需要实现一个自定义切词类,来完全满足我们的私人定制(加各种词典干预).上篇文章<IKAnalyzer>介绍了IKAnalyz ...
- 利用Docker技术实现UDP广播效果(网络编程python版)
docker的安装见官方文档 我使用的系统为Ubuntu16.04 Ubuntu系统安装docker文档地址:https://docs.docker.com/engine/installation/l ...
- 查看Job执行的历史记录
SQL Server将Job的信息存放在msdb中,Schema是dbo,表名以“sysjob”开头. 一,基础表 1, 查看Job和Step,Step_ID 是从1 开始的. select j.jo ...
- PHP 面向对象编程和设计模式 (1/5) - 抽象类、对象接口、instanceof 和契约式编程
PHP高级程序设计 学习笔记 2014.06.09 什么是面向对象编程 面向对象编程(Object Oriented Programming,OOP)是一种计算机编程架构.OOP 的一条基本原则是计算 ...
- [摘录]第三部分 IBM文化(2)
第二十二章 原则性领导 在一个组织程序已经变得不受其来源和内容的约束,而且其编纂出来的组织宗旨已经代替了个人责任的组织之中,你所面临的首要任务,就是要全盘抹掉这个程序本身,以便让整个封闭的系统呼吸新鲜 ...
- 【目录】Zookeeper目录
Zookeeper的目录整理如下 1. [分布式]分布式架构 2. [分布式]一致性协议 3. [分布式]Chubby与Paxos 4. [分布式]Zookeeper与Paxos 5. [分布式]Zo ...
- Spark的DataFrame的窗口函数使用
作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 SparkSQL这块儿从1.4开始支持了很多的窗口分析函数,像row_number这些,平时写程 ...
- [Spring]04_最小化Spring XML配置
4.1 自动装配 Bean Spring 装配 bean 时,有时非常明确,就是需要将某个 bean 的引用装配给指定属性. 例如,若应用上下文中只有一个 javax.sql.DataSource 类 ...
- 打造高效前端工作环境 - tmux
打造高效前端工作环境 - tmux 前言 现在前端开发可不容易啊,先打开个VIM,然后再打开个lite-server,一不小心写个ES2015还要打开个gulp来做预编译,如果能把这么多个窗口放在一 ...
- javascript性能优化:创建javascript无阻塞脚本
javaScript 在浏览器中的运行性能,在web2.0时代显得尤为重要,成千上万行javaScript代码无疑会成为性能杀手, 在较低版本的浏览器执行JavaScript代码的时候,由于浏览器只使 ...