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

make REPL ast transforms instance-specific #34626

Merged
merged 1 commit into from
Feb 10, 2020
Merged
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
27 changes: 19 additions & 8 deletions stdlib/REPL/src/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,28 @@ mutable struct REPLBackend
response_channel::Channel
"flag indicating the state of this backend"
in_eval::Bool
"transformation functions to apply before evaluating expressions"
ast_transforms::Vector{Any}
"current backend task"
backend_task::Task

REPLBackend(repl_channel, response_channel, in_eval) =
new(repl_channel, response_channel, in_eval)
REPLBackend(repl_channel, response_channel, in_eval, ast_transforms=copy(repl_ast_transforms)) =
new(repl_channel, response_channel, in_eval, ast_transforms)
end

function softscope!(ex)
"""
softscope(ex)

Return a modified version of the parsed expression `ex` that uses
the REPL's "soft" scoping rules for global syntax blocks.
"""
function softscope(@nospecialize ex)
if ex isa Expr
h = ex.head
if h === :toplevel
for i = 1:length(ex.args)
ex.args[i] = softscope!(ex.args[i])
end
ex′ = Expr(h)
map!(softscope, resize!(ex′.args, length(ex.args)), ex.args)
return ex′
elseif h in (:meta, :import, :using, :export, :module, :error, :incomplete, :thunk)
return ex
else
Expand All @@ -89,7 +97,10 @@ function softscope!(ex)
return ex
end

const repl_ast_transforms = Any[softscope!]
# Temporary alias until Documenter updates
const softscope! = softscope

const repl_ast_transforms = Any[softscope] # defaults for new REPL backends
StefanKarpinski marked this conversation as resolved.
Show resolved Hide resolved

function eval_user_input(@nospecialize(ast), backend::REPLBackend)
lasterr = nothing
Expand All @@ -101,7 +112,7 @@ function eval_user_input(@nospecialize(ast), backend::REPLBackend)
put!(backend.response_channel, (lasterr,true))
else
backend.in_eval = true
for xf in repl_ast_transforms
for xf in backend.ast_transforms
ast = xf(ast)
end
value = Core.eval(Main, ast)
Expand Down