Sha256: 15da83b6d595a276fdca7e7e62eb57d96bfee40e62f69bda092a3f38ad4ec5f5

Contents?: true

Size: 930 Bytes

Versions: 3

Compression:

Stored size: 930 Bytes

Contents

# frozen_string_literal: true

require_relative 'reference'

module Basic101

  class ArrayReference < Reference

    def initialize(identifier, arguments)
      super(identifier)
      @arguments = arguments
    end

    def eval(runtime)
      get_array(runtime).get(indices(runtime))
    end

    def assign(runtime, value)
      get_array(runtime).set(indices(runtime), value)
    end

    def dimension_array(runtime)
      get_array(runtime).dimension(indices(runtime))
    end

    protected

    def state
      super + [@arguments]
    end

    private

    def get_array(runtime)
      runtime.get_array(@identifier, num_dimensions)
    end

    def num_dimensions
      @arguments.size
    end

    def indices(runtime)
      argument_values(runtime).map(&:to_integer).map(&:to_i)
    end

    def argument_values(runtime)
      @arguments.map do |argument|
        argument.eval(runtime)
      end
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
basic101-1.0.2 lib/basic101/array_reference.rb
basic101-1.0.1 lib/basic101/array_reference.rb
basic101-1.0.0 lib/basic101/array_reference.rb