3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-19 10:29:30 +01:00

CGrammar: Add designated initializers

This commit is contained in:
Pragmatic Software 2014-07-02 01:32:13 +00:00
parent aca698374c
commit 5eee740cd7
2 changed files with 53 additions and 7 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 => 691, BUILD_REVISION => 692,
BUILD_DATE => "2014-07-01", BUILD_DATE => "2014-07-01",
}; };

View File

@ -804,7 +804,9 @@ init_declarator:
} }
initializer: initializer:
comment(?) assignment_expression comment(?) designation initializer
{ $return = "$item[1] $item[2]"; }
| comment(?) assignment_expression comment(?)
{ {
$return = $item[2]; $return = $item[2];
@ -817,7 +819,7 @@ initializer:
} }
} }
| '{' comment(?) initializer_list (',' )(?) '}' | '{' comment(?) initializer_list (',' )(?) '}'
{ $return = 'the set { ' . $item{'initializer_list'} . ' }'; } { $return = 'the list { ' . $item{'initializer_list'} . ' }'; }
initializer_list: initializer_list:
<leftop: initializer ',' initializer > <leftop: initializer ',' initializer >
@ -828,12 +830,56 @@ initializer_list:
my $init = pop @inits; my $init = pop @inits;
$return = join(', ',@inits) . ', and ' .$init; $return = join(', ',@inits) . ', and ' .$init;
} elsif ($#inits == 1) { } elsif ($#inits == 1) {
$return = $inits[0] . ' and ' . $inits[1]; $return = $inits[0] . ', and ' . $inits[1];
} else { } else {
$return = $inits[0]; $return = $inits[0];
} }
} }
designation:
designator_list '='
{
$return = $item{designator_list};
}
designator_list:
designator(s)
{
$return = join('', @{$item{'designator(s)'}});
}
designator:
'[' constant_expression ']'
{
my $expression = $item{constant_expression};
if ($expression =~ /^\d+$/) {
$expression++;
my ($last_digit) = $expression =~ /(\d)$/;
if ($last_digit == 1) {
if ($expression =~ /11$/) {
$expression .= 'th';
} else {
$expression .= 'st';
}
} elsif ($last_digit == 2) {
$expression .= 'nd';
} elsif ($last_digit == 3) {
$expression .= 'rd';
} else {
$expression .= 'th';
}
$expression = "the $expression element";
} else {
$expression = "the element at location $expression";
}
$return = "$expression set to";
}
| '.' identifier
{
$return = "the member $item{identifier} set to";
}
unary_expression: unary_expression:
postfix_expression postfix_expression
{ $return = $item{postfix_expression}; } { $return = $item{postfix_expression}; }
@ -918,7 +964,7 @@ postfix_productions:
{ {
my $expression = ''; my $expression = '';
if (@{$item[-2]}) { if (@{$item[-2]}) {
$expression = join(' and ', @{$item[-2]}); $expression = join(', and ', @{$item[-2]});
} }
my $postfix = $item[-1]->[0]; my $postfix = $item[-1]->[0];
@ -1083,7 +1129,7 @@ postfix_expression:
| '(' type_name ')' '{' initializer_list '}' postfix_productions[context => "$arg{context}|compound literal"](?) | '(' type_name ')' '{' initializer_list '}' postfix_productions[context => "$arg{context}|compound literal"](?)
{ {
my $postfix = $item[-1]->[0]; my $postfix = $item[-1]->[0];
$return = "A compound-literal of type $item{type_name} initialized to the set { $item{initializer_list} }"; $return = "A compound-literal of type $item{type_name} initialized to the list { $item{initializer_list} }";
$return = "$postfix $return" if $postfix; $return = "$postfix $return" if $postfix;
} }
@ -1516,7 +1562,7 @@ enum_specifier:
$return .= " comprising $enumerator_list[0]"; $return .= " comprising $enumerator_list[0]";
} else { } else {
my $last = pop @enumerator_list; my $last = pop @enumerator_list;
$return .= ' comprising ' . join(', ', @enumerator_list) . " and $last"; $return .= ' comprising ' . join(', ', @enumerator_list) . ", and $last";
} }
} }