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

compiler_vm: improved function extraction

This commit is contained in:
Pragmatic Software 2012-03-21 15:58:07 +00:00
parent 2fc11ea1fd
commit 0e957ba414
2 changed files with 9 additions and 6 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 => 366, BUILD_REVISION => 367,
BUILD_DATE => "2012-03-21", BUILD_DATE => "2012-03-21",
}; };

View File

@ -610,7 +610,7 @@ if($lang eq 'C' or $lang eq 'C99' or $lang eq 'C11' or $lang eq 'C++') {
my $prelude = ''; my $prelude = '';
$prelude = "$1$2" if $precode =~ s/^\s*(#.*)(#.*?>\s*\n)//s; $prelude = "$1$2" if $precode =~ s/^\s*(#.*)(#.*?>\s*\n)//s;
print "*** prelude: [$prelude]\nprecode: [$precode]\n" if $debug; print "*** prelude: [$prelude]\n precode: [$precode]\n" if $debug;
# strip C and C++ style comments # strip C and C++ style comments
$precode =~ s#/\*[^*]*\*+([^/*][^*]*\*+)*/|//([^\\]|[^\n][\n]?)*?\n|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $3 ? $3 : ""#gse; $precode =~ s#/\*[^*]*\*+([^/*][^*]*\*+)*/|//([^\\]|[^\n][\n]?)*?\n|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)#defined $3 ? $3 : ""#gse;
@ -627,8 +627,10 @@ 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*(\{.*)/;
# look for potential functions to extract # look for potential functions to extract
while($preprecode =~ m/([ a-zA-Z0-9_*[\]]+)\s+([a-zA-Z0-9_*]+)\s*\((.*?)\)\s*(\{.*)/) { while($preprecode =~ $func_regex) {
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][...], has main: $has_main\n" if $debug >= 2;
@ -638,7 +640,7 @@ if($lang eq 'C' or $lang eq 'C99' or $lang eq 'C11' or $lang eq 'C++') {
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/([ a-zA-Z0-9_*[\]]+)\s+([a-zA-Z0-9_*]+)\s*\((.*?)\)\s*(\{.*)//; $preprecode =~ s/$func_regex//;
# 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);
@ -647,7 +649,7 @@ if($lang eq 'C' or $lang eq 'C99' or $lang eq 'C11' or $lang eq 'C++') {
$precode = substr($precode, 0, $extract_pos); $precode = substr($precode, 0, $extract_pos);
print "precode: [$precode]\n" if $debug; print "precode: [$precode]\n" if $debug;
$tmpcode =~ m/([ a-zA-Z0-9_*[\]]+)\s+([a-zA-Z0-9_*]+)\s*\((.*?)\)\s*(\{.*)/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 "[$ret][$ident][$params][$potential_body]\n" if $debug;
@ -659,7 +661,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/([ a-zA-Z0-9_*[\]]+)\s+([a-zA-Z0-9_*]+)\s*\((.*?)\)\s*(\{.*)//; $tmpcode =~ s/([ a-zA-Z0-9_*\[\]]+)\s+([a-zA-Z0-9_*]+)\s*\((.*?)\)\s*(\{.*)//;
} }
my @extract = extract_bracketed($potential_body, '{}'); my @extract = extract_bracketed($potential_body, '{}');
@ -687,6 +689,7 @@ if($lang eq 'C' or $lang eq 'C99' or $lang eq 'C11' or $lang eq 'C++') {
$code = "$prelude\n\n$code\n\nint main(int argc, char **argv) {\n$precode\n;\nreturn 0;\n}\n"; $code = "$prelude\n\n$code\n\nint main(int argc, char **argv) {\n$precode\n;\nreturn 0;\n}\n";
$nooutput = "No warnings, errors or output."; $nooutput = "No warnings, errors or output.";
} else { } else {
print "code: [$code]; precode: [$precode]\n" if $debug;
$code = "$prelude\n\n$precode\n\n$code\n"; $code = "$prelude\n\n$precode\n\n$code\n";
$nooutput = "No warnings, errors or output."; $nooutput = "No warnings, errors or output.";
} }