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

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

Instance Method Summary collapse

Constructor Details

#initialize(buffer, binds, kernels) ⇒ DeclarationEmitter

Returns a new instance of DeclarationEmitter.



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

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

Instance Method Details

#emit(name, ops) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kumi/core/analyzer/passes/codegen/js/declaration_emitter.rb', line 26

def emit(name, ops)
  @ops = ops
  @yield_depth = find_yield_depth(ops)
  has_yield = @ops.any? { |op| op.opcode == :Yield }
  @aliases.clear

  # Exported, pure function per declaration
  write "export function _#{name}(input) {"
  indent!

  write "let out = [];" if has_yield && @yield_depth.positive?
  @out_containers = ["out"]

  @ops.each_with_index { |ins, i| emit_ins(ins, i) }

  write "return out;" if has_yield && @yield_depth.positive?

  dedent!
  write "}\n"
end