1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- module Chervil
- RSpec.describe AST::List do
- it 'evaluates addition' do
- env = Env.new
- expect(
- AST::List.new(
- [AST::Identifier.new('+'),
- AST::Number.new(1.0), AST::Number.new(2.0)]
- )
- .evaluate(env)
- ).to eq(3.0)
- end
-
- it 'evaluates nested arithmetic' do
- env = Env.new
- expect(
- AST::List.new(
- [
- AST::Identifier.new('+'),
- AST::List.new(
- [
- AST::Identifier.new('+'),
- AST::Number.new(1.0),
- AST::Number.new(2.0)
- ]
- ),
- AST::List.new(
- [
- AST::Identifier.new('+'),
- AST::Number.new(3.0),
- AST::Number.new(4.0)
- ]
- )
- ]
- )
- .evaluate(env)
- ).to eq(10.0)
- end
-
- it 'returns an error if the function is not defined' do
- env = Env.new
- expect(AST::List.new([AST::Identifier.new("x")])
- .evaluate(env))
- .to eq(Error.new("Unbound variable x"))
- end
-
- it 'applies a function in the first position' do
- env = Env.new
- list = AST::List.new(
- [
- AST::Function.new(
- AST::Identifier.new("lambda"),
- [AST::Identifier.new("x")],
- [
- AST::List.new(
- [
- AST::Identifier.new("+"),
- AST::Identifier.new("x"),
- AST::Number.new(1.0),
- ]
- )
- ]
- ),
- AST::Number.new(5.0),
- ]
- )
-
- expect(list.evaluate(env)).to eq(6.0)
- end
- end
- end
|