fork download
  1. require 'continuation'
  2.  
  3. class GoToProc
  4. def initialize(&b)
  5. @proc, @labels, @first_run = b, {}, true
  6. self.run
  7. @first_run = false
  8. end
  9.  
  10. def run
  11. callcc do |k|
  12. @exit = k
  13. self.instance_eval &@proc
  14. end
  15. end
  16.  
  17. def label(x)
  18. callcc { |cont| @labels[x] = cont }
  19. yield unless @first_run
  20. end
  21.  
  22. def goto(x)
  23. @labels[x].call()
  24. end
  25.  
  26. def exit
  27. @exit.call()
  28. end
  29. end
  30.  
  31. factorial = GoToProc.new do
  32. label(1) { @x, @f = 10, 1 }
  33. label(2) { goto 5 if @x == 1 }
  34. label(3) { @f *= @x; @x -= 1 }
  35. label(4) { goto 2 }
  36. label(5) { puts "factorial = #{@f}" }
  37. label(6) { exit }
  38. end
  39.  
  40. factorial.run
Success #stdin #stdout 0.01s 7560KB
stdin
Standard input is empty
stdout
factorial = 3628800