Class: Interscript::Interpreter

Inherits:
Compiler
  • Object
show all
Defined in:
lib/interscript/interpreter.rb

Defined Under Namespace

Classes: Stage

Instance Attribute Summary collapse

Attributes inherited from Compiler

#code

Instance Method Summary collapse

Methods inherited from Compiler

call

Instance Attribute Details

#mapObject

Returns the value of attribute map.



2
3
4
# File 'lib/interscript/interpreter.rb', line 2

def map
  @map
end

Instance Method Details

#call(str, stage = :main, each: false, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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) ⇒ Object



3
4
5
6
# File 'lib/interscript/interpreter.rb', line 3

def compile(map, _:nil)
  @map = map
  self
end