forked from NetComposer/nklib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nklib_code.erl
438 lines (337 loc) · 12.7 KB
/
nklib_code.erl
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
%% -------------------------------------------------------------------
%%
%% Copyright (c) 2019 Carlos Gonzalez Florido. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------
%% @doc NetComposer Erlang code parser and hot loader utilities
-module(nklib_code).
-author('Carlos Gonzalez <[email protected]>').
-export([expression/1, getter/2, getter_args/4, fun_expr/4, call_expr/4, callback_expr/3]).
-export([case_expr/5, case_expr_ok/5, export_attr/1, compile/2, do_compile/3, write/3]).
-export([get_funs/1]).
-export([forms_find_attribute/2, forms_add_attributes/2, forms_get_attributes/1,
forms_find_exported/1, forms_get_funs/1, forms_add_funs/2,
forms_replace_fun/4, forms_print/1]).
-include_lib("syntax_tools/include/merl.hrl").
%% ===================================================================
%% Private
%% ===================================================================
%% @doc Parses an erlang expression intro a syntaxTree()
%% i.e. expres
-spec expression(string()) ->
{ok, erl_syntax:syntaxTree()} | error.
expression(Expr) ->
case erl_scan:string(Expr) of
{ok, Tokens, _} ->
case erl_parse:parse_form(Tokens) of
{ok, Form} -> {ok, Form};
_ -> error
end;
_ ->
error
end.
%% @doc Generates a getter function (fun() -> Value.)
-spec getter(atom(), term()) ->
erl_syntax:syntaxTree().
getter(Fun, Value) ->
try
erl_syntax:function(
erl_syntax:atom(Fun),
[erl_syntax:clause([], none, [erl_syntax:abstract(Value)])])
catch
error:Error -> lager:warning("Could not make getter for ~p", [Value]),
error(Error)
end.
%% @doc Generates a getter function (fun(X, Y, Z) -> Value; fun(_, _, _) -> Default
% If Default=none, not catch-all clause will be added
-spec getter_args(atom(), integer(), [{[term()], term()}], term()|none) ->
erl_syntax:syntaxTree().
getter_args(Fun, Arity, ArgsValues, Default) ->
try
erl_syntax:function(
erl_syntax:atom(Fun),
[
erl_syntax:clause(
[erl_syntax:abstract(Arg) || Arg <- Args],
none, [erl_syntax:abstract(Val)])
||
{Args, Val} <- ArgsValues, length(Args)==Arity
]
++
case Default of
none ->
[];
_ ->
[
erl_syntax:clause(
[erl_syntax:variable('_') || _ <- lists:seq(1,Arity)],
none, [erl_syntax:abstract(Default)])
]
end)
catch
error:Error -> lager:warning("Could not make getter for ~p", [ArgsValues]),
error(Error)
end.
%% @doc Generates a function expression (fun(A1,B1,..) -> Value)
%% Vers represents the suffix to use in the variable names
-spec fun_expr(atom(), integer(), integer(), term()) ->
erl_syntax:syntaxTree().
fun_expr(Fun, Arity, Vers, Value) ->
erl_syntax:function(
erl_syntax:atom(Fun),
[erl_syntax:clause(var_list(Arity, Vers), none, Value)]).
%% @doc Generates a call expression (mod:fun(A1,B1,..))
%% Vers represents the suffix to use in the variable names.
-spec call_expr(atom(), atom(), integer(), integer()) ->
erl_syntax:syntaxTree().
call_expr(Mod, Fun, Arity, Vers) ->
erl_syntax:application(
case Mod of none -> none; _ -> erl_syntax:atom(Mod) end,
erl_syntax:atom(Fun),
var_list(Arity, Vers)).
%% @doc Generates a call expression (fun(A0,B0...) -> mod:fun(A0,B0,..))
-spec callback_expr(atom(), atom(), integer()) ->
erl_syntax:syntaxTree().
callback_expr(Mod, Fun, Arity) ->
fun_expr(Fun, Arity, 0, [call_expr(Mod, Fun, Arity, 0)]).
%% @doc Generates a case expression
%% case mod:fun(A2,B2...) of
%% continue -> [A1,B1..] = [A2,B2..], (NextCode);
%% {continue, (NextCode);
%% Other -> Other
%% end
%% Vers represents the suffix to use in the variable names.
-spec case_expr(atom(), atom(), integer(), integer(),
[erl_syntax:syntaxTree()]) ->
erl_syntax:syntaxTree().
case_expr(Mod, Fun, Arity, Vers, NextCode) ->
erl_syntax:case_expr(
call_expr(Mod, Fun, Arity, Vers),
[
erl_syntax:clause(
[erl_syntax:atom(continue)],
none,
case Arity of
0 ->
NextCode;
_ ->
[
erl_syntax:match_expr(
erl_syntax:list(var_list(Arity, Vers-1)),
erl_syntax:list(var_list(Arity, Vers)))
| NextCode]
end),
erl_syntax:clause(
[erl_syntax:tuple([
erl_syntax:atom(continue),
erl_syntax:list(var_list(Arity, Vers-1))])],
none,
NextCode),
erl_syntax:clause(
[erl_syntax:variable('Other')],
none,
[erl_syntax:variable('Other')])
]).
%% @doc Generates a case expression
%% case mod:fun(A2,B2) of
%% ok -> [A1,B1..] = [A2,B2..], (NextCode);
%% {ok, B1} -> [A1=A2], (NextCode);
%% Other -> Other
%% end
%% Vers represents the suffix to use in the variable names.
-spec case_expr_ok(atom(), atom(), integer(), integer(),
[erl_syntax:syntaxTree()]) ->
erl_syntax:syntaxTree().
case_expr_ok(Mod, Fun, 2, Vers, NextCode) ->
erl_syntax:case_expr(
call_expr(Mod, Fun, 2, Vers),
[
erl_syntax:clause(
[erl_syntax:atom(ok)],
none,
[
erl_syntax:match_expr(
erl_syntax:list(var_list(2, Vers-1)),
erl_syntax:list(var_list(2, Vers)))
| NextCode]),
erl_syntax:clause(
[erl_syntax:tuple([
erl_syntax:atom(ok),
erl_syntax:variable([$B|integer_to_list(Vers-1)])])],
none,
[
erl_syntax:match_expr(
erl_syntax:list(var_list(1, Vers-1)),
erl_syntax:list(var_list(1, Vers)))
| NextCode]),
erl_syntax:clause(
[erl_syntax:variable('Other')],
none,
[erl_syntax:variable('Other')])
]).
%% @doc
export_attr(List) ->
erl_syntax:attribute(
erl_syntax:atom(export),
[erl_syntax:list(
[
erl_syntax:arity_qualifier(
erl_syntax:atom(Name), erl_syntax:integer(Arity))
|| {Name, Arity} <- List
]
)]).
%% @doc Compiles a syntaxTree into a module
-spec compile(atom(), [erl_syntax:syntaxTree()]) ->
ok | {error, term()}.
compile(Mod, Tree) ->
Tree1 = [
erl_syntax:attribute(
erl_syntax:atom(module),
[erl_syntax:atom(Mod)]),
erl_syntax:attribute(
erl_syntax:atom(compile),
[erl_syntax:list([erl_syntax:atom(nowarn_export_all)])]),
erl_syntax:attribute(
erl_syntax:atom(compile),
[erl_syntax:list([erl_syntax:atom(export_all)])])
| Tree
],
do_compile(Mod, Tree1, [report_errors, report_warnings, return_errors]).
%% @doc Compiles a syntaxTree into a module
-spec do_compile(atom(), [erl_syntax:syntaxTree()], list()) ->
ok | {error, term()}.
do_compile(Mod, Tree, Opts) ->
%% io:format("\nGenerated ~p:\n\n", [Mod]),
%% [io:format("~s\n\n", [erl_prettypr:format(S)]) || S<-Tree],
Forms1 = [erl_syntax:revert(X) || X <- Tree],
case compile:noenv_forms(Forms1, Opts) of
{ok, Mod, Bin} ->
code:purge(Mod),
File = atom_to_list(Mod)++".erl",
case code:load_binary(Mod, File, Bin) of
{module, Mod} ->
{ok, Tree};
Error ->
{error, Error}
end;
Error ->
{error, Error}
end.
%% @doc Writes a generated tree as a standard erlang file
-spec write(atom(), [erl_syntax:syntaxTree()], string()) ->
ok | {error, term()}.
write(Mod, Tree, BasePath) ->
Path = filename:join(BasePath, nklib_util:to_list(Mod)++".erl"),
Content = list_to_binary(
[io_lib:format("~s\n\n", [erl_prettypr:format(S)]) || S <-Tree]),
file:write_file(Path, Content).
%% @doc Gets the list of exported functions of a module
-spec get_funs(atom()) ->
[{atom(), integer()}] | error.
get_funs(Mod) ->
case catch Mod:module_info() of
List when is_list(List) ->
lists:foldl(
fun({Fun, Arity}, Acc) ->
case Fun of
module_info -> Acc;
behaviour_info -> Acc;
_ -> [{Fun, Arity}|Acc]
end
end,
[],
nklib_util:get_value(exports, List));
_ ->
error
end.
%% ===================================================================
%% Forms utilities
%% ===================================================================
%% @doc Finds the module attribute
forms_find_attribute(_Key, []) ->
undefined;
forms_find_attribute(Key, [{attribute, _, Key, Value}|_]) ->
Value;
forms_find_attribute(Key, [_|Rest]) ->
forms_find_attribute(Key, Rest).
%% @private Adds an attribute at the end of the attribute list
forms_add_attributes(Attrs, Forms) ->
forms_add_attributes(Forms, Attrs, []).
%% @private
forms_add_attributes([{attribute, _, _, _}=Attr|Rest], Attrs, Acc) ->
forms_add_attributes(Rest, Attrs, [Attr|Acc]);
forms_add_attributes(Rest, Attrs, Acc) ->
lists:reverse(Acc) ++ Attrs ++ Rest.
%% @private Gets all attributes
forms_get_attributes(Forms) ->
forms_get_attributes(Forms, []).
%% @private
forms_get_attributes([], Acc) ->
lists:reverse(Acc);
forms_get_attributes([{attribute, _, _, _}=Attr|Rest], Acc) ->
forms_get_attributes(Rest, [Attr|Acc]);
forms_get_attributes([_|Rest], Acc) ->
forms_get_attributes(Rest, Acc).
%% @private Finds all exported functions
forms_find_exported(Forms) ->
forms_find_exported(Forms, []).
forms_find_exported([], Acc) ->
Acc;
forms_find_exported([{attribute, _, export, List}|Rest], Acc) ->
forms_find_exported(Rest, Acc++List);
forms_find_exported([_|Rest], Acc) ->
forms_find_exported(Rest, Acc).
%% @private Gets all defined funs
forms_get_funs(Forms) ->
forms_get_funs(Forms, #{}).
%% @private
forms_get_funs([], Acc) ->
Acc;
forms_get_funs([{function, _Line, Name, Arity, Spec}|Rest], Acc) ->
forms_get_funs(Rest, Acc#{{Name, Arity}=>Spec});
forms_get_funs([_|Rest], Acc) ->
forms_get_funs(Rest, Acc).
%% @private Adds a number of funs at the end of the module
%% Funs must be [{function, ...}]
forms_add_funs(Funs, Forms) ->
forms_add_funs(Forms, Funs, []).
%% @private
forms_add_funs([{eof, _}|_]=End, Funs, Acc) ->
lists:reverse(Acc) ++ Funs ++ End;
forms_add_funs([Term|Rest], Funs, Acc) ->
forms_add_funs(Rest, Funs, [Term|Acc]).
%% @private
forms_replace_fun(Name, Arity, Spec, Forms) ->
forms_replace_fun(Forms, Name, Arity, Spec, []).
%% @private
forms_replace_fun([], _Name, _Arity, _Spec, Acc) ->
lists:reverse(Acc);
forms_replace_fun([{function, _Line, Name, Arity, _}|Rest], Name, Arity, Spec, Acc) ->
forms_replace_fun(Rest, Name, Arity, Spec, [Spec|Acc]);
forms_replace_fun([Other|Rest], Name, Arity, Spec, Acc) ->
forms_replace_fun(Rest, Name, Arity, Spec, [Other|Acc]).
%% @private
forms_print(Forms) ->
Content = list_to_binary([io_lib:format("~s\n\n", [erl_prettypr:format(S)]) || S <- Forms]),
io:format("Forms: \n~s\n", [Content]).
%% ===================================================================
%% Internal
%% ===================================================================
%% @private Generates a var list (A1,B1..)
var_list(Arity, Vers) ->
VersS = nklib_util:to_list(Vers),
[erl_syntax:variable([V|VersS]) || V <- lists:seq(65, 64+Arity)].