ruby: add df-linked-list support (also, xml could be simplified)

develop
jj 2012-04-20 18:20:24 +02:00
parent f57d63ed95
commit 417356c3cb
2 changed files with 40 additions and 9 deletions

@ -309,10 +309,13 @@ sub render_item_container {
my $subtype = $item->getAttribute('ld:subtype');
my $rbmethod = join('_', split('-', $subtype));
my $tg = $item->findnodes('child::ld:item')->[0];
# df-linked-list has no list[0]
if ($tg and $rbmethod ne 'df_linked_list') {
if ($tg) {
if ($rbmethod eq 'df_linked_list') {
push @lines_rb, "$rbmethod {";
} else {
my $tglen = get_tglen($tg, $cppvar);
push @lines_rb, "$rbmethod($tglen) {";
}
indent_rb {
render_item($tg, "${cppvar}[0]");
};

@ -2,7 +2,7 @@ module DFHack
module MemHack
class MemStruct
attr_accessor :_memaddr
def _at(addr) ; @_memaddr = addr ; dup ; end
def _at(addr) ; d = dup ; d._memaddr = addr ; d ; end
def _get ; self ; end
def inspect ; _get.inspect ; end
end
@ -68,7 +68,7 @@ class Compound < MemStruct
DfArray.new(tglen, (yield if tglen))
end
def df_linked_list
DfLinkedList.new((yield if block_given?))
DfLinkedList.new(yield)
end
def global(glob)
@ -474,13 +474,41 @@ class DfArray < Compound
def each ; (0...length).each { |i| yield self[i] } ; end
def inspect ; to_a.inspect ; end
end
class DfLinkedList < MemStruct
class DfLinkedList < Compound
attr_accessor :_tg
def initialize(tg)
@_tg = tg
end
# TODO
def inspect ; "#<DfLinkedList>" ; end
field(:_ptr, 0) { number 32, false }
field(:_prev, 4) { number 32, false }
field(:_next, 8) { number 32, false }
def item
addr = _ptr
return if addr == 0
@_tg._at(addr)._get
end
def item=(v)
addr = _ptr
raise 'null pointer' if addr == 0
@_tg.at(addr)._set(v)
end
def prev
addr = _prev
return if addr == 0
_at(addr)
end
def next
addr = _next
return if addr == 0
_at(addr)
end
def inspect ; "#<DfLinkedList prev=#{'0x%X' % _prev} next=#{'0x%X' % _next} #{item.inspect}>" ; end
end
class Global < MemStruct