Class: Faust2Ruby::IRBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/faust2ruby/ir_builder.rb

Overview

Converts Faust AST nodes to Ruby2Faust IR nodes. This enables semantic analysis and Ruby code generation.

Constant Summary collapse

Node =
Ruby2Faust::Node
NodeType =
Ruby2Faust::NodeType

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIRBuilder

Returns a new instance of IRBuilder.



14
15
16
17
# File 'lib/faust2ruby/ir_builder.rb', line 14

def initialize
  @definitions = {}  # name => AST::Definition
  @errors = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



19
20
21
# File 'lib/faust2ruby/ir_builder.rb', line 19

def errors
  @errors
end

Instance Method Details

#build(program) ⇒ Object

Build IR from a parsed program Returns a hash with :process (the main process IR), :imports, :declares



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
# File 'lib/faust2ruby/ir_builder.rb', line 23

def build(program)
  # First pass: collect all definitions
  program.statements.each do |stmt|
    case stmt
    when AST::Definition
      @definitions[stmt.name] = stmt
    end
  end

  # Find process definition
  process_def = @definitions["process"]
  unless process_def
    @errors << "No 'process' definition found"
    return nil
  end

  # Build IR for process
  process_ir = build_expression(process_def.expression)

  # Collect imports and declares
  imports = program.statements.select { |s| s.is_a?(AST::Import) }.map(&:path)
  declares = program.statements.select { |s| s.is_a?(AST::Declare) }.to_h { |d| [d.key, d.value] }

  {
    process: process_ir,
    imports: imports,
    declares: declares,
    definitions: @definitions.reject { |k, _| k == "process" }
  }
end