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.

index.rb 833B

12345678910111213141516171819202122232425262728293031323334353637
  1. class AST::Index
  2. attr_reader :object, :key
  3. def initialize(object, key)
  4. @object = object
  5. @key = key
  6. end
  7. def ==(other)
  8. other.is_a?(AST::Index) && other.object == @object && other.key == @key
  9. end
  10. def execute(env)
  11. object = @object.execute(env)
  12. key = @key.execute(env)
  13. if object.is_a?(Array)
  14. if key.is_a?(Float)
  15. if key.to_i == key && object.size > key
  16. object[key]
  17. elsif key.to_i != key
  18. raise "Array index requires integer key"
  19. elsif object.size <= key
  20. raise "Array index out of bounds"
  21. end
  22. else
  23. raise "Array index requires integer key"
  24. end
  25. elsif object.is_a?(Hash)
  26. if object.has_key?(key)
  27. object[key]
  28. else
  29. raise "Key #{key} does not exist on object"
  30. end
  31. end
  32. end
  33. end