From a59cc20128679cfaee21ff85cd7bc3fe55f8b678 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Thu, 17 Jul 2014 18:51:29 +0000 Subject: [PATCH] CGrammar: Improve if/else in switch block, and improve "Fall through to next case" --- PBot/VERSION.pm | 2 +- modules/c2english/CGrammar.pm | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/PBot/VERSION.pm b/PBot/VERSION.pm index d6a2b781..20de070b 100644 --- a/PBot/VERSION.pm +++ b/PBot/VERSION.pm @@ -13,7 +13,7 @@ use warnings; # These are set automatically by the build/commit script use constant { BUILD_NAME => "PBot", - BUILD_REVISION => 736, + BUILD_REVISION => 737, BUILD_DATE => "2014-07-17", }; diff --git a/modules/c2english/CGrammar.pm b/modules/c2english/CGrammar.pm index 63eafc44..85e3dd08 100644 --- a/modules/c2english/CGrammar.pm +++ b/modules/c2english/CGrammar.pm @@ -216,10 +216,12 @@ compound_statement: $return .= "End block.\n" if not $arg{context}; if ($arg{context} - and $arg{context} ne 'do loop' - and $arg{context} ne 'case' - and $arg{context} ne 'function definition statement' - and $arg{context} ne 'function definition') { + and $arg{context} !~ /do loop/ + and $arg{context} !~ /if statement/ + and $arg{context} !~ /else block/ + and $arg{context} !~ /case/ + and $arg{context} !~ /function definition statement/ + and $arg{context} !~ /function definition/) { $return .= "End $arg{context}.\n"; } 1; @@ -327,7 +329,7 @@ for_increment: expression[context => 'for increment statement'] selection_statement: - 'if' '(' expression[context => 'if conditional'] ')' statement[context => 'if statement'] + 'if' '(' expression[context => 'if conditional'] ')' statement[context => "$arg{context}|if statement"] { if ($item{expression} =~ /^(\d+)$/) { if ($1 == 0) { @@ -341,7 +343,7 @@ selection_statement: } $return .= "^L$item{statement}"; } - ('else' statement[context => 'else block'] + ('else' statement[context => "$arg{context}|else block"] { $return = "Otherwise, ^L$item{statement}"; } )(?) { $return .= join('',@{$item[-1]}); } @@ -353,7 +355,7 @@ selection_statement: jump_statement: 'break' ';' { - if ($arg{context} eq 'switch' or $arg{context} eq 'case') { + if ($arg{context} =~ /switch/ or $arg{context} =~ /case/) { $return = "Exit switch block.\n"; } elsif (length $arg{context}) { $return = "Break from the $arg{context}.\n"; @@ -416,7 +418,7 @@ labeled_statement: { my $last = pop @{$item[-1]}; my $statements = join('', @{$item[-1]}); - if (length $statements and $statements !~ /Exit switch block/) { + if (length $statements and $statements !~ /Exit switch block\.\s*$/) { $statements .= "Fall through to the next case.\n"; } $return = "If it has the value $item[-2], ^L$statements$last";