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

1234567891011121314151617181920212223242526272829303132333435363738
  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. function = env.get(@function.name)
  13. if function.is_a?(AST::FunctionDefinition)
  14. if @arguments.size != function.parameters.size
  15. raise "Arity mismatch in call to function #{function.name}"
  16. end
  17. function.parameters.each_with_index do |param, i|
  18. env.set(param.name, @arguments[i].execute(env))
  19. end
  20. value = nil
  21. function.body.statements.each do |statement|
  22. value = statement.execute(env)
  23. end
  24. value
  25. elsif function.is_a?(Proc)
  26. arguments = @arguments.map { |arg| arg.execute(env) }
  27. function.call(*arguments)
  28. end
  29. end
  30. end