Class: HDLRuby::Low::Chunk
- Inherits:
-
Object
- Object
- HDLRuby::Low::Chunk
- Includes:
- Hparent
- Defined in:
- lib/HDLRuby/hruby_low.rb,
lib/HDLRuby/hruby_low2c.rb,
lib/HDLRuby/hruby_low2high.rb,
lib/HDLRuby/backend/hruby_c_allocator.rb
Overview
Describes a non-HDLRuby code chunk.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
The name of the code chunk.
Attributes included from Hparent
Instance Method Summary collapse
-
#add_lump(lump) ⇒ Object
Adds a +lump+ of code, it is ment to become an expression or some text.
-
#c_code_allocate!(allocator) ⇒ Object
Allocates signal within C code using +allocator+ and self-modify the code correspondingly.
-
#each_deep(&ruby_block) ⇒ Object
Iterates over each object deeply.
-
#each_lump(&ruby_block) ⇒ Object
Iterates over the code lumps.
-
#initialize(name, *lumps) ⇒ Chunk
constructor
Creates new code chunk +name+ with made of +lumps+ piece of text.
-
#to_c(res, level = 0) ⇒ Object
Generates the C text of the equivalent HDLRuby code.
-
#to_high ⇒ Object
Creates a new high code chunk.
Methods included from Hparent
#absolute_ref, #hierarchy, #no_parent!, #scope
Constructor Details
#initialize(name, *lumps) ⇒ Chunk
Creates new code chunk +name+ with made of +lumps+ piece of text.
2959 2960 2961 2962 2963 2964 2965 |
# File 'lib/HDLRuby/hruby_low.rb', line 2959 def initialize(name,*lumps) # Check and set the name. @name = name.to_sym # Set the content. @lumps = [] lumps.each { |lump| self.add_lump(lump) } end |
Instance Attribute Details
#name ⇒ Object (readonly)
The name of the code chunk.
2956 2957 2958 |
# File 'lib/HDLRuby/hruby_low.rb', line 2956 def name @name end |
Instance Method Details
#add_lump(lump) ⇒ Object
Adds a +lump+ of code, it is ment to become an expression or some text.
2972 2973 2974 2975 2976 2977 2978 |
# File 'lib/HDLRuby/hruby_low.rb', line 2972 def add_lump(lump) # Set its parent if relevant. lump.parent = self if lump.respond_to?(:parent) # And add it @lumps << lump return lump end |
#c_code_allocate!(allocator) ⇒ Object
Allocates signal within C code using +allocator+ and self-modify the code correspondingly. NOTE: non-C chunks are ignored.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/HDLRuby/backend/hruby_c_allocator.rb', line 45 def c_code_allocate!(allocator) # Checks the chunk is actually C. return self unless self.name == :c # Process each lump. @lumps.map! do |lump| lump_r = lump.resolve if lump.respond_to?(:resolve) if lump_r.is_a?(SignalI) then # The lump is a signal, performs the allocation and # change it to an address access. "*(0x#{allocator.allocate(lump_r).to_s(16)})" else lump end end self end |
#each_deep(&ruby_block) ⇒ Object
Iterates over each object deeply.
Returns an enumerator if no ruby block is given.
2993 2994 2995 2996 2997 2998 |
# File 'lib/HDLRuby/hruby_low.rb', line 2993 def each_deep(&ruby_block) # No ruby block? Return an enumerator. return to_enum(:each_deep) unless ruby_block # A ruby block? First apply it to current. ruby_block.call(self) end |
#each_lump(&ruby_block) ⇒ Object
Iterates over the code lumps.
Returns an enumerator if no ruby block is given.
2983 2984 2985 2986 2987 2988 |
# File 'lib/HDLRuby/hruby_low.rb', line 2983 def each_lump(&ruby_block) # No ruby block? Return an enumerator. return to_enum(:each_lump) unless ruby_block # A ruby block? Apply it on each lump. @lumps.each(&ruby_block) end |
#to_c(res, level = 0) ⇒ Object
Generates the C text of the equivalent HDLRuby code. +level+ is the hierachical level of the object. def to_c(level = 0)
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 |
# File 'lib/HDLRuby/hruby_low2c.rb', line 1179 def to_c(res,level = 0) # res = " " * level res << " " * level # res << self.each_lump.map do |lump| # if !lump.is_a?(String) then # lump.respond_to?(:to_c) ? lump.to_c(level+1) : lump.to_s # else # lump # end # end.join self.each_lump do |lump| if !lump.is_a?(String) then if lump.respond_to?(:to_c) then lump.to_c(res,level+1) else res << lump.to_s end else res << lump end end return res end |