3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-20 02:49:49 +01:00

CGrammar: Improve and optimize conditional_expression rule; improve translation of parenthetical expressions

This commit is contained in:
Pragmatic Software 2014-06-15 00:46:49 +00:00
parent bf9586b6a8
commit 632a9582d5
2 changed files with 29 additions and 46 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 => 639, BUILD_REVISION => 640,
BUILD_DATE => "2014-06-14", BUILD_DATE => "2014-06-14",
}; };

View File

@ -419,14 +419,19 @@ assignment_expression:
| conditional_expression[context => $arg{context}] | conditional_expression[context => $arg{context}]
conditional_expression: conditional_expression:
logical_OR_AND_expression[context => $arg{context}] logical_OR_AND_expression[context => $arg{context}] conditional_ternary_expression
'?' expression[context => 'conditional_expression1']
':' conditional_expression[context => 'conditional_expression2']
{ {
$return = "the value depending on if $item{logical_OR_AND_expression} is true" . if($item{conditional_ternary_expression}) {
" then $item{expression} otherwise $item{conditional_expression}"; $return = "depending on if $item{logical_OR_AND_expression} is true then $item{conditional_ternary_expression}";
} else {
$return = $item{logical_OR_AND_expression};
} }
| logical_OR_AND_expression[context => $arg{context}] }
conditional_ternary_expression:
'?' expression ':' conditional_expression
{ $return = "$item{expression} otherwise $item{conditional_expression}"; }
| { "" } # nothing
assignment_operator: assignment_operator:
'=' '='
@ -547,7 +552,7 @@ logical_OR_AND_expression:
<leftop: <leftop:
rel_add_mul_shift_expression[context => $arg{context}] rel_add_mul_shift_expression[context => $arg{context}]
log_OR_AND_bit_or_and_eq log_OR_AND_bit_or_and_eq
rel_add_mul_shift_expression[context => 'logical_OR_AND_expression'] > rel_add_mul_shift_expression[context => 'logical_OR_AND_expression']>
{ {
if (defined $arg{context} and $arg{context} eq 'for_expression') { print STDERR "hmm2\n"; } if (defined $arg{context} and $arg{context} eq 'for_expression') { print STDERR "hmm2\n"; }
my @ands = @{$item[1]}; my @ands = @{$item[1]};
@ -606,19 +611,8 @@ cast_expression:
{ $return = "$item{cast_expression} type-casted as $item{type_name}"; } { $return = "$item{cast_expression} type-casted as $item{type_name}"; }
| unary_expression[context => $arg{context}] | unary_expression[context => $arg{context}]
{ $return = $item{unary_expression}; } { $return = $item{unary_expression}; }
#( ...closure )(?)
# {
# if ($arg{context} eq 'statement' #&& !($return =~ /^Perform/)
# )
# {
# if (${$item[-1]}[0]) {
# $return .= ".\n";
# }
# }
#}
declaration_list: declaration_list:
# <skip: '\s*'>
preproc[context => 'statement'](?) declaration(s) preproc[context => 'statement'](?) declaration(s)
{ $return = join('', @{$item{'preproc(?)'}}) . join('', @{$item{'declaration(s)'}}); } { $return = join('', @{$item{'preproc(?)'}}) . join('', @{$item{'declaration(s)'}}); }
@ -1000,39 +994,28 @@ narrow_closure:
primary_expression: primary_expression:
'(' expression ')' (...narrow_closure)(?) '(' expression ')' (...narrow_closure)(?)
{ {
print STDERR "wtf\n";
print STDERR "expression: [$item{expression}]; text: [$text]\n";
my $expression = $item{expression} ; my $expression = $item{expression} ;
my $repeats = 1; my $repeats = 1;
my $ending = 1;
if ($expression =~ /^the (\d+)-layered parenthetical expression/) { if ($expression =~ /^the expression (\(+)/) {
$repeats = $1 + 1; $repeats = (length $1) + 1;
$expression =~ s/^the \d+-layered parenthetical expression //; print STDERR "Got repeats: $repeats from matching [$1]\n";
} elsif ($expression =~ /^the parenthetical expression/) { $expression =~ s/^the expression \(+//;
$repeats = 2;
$expression =~ s/^the parenthetical expression //;
} }
if ($expression =~ / now$/) { print STDERR "repeats: $repeats\n";
$ending++;
$expression =~ s/ now$//;
$expression .= " (now drop $ending layers of context)" ;
} elsif ($expression =~ /now drop (\d+) layers of context\)$/ ) {
$ending = $1 + 1;
$expression =~ s/\d+ layers of context\)$/$ending layers of context \)/;
} else { $expression .= ' now'; }
if ($repeats > 1) {
$return = "the $repeats-layered parenthetical expression $expression";
} else {
$return = "the parenthetical expression $expression";
}
if (@{$item[-1]}) { $expression .= ')';
$return =~ s/ now$//; $return = "the expression ";
} $return .= '(' x $repeats;
$return .= $expression;
} }
| constant | constant
| string | string
| identifier | identifier
| { "" } # nothing
declarator: declarator:
direct_declarator direct_declarator