From 888f6635bcb996f135216f4df358d441b07dbcc3 Mon Sep 17 00:00:00 2001 From: aminya Date: Sun, 9 Feb 2020 18:50:35 -0600 Subject: [PATCH 1/6] unwrap_fun --- src/fexpr.jl | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/fexpr.jl diff --git a/src/fexpr.jl b/src/fexpr.jl new file mode 100644 index 0000000..0d07a5d --- /dev/null +++ b/src/fexpr.jl @@ -0,0 +1,55 @@ +export unwrap_fun, wrap_fun, unwrap_head, wrap_head, unwrap_fcall, wrap_fcall +################################################################ +""" + head, body = unwrap_fun(fexpr) + fcall, wherestack, body = unwrap_fun(fexpr,true) + f, args, wherestack, body = unwrap_fun(fexpr, true, true) + +Unwraps function expression. +""" +function unwrap_fun(expr::Expr) + if expr.head in (:function, :(=)) + fexpr = expr + elseif expr.head == :block + fexpr = expr.args[2] # separate fexpr from block + else + error("Expression is not supported") + end + + head = fexpr.args[1] + body = fexpr.args[2] + return head, body +end + +function unwrap_fun(expr::Expr, should_unwrap_head::Bool) + if expr.head in (:function, :(=)) + fexpr = expr + elseif expr.head == :block + fexpr = expr.args[2] # separate fexpr from block + else + error("Expression is not supported") + end + + head = fexpr.args[1] + fcall, wherestack = unwrap_head(head) + body = fexpr.args[2] + return fcall, wherestack, body +end + +function unwrap_fun(expr::Expr, should_unwrap_head::Bool, should_unwrap_fcall::Bool) + if expr.head in (:function, :(=)) + fexpr = expr + elseif expr.head == :block + fexpr = expr.args[2] # separate fexpr from block + else + error("Expression is not supported") + end + + head = fexpr.args[1] + fcall, wherestack = unwrap_head(head) + f, args = unwrap_fcall(fcall) + + body = fexpr.args[2] + return f, args, wherestack, body +end +################################################################ From c4ae894d0ccc6bf816de0d6bd8f4184dc7ede072 Mon Sep 17 00:00:00 2001 From: aminya Date: Sun, 9 Feb 2020 18:50:47 -0600 Subject: [PATCH 2/6] wrap_fun --- src/fexpr.jl | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/fexpr.jl b/src/fexpr.jl index 0d07a5d..c834117 100644 --- a/src/fexpr.jl +++ b/src/fexpr.jl @@ -53,3 +53,37 @@ function unwrap_fun(expr::Expr, should_unwrap_head::Bool, should_unwrap_fcall::B return f, args, wherestack, body end ################################################################ +""" + fexpr = wrap_fun(f, args, wherestack, body) + fexpr = wrap_fun(fcall, wherestack, body) + fexpr = wrap_fun(head, body) + fexpr = wrap_fun(fexpr) + +Returns a function definition expression +""" +function wrap_fun(f, args, wherestack, body) + fcall = wrap_fcall(f, args) + head = wrap_head(fcall, wherestack) + return Expr(:function, head, Expr(:block, body)) +end + +function wrap_fun(fcall, wherestack, body) + head = wrap_head(fcall, wherestack) + return Expr(:function, head, Expr(:block, body)) +end + +function wrap_fun(head::Expr, body::Expr) + return Expr(:function, head, Expr(:block, body)) +end + +function wrap_fun(fexpr::Expr) + if fexpr.head in (:function, :(=)) + return fexpr + elseif fexpr.head == :block + fexpr = fexpr.args[2] # separate fexpr from block + return fexpr + else + error("Expression is not supported") + end +end + From 84cdfbb9ffe76f5095e7d5fc2f18da2aa96a7bc9 Mon Sep 17 00:00:00 2001 From: aminya Date: Sun, 9 Feb 2020 18:50:56 -0600 Subject: [PATCH 3/6] unwrap_head --- src/fexpr.jl | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/fexpr.jl b/src/fexpr.jl index c834117..6b88f85 100644 --- a/src/fexpr.jl +++ b/src/fexpr.jl @@ -87,3 +87,13 @@ function wrap_fun(fexpr::Expr) end end +################################################################ +function unwrap_head(head) + wherestack = Any[] + while head isa Expr && head.head == :where + push!(wherestack, head.args[2]) + head = head.args[1] + end + fcall = head + fcall, wherestack +end From 840aca169829292fe6b088cfd11b5f679b4fb524 Mon Sep 17 00:00:00 2001 From: aminya Date: Sun, 9 Feb 2020 18:51:02 -0600 Subject: [PATCH 4/6] wrap_head --- src/fexpr.jl | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/fexpr.jl b/src/fexpr.jl index 6b88f85..b10981f 100644 --- a/src/fexpr.jl +++ b/src/fexpr.jl @@ -97,3 +97,13 @@ function unwrap_head(head) fcall = head fcall, wherestack end + +function wrap_head(fcall, wherestack) + for w in Iterators.reverse(wherestack) + fcall = Expr(:where, fcall, w) + # fcall = Expr(:where, fcall, esc(w)) + end + head = fcall + return head +end +################################################################ From 4f471f5dad90c77de26d0960dbed82672bb5a5c3 Mon Sep 17 00:00:00 2001 From: aminya Date: Sun, 9 Feb 2020 18:51:09 -0600 Subject: [PATCH 5/6] unwrap_fcall --- src/fexpr.jl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/fexpr.jl b/src/fexpr.jl index b10981f..b3956d3 100644 --- a/src/fexpr.jl +++ b/src/fexpr.jl @@ -107,3 +107,12 @@ function wrap_head(fcall, wherestack) return head end ################################################################ +function unwrap_fcall(fcall::Expr) + if !(fcall.head == :call) + error("Expression is not supported") + end + f = fcall.args[1] + args = fcall.args[2:end] + return f, args +end + From 039a06562602585fa2b34f0bd63a3241056b8438 Mon Sep 17 00:00:00 2001 From: aminya Date: Sun, 9 Feb 2020 19:01:00 -0600 Subject: [PATCH 6/6] wrap_fcall --- src/fexpr.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/fexpr.jl b/src/fexpr.jl index b3956d3..efd337e 100644 --- a/src/fexpr.jl +++ b/src/fexpr.jl @@ -101,7 +101,6 @@ end function wrap_head(fcall, wherestack) for w in Iterators.reverse(wherestack) fcall = Expr(:where, fcall, w) - # fcall = Expr(:where, fcall, esc(w)) end head = fcall return head @@ -116,3 +115,8 @@ function unwrap_fcall(fcall::Expr) return f, args end +function wrap_fcall(f, args) + fcall = :($f($((args)...))) + return fcall +end +################################################################