Skip to content

Commit 888f663

Browse files
committed
unwrap_fun
1 parent 4af2b1a commit 888f663

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

src/fexpr.jl

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
export unwrap_fun, wrap_fun, unwrap_head, wrap_head, unwrap_fcall, wrap_fcall
2+
################################################################
3+
"""
4+
head, body = unwrap_fun(fexpr)
5+
fcall, wherestack, body = unwrap_fun(fexpr,true)
6+
f, args, wherestack, body = unwrap_fun(fexpr, true, true)
7+
8+
Unwraps function expression.
9+
"""
10+
function unwrap_fun(expr::Expr)
11+
if expr.head in (:function, :(=))
12+
fexpr = expr
13+
elseif expr.head == :block
14+
fexpr = expr.args[2] # separate fexpr from block
15+
else
16+
error("Expression is not supported")
17+
end
18+
19+
head = fexpr.args[1]
20+
body = fexpr.args[2]
21+
return head, body
22+
end
23+
24+
function unwrap_fun(expr::Expr, should_unwrap_head::Bool)
25+
if expr.head in (:function, :(=))
26+
fexpr = expr
27+
elseif expr.head == :block
28+
fexpr = expr.args[2] # separate fexpr from block
29+
else
30+
error("Expression is not supported")
31+
end
32+
33+
head = fexpr.args[1]
34+
fcall, wherestack = unwrap_head(head)
35+
body = fexpr.args[2]
36+
return fcall, wherestack, body
37+
end
38+
39+
function unwrap_fun(expr::Expr, should_unwrap_head::Bool, should_unwrap_fcall::Bool)
40+
if expr.head in (:function, :(=))
41+
fexpr = expr
42+
elseif expr.head == :block
43+
fexpr = expr.args[2] # separate fexpr from block
44+
else
45+
error("Expression is not supported")
46+
end
47+
48+
head = fexpr.args[1]
49+
fcall, wherestack = unwrap_head(head)
50+
f, args = unwrap_fcall(fcall)
51+
52+
body = fexpr.args[2]
53+
return f, args, wherestack, body
54+
end
55+
################################################################

0 commit comments

Comments
 (0)