Sha256: f289efd1d675b7ad70934b6071bc5fd2d561ab6a16e73cd05baac25eba5f53c0

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

require 'gobstones/runner/head'

module Gobstones

  module Runner

    class ExecutionContext

      def initialize
        @values = {}
      end

      def set(var_name, value)
        @values[var_name] = value
      end

      def get(var_name)
        @values[var_name] || (raise UndefinedVariableError)
      end

      def clear(var_name)
        @values.delete(var_name)
      end

      def has_variable_named?(name)
        @values.keys.any? { |var| var.name == name }
      end

    end

    class ProgramExecutionContext < ExecutionContext

      attr_reader :head

      def self.for(program)
        self.new program
      end

      def initialize(program)
        super()
        @head = Head.new
        @program = program
      end

      def definition_named(name, found_block, not_found_block)
        found_definition = @program.definitions.find { |definition|
          definition.named? name
        }
        if found_definition
          found_block.call found_definition
        else
          not_found_block.call
        end
      end

      def program_context
        self
      end

      def board
        head.board
      end

    end

    class ProcedureExecutionContext < ExecutionContext

      def self.based_on(outer_context)
        self.new outer_context
      end

      def initialize(outer_context)
        super()
        @outer_context = outer_context
      end

      def head
        @outer_context.head
      end

      def program_context
        @outer_context.program_context
      end

    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gobstones-0.0.1.1 lib/gobstones/runner/execution_context.rb