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 738B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 evaluate(env)
  13. self
  14. end
  15. def call(args)
  16. unless @params.size == args.size
  17. raise "Expected #{params.size} arguments but received #{args.size}"
  18. end
  19. env = ::Chervil::Env.new
  20. @params.zip(args).each do |key, value|
  21. env.set(key.name, value)
  22. end
  23. current_expr = nil
  24. @body.each do |expr|
  25. current_expr = expr.evaluate(env)
  26. end
  27. current_expr
  28. end
  29. def to_s
  30. "<function>"
  31. end
  32. end
  33. end