A toy dynamic programming language written in Ruby
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

for_loop.rb 512B

12345678910111213141516171819202122
  1. class AST::ForLoop
  2. attr_reader :iterator, :iterable, :block
  3. def initialize(iterator, iterable, block)
  4. @iterator = iterator
  5. @iterable = iterable
  6. @block = block
  7. end
  8. def ==(other)
  9. other.is_a?(AST::ForLoop) && other.iterator == @iterator &&
  10. other.iterable == @iterable &&
  11. other.block == @block
  12. end
  13. def execute(env)
  14. @iterable.execute(env).each do |i|
  15. env.set(@iterator.name, i)
  16. @block.statements.each { |statement| statement.execute(env) }
  17. end
  18. end
  19. end