From de375e63a1586dee4585cfda93ea9b80f4c76ef9 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Mon, 21 Jul 2014 07:11:11 +0000 Subject: [PATCH] CGrammar: Add _Generic; fix issue with compound-literals --- PBot/VERSION.pm | 4 ++-- modules/c2english/CGrammar.pm | 42 ++++++++++++++++++++++++++++------- modules/c2english/c2eng.pl | 5 ++--- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/PBot/VERSION.pm b/PBot/VERSION.pm index 0f6ef9e6..2ac0284e 100644 --- a/PBot/VERSION.pm +++ b/PBot/VERSION.pm @@ -13,8 +13,8 @@ use warnings; # These are set automatically by the build/commit script use constant { BUILD_NAME => "PBot", - BUILD_REVISION => 740, - BUILD_DATE => "2014-07-17", + BUILD_REVISION => 741, + BUILD_DATE => "2014-07-21", }; 1; diff --git a/modules/c2english/CGrammar.pm b/modules/c2english/CGrammar.pm index f0fe1ed0..391e29b6 100644 --- a/modules/c2english/CGrammar.pm +++ b/modules/c2english/CGrammar.pm @@ -623,7 +623,11 @@ logical_OR_AND_expression: { if (defined $arg{context} and $arg{context} eq 'for conditional') { print STDERR "hmm2\n"; } my $expression = join('', @{$item[1]}); - if($arg{context} =~ /initializer expression$/ and $expression =~ / / and $expression !~ /^the .*? number \S+$/i and $expression !~ /^the result of the/) { + if($arg{context} =~ /initializer expression$/ + and $expression =~ / / + and $expression !~ /^the .*? number \S+$/i + and $expression !~ /the size of/i + and $expression !~ /^the result of the/) { $return = 'the result of the expression ^L'; } $return .= $expression; @@ -1213,7 +1217,13 @@ postfix_productions: | {""} postfix_expression: - primary_expression postfix_productions[primary_expression => $item[1], context => $arg{context}] + '(' type_name ')' '{' initializer_list '}' postfix_productions[context => "$arg{context}|compound literal"](?) + { + my $postfix = $item[-1]->[0]; + $return = "A compound-literal of type $item{type_name} initialized to { $item{initializer_list} }"; + $return = "$postfix $return" if $postfix; + } + | primary_expression postfix_productions[primary_expression => $item[1], context => $arg{context}] { my $postfix_productions = $item{'postfix_productions'}; @@ -1227,12 +1237,6 @@ postfix_expression: $return = undef; } } - | '(' type_name ')' '{' initializer_list '}' postfix_productions[context => "$arg{context}|compound literal"](?) - { - my $postfix = $item[-1]->[0]; - $return = "A compound-literal of type $item{type_name} initialized to { $item{initializer_list} }"; - $return = "$postfix $return" if $postfix; - } postfix_suffix: '[' expression ']' @@ -1286,8 +1290,30 @@ primary_expression: | constant | string | identifier + | generic_selection | {} # nothing +generic_selection: + '_Generic' '(' assignment_expression ',' generic_assoc_list ')' + { $return = "a generic-selection on $item{assignment_expression} yielding $item{generic_assoc_list}"; } + +generic_assoc_list: + + { + if (@{$item[-1]} == 1) { + $return = $item[-1]->[0]; + } else { + my $last = pop @{$item[-1]}; + $return = join(', ', @{$item[-1]}) . " and $last"; + } + } + +generic_association: + type_name ':' assignment_expression + { $return = "$item{assignment_expression} in the case that it has type $item{type_name}"; } + | 'default' ':' assignment_expression + { $return = "$item{assignment_expression} in the default case"; } + Alignas: '_Alignas' | 'alignas' diff --git a/modules/c2english/c2eng.pl b/modules/c2english/c2eng.pl index f7d6d470..c7fe4001 100755 --- a/modules/c2english/c2eng.pl +++ b/modules/c2english/c2eng.pl @@ -19,9 +19,6 @@ if ($opt_T ) { $::RD_HINT = 1; $Parse::RecDescent::skip = '\s*'; -# This may be necessary.. -# $::RD_AUTOACTION = q { [@item] }; - my $parser; if($opt_P or !eval { require PCGrammar }) { @@ -76,6 +73,8 @@ foreach my $arg (@ARGV) { $output =~ s/by evaluate the/by the/g; $output =~ s/the a /the /g; $output =~ s/Then if it has the value/If it has the value/g; + $output =~ s/result of the expression a generic-selection/result of a generic-selection/g; + $output =~ s/the function a generic-selection/the function resulting from a generic-selection/g; $output =~ s/\.\s+Then exit switch block/ and then exit switch block/g; foreach my $quote (@quotes) {