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.rb 777B

12345678910111213141516171819202122232425262728293031323334
  1. module Chervil::AST
  2. class List
  3. attr_reader :elements
  4. def initialize(elements)
  5. @elements = elements
  6. end
  7. def ==(other)
  8. @elements == other.elements
  9. end
  10. def evaluate(env)
  11. if elements.empty?
  12. []
  13. else
  14. head = @elements.first
  15. if head.is_a?(Identifier)
  16. function = env.get(head.name)
  17. if function.nil?
  18. ::Chervil::Error.new("Unbound variable #{head.name}")
  19. else
  20. function.call(@elements[1..-1].map { |arg| arg.evaluate(env) }, env)
  21. end
  22. elsif head.is_a?(Function)
  23. head.call(@elements[1..-1].map { |arg| arg.evaluate(env) }, env)
  24. else
  25. @elements.map { |el| el.evaluate(env) }
  26. end
  27. end
  28. end
  29. end
  30. end