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.

function.rb 655B

123456789101112131415161718192021222324252627282930313233
  1. module Chervil::AST
  2. class Function
  3. attr_reader :params
  4. attr_reader :body
  5. def initialize(params, body)
  6. @params = params
  7. @body = body
  8. end
  9. def ==(other)
  10. @params == other.params && @body == other.body
  11. end
  12. def call(args)
  13. unless @params.size == args.size
  14. raise "Expected #{params.size} arguments but received #{args.size}"
  15. end
  16. env = ::Chervil::Env.new
  17. @params.zip(args).each do |key, value|
  18. env.set(key.name, value)
  19. end
  20. current_expr = nil
  21. @body.each do |expr|
  22. current_expr = expr.evaluate(env)
  23. end
  24. current_expr
  25. end
  26. end
  27. end