1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
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} |
1
2
3
4
5
6
7
8
9
|
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!!\". " } |
1
2
3
4
5
6
7
8
|
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>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
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>
|
1
2
3
4
5
6
7
|
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 |
1
2
3
4
5
6
|
-module(print_form). -export([parse_transform/2]). parse_transform(Forms,
_Options) -> io:format( "forms:
~p~n" ,
[Forms]), Forms. |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[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}] |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
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} |
1
2
3
4
5
6
7
8
9
|
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!!\". " } |
1
2
3
4
5
6
7
8
|
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>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
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>
|
1
2
3
4
5
6
7
|
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 |
1
2
3
4
5
6
|
-module(print_form). -export([parse_transform/2]). parse_transform(Forms,
_Options) -> io:format( "forms:
~p~n" ,
[Forms]), Forms. |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[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}] |