Add a new gem: mruby-catch.

Implements `catch`/`throw` non-local jump inherited from Lisp.

`catch([tag]) {|tag| block }  -> obj`

Example:
```
  catch(:foo) { 123 }               # => 123
  catch(:foo) { throw(:foo, 456) }  # => 456
  catch(:foo) { throw(:foo) }       # => nil
```
This commit is contained in:
Yukihiro "Matz" Matsumoto
2020-08-28 17:49:27 +09:00
parent 471479e723
commit 7eaaee5405
2 changed files with 32 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
MRuby::Gem::Specification.new('mruby-catch') do |spec|
spec.license = 'MIT'
spec.author = 'mruby developers'
spec.summary = 'Catch / Throw non-local Jump'
end
+27
View File
@@ -0,0 +1,27 @@
class ThrowCatchJump < Exception
def initialize(tag, val)
@tag = tag
@val = val
super("uncaught throw :#{tag}")
end
def _tag
@tag
end
def _val
@val
end
end
module Kernel
def catch(tag, &block)
block.call(tag)
rescue ThrowCatchJump => e
unless e._tag == tag
raise e
end
return e._val
end
def throw(tag, val=nil)
raise ThrowCatchJump.new(tag, val)
end
end