ruby: use index-enum xml attr, fix bitfield[i] = 0

develop
jj 2012-04-25 17:18:24 +02:00
parent d1ea97c272
commit b0acad6d59
3 changed files with 54 additions and 10 deletions

@ -315,6 +315,7 @@ sub render_item_container {
my $subtype = $item->getAttribute('ld:subtype'); my $subtype = $item->getAttribute('ld:subtype');
my $rbmethod = join('_', split('-', $subtype)); my $rbmethod = join('_', split('-', $subtype));
my $tg = $item->findnodes('child::ld:item')->[0]; my $tg = $item->findnodes('child::ld:item')->[0];
my $indexenum = $item->getAttribute('index-enum');
if ($tg) { if ($tg) {
if ($rbmethod eq 'df_linked_list') { if ($rbmethod eq 'df_linked_list') {
push @lines_rb, "$rbmethod {"; push @lines_rb, "$rbmethod {";
@ -326,6 +327,9 @@ sub render_item_container {
render_item($tg, $pns); render_item($tg, $pns);
}; };
push @lines_rb, "}"; push @lines_rb, "}";
} elsif ($indexenum) {
$indexenum = rb_ucase($indexenum);
push @lines_rb, "$rbmethod(:$indexenum)";
} else { } else {
push @lines_rb, "$rbmethod"; push @lines_rb, "$rbmethod";
} }
@ -354,7 +358,13 @@ sub render_item_staticarray {
my $count = $item->getAttribute('count'); my $count = $item->getAttribute('count');
my $tg = $item->findnodes('child::ld:item')->[0]; my $tg = $item->findnodes('child::ld:item')->[0];
my $tglen = get_tglen($tg, $pns); my $tglen = get_tglen($tg, $pns);
push @lines_rb, "static_array($count, $tglen) {"; my $indexenum = $item->getAttribute('index-enum');
if ($indexenum) {
$indexenum = rb_ucase($indexenum);
push @lines_rb, "static_array($count, $tglen, :$indexenum) {";
} else {
push @lines_rb, "static_array($count, $tglen) {";
}
indent_rb { indent_rb {
render_item($tg, $pns); render_item($tg, $pns);
}; };

@ -36,8 +36,8 @@ class Compound < MemStruct
def pointer_ary(tglen) def pointer_ary(tglen)
PointerAry.new(tglen, yield) PointerAry.new(tglen, yield)
end end
def static_array(len, tglen) def static_array(len, tglen, indexenum=nil)
StaticArray.new(tglen, len, yield) StaticArray.new(tglen, len, indexenum, yield)
end end
def static_string(len) def static_string(len)
StaticString.new(len) StaticString.new(len)
@ -61,8 +61,8 @@ class Compound < MemStruct
StlDeque.new(tglen, yield) StlDeque.new(tglen, yield)
end end
def df_flagarray def df_flagarray(indexenum=nil)
DfFlagarray.new DfFlagarray.new(indexenum)
end end
def df_array(tglen) def df_array(tglen)
DfArray.new(tglen, yield) DfArray.new(tglen, yield)
@ -250,11 +250,22 @@ class PointerAry < MemStruct
def inspect ; "#<PointerAry #{'0x%X' % _getp}>" ; end def inspect ; "#<PointerAry #{'0x%X' % _getp}>" ; end
end end
module IndexEnum
def indexenum(idx)
if idx.kind_of?(::Symbol) or idx.kind_of?(::String) and _indexenum
DFHack.const_get(_indexenum).const_get(idx) || idx
else
idx
end
end
end
class StaticArray < MemStruct class StaticArray < MemStruct
attr_accessor :_tglen, :_length, :_tg include IndexEnum
def initialize(tglen, length, tg) attr_accessor :_tglen, :_length, :_indexenum, :_tg
def initialize(tglen, length, indexenum, tg)
@_tglen = tglen @_tglen = tglen
@_length = length @_length = length
@_indexenum = indexenum
@_tg = tg @_tg = tg
end end
def _set(a) def _set(a)
@ -266,17 +277,26 @@ class StaticArray < MemStruct
@_tg._at(@_memaddr + i*@_tglen) if i >= 0 and i < @_length @_tg._at(@_memaddr + i*@_tglen) if i >= 0 and i < @_length
end end
def [](i) def [](i)
i = indexenum(i)
i += @_length if i < 0 i += @_length if i < 0
_tgat(i)._get _tgat(i)._get
end end
def []=(i, v) def []=(i, v)
i = indexenum(i)
i += @_length if i < 0 i += @_length if i < 0
_tgat(i)._set(v) _tgat(i)._set(v)
end end
include Enumerable include Enumerable
def each ; (0...length).each { |i| yield self[i] } ; end def each ; (0...length).each { |i| yield self[i] } ; end
def inspect ; to_a.inspect ; end def inspect
if _indexenum
e = DFHack.const_get(_indexenum)::ENUM
'[' + (0...length).map { |i| "#{e[i]}=#{self[i].inspect}" }.join(' ') + ']'
else
to_a.inspect
end
end
end end
class StaticString < MemStruct class StaticString < MemStruct
attr_accessor :_length attr_accessor :_length
@ -431,6 +451,11 @@ class StlDeque < MemStruct
end end
class DfFlagarray < MemStruct class DfFlagarray < MemStruct
include IndexEnum
attr_accessor :_indexenum
def initialize(indexenum)
@_indexenum = indexenum
end
def length def length
DFHack.memory_bitarray_length(@_memaddr) DFHack.memory_bitarray_length(@_memaddr)
end end
@ -439,10 +464,12 @@ class DfFlagarray < MemStruct
DFHack.memory_bitarray_resize(@_memaddr, len) DFHack.memory_bitarray_resize(@_memaddr, len)
end end
def [](idx) def [](idx)
idx = indexenum(idx)
idx += length if idx < 0 idx += length if idx < 0
DFHack.memory_bitarray_isset(@_memaddr, idx) if idx >= 0 and idx < length DFHack.memory_bitarray_isset(@_memaddr, idx) if idx >= 0 and idx < length
end end
def []=(idx, v) def []=(idx, v)
idx = indexenum(idx)
idx += length if idx < 0 idx += length if idx < 0
if idx >= length or idx < 0 if idx >= length or idx < 0
raise 'invalid idx' raise 'invalid idx'
@ -453,7 +480,14 @@ class DfFlagarray < MemStruct
include Enumerable include Enumerable
def each ; (0...length).each { |i| yield self[i] } ; end def each ; (0...length).each { |i| yield self[i] } ; end
def inspect ; to_a.inspect ; end def inspect
if _indexenum
e = DFHack.const_get(_indexenum)::ENUM
'[' + (0...length).map { |i| if self[i] ; e[i] || i ; end }.compact.join(' ') + ']'
else
to_a.inspect
end
end
end end
class DfArray < Compound class DfArray < Compound
attr_accessor :_tglen, :_tg attr_accessor :_tglen, :_tg

@ -278,7 +278,7 @@ static void df_rubythread(void *p)
} }
#define BOOL_ISFALSE(v) ((v) == Qfalse || (v) == Qnil || (v) == FIX2INT(0)) #define BOOL_ISFALSE(v) ((v) == Qfalse || (v) == Qnil || (v) == INT2FIX(0))
// main DFHack ruby module // main DFHack ruby module
static VALUE rb_cDFHack; static VALUE rb_cDFHack;