3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-10-01 17:16:39 +02:00

CGrammar: Improve translation of switch/case block

This commit is contained in:
Pragmatic Software 2014-07-17 18:36:09 +00:00
parent 46a0184791
commit af8787797a
3 changed files with 30 additions and 15 deletions

View File

@ -13,8 +13,8 @@ use warnings;
# These are set automatically by the build/commit script
use constant {
BUILD_NAME => "PBot",
BUILD_REVISION => 735,
BUILD_DATE => "2014-07-16",
BUILD_REVISION => 736,
BUILD_DATE => "2014-07-17",
};
1;

View File

@ -220,7 +220,7 @@ compound_statement:
and $arg{context} ne 'case'
and $arg{context} ne 'function definition statement'
and $arg{context} ne 'function definition') {
$return .= "End $arg{context}.";
$return .= "End $arg{context}.\n";
}
1;
}
@ -295,7 +295,7 @@ iteration_statement:
}
if ($item{statement}) {
$return .= $item{statement} . "\n";
$return .= $item{statement} . "\n";
} else {
$return .= "do nothing.\n";
}
@ -347,14 +347,14 @@ selection_statement:
{ $return .= join('',@{$item[-1]}); }
| 'switch' '(' expression[context => 'switch conditional'] ')' statement[context => 'switch']
{
$return = "Given the expression \'^L$item{expression}\',\n^L$item{statement}";
$return = "When given the expression \'^L$item{expression}\', ^L$item{statement}";
}
jump_statement:
'break' ';'
{
if ($arg{context} eq 'switch' or $arg{context} eq 'case') {
$return = "Break case.\n";
$return = "Exit switch block.\n";
} elsif (length $arg{context}) {
$return = "Break from the $arg{context}.\n";
} else {
@ -393,15 +393,34 @@ expression_statement:
}
} else {
$return = $expression;
$return .= ".\n" unless $arg{context} =~ /^for /;
$return .= ".\n" unless $arg{context} =~ /for conditional$/;
}
}
labeled_statement:
identifier ':' statement[context => 'label'] (';')(?)
{ $return = "Let there be a label $item{identifier}.\n$item{statement}"; }
| 'case' constant_expression ':' statement[context => 'case']
{ $return = "When it has the value $item{constant_expression}, ^L$item{statement}"; }
| ('case' constant_expression
{ $return = $item{constant_expression}; }
':')(s)
{
my @items = @{$item[1]};
if (@items <= 2) {
$return = join(' or ', @{$item[1]});
} else {
my $last = pop @items;
$return = join(', ', @items) . " or $last";
}
}
(statement[context => 'case'])(s)
{
my $last = pop @{$item[-1]};
my $statements = join('', @{$item[-1]});
if (length $statements and $statements !~ /Exit switch block/) {
$statements .= "Fall through to the next case.\n";
}
$return = "If it has the value $item[-2], ^L$statements$last";
}
| 'default' ':' statement
{ $return = "In the default case, ^L$item{statement}"; }
@ -1172,12 +1191,6 @@ postfix_productions:
}
}
}
# having done the simplest cases, we go to the catch all for left recursions.
| primary_expression postfix_suffix(s)
{
print STDERR "Untested code!\n";
$return = $item{primary_expression} . "'s " . join('',@{$item{'postfix_suffix(s)'}});
}
| {""}
postfix_expression:

View File

@ -75,6 +75,8 @@ foreach my $arg (@ARGV) {
$output =~ s/the evaluate the/the/g;
$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/\.\s+Then exit switch block/ and then exit switch block/g;
foreach my $quote (@quotes) {
next unless $quote;