Skip to content

Instantly share code, notes, and snippets.

@SimonDanisch
Last active January 3, 2018 15:40
Show Gist options
  • Save SimonDanisch/5bb003514d145288c921ef851d608a5d to your computer and use it in GitHub Desktop.
Save SimonDanisch/5bb003514d145288c921ef851d608a5d to your computer and use it in GitHub Desktop.
# Modular pass manager:
context = ExecutionContext()
# adds a predefined function from Sugar as a pass.
# this one takes care of e.g.:
# * turning Expr(:invoke, args...) into Expr(:call, args...)
# * removes apply_type
# * turn all functions into GlobalRef
# * etc
add_pass!(context, ast_normalization)
# turn gotos into if/else/while/for
add_pass!(context, remove_goto)
# Custom pass with Matcha like matching.
add_pass!(context, x-> match_head(x, :static_parameter)) do context, expr
param = context.sparams[expr.args[1]]
specialized_typeof(param)
end
using Cassette
function fortest(a)
l = 0
for i=1:a
l += 1
end
l
end
Cassette.@context LolCTX
function Cassette.getpass(::Type{<: LolCTX}, M)
function (sig, body)
push!(body.code, :(println($(QuoteNode(body.code)))))
body
end
end
Cassette.overdub(LolCTX, fortest)(1)
# MacroTools like matching
add_pass!(context, :(a_ + b_) => :(a - b))
ast = getast(context, func, args; optimize = true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment