Rubyã§ã¤ããRuby ã¼ãããå¦ã³ãªããããã°ã©ãã³ã°è¨èªå ¥é
- ä½è : é è¤ä¾ä»
- åºç社/ã¡ã¼ã«ã¼: ã©ã ããã¼ã
- çºå£²æ¥: 2017/03/31
- ã¡ãã£ã¢: ããã¹ã
- ãã®ååãå«ãããã°ãè¦ã
RubyでつくるRuby ゼロから学びなおすプログラミング言語入門(紙書籍)www.lambdanote.com
minrubyãæ¹é ãã¦æ«å°¾å¼ã³åºãã®æé©åãã§ããªãã試ãã¦ã¿ãã
def bar(n) foo(n - 1) end def foo(n) if n > 0 if n % 1000 == 0 p n end bar(n) else 0 end end foo(10000)
ã«ã¦ã³ããã¦ã³ãäºã¤ã®é¢æ°ãã¾ããå帰å¼ã³åºãã§å®è£ ããä¾ããµãã«ã¼ãã³ã®æ«å°¾ï¼æ§ææ¨ã§ããæ«å°¾ï¼ï¼ãfunc_callã ã£ããããã«ãã¼ã¯ãã¤ããããã«ããã®ã§ãifã®åå²å ã®æ«å°¾ã¨ãã対象ã«ãªãã¾ããã£ã¦ãããminrubyã«ã¯returnããªãã®ã§åå²ã®å ã§ã®func_callã«å¯¾å¿ããå¿ è¦ãããã¾ããã
rubyã ã¨ãããªæãã§ã¹ã¿ãã¯ã足ãã¾ããã
$ ruby loop.rb 10000 9000 8000 7000 6000 5000 Traceback (most recent call last): 11913: from loop.rb:27:in `<main>' 11912: from loop.rb:10:in `foo' 11911: from loop.rb:2:in `bar' 11910: from loop.rb:10:in `foo' 11909: from loop.rb:2:in `bar' 11908: from loop.rb:10:in `foo' 11907: from loop.rb:2:in `bar' 11906: from loop.rb:10:in `foo' ... 11901 levels... 4: from loop.rb:10:in `foo' 3: from loop.rb:2:in `bar' 2: from loop.rb:10:in `foo' 1: from loop.rb:2:in `bar' loop.rb:10:in `foo': stack level too deep (SystemStackError)
æ¹é ããminrubyã§å®è¡ããã¨ããã
$ ruby interp.rb loop.rb 10000 9000 8000 7000 6000 5000 4000 3000 2000 1000
ãªãã¨ãå ¥ãåã§ãåãããã«ããã
$ ruby interp.rb interp.rb loop.rb 10000 9000 8000 7000 6000 5000 4000 3000 2000 1000
追è¨
æ«å°¾ã«ãã¼ã¯ã¤ããå¦çãæ«å°¾å¼ã³åºãæé©åããã¦ã¦ãã£ããããªãï¼
def mark_tail(tree, genv) case tree && tree[0] when "func_call" mhd = genv[tree[1]] if mhd == nil || mhd[0] == "user_defined" tree[0] = "tail" end when "stmts" mark_tail(tree[-1], genv) # ãã㨠when "if" mark_tail(tree[2], genv) # ããã¯éã mark_tail(tree[3], genv) # ãã end end
ã³ã¼ãã¯gistã«ããã
æ¹é ããminrubyã¯ãããªã®ããã£ã¨ãã¾ãæ¸ãããã ãããªãã