@@ -18,15 +18,14 @@ const BIG_ENDIAN = (ENDIAN_BOM == 0x01020304)
18
18
Base. parse (:: Type{Expr} , args... ; kwargs... ) =
19
19
Meta. parse (args... ; kwargs... )
20
20
21
- export @api , @def , V6_COMPAT, BIG_ENDIAN
21
+ export @api , V6_COMPAT, BIG_ENDIAN
22
22
23
- macro def (name, definition)
23
+ _api_def (name, definition) =
24
24
quote
25
25
macro $ (esc (name))()
26
26
esc ($ (Expr (:quote , definition)))
27
27
end
28
28
end
29
- end
30
29
31
30
const SymSet = Set{Symbol}
32
31
71
70
""" Get current module"""
72
71
cur_mod () = ccall (:jl_get_current_module , Ref{Module}, ())
73
72
73
+ m_eval (mod, expr) = Core. eval (mod, expr)
74
+ m_eval (expr) = m_eval (cur_mod (), expr)
75
+
74
76
"""
75
77
@api <cmd> [<symbols>...]
76
78
@@ -89,6 +91,9 @@ cur_mod() = ccall(:jl_get_current_module, Ref{Module}, ())
89
91
* @api public <names...> # Add functions that are part of the public API
90
92
* @api develop <names...> # Add functions that are part of the development API
91
93
* @api modules <names...> # Add submodule names that are part of the API
94
+
95
+ * @api def <name> <expr> # Same as the @def macro, creates a macro with the given name
96
+
92
97
"""
93
98
macro api (cmd:: Symbol )
94
99
mod = @static V6_COMPAT ? current_module () : __module__
@@ -97,20 +102,14 @@ macro api(cmd::Symbol)
97
102
error (" @api unrecognized command: $cmd " )
98
103
end
99
104
100
- function _api_display (api:: AbstractAPI )
101
- show (api)
102
- println ()
103
- end
105
+ _api_display (mod, nam) =
106
+ isdefined (mod, nam) && (api = m_eval (mod, nam)) != = nothing && (show (api) ; println ())
104
107
105
- function _api_list (mod:: Module )
106
- isdefined (mod, :__api__ ) && _api_display (eval (mod, :__api__ ))
107
- isdefined (mod, :__tmp_api__ ) && _api_display (eval (mod, :__tmp_api__ ))
108
- nothing
109
- end
108
+ _api_list (mod:: Module ) = (_api_display (mod, :__api__ ) ; _api_display (mod, :__tmp_api__ ))
110
109
111
110
function _api_freeze (mod:: Module )
112
111
ex = :( global const __api__ = APITools. API (__tmp_api__) ; __tmp_api__ = nothing )
113
- isdefined (mod, :__tmp_api__ ) && eval (mod, :( __tmp_api__ != = nothing ) ) && eval (mod, ex)
112
+ isdefined (mod, :__tmp_api__ ) && m_eval (mod, :( __tmp_api__ != = nothing ) ) && m_eval (mod, ex)
114
113
nothing
115
114
end
116
115
@@ -131,23 +130,23 @@ function _add_def!(curmod, grp, exp)
131
130
error (" @api $grp : syntax error $exp " )
132
131
end
133
132
if isdefined (Base, sym)
134
- eval (curmod, :(import Base.$ sym ))
135
- eval (curmod, :(push! (__tmp_api__. base, $ (QuoteNode (sym)))))
133
+ m_eval (curmod, :(import Base.$ sym ))
134
+ m_eval (curmod, :(push! (__tmp_api__. base, $ (QuoteNode (sym)))))
136
135
else
137
- eval (curmod, :(function $ sym end ))
138
- eval (curmod, :(push! (__tmp_api__. public!, $ (QuoteNode (sym)))))
136
+ m_eval (curmod, :(function $ sym end ))
137
+ m_eval (curmod, :(push! (__tmp_api__. public!, $ (QuoteNode (sym)))))
139
138
end
140
139
end
141
140
142
141
""" Add symbols"""
143
142
function _add_symbols (curmod, grp, exprs)
144
143
if debug[]
145
144
print (" _add_symbols($curmod , $grp , $exprs )" )
146
- isdefined (curmod, :__tmp_api__ ) && print (" => " , eval (curmod, :__tmp_api__ ))
145
+ isdefined (curmod, :__tmp_api__ ) && print (" => " , m_eval (curmod, :__tmp_api__ ))
147
146
println ()
148
147
end
149
148
ex = :( export @api , APITools ; global __tmp_api__ = APITools. TMP_API ($ curmod) )
150
- isdefined (curmod, :__tmp_api__ ) || eval (curmod, ex)
149
+ isdefined (curmod, :__tmp_api__ ) || m_eval (curmod, ex)
151
150
if grp == :base!
152
151
for ex in exprs
153
152
if isa (ex, Expr) && ex. head == :tuple
@@ -173,14 +172,14 @@ function _add_symbols(curmod, grp, exprs)
173
172
end
174
173
if grp == :base
175
174
for sym in symbols
176
- eval (curmod, :( import Base.$ sym ))
175
+ m_eval (curmod, :( import Base.$ sym ))
177
176
end
178
177
end
179
178
for sym in symbols
180
- eval (curmod, :( push! (__tmp_api__.$ grp, $ (QuoteNode (sym)) )))
179
+ m_eval (curmod, :( push! (__tmp_api__.$ grp, $ (QuoteNode (sym)) )))
181
180
end
182
181
end
183
- debug[] && println (" after add symbols: " , eval (curmod, :__tmp_api__ ))
182
+ debug[] && println (" after add symbols: " , m_eval (curmod, :__tmp_api__ ))
184
183
nothing
185
184
end
186
185
@@ -189,9 +188,9 @@ function _api_extend(curmod, modules)
189
188
use = :using
190
189
191
190
for nam in modules
192
- mod = eval (curmod, nam)
191
+ mod = m_eval (curmod, nam)
193
192
if isdefined (mod, :__api__ )
194
- api = eval (mod, :__api__ )
193
+ api = m_eval (mod, :__api__ )
195
194
_do_list (curmod, imp, api, :Base , :base )
196
195
_do_list (curmod, imp, api, nam, :public! )
197
196
_do_list (curmod, imp, api, nam, :develop! )
@@ -207,28 +206,39 @@ end
207
206
208
207
function _api_use (curmod, modules)
209
208
for nam in modules
210
- mod = eval (curmod, nam)
209
+ mod = m_eval (curmod, nam)
211
210
if isdefined (mod, :__api__ )
212
- api = eval (mod, :__api__ )
211
+ api = m_eval (mod, :__api__ )
213
212
_do_list (curmod, :using , api, nam, :public )
214
213
_do_list (curmod, :using , api, nam, :public! )
215
214
end
216
215
end
217
216
nothing
218
217
end
219
218
220
- _api_export (curmod, modules) =
221
- esc (Expr (:toplevel ,
222
- [:(eval (Expr ( :export , $ mod. __api__.$ grp... )))
223
- for mod in modules, grp in (:modules , :public , :public! )]. .. ,
224
- nothing ))
219
+ function _api_export (curmod, modules)
220
+ for nam in modules
221
+ mod = m_eval (curmod, nam)
222
+ if isdefined (mod, :__api__ )
223
+ api = m_eval (mod, :__api__ )
224
+ m_eval (curmod, Expr ( :export , getfield (api, :modules )... ))
225
+ m_eval (curmod, Expr ( :export , getfield (api, :public )... ))
226
+ m_eval (curmod, Expr ( :export , getfield (api, :public! )... ))
227
+ end
228
+ end
229
+ nothing
230
+ end
225
231
226
- _api_list (curmod, modules) =
227
- Expr (:toplevel ,
228
- [:(eval (APITools. _api_display ($ mod))) for mod in modules]. .. ,
229
- nothing )
232
+ function _api_list (curmod, modules)
233
+ for nam in modules
234
+ _api_list (m_eval (curmod, nam))
235
+ end
236
+ nothing
237
+ end
230
238
231
239
function _api (curmod:: Module , cmd:: Symbol , exprs)
240
+ cmd == :def && return _api_def (exprs... )
241
+
232
242
ind = _ff (_cmdadd, cmd)
233
243
ind == 0 || return _add_symbols (curmod, cmd, exprs)
234
244
@@ -239,10 +249,10 @@ function _api(curmod::Module, cmd::Symbol, exprs)
239
249
for ex in exprs
240
250
if isa (ex, Expr) && ex. head == :tuple
241
251
push! (modules, ex. args... )
242
- for sym in ex. args ; eval (curmod, :(import $ sym)) ; end
252
+ for sym in ex. args ; m_eval (curmod, :(import $ sym)) ; end
243
253
elseif isa (ex, Symbol)
244
254
push! (modules, ex)
245
- eval (curmod, :(import $ ex))
255
+ m_eval (curmod, :(import $ ex))
246
256
else
247
257
error (" @api $cmd : syntax error $ex " )
248
258
end
@@ -253,14 +263,14 @@ function _api(curmod::Module, cmd::Symbol, exprs)
253
263
cmd == :list && return _api_list (curmod, modules)
254
264
255
265
for nam in modules
256
- mod = eval (curmod, nam)
257
- for sym in getfield (eval (mod, :__api__ ), :modules )
258
- eval (curmod, :(using $ nam.$ sym))
266
+ mod = m_eval (curmod, nam)
267
+ for sym in getfield (m_eval (mod, :__api__ ), :modules )
268
+ m_eval (curmod, :(using $ nam.$ sym))
259
269
end
260
270
end
261
271
262
272
# Be nice and set up standard Test
263
- cmd == :test && eval (curmod, V6_COMPAT ? :(using Base. Test) : :(using Test))
273
+ cmd == :test && m_eval (curmod, V6_COMPAT ? :(using Base. Test) : :(using Test))
264
274
265
275
cmd == :use ? _api_use (curmod, modules) : _api_extend (curmod, modules)
266
276
end
@@ -274,13 +284,13 @@ function _do_list(curmod, cmd, api, mod, grp)
274
284
for nam in lst
275
285
exp = Expr (cmd, mod, nam)
276
286
debug[] && println (" V6: $cmd , $mod , $mod , $exp " )
277
- eval (curmod, exp)
287
+ m_eval (curmod, exp)
278
288
end
279
289
else
280
290
exp = Expr (cmd, Expr (:(:), _dot_name (mod), _dot_name .(lst)... ))
281
291
debug[] && println (" V7: $cmd , $mod , $mod , $exp " )
282
292
try
283
- eval (curmod, exp)
293
+ m_eval (curmod, exp)
284
294
catch ex
285
295
println (" APITools: Error evaluating $exp " )
286
296
dump (exp)
0 commit comments