123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- RSpec.describe AST::ForLoop do
- it 'evaluates' do
- expect {
- AST::ForLoop.new(
- AST::Identifier.new('x'),
- AST::Array.new(
- [AST::Number.new(1.0), AST::Number.new(2.0), AST::Number.new(3.0)]
- ),
- AST::Block.new(
- [
- AST::FunctionCall.new(
- AST::Identifier.new('print'),
- [AST::Identifier.new('x')]
- )
- ]
- )
- )
- .execute(Environment.new)
- }.to output("1.0\n2.0\n3.0\n").to_stdout
- end
-
- it 'does not create a new scope inside the loop' do
- env = Environment.new
- AST::VariableDeclaration.new(AST::Identifier.new('x'), AST::Number.new(0.0))
- .execute(env)
- result =
- AST::ForLoop.new(
- AST::Identifier.new('y'),
- AST::Array.new(
- [AST::Number.new(1.0), AST::Number.new(2.0), AST::Number.new(3.0)]
- ),
- AST::Block.new(
- [
- AST::Assignment.new(
- AST::Identifier.new('x'),
- AST::Binary.new(
- AST::Operators::ADD,
- AST::Identifier.new('x'),
- AST::Identifier.new('y')
- )
- )
- ]
- )
- )
- .execute(env)
- expect(env.get('x')).to eq(6.0)
- end
- end
|