From a7afe04f3bd46dda12728f2cc002f7c9443f055f Mon Sep 17 00:00:00 2001 From: jj Date: Fri, 27 Apr 2012 17:59:54 +0200 Subject: [PATCH] ruby: name nested compounds, to allow cpp_new etc --- plugins/ruby/codegen.pl | 14 ++++++++------ plugins/ruby/ruby-memstruct.rb | 3 ++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/plugins/ruby/codegen.pl b/plugins/ruby/codegen.pl index 4add74ddd..9adff9d20 100755 --- a/plugins/ruby/codegen.pl +++ b/plugins/ruby/codegen.pl @@ -328,8 +328,13 @@ sub render_item_compound { my $cppns = $pns . '::' . $item->getAttribute('ld:typedef-name'); my $subtype = $item->getAttribute('ld:subtype'); + + my @namecomponents = split('::', $cppns); + shift @namecomponents; + my $classname = join('_', map { rb_ucase($_) } @namecomponents); + if (!$subtype || $subtype eq 'bitfield') { - push @lines_rb, "compound {"; + push @lines_rb, "compound(:$classname) {"; indent_rb { if (!$subtype) { render_struct_fields($item, $cppns); @@ -339,10 +344,7 @@ sub render_item_compound { }; push @lines_rb, "}" } elsif ($subtype eq 'enum') { - my @namecomponents = split('::', $cppns); - shift @namecomponents; - my $enumclassname = join('_', map { rb_ucase($_) } @namecomponents); - push @lines_rb, "class ::DFHack::$enumclassname"; + push @lines_rb, "class ::DFHack::$classname"; indent_rb { # declare constants render_enum_fields($item); @@ -350,7 +352,7 @@ sub render_item_compound { push @lines_rb, "end\n"; # actual field - render_item_number($item, $enumclassname); + render_item_number($item, $classname); } else { print "no render compound $subtype\n"; } diff --git a/plugins/ruby/ruby-memstruct.rb b/plugins/ruby/ruby-memstruct.rb index 2e935b9c1..3b30b0e9d 100644 --- a/plugins/ruby/ruby-memstruct.rb +++ b/plugins/ruby/ruby-memstruct.rb @@ -82,8 +82,9 @@ class Compound < MemStruct def global(glob) Global.new(glob) end - def compound(&b) + def compound(name=nil, &b) m = Class.new(Compound) + DFHack.const_set(name, m) if name m.instance_eval(&b) m.new end