Sha256: 3f22fd69504bb8b5878b0ec4e2eda6092602884d1445da66a56954f881522e13

Contents?: true

Size: 896 Bytes

Versions: 29

Compression:

Stored size: 896 Bytes

Contents

# frozen_string_literal: true

class Code
  GLOBALS = %i[output error context object].freeze
  DEFAULT_TIMEOUT = 0

  def initialize(
    input,
    output: StringIO.new,
    error: StringIO.new,
    timeout: DEFAULT_TIMEOUT
  )
    @input = input
    @output = output
    @error = error
    @timeout = timeout || DEFAULT_TIMEOUT
    @context = Object::Context.new
  end

  def self.parse(input, timeout: DEFAULT_TIMEOUT)
    Timeout.timeout(timeout) { Parser.parse(input).to_raw }
  end

  def self.evaluate(
    input,
    output: StringIO.new,
    error: StringIO.new,
    timeout: DEFAULT_TIMEOUT
  )
    new(input, output:, error:, timeout:).evaluate
  end

  def evaluate
    Timeout.timeout(timeout) do
      parsed = Code.parse(input)
      Node::Code.new(parsed).evaluate(context:, output:, error:)
    end
  end

  private

  attr_reader :input, :timeout, :output, :error, :context
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
code-ruby-1.1.3 lib/code.rb
code-ruby-1.1.1 lib/code.rb
code-ruby-1.1.0 lib/code.rb
code-ruby-1.0.0 lib/code.rb
code-ruby-0.15.16 lib/code.rb
code-ruby-0.15.15 lib/code.rb
code-ruby-0.15.14 lib/code.rb
code-ruby-0.15.13 lib/code.rb
code-ruby-0.15.12 lib/code.rb
code-ruby-0.15.11 lib/code.rb
code-ruby-0.15.10 lib/code.rb
code-ruby-0.15.9 lib/code.rb
code-ruby-0.15.8 lib/code.rb
code-ruby-0.15.7 lib/code.rb
code-ruby-0.15.6 lib/code.rb
code-ruby-0.15.5 lib/code.rb
code-ruby-0.15.4 lib/code.rb
code-ruby-0.15.3 lib/code.rb
code-ruby-0.15.2 lib/code.rb
code-ruby-0.15.0 lib/code.rb