Class: Kumi::Core::Analyzer::Passes::Codegen::Ruby::DeclarationEmitter

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/kumi/core/analyzer/passes/codegen/ruby/declaration_emitter.rb

Constant Summary collapse

LIR =
Kumi::Core::LIR

Instance Method Summary collapse

Constructor Details

#initialize(buffer, binds, kernels) ⇒ DeclarationEmitter

Returns a new instance of DeclarationEmitter.



17
18
19
20
21
22
23
# File 'lib/kumi/core/analyzer/passes/codegen/ruby/declaration_emitter.rb', line 17

def initialize(buffer, binds, kernels)
  @buffer = buffer
  @binds = binds
  @kernels = kernels
  @stack = []
  @out_containers = []
end

Instance Method Details

#emit(name, ops) ⇒ Object



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
# File 'lib/kumi/core/analyzer/passes/codegen/ruby/declaration_emitter.rb', line 25

def emit(name, ops)
  @ops = ops
  # Pre-calculate the depth of the single Yield instruction.
  @yield_depth = find_yield_depth(ops)

  write "def self._#{name}(input)"
  indent!

  # Setup the top-level 'out' container if the result is an array.
  write "out = []" if @yield_depth.positive?
  @out_containers = ["out"]

  # Simple, robust iteration.
  @ops.each_with_index { |ins, i| emit_ins(ins, i) }

  # Determine the final return value.
  if @ops.any? { _1.opcode == :Yield } && @yield_depth.zero?
    # Scalar result is handled by emit_yield's optimization.
  else
    write "out"
  end

  dedent!
  write "end\n"
end