class Interscript::Interpreter

Attributes

map[RW]

Public Instance Methods

call(str, stage=:main, each: false, &block) click to toggle source
# File lib/interscript/interpreter.rb, line 8
def call(str, stage=:main, each: false, &block)
  stage = @map.stages[stage]
  s =
  if each
    e = Enumerator.new do |yielder|
      options = []
      options_set = false
      choices = nil

      i = 0

      loop do
        result = nil

        f = Fiber.new do
          $select_nth_string = true
          result = Stage.new(@map, str).execute_rule(stage)
          $select_nth_string = false
          Fiber.yield(:end)
        end

        iter = 0

        loop do
          break if f.resume == :end
          # hash is unused for now... some problems may arise in certain
          # scenarios that are not a danger right now, but i'm genuinely
          # unsure how it can be handled.
          #
          # This scenario is described in a commented out test.
          type, value, hash = f.resume
          if options_set
            f.resume(choices[i][iter])
          else
            options[iter] = value
            f.resume(0)
          end
          iter += 1
        end

        unless options_set
          options_set = true

          opts = options.map { |i| (0...i).to_a }
          choices = opts[0].product(*opts[1..-1])
        end

        yielder.yield(result)

        i += 1
        break if i == choices.length
      end
    end

    if block_given?
      e.each(&block)
    else
      e
    end
  else
    Stage.new(@map, str).execute_rule(stage)
  end
end
compile(map, _:nil) click to toggle source
# File lib/interscript/interpreter.rb, line 3
def compile(map, _:nil)
  @map = map
  self
end