3
0
mirror of https://github.com/pragma-/pbot.git synced 2025-05-06 06:37:23 +02:00

CGrammar: Improve handling of declarations and initializers; improve translation of sizeof in statement context

This commit is contained in:
Pragmatic Software 2014-07-08 00:33:50 +00:00
parent 4a646f61c9
commit cc0957b060
2 changed files with 40 additions and 40 deletions

View File

@ -13,7 +13,7 @@ use warnings;
# These are set automatically by the build/commit script # These are set automatically by the build/commit script
use constant { use constant {
BUILD_NAME => "PBot", BUILD_NAME => "PBot",
BUILD_REVISION => 717, BUILD_REVISION => 718,
BUILD_DATE => "2014-07-07", BUILD_DATE => "2014-07-07",
}; };

View File

@ -681,22 +681,16 @@ declaration:
my @identifiers = ($first_identifier) unless not length $first_identifier; my @identifiers = ($first_identifier) unless not length $first_identifier;
my $next_arg = shift @args; my $sep = '';
if ($next_arg =~ m/initialized/) { foreach my $arg (@args) {
$first_initializer = $next_arg; if ($arg =~ /initialized/) {
$first_qualifier = shift @args // ''; $first_initializer .= "$sep$arg";
} else { } else {
$first_qualifier = $next_arg; $first_qualifier .= "$sep$arg";
$first_initializer = shift @args // ''; }
$sep = ' ';
} }
if ($first_initializer !~ /^initialized/) {
$first_qualifier .= " $first_initializer" if $first_initializer;
$first_initializer = '';
}
my $remaining_args = join(' ', @args);
my @initializers; my @initializers;
if ($first_initializer) { if ($first_initializer) {
push @initializers, [ $first_identifier, $first_initializer ]; push @initializers, [ $first_identifier, $first_initializer ];
@ -708,13 +702,14 @@ declaration:
my ($qualifier, $initializer); my ($qualifier, $initializer);
my $identifier = shift @args; my $identifier = shift @args;
$next_arg = shift @args; my $sep = '';
if ($next_arg =~ m/initialized/) { foreach my $arg (@args) {
$initializer = $next_arg; if ($arg =~ /initialized/) {
$qualifier = shift @args // ''; $initializer .= "$sep$arg";
} else { } else {
$qualifier = $next_arg; $qualifier .= "$sep$arg";
$initializer = shift @args // ''; }
$sep = ' ';
} }
next unless $qualifier eq $first_qualifier; next unless $qualifier eq $first_qualifier;
@ -799,13 +794,11 @@ declaration:
$first_qualifier =~ s/an array/arrays/; $first_qualifier =~ s/an array/arrays/;
} }
$return .= "$first_qualifier "; $return .= "$first_qualifier ";
$return .= "$remaining_args " if $remaining_args;
$return .= $item{declaration_specifiers}; $return .= $item{declaration_specifiers};
} else { } else {
if (@identifiers == 1 and $item{declaration_specifiers} !~ /^(a|an)\s+/) { if (@identifiers == 1 and $item{declaration_specifiers} !~ /^(a|an)\s+/) {
$return .= $item{declaration_specifiers} =~ m/^[aeiouy]/ ? 'an ' : 'a '; $return .= $item{declaration_specifiers} =~ m/^[aeiouy]/ ? 'an ' : 'a ';
} }
$return .= "$remaining_args " if $remaining_args;
$return .= $item{declaration_specifiers}; $return .= $item{declaration_specifiers};
} }
@ -824,7 +817,7 @@ declaration:
$comma = ', '; $comma = ', ';
$and = ' and '; $and = ' and ';
} else { } else {
$initializer =~ s/^initialized to \^L//; $initializer =~ s/^\s*initialized to \^L//;
$return .= "$and$identifier to $initializer"; $return .= "$and$identifier to $initializer";
if ($i < @initializers - 2) { if ($i < @initializers - 2) {
$and = $comma = ', '; $and = $comma = ', ';
@ -881,18 +874,7 @@ initializer:
initializer_list: initializer_list:
<leftop: initializer ',' initializer > <leftop: initializer ',' initializer >
{ { $return = join(', ', @{$item[1]}); }
my @inits = @{$item[1]};
if ($#inits >1) {
my $init = pop @inits;
$return = join(', ',@inits) . ', and ' .$init;
} elsif ($#inits == 1) {
$return = $inits[0] . ', and ' . $inits[1];
} else {
$return = $inits[0];
}
}
designation: designation:
designator_list '=' designator_list '='
@ -979,11 +961,29 @@ unary_expression:
} }
} }
| 'sizeof' unary_expression[context => 'sizeof'] | 'sizeof' unary_expression[context => 'sizeof']
{ $return = "the size of $item{unary_expression}"; } {
if ($arg{context} =~ /statement$/) {
$return = "Evaluate and discard the size of ^L$item{unary_expression}";
} else {
$return = "the size of ^L$item{unary_expression}";
}
}
| 'sizeof' '(' type_name[context => 'sizeof'] ')' | 'sizeof' '(' type_name[context => 'sizeof'] ')'
{ $return = "the size of the type $item{type_name}"; } {
if ($arg{context} =~ /statement$/) {
$return = "Evaluate and discard the size of the type $item{type_name}";
} else {
$return = "the size of the type $item{type_name}";
}
}
| 'sizeof' '(' assignment_expression[context => 'sizeof'] ')' | 'sizeof' '(' assignment_expression[context => 'sizeof'] ')'
{ $return = "the size of the type of the expression ($item{assignment_expression})"; } {
if ($arg{context} =~ /statement$/) {
$return = "Evaluate and discard the size of the type of the expression (^L$item{assignment_expression})";
} else {
$return = "the size of the type of the expression (^L$item{assignment_expression})";
}
}
| Alignof '(' type_name ')' | Alignof '(' type_name ')'
{ $return = "the alignment of the type $item{type_name}"; } { $return = "the alignment of the type $item{type_name}"; }