Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC/WIP: Lower AST using an external package (GroundEffects.jl) #68

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.12.1"

[deps]
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
GroundEffects = "d00d4687-65cd-4fba-8fd9-ecb45a036ebe"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Expand Down
13 changes: 7 additions & 6 deletions src/lazymacro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# and @chethega https://github.com/JuliaLang/julia/pull/30939

using MacroTools
using GroundEffects

lazy(::Any) = throw(ArgumentError("function `lazy` exists only for its effect on broadcasting, see the macro @~"))
struct LazyCast{T}
Expand All @@ -13,12 +14,11 @@ Broadcast.broadcasted(::typeof(lazy), x) = LazyCast(x)
Broadcast.materialize(x::LazyCast) = x.value


is_call(ex::Expr) =
ex.head == :call && !startswith(String(ex.args[1]), ".")
is_call(ex) = isexpr(ex, :call) && !is_dotcall(ex)

is_dotcall(ex::Expr) =
(ex.head == :. && ex.args[2].head === :tuple) ||
(ex.head == :call && startswith(String(ex.args[1]), "."))
is_dotcall(ex) =
(isexpr(ex, :.) && ex.args[2].head === :tuple) ||
(isexpr(ex, :call) && ex.args[1] isa Symbol && startswith(String(ex.args[1]), "."))
# e.g., `f.(x, y, z)` or `x .+ y .+ z`

lazy_expr(x) = x
Expand Down Expand Up @@ -105,5 +105,6 @@ julia> @~ A * B + C
macro ~(ex)
checkex(ex)
# Expanding macro here to support, e.g., `@.`
esc(:($instantiate($(lazy_expr(macroexpand(__module__, ex))))))
lex = lazy_expr(GroundEffects.lower(macroexpand(__module__, ex)))
esc(:($instantiate($lex)))
end