Skip to content

Commit

Permalink
retain the non-matching value in a PatternMismatch error
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Suraci committed May 19, 2011
1 parent 7a41fd6 commit 469cba6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
7 changes: 4 additions & 3 deletions kernel/errors.ay
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
MethodFail message :=
("method '" + @method-name to-s + "' did not understand " +
"its arguments (non-exhaustive patterns)")
"method '" + @method-name to-s + "' did not understand " +
"its arguments (non-exhaustive patterns)"

PatternMismatch message :=
"irrefutable pattern failed for " + @pattern to-s
"pattern of type `" + @pattern class name split("::") last +
"' did not match value `" + @value inspect + "'"
19 changes: 12 additions & 7 deletions lib/patterns.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class PatternMismatch < RuntimeError
def initialize(p)
def initialize(p, v)
@pattern = p
@value = v
end
end

Expand Down Expand Up @@ -52,7 +53,7 @@ def assign(g, expr, set = false)
# try pattern-matching, erroring on failure
# effect on the stack: top value removed
def match(g, set = false, locals = {})
error = g.new_label
mismatch = g.new_label
done = g.new_label

local_names.each do |n|
Expand All @@ -61,16 +62,20 @@ def match(g, set = false, locals = {})

g.dup
matches?(g)
g.gif error
g.gif mismatch

deconstruct(g, locals)
g.goto done

error.set!
g.pop
mismatch.set!
g.push_self
g.push_const :PatternMismatch
g.swap
g.push_cpath_top
g.find_const :PatternMismatch
g.swap
construct(g)
g.send :new, 1
g.swap
g.send :new, 2
g.allow_private
g.send :raise, 1
g.pop
Expand Down

0 comments on commit 469cba6

Please sign in to comment.