From 61ba30f49948649f29a1c1a878a6ac55886cffa6 Mon Sep 17 00:00:00 2001 From: Ben Lubar Date: Thu, 14 May 2020 15:31:46 -0500 Subject: [PATCH] begin implementing lisp structures attributes. see DFHack/df-structures#404 for ref-size. --- Common.pm | 110 +++++++++++++++++++++++++++++++++++++++++++- StructFields.pm | 16 +++++++ StructType.pm | 11 ----- data-definition.xsd | 2 + df.entities.xml | 2 +- df.units.xml | 2 +- df.world-data.xml | 6 +-- 7 files changed, 132 insertions(+), 17 deletions(-) diff --git a/Common.pm b/Common.pm index 22974be24..57b9c1346 100644 --- a/Common.pm +++ b/Common.pm @@ -19,7 +19,8 @@ BEGIN { &is_primitive_type &primitive_type_name &get_primitive_base - *weak_refs *strong_refs *header_refs &header_ref ®ister_ref &decode_type_name_ref + *weak_refs *strong_refs *header_refs &header_ref ®ister_ref + &decode_type_name_ref &translate_lookup &with_capture_traits &with_emit_traits *cur_header_name %header_data &with_header_file @@ -265,6 +266,113 @@ sub decode_type_name_ref($;%) { } } +sub translate_lookup { + my ($str) = @_; + return undef unless defined $str; + if ($str eq "0") { + return "&zero"; + } + if ($str eq "-1") { + return "&negone"; + } + if ($str =~ /^\$global\.([a-zA-Z][_a-zA-Z0-9]*)$/) { + return "df::global::$1"; + } + if ($str eq "\$\$") { + return "aux"; + } + if ($str eq "\$") { + return "val"; + } + if ($str =~ /^(.+)\.([a-zA-Z][_a-zA-Z0-9]*)$/) { + my $field = $2; + my $base = translate_lookup($1); + return "_fieldptr($base, $field)"; + } + if ($str =~ /^(.+)\.ref-target$/) { + my $base = translate_lookup($1); + return "_reftarget($base)"; + } + if ($str =~ /^(.+)\.refers-to$/) { + my $base = translate_lookup($1); + return "_refersto($base)"; + } + if ($str =~ /^(.+)\.index-refers-to$/) { + my $base = translate_lookup($1); + return "_indexrefersto($base)"; + } + if ($str =~ /^(.+)\._parent$/) { + my $base = translate_lookup($1); + return "_TODOparent($base)"; + } + if ($str =~ /^(.+)\._global$/) { + my $base = translate_lookup($1); + return "_TODOglobal($base)"; + } + if ($str =~ /^(.+)\._upglobal$/) { + my $base = translate_lookup($1); + return "_TODOglobal(_TODOparent($base))"; + } + if ($str =~ /^(.+)\._key$/) { + my $base = translate_lookup($1); + return "_TODOkey($base)"; + } + if ($str =~ /^(.+)\[([^\[\]]+)\]$/) { + my $idx = $2; + my $base = translate_lookup($1); + $idx = translate_lookup($idx); + return "_index($base, $idx)"; + } + if ($str =~ /^\(find-plant-raw ([^ ]+)\)$/) { + my $idx = translate_lookup($1); + return "df::plant_raw::find($idx)"; + } + if ($str =~ /^\(food-mat-by-idx \$([a-zA-Z][_a-zA-Z0-9]*) ([^ ]+)\)$/) { + my $cat = $1; + my $idx = translate_lookup($2); + return "_foodmat($cat, $idx)"; + } + if ($str =~ /^\(find-creature ([^ ]+)\)$/) { + my $idx = translate_lookup($1); + return "df::creature_raw::find($idx)"; + } + if ($str =~ /^\(find-entity ([^ ]+)\)$/) { + my $idx = translate_lookup($1); + return "df::historical_entity::find($idx)"; + } + if ($str =~ /^\(find-instance \$([a-zA-Z][_a-zA-Z0-9]*) ([^ ]+)\)$/) { + my $type = $1; + my $idx = translate_lookup($2); + return "df::${type}::find($idx)"; + } + if ($str =~ /^\(find-by-id ([^ ]+) \$([a-zA-Z][_a-zA-Z0-9]*) ([^ ]+)\)$/) { + my $idx = $3; + my $fld = $2; + my $vec = translate_lookup($1); + $idx = translate_lookup($idx); + return "_TODOfind($vec, $fld, $idx)"; + } + if ($str =~ /^\(\* ([^ ]+) ([^ ]+)\)$/) { + my $b = $2; + my $a = translate_lookup($1); + $b = translate_lookup($b); + return "_TODOmultiply($a, $b)"; + } + if ($str =~ /^\(material-by-id ([^ ]+) ([^ ]+)\)$/) { + my $idx = $2; + my $type = translate_lookup($1); + $idx = translate_lookup($idx); + return "_material($type, $idx)"; + } + if ($str =~ /^\(item-subtype-target ([^ ]+) ([^ ]+)\)$/) { + my $st = $2; + my $type = translate_lookup($1); + $st = translate_lookup($st); + return "_itemsubtype($type, $st)"; + } + die "unhandled lookup expression ${str}"; +} + # Trait generation our @trait_lines; diff --git a/StructFields.pm b/StructFields.pm index 65ee580de..eba7c11d9 100644 --- a/StructFields.pm +++ b/StructFields.pm @@ -460,6 +460,22 @@ sub render_field_metadata_rec($$) { $field_defs_extra{$name}{union_tag_attr} = "\"$uta\""; $extra = "&EXTRA($name)"; } + if (my $rt = $field->getAttribute('refers-to')) { + # TODO + translate_lookup $rt; + } + if (my $irt = $field->getAttribute('index-refers-to')) { + # TODO + translate_lookup $irt; + } + if (my $av = $field->getAttribute('aux-value')) { + # TODO + translate_lookup $av; + } + if (my $rs = $field->getAttribute('ref-size')) { + # TODO + translate_lookup $rs; + } if ($meta eq 'number') { my $tname = primitive_type_name($subtype); diff --git a/StructType.pm b/StructType.pm index b53157e39..c5bd936cd 100644 --- a/StructType.pm +++ b/StructType.pm @@ -24,17 +24,6 @@ use StructFields; # MISC -sub translate_lookup($) { - my ($str) = @_; - return undef unless $str && $str =~ /^\$global((\.[_a-zA-Z0-9]+)+)$/; - my @fields = split /\./, substr($1,1); - my $expr = "df::global::".shift(@fields); - for my $fn (@fields) { - $expr = "_fieldptr($expr, $fn)"; - } - return $expr; -} - sub emit_find_instance(\%$) { my ($rinfo, $tag) = @_; diff --git a/data-definition.xsd b/data-definition.xsd index ffdbb3334..58e6a1b1d 100644 --- a/data-definition.xsd +++ b/data-definition.xsd @@ -458,6 +458,8 @@ + + diff --git a/df.entities.xml b/df.entities.xml index 65a046b78..6d0297e80 100644 --- a/df.entities.xml +++ b/df.entities.xml @@ -1191,7 +1191,7 @@ - + diff --git a/df.units.xml b/df.units.xml index 8d3f83cea..0ced6b9a7 100644 --- a/df.units.xml +++ b/df.units.xml @@ -1636,7 +1636,7 @@ - + diff --git a/df.world-data.xml b/df.world-data.xml index 21b542112..1070363fb 100644 --- a/df.world-data.xml +++ b/df.world-data.xml @@ -821,10 +821,10 @@ - - + + - 2d byte array with size based on map size + 2d byte array with size based on map size