A toy dynamic programming language written in Ruby
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

for_loop_spec.rb 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. RSpec.describe AST::ForLoop do
  2. it 'evaluates' do
  3. expect {
  4. AST::ForLoop.new(
  5. AST::Identifier.new('x'),
  6. AST::Array.new(
  7. [AST::Number.new(1.0), AST::Number.new(2.0), AST::Number.new(3.0)]
  8. ),
  9. AST::Block.new(
  10. [
  11. AST::FunctionCall.new(
  12. AST::Identifier.new('print'),
  13. [AST::Identifier.new('x')]
  14. )
  15. ]
  16. )
  17. )
  18. .execute(Environment.new)
  19. }.to output("1.0\n2.0\n3.0\n").to_stdout
  20. end
  21. it 'does not create a new scope inside the loop' do
  22. env = Environment.new
  23. AST::VariableDeclaration.new(AST::Identifier.new('x'), AST::Number.new(0.0))
  24. .execute(env)
  25. result =
  26. AST::ForLoop.new(
  27. AST::Identifier.new('y'),
  28. AST::Array.new(
  29. [AST::Number.new(1.0), AST::Number.new(2.0), AST::Number.new(3.0)]
  30. ),
  31. AST::Block.new(
  32. [
  33. AST::Assignment.new(
  34. AST::Identifier.new('x'),
  35. AST::Binary.new(
  36. AST::Operators::ADD,
  37. AST::Identifier.new('x'),
  38. AST::Identifier.new('y')
  39. )
  40. )
  41. ]
  42. )
  43. )
  44. .execute(env)
  45. expect(env.get('x')).to eq(6.0)
  46. end
  47. end