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

Prototype: Headless anonymous function syntax #199

Draft
wants to merge 2 commits into
base: main
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
39 changes: 35 additions & 4 deletions src/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@ function reorder_parameters!(args, params_pos)
insert!(args, params_pos, pop!(args))
end

function lower_underscores!(anon_args, args, argrange=1:length(args))
for i in argrange
a = args[i]
if a == :_
if isempty(anon_args)
g = gensym()
push!(anon_args, g)
else
g = anon_args[1]
end
args[i] = g
elseif a isa Expr
if Meta.isexpr(a, :call) && length(a.args) > 2 &&
Meta.isexpr(a.args[2], :parameters)
lower_underscores!(anon_args, a.args, 1:1)
lower_underscores!(anon_args, a.args, 3:length(a.args))
lower_underscores!(anon_args, a.args, 2:2)
else
# FIXME: Other out-of-source-order Exprs
lower_underscores!(anon_args, a.args)
end
end
end
end

function _to_expr(node::SyntaxNode; iteration_spec=false, need_linenodes=true,
eq_to_kw=false, map_kw_in_params=false)
if !haschildren(node)
Expand Down Expand Up @@ -250,11 +275,17 @@ function _to_expr(node::SyntaxNode; iteration_spec=false, need_linenodes=true,
# Block for conditional's source location
args[1] = Expr(:block, loc, args[1])
elseif headsym === :(->)
if Meta.isexpr(args[2], :block)
pushfirst!(args[2].args, loc)
if is_prefix_op_call(node)
anon_args = Symbol[]
lower_underscores!(anon_args, args)
pushfirst!(args, Expr(:tuple, anon_args...))
else
# Add block for source locations
args[2] = Expr(:block, loc, args[2])
if Meta.isexpr(args[2], :block)
pushfirst!(args[2].args, loc)
else
# Add block for source locations
args[2] = Expr(:block, loc, args[2])
end
end
elseif headsym === :function
if length(args) > 1
Expand Down
9 changes: 6 additions & 3 deletions src/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ function is_syntactic_operator(k)
end

function is_syntactic_unary_op(k)
kind(k) in KSet"$ & ::"
kind(k) in KSet"$ & :: ->"
end

function is_type_operator(t)
Expand Down Expand Up @@ -1417,6 +1417,7 @@ end
# &a ==> (& a)
# ::a ==> (::-pre a)
# $a ==> ($ a)
# ->a ==> (-> a)
#
# flisp: parse-unary-prefix
function parse_unary_prefix(ps::ParseState)
Expand All @@ -1434,14 +1435,16 @@ function parse_unary_prefix(ps::ParseState)
if k in KSet"& ::"
# &a ==> (& a)
parse_where(ps, parse_call)
elseif k == K"->"
# -> binds loosely on the right
parse_eq_star(ps)
else
# $a ==> ($ a)
# $$a ==> ($ ($ a))
# $&a ==> ($ (& a))
parse_unary_prefix(ps)
end
# Only need PREFIX_OP_FLAG for ::
f = k == K"::" ? PREFIX_OP_FLAG : EMPTY_FLAGS
f = (k == K"::" || k == K"->") ? PREFIX_OP_FLAG : EMPTY_FLAGS
emit(ps, mark, k, f)
end
else
Expand Down