From b0ce2d422e14f7d86f2a2382359156196cf80df9 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Mon, 16 Jun 2014 21:13:30 +0000 Subject: [PATCH] CGrammar: Improve structures and constant literals --- PBot/VERSION.pm | 4 ++-- modules/c2english.pl | 8 +++++-- modules/c2english/CGrammar.pm | 41 ++++++++++++++++++++--------------- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/PBot/VERSION.pm b/PBot/VERSION.pm index 978c68f5..c7aa4c1c 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 => 645, - BUILD_DATE => "2014-06-15", + BUILD_REVISION => 646, + BUILD_DATE => "2014-06-16", }; 1; diff --git a/modules/c2english.pl b/modules/c2english.pl index 14049a8a..f4f00315 100755 --- a/modules/c2english.pl +++ b/modules/c2english.pl @@ -386,7 +386,7 @@ open $fh, '>', 'code2eng.c' or die "Could not write code: $!"; print $fh $code; close $fh; -$output = `./c2eng.pl code2eng.c` if not defined $output; +$output = `./c2eng.pl -P code2eng.c` if not defined $output; if(not $has_function and not $has_main) { $output =~ s/Let .main. be a function taking no parameters and returning int.\s*To perform the function.\s*(return 0.)?//i; @@ -401,9 +401,13 @@ if(not $has_function and not $has_main) { $output =~ s/\s+/ /; if(not $output) { - $output = "Does not compute. I only know about C89 and valid code.\n"; + $output = "Does not compute; I only know about C89 and valid code -- though I am still under construction.\n"; } +# beautification +$output =~ s/the value the expression/the value of the expression/g; +$output =~ s/the value the address/the address/g; + print "[Work-in-progress; there will be issues!] $output\n"; sub execute { diff --git a/modules/c2english/CGrammar.pm b/modules/c2english/CGrammar.pm index eeb3e992..7f561c20 100644 --- a/modules/c2english/CGrammar.pm +++ b/modules/c2english/CGrammar.pm @@ -861,6 +861,7 @@ postfix_productions: } if($postfix) { + $return =~ s/^(Call|Insert)/the result of/; $return = "$postfix $return"; } 1; @@ -1246,12 +1247,14 @@ struct_or_union_specifier: my $identifier = join('',@{$item{'identifier(?)'}}); $return = join('',@{$item{'comment(?)'}}) . $item{struct_or_union}; if ($identifier) { $return .= " tagged $identifier"; } - $return .= " with members $item{struct_declaration_list}"; + my $plural = $item{struct_declaration_list} =~ / and / ? 's' : ''; + $return .= " with member$plural $item{struct_declaration_list}"; } | struct_or_union identifier { - $item{struct_or_union} =~ s/^(a|an)/the/; - $return = "$item{struct_or_union} $item{identifier}"; + $item{struct_or_union} =~ s/^(a|an)//; + $return = $item{identifier} =~ m/^`[aeiouy]/ ? 'an' : 'a'; + $return .= " $item{identifier} $item{struct_or_union}"; } struct_declaration_list: @@ -1372,29 +1375,33 @@ comment_cxx: } constant: - /-?[0-9]*\.[0-9]+f?/ + /-?[0-9]*\.[0-9]*[lf]{0,2}/i { - if ($item[1] =~ /\D/) { + if ($item[1] =~ s/f$//i) { $return = "the floating point number $item[1]"; - } else { + } elsif ($item[1] =~ s/l$//i) { + $return = "long double $item[1]"; + } else { $return = $item[1]; } } - | /0x[0-9a-fA-F]+/ ('L')(?) + | /0x[0-9a-f]+[lu]{0,2}/i { - if ($item[-1]) { - $return = 'the long ' ."hexadecimal number $item[1]"; - } else { - $return = 'the ' . "hexadecimal number $item[1]"; - } + $return .= 'unsigned ' if $item[1] =~ s/[Uu]//; + $return .= 'long ' if $item[1] =~ s/[Ll]//; + $return = "the $return" . "hexadecimal number $item[1]"; } - | /0\d+/ - { $return = "the octal number $item[1]"; } - |/-?[0-9]+[lu]?/i # integer constant + | /0\d+[lu]{0,2}/i + { + $return .= 'unsigned ' if $item[1] =~ s/[Uu]//; + $return .= 'long ' if $item[1] =~ s/[Ll]//; + $return = "the $return" . "octal number $item[1]"; + } + |/-?[0-9]+[lu]{0,2}/i # integer constant { $return = $item[-1]; - $return =~ s/[Uu]$/(unsigned)/; - $return =~ s/[Ll]$/(long)/; + $return = "long $return" if $return =~ s/[Ll]//; + $return = "unsigned $return" if $return =~ s/[Uu]//; } | /(?:\'((?:\\\'|(?!\').)*)\')/ # character constant {