Skip to content

Commit

Permalink
Merge pull request #104 from MikeInnes/mji/exceptions
Browse files Browse the repository at this point in the history
Avoid using exceptions for control flow
  • Loading branch information
MikeInnes authored Mar 23, 2020
2 parents f155c51 + cdbfb0c commit 0cf0f7c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
45 changes: 23 additions & 22 deletions src/match/match.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
mutable struct MatchError
struct MatchError
pat
ex
end

nomatch(pat, ex) = throw(MatchError(pat, ex))
macro nomatch(pat, ex)
:(return MatchError($(esc(pat)), $(esc(ex))))
end

macro trymatch(ex)
quote
r = $(esc(ex))
r isa MatchError && return r
r
end
end

function store!(env, name, ex)
haskey(env, name) && !(env[name] == ex) && nomatch(name, ex)
haskey(env, name) && !(env[name] == ex) && @nomatch(name, ex)
assoc!(env, name, ex)
end

Expand All @@ -18,7 +28,7 @@ function bname(s::Symbol)
end

function match_inner(pat, ex, env)
pat == ex || nomatch(pat, ex)
pat == ex || @nomatch(pat, ex)
return env
end

Expand All @@ -43,28 +53,28 @@ inrange(i, range, len) =
range (0,0) && i range[1] && i len+1-range[2]

function match_inner(pat::Expr, ex::Expr, env)
match(pat.head, ex.head, env)
@trymatch match(pat.head, ex.head, env)
pat, ex = rmlines(pat), rmlines(ex)
sr = slurprange(pat.args)
slurp = Any[]
i = 1
for p in pat.args
i > length(ex.args) &&
(isslurp(p) ? store!(env, bname(p), slurp) : nomatch(pat, ex))
(isslurp(p) ? @trymatch(store!(env, bname(p), slurp)) : @nomatch(pat, ex))

while inrange(i, sr, length(ex.args))
push!(slurp, ex.args[i])
i += 1
end

if isslurp(p)
p :__ && store!(env, bname(p), slurp)
p :__ && @trymatch store!(env, bname(p), slurp)
else
match(p, ex.args[i], env)
@trymatch match(p, ex.args[i], env)
i += 1
end
end
i == length(ex.args)+1 || nomatch(pat, ex)
i == length(ex.args)+1 || @nomatch(pat, ex)
return env
end

Expand Down Expand Up @@ -96,19 +106,10 @@ end

match(pat, ex) = match(pat, ex, Dict())

function ismatch(pat, ex)
try
match(pat, ex)
return true
catch e
isa(e, MatchError) ? (return false) : rethrow()
end
end
ismatch(pat, ex) = !(match(pat, ex) isa MatchError)

function trymatch(pat, ex)
try
match(pat, ex)
catch e
isa(e, MatchError) ? (return) : rethrow()
end
r = match(pat, ex)
r isa MatchError && return
return r
end
2 changes: 1 addition & 1 deletion src/match/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function tbnew(s::Symbol)
end

match_inner(b::TypeBind, ex, env) =
isexpr(ex, b.ts...) ? (env[tbname(b)] = ex; env) : nomatch(b, ex)
isexpr(ex, b.ts...) ? (env[tbname(b)] = ex; env) : @nomatch(b, ex)

subtb(s) = s
subtb(s::Symbol) = tbnew(s)
Expand Down

2 comments on commit 0cf0f7c

@MikeInnes
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request updated: JuliaRegistries/General/11418

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.5 -m "<description of version>" 0cf0f7c69399e97ece5253d86096f18e279d33f2
git push origin v0.5.5

Please sign in to comment.