Chervil is a toy Lisp interpreter 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.

list_spec.rb 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. module Chervil
  2. RSpec.describe AST::List do
  3. it 'evaluates addition' do
  4. env = Env.new
  5. expect(
  6. AST::List.new(
  7. [AST::Identifier.new('+'),
  8. AST::Number.new(1.0), AST::Number.new(2.0)]
  9. )
  10. .evaluate(env)
  11. ).to eq(3.0)
  12. end
  13. it 'evaluates nested arithmetic' do
  14. env = Env.new
  15. expect(
  16. AST::List.new(
  17. [
  18. AST::Identifier.new('+'),
  19. AST::List.new(
  20. [
  21. AST::Identifier.new('+'),
  22. AST::Number.new(1.0),
  23. AST::Number.new(2.0)
  24. ]
  25. ),
  26. AST::List.new(
  27. [
  28. AST::Identifier.new('+'),
  29. AST::Number.new(3.0),
  30. AST::Number.new(4.0)
  31. ]
  32. )
  33. ]
  34. )
  35. .evaluate(env)
  36. ).to eq(10.0)
  37. end
  38. it 'returns an error if the function is not defined' do
  39. env = Env.new
  40. expect(AST::List.new([AST::Identifier.new("x")])
  41. .evaluate(env))
  42. .to eq(Error.new("Unbound variable x"))
  43. end
  44. it 'applies a function in the first position' do
  45. env = Env.new
  46. list = AST::List.new(
  47. [
  48. AST::Function.new(
  49. AST::Identifier.new("lambda"),
  50. [AST::Identifier.new("x")],
  51. [
  52. AST::List.new(
  53. [
  54. AST::Identifier.new("+"),
  55. AST::Identifier.new("x"),
  56. AST::Number.new(1.0),
  57. ]
  58. )
  59. ]
  60. ),
  61. AST::Number.new(5.0),
  62. ]
  63. )
  64. expect(list.evaluate(env)).to eq(6.0)
  65. end
  66. end
  67. end