3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-23 12:29:27 +01:00

CGrammar: Optimize preproc rules

This commit is contained in:
Pragmatic Software 2014-06-15 01:09:06 +00:00
parent 632a9582d5
commit 2dba87f453
3 changed files with 25 additions and 31 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 => 640, BUILD_REVISION => 641,
BUILD_DATE => "2014-06-14", BUILD_DATE => "2014-06-14",
}; };

View File

@ -391,8 +391,7 @@ close $fh;
$output = `./c2eng.pl code2eng.c` if not defined $output; $output = `./c2eng.pl code2eng.c` if not defined $output;
if(not $has_function and not $has_main) { 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*//; $output =~ s/Let .main. be a function taking no parameters and returning int.\s*To perform the function.\s*(return 0.)?//i;
$output =~ s/\s*Return 0.\s*$//;
$output =~ s/\s*Return 0.\s*End of function .main..\s*//; $output =~ s/\s*Return 0.\s*End of function .main..\s*//;
$output =~ s/\s*Do nothing.\s*$//; $output =~ s/\s*Do nothing.\s*$//;
$output =~ s/^\s*(.)/\U$1/; $output =~ s/^\s*(.)/\U$1/;

View File

@ -31,17 +31,17 @@ translation_unit:
| preproc[matchrule => 'translation_unit'] | preproc[matchrule => 'translation_unit']
preproc: preproc:
definition '#' (definition
| undefinition | undefinition
| inclusion | inclusion
| line | line
| error | error
| pragma | pragma
| preproc_conditional[matchrule => $arg{matchrule}] | preproc_conditional[matchrule => $arg{matchrule}])
definition: definition:
macro_definition macro_definition
| '#' 'define' identifier token_sequence(?) <skip: '[ \t]*'> "\n" | 'define' identifier token_sequence(?) <skip: '[ \t]*'> "\n"
{ {
my $token_sequence = join('',@{$item{'token_sequence(?)'}}); my $token_sequence = join('',@{$item{'token_sequence(?)'}});
$return = "Define the macro $item{identifier}"; $return = "Define the macro $item{identifier}";
@ -50,7 +50,7 @@ definition:
} }
macro_definition: macro_definition:
'#' 'define' identifier '(' <leftop: identifier ',' identifier> ')' token_sequence <skip: '[ \t]*'> "\n" 'define' identifier '(' <leftop: identifier ',' identifier> ')' token_sequence <skip: '[ \t]*'> "\n"
{ {
my @symbols = @{$item[-5]}; my @symbols = @{$item[-5]};
my $last; my $last;
@ -66,35 +66,35 @@ macro_definition:
} }
undefinition: undefinition:
'#' 'undef' identifier <skip: '[ \t]*'> "\n" 'undef' identifier <skip: '[ \t]*'> "\n"
{ {
@macros = grep { $_ ne $item{identifier} } @macros; @macros = grep { $_ ne $item{identifier} } @macros;
$return = "\nAnnul the definition of $item{identifier}.\n"; $return = "\nAnnul the definition of $item{identifier}.\n";
} }
inclusion: inclusion:
'#' 'include' '<' filename '>' <skip: '[ \t]*'> "\n" 'include' '<' filename '>' <skip: '[ \t]*'> "\n"
{ $return = "\nInclude system file $item{filename}.\n"; } { $return = "\nInclude system file $item{filename}.\n"; }
| '#' 'include' '"' filename '"' <skip: '[ \t]*'> "\n" | 'include' '"' filename '"' <skip: '[ \t]*'> "\n"
{ $return = "\nInclude user file $item{filename}.\n"; } { $return = "\nInclude user file $item{filename}.\n"; }
| '#' 'include' token | 'include' token
{ $return = "\nImport code noted by the token $item{token}.\n"; } { $return = "\nImport code noted by the token $item{token}.\n"; }
filename: filename:
/[_\.\-\w\/]+/ /[_\.\-\w\/]+/
line: line:
'#' 'line' constant ('"' filename '"' 'line' constant ('"' filename '"'
{ $return = "and filename $item{filename}"; } { $return = "and filename $item{filename}"; }
)(?) <skip: '[ \t]*'> "\n" )(?) <skip: '[ \t]*'> "\n"
{ $return = "\nThis is line number $item{constant} " . join('', @{$item[-3]}) . ".\n"; } { $return = "\nThis is line number $item{constant} " . join('', @{$item[-3]}) . ".\n"; }
error: error:
'#' 'error' token_sequence(?) <skip: '[ \t]*'> "\n" 'error' token_sequence(?) <skip: '[ \t]*'> "\n"
{ $return = "Stop compilation with error \"" . join('', @{$item{'token_sequence(?)'}}) . "\".\n"; } { $return = "Stop compilation with error \"" . join('', @{$item{'token_sequence(?)'}}) . "\".\n"; }
pragma: pragma:
'#' 'pragma' token_sequence(?) <skip: '[ \t]*'> "\n" 'pragma' token_sequence(?) <skip: '[ \t]*'> "\n"
{ {
my $pragma = join('',@{$item{'token_sequence(?)'}}); my $pragma = join('',@{$item{'token_sequence(?)'}});
if ($pragma) { $pragma = ' "$pragma"'; } if ($pragma) { $pragma = ' "$pragma"'; }
@ -109,19 +109,19 @@ preproc_conditional:
(elif_parts[matchrule => $rule_name])(?) (elif_parts[matchrule => $rule_name])(?)
(else_parts[matchrule => $rule_name])(?) (else_parts[matchrule => $rule_name])(?)
{ $return .= join('',@{$item[-2]}) . join('',@{$item[-1]}); } { $return .= join('',@{$item[-2]}) . join('',@{$item[-1]}); }
'#' 'endif' 'endif'
{ $return .= "End preprocessor conditional.\n"; } { $return .= "End preprocessor conditional.\n"; }
if_line: if_line:
'#' 'ifdef' identifier <skip: '[ \t]*'> "\n" 'ifdef' identifier <skip: '[ \t]*'> "\n"
{ $return .= "If the macro $item{identifier} is defined, then ^L"; } { $return .= "If the macro $item{identifier} is defined, then ^L"; }
| '#' 'ifndef' identifier <skip: '[ \t]*'> "\n" | 'ifndef' identifier <skip: '[ \t]*'> "\n"
{ $return .= "If the macro $item{identifier} is not defined, then ^L"; } { $return .= "If the macro $item{identifier} is not defined, then ^L"; }
| '#' 'if' constant_expression <skip: '[ \t]*'> "\n" | 'if' constant_expression <skip: '[ \t]*'> "\n"
{ $return .= "If the preprocessor condition^L $item{constant_expression} is true, then ^L"; } { $return .= "If the preprocessor condition^L $item{constant_expression} is true, then ^L"; }
elif_parts: elif_parts:
('#' 'elif' constant_expression ('elif' constant_expression
{ $return .= "Otherwise, if the preprocessor condition $item{constant_expression} is true, then ^L"; } { $return .= "Otherwise, if the preprocessor condition $item{constant_expression} is true, then ^L"; }
(<matchrule: $rule_name> )[matchrule => $arg{matchrule}](s?) (<matchrule: $rule_name> )[matchrule => $arg{matchrule}](s?)
{ $return .= join('',@{$item[-1]}); } { $return .= join('',@{$item[-1]}); }
@ -129,7 +129,7 @@ elif_parts:
{ $return = join('', @{$item[-1]}); } { $return = join('', @{$item[-1]}); }
else_parts: else_parts:
'#' 'else' 'else'
{ $rule_name = $arg{matchrule}; } { $rule_name = $arg{matchrule}; }
(<matchrule: $rule_name>)[matchrule => $arg{matchrule}](s?) (<matchrule: $rule_name>)[matchrule => $arg{matchrule}](s?)
{ $return = "Otherwise, ^L" . join('',@{$item[-1]}); } { $return = "Otherwise, ^L" . join('',@{$item[-1]}); }
@ -994,19 +994,14 @@ 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;
if ($expression =~ /^the expression (\(+)/) { if ($expression =~ /^the expression (\(+)/) {
$repeats = (length $1) + 1; $repeats = (length $1) + 1;
print STDERR "Got repeats: $repeats from matching [$1]\n";
$expression =~ s/^the expression \(+//; $expression =~ s/^the expression \(+//;
} }
print STDERR "repeats: $repeats\n";
$expression .= ')'; $expression .= ')';
$return = "the expression "; $return = "the expression ";
$return .= '(' x $repeats; $return .= '(' x $repeats;