A toy dynamic programming language written in Ruby
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

function_call.rb 951B

123456789101112131415161718192021222324252627282930313233343536373839
  1. class AST::FunctionCall
  2. attr_reader :function, :arguments
  3. def initialize(function, arguments)
  4. @function = function
  5. @arguments = arguments
  6. end
  7. def ==(other)
  8. other.is_a?(AST::FunctionCall) && other.function == @function &&
  9. other.arguments == @arguments
  10. end
  11. def execute(env)
  12. env = Environment.new(env)
  13. function = env.get(@function.name)
  14. if function.is_a?(AST::FunctionDefinition)
  15. if @arguments.size != function.parameters.size
  16. raise "Arity mismatch in call to function #{function.name}"
  17. end
  18. function.parameters.each_with_index do |param, i|
  19. env.set(param.name, @arguments[i].execute(env))
  20. end
  21. value = nil
  22. function.body.statements.each do |statement|
  23. value = statement.execute(env)
  24. end
  25. value
  26. elsif function.is_a?(Proc)
  27. arguments = @arguments.map { |arg| arg.execute(env) }
  28. function.call(*arguments)
  29. end
  30. end
  31. end