Andelf Blog

A blogging framework for hackers.

Rust Syntax Extensions

All rust ASTs are in syntax::ast. And extensions are in syntax::ext.

1
2
3
4
5
6
7
8
extern crate syntax;

use syntax::ast;
use syntax::ast::Name;
use syntax::ext::base::SyntaxExtension;

#[macro_registrar]
pub fn macro_registrar(register: |Name, SyntaxExtension|);

where:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
pub enum SyntaxExtension {
    ItemDecorator(ItemDecorator),
    ItemModifier(ItemModifier),
    NormalTT(~MacroExpander:'static, Option<Span>),
    IdentTT(~IdentMacroExpander:'static, Option<Span>),
}
// and

type ItemDecorator = fn(&mut ExtCtxt, Span, @MetaItem, @Item, |@Item|);
type ItemModifier = fn(&mut ExtCtxt, Span, @MetaItem, @Item) -> @Item;

pub trait MacroExpander {
    fn expand(&self, ecx: &mut ExtCtxt, span: Span, token_tree: &[TokenTree]) -> ~MacResult;
}

pub trait IdentMacroExpander {
    fn expand(&self, cx: &mut ExtCtxt, sp: Span, ident: Ident, token_tree: Vec<TokenTree>) -> ~MacResult;
}

Erlang 的一些好玩 Tips

可以模拟OO, 模块级别的继承.

-extends(parent).

反编译 beam, 对加密过或者 hipe 的似乎无效.

1
2
{ok,{_,[{abstract_code,{_,AC}}]}} = beam_lib:chunks(Beam,[abstract_code]).
io:fwrite("~s~n", [erl_prettypr:format(erl_syntax:form_list(AC))]).

语句块, 比较少用到.

1
2
3
begin
   ...
end

Parameterised Modules 参数化模块, 也是模拟 OO 的. 很多 web 框架都这么用, 不过这货在 R16 被去掉了, 也是… 其实挺恶心.

-module(xxx, [Arg1, Arg2]).

Stateful Modules, 用于替换 Parameterised Modules. 将模块和对应状态保存在 tuple 里, 看起来像是调用某一对象的方法一样.

1
2
3
4
X = {Mod, P1, P2, ..., Pn},
X:Func(A1, A2, ..., An).
%% 等价于
Mod:Func(A1, A2, ..., An, X).

所以 web 框架就转为使用这个特性了, 看起来很无害而且不反 FP.

Hello World!

1
main = putStrLn "Hello World"