3
0
mirror of https://github.com/pragma-/pbot.git synced 2025-01-26 20:14:34 +01:00

compiler_vm: improve parsing of cpp and functions (properly handle \n)

This commit is contained in:
Pragmatic Software 2012-09-03 18:48:47 +00:00
parent 7ed26f928e
commit a0bcdfef2f
2 changed files with 16 additions and 11 deletions

View File

@ -13,8 +13,8 @@ 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 => 381, BUILD_REVISION => 382,
BUILD_DATE => "2012-09-02", BUILD_DATE => "2012-09-03",
}; };
1; 1;

View File

@ -659,12 +659,17 @@ if($code =~ m/#include/) {
} }
$code = ''; $code = '';
print "--- precode: [$precode]\n" if $debug;
if($lang eq 'C' or $lang eq 'C99' or $lang eq 'C11' or $lang eq 'C++') { if($lang eq 'C' or $lang eq 'C99' or $lang eq 'C11' or $lang eq 'C++') {
my $has_main = 0; my $has_main = 0;
my $prelude = ''; my $prelude = '';
#$prelude = "$1$2" if $precode =~ s/^\s*(#.*)(#.*?>\s*\n)//s; while($precode =~ s/^\s*(#.*\n)//g) {
$prelude = "$1" if $precode =~ s/^\s*(#.*\n)//s; $prelude .= $1;
}
#$prelude = "$1" if $precode =~ s/^\s*(#.*\n)//s;
print "*** prelude: [$prelude]\n precode: [$precode]\n" if $debug; print "*** prelude: [$prelude]\n precode: [$precode]\n" if $debug;
@ -683,20 +688,20 @@ if($lang eq 'C' or $lang eq 'C99' or $lang eq 'C11' or $lang eq 'C++') {
print "looking for functions, has main: $has_main\n" if $debug >= 2; print "looking for functions, has main: $has_main\n" if $debug >= 2;
my $func_regex = qr/([ a-zA-Z0-9_*\[\]]+)\s+([a-zA-Z0-9_*]+)\s*\(([^;]*)\)\s*(\{.*)/; my $func_regex = qr/([ a-zA-Z0-9_*\[\]]+)\s+([a-zA-Z0-9_*]+)\s*\(([^;]*)\)\s*(\{.*)/ms;
# look for potential functions to extract # look for potential functions to extract
while($preprecode =~ $func_regex) { while($preprecode =~ /$func_regex/ms) {
my ($pre_ret, $pre_ident, $pre_params, $pre_potential_body) = ($1, $2, $3, $4); my ($pre_ret, $pre_ident, $pre_params, $pre_potential_body) = ($1, $2, $3, $4);
print "looking for functions, found [$pre_ret][$pre_ident][$pre_params][...], has main: $has_main\n" if $debug >= 2; print "looking for functions, found [$pre_ret][$pre_ident][$pre_params][$pre_potential_body], has main: $has_main\n" if $debug >= 2;
# find the pos at which this function lives, for extracting from precode # find the pos at which this function lives, for extracting from precode
$preprecode =~ m/(\Q$pre_ret\E\s+\Q$pre_ident\E\s*\(\s*\Q$pre_params\E\s*\)\s*\Q$pre_potential_body\E)/g; $preprecode =~ m/(\Q$pre_ret\E\s+\Q$pre_ident\E\s*\(\s*\Q$pre_params\E\s*\)\s*\Q$pre_potential_body\E)/g;
my $extract_pos = (pos $preprecode) - (length $1); my $extract_pos = (pos $preprecode) - (length $1);
# now that we have the pos, substitute out the extracted potential function from preprecode # now that we have the pos, substitute out the extracted potential function from preprecode
$preprecode =~ s/$func_regex//; $preprecode =~ s/$func_regex//ms;
# create tmpcode object that starts from extract pos, to skip any quoted code # create tmpcode object that starts from extract pos, to skip any quoted code
my $tmpcode = substr($precode, $extract_pos); my $tmpcode = substr($precode, $extract_pos);
@ -708,7 +713,7 @@ if($lang eq 'C' or $lang eq 'C99' or $lang eq 'C11' or $lang eq 'C++') {
$tmpcode =~ m/$func_regex/ms; $tmpcode =~ m/$func_regex/ms;
my ($ret, $ident, $params, $potential_body) = ($1, $2, $3, $4); my ($ret, $ident, $params, $potential_body) = ($1, $2, $3, $4);
print "[$ret][$ident][$params][$potential_body]\n" if $debug; print "1st extract: [$ret][$ident][$params][$potential_body]\n" if $debug;
$ret =~ s/^\s+//; $ret =~ s/^\s+//;
$ret =~ s/\s+$//; $ret =~ s/\s+$//;
@ -717,7 +722,7 @@ if($lang eq 'C' or $lang eq 'C99' or $lang eq 'C11' or $lang eq 'C++') {
$precode .= "$ret $ident ($params) $potential_body"; $precode .= "$ret $ident ($params) $potential_body";
next; next;
} else { } else {
$tmpcode =~ s/$func_regex//; $tmpcode =~ s/$func_regex//ms;
} }
my @extract = extract_bracketed($potential_body, '{}'); my @extract = extract_bracketed($potential_body, '{}');
@ -731,7 +736,7 @@ if($lang eq 'C' or $lang eq 'C99' or $lang eq 'C11' or $lang eq 'C++') {
$precode .= $extract[1]; $precode .= $extract[1];
} }
print "[$ret][$ident][$params][$body]\n" if $debug; print "final extract: [$ret][$ident][$params][$body]\n" if $debug;
$code .= "$ret $ident($params) $body\n\n"; $code .= "$ret $ident($params) $body\n\n";
$has_main = 1 if $ident eq 'main'; $has_main = 1 if $ident eq 'main';
} }