Class: Amrita2::Core::CodeGenerator::CGModule

Inherits:
Object
  • Object
show all
Defined in:
lib/amrita2/template.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ CGModule

Returns a new instance of CGModule.



694
695
696
697
698
# File 'lib/amrita2/template.rb', line 694

def initialize(name)
  @name = name
  @constants = []
  @methods = []
end

Instance Attribute Details

#constantsObject (readonly)

Returns the value of attribute constants.



691
692
693
# File 'lib/amrita2/template.rb', line 691

def constants
  @constants
end

#methodsObject (readonly)

Returns the value of attribute methods.



692
693
694
# File 'lib/amrita2/template.rb', line 692

def methods
  @methods
end

#nameObject (readonly)

Returns the value of attribute name.



690
691
692
# File 'lib/amrita2/template.rb', line 690

def name
  @name
end

Instance Method Details

#define_constant(const_def) ⇒ Object



700
701
702
703
704
705
706
707
708
709
710
711
712
713
# File 'lib/amrita2/template.rb', line 700

def define_constant(const_def)
  name = nil
  @constants.each do |n, cdef|
    if cdef == const_def
      name = n
      break
    end
  end
  unless name
    name = "C#{sprintf("%03d", @constants.size)}"
    @constants << [name, const_def]
  end
  name
end

#define_method(vars, &block) ⇒ Object



715
716
717
718
719
# File 'lib/amrita2/template.rb', line 715

def define_method(vars, &block)
  name = "m#{sprintf("%03d", @methods.size)}"
  @methods << [name, vars, block]
  name
end

#end_of_module(cg) ⇒ Object



721
722
723
724
725
726
727
728
729
730
# File 'lib/amrita2/template.rb', line 721

def end_of_module(cg)
  @constants.each do |name, defs|
    cg.code("#{name} = #{defs}")
  end
  @methods.each do |name, vars, body|
    cg.define_method(name, vars) do
      body.call
    end
  end
end