3
0
mirror of https://github.com/pragma-/pbot.git synced 2025-02-17 05:50:56 +01:00

Use Text::Balanced to extract code blocks from functions

This commit is contained in:
Pragmatic Software 2010-05-13 20:17:16 +00:00
parent c699ae26a8
commit 3acdd74920

View File

@ -7,12 +7,14 @@ use SOAP::Lite;
$SOAP::Constants::DO_NOT_USE_XML_PARSER = 1; $SOAP::Constants::DO_NOT_USE_XML_PARSER = 1;
use IPC::Open2; use IPC::Open2;
use HTML::Entities; use HTML::Entities;
use Text::Balanced qw(extract_codeblock);
my $user = 'test'; my $user = 'test';
my $pass = 'test'; my $pass = 'test';
my $soap = SOAP::Lite->new(proxy => 'http://ideone.com/api/1/service'); my $soap = SOAP::Lite->new(proxy => 'http://ideone.com/api/1/service');
my $result; my $result;
my $output = "";
my $nooutput = 'No output.'; my $nooutput = 'No output.';
my %languages = ( my %languages = (
@ -135,9 +137,21 @@ if($languages{$lang}{'id'} == 1 or $languages{$lang}{'id'} == 11 or $languages{$
my $prelude = ''; my $prelude = '';
$prelude = "$1$2" if $precode =~ s/^\s*(#.*)(#.*?[>\n])//s; $prelude = "$1$2" if $precode =~ s/^\s*(#.*)(#.*?[>\n])//s;
while($precode =~ s/([a-zA-Z0-9_*\[\]]+)\s+([a-zA-Z0-9_*]+)\s*\((.*?)\)\s*{(.*?)}//) { while($precode =~ s/([ a-zA-Z0-9_*\[\]]+)\s+([a-zA-Z0-9_*]+)\s*\((.*?)\)\s*{//) {
my ($ret, $ident, $params, $body) = ($1, $2, $3, $4); my ($ret, $ident, $params) = ($1, $2, $3);
$code .= "$ret $ident($params) { $body }\n\n";
$precode = "{$precode";
my @extract = extract_codeblock($precode, '{}');
my $body;
if(not defined $extract[0]) {
$output .= "error: unmatched brackets for function '$ident';\n";
$body = $extract[1];
$precode = '';
} else {
$body = $extract[0];
$precode = $extract[1];
}
$code .= "$ret $ident($params) $body\n\n";
$has_main = 1 if $ident eq 'main'; $has_main = 1 if $ident eq 'main';
} }
@ -148,7 +162,7 @@ if($languages{$lang}{'id'} == 1 or $languages{$lang}{'id'} == 11 or $languages{$
$code = "$prelude\n\n$code\n\nint main(int argc, char **argv) { $precode return 0;}\n"; $code = "$prelude\n\n$code\n\nint main(int argc, char **argv) { $precode return 0;}\n";
$nooutput = "Success [no output]."; $nooutput = "Success [no output].";
} else { } else {
$code = "$prelude\n\n$code\n\n$precode\n"; $code = "$prelude\n\n$precode\n\n$code\n";
$nooutput = "No output."; $nooutput = "No output.";
} }
} else { } else {
@ -179,8 +193,6 @@ while(1) {
$result = get_result($soap->getSubmissionDetails($user, $pass, $url, 0, 0, 1, 1, 1)); $result = get_result($soap->getSubmissionDetails($user, $pass, $url, 0, 0, 1, 1, 1));
my $output = "";
my $COMPILER_ERROR = 11; my $COMPILER_ERROR = 11;
my $RUNTIME_ERROR = 12; my $RUNTIME_ERROR = 12;
my $TIMELIMIT = 13; my $TIMELIMIT = 13;