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 543B

1234567891011121314151617181920212223
  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 = Environment.new
  16. _env.set(@iterator.name, i)
  17. @block.statements.each { |statement| statement.execute(_env) }
  18. end
  19. end
  20. end