3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-26 22:09:26 +01:00

Run code through gcc for syntax checking before attempting to parse to english

This commit is contained in:
Pragmatic Software 2013-10-19 17:56:43 +00:00
parent aeec4fb2f5
commit c6e67200c1
2 changed files with 103 additions and 5 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 => 453, BUILD_REVISION => 454,
BUILD_DATE => "2013-10-14", BUILD_DATE => "2013-10-19",
}; };
1; 1;

View File

@ -17,8 +17,8 @@ $code = '';
my $has_main = 0; my $has_main = 0;
my $prelude = ''; my $prelude = "#define _XOPEN_SOURCE 9001\n#define __USE_XOPEN\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include <unistd.h>\n#include <math.h>\n#include <limits.h>\n#include <sys/types.h>\n#include <stdint.h>\n#include <errno.h>\n#include <ctype.h>\n#include <assert.h>\n#include <prelude.h>\n\n";
$prelude = "$1$2" if $precode =~ s/^\s*(#.*)(#.*?[>\n])//s; $prelude .= "$1$2" if $precode =~ s/^\s*(#.*)(#.*?[>\n])//s;
my $preprecode = $precode; my $preprecode = $precode;
@ -53,7 +53,7 @@ $precode =~ s/^\s+//;
$precode =~ s/\s+$//; $precode =~ s/\s+$//;
if(not $has_main) { if(not $has_main) {
$code = "$prelude\n\n$code\n\nint main(void) { $precode\n return 0;}\n"; $code = "$prelude\n\n$code\n\nint main(void) { $precode\n;\nreturn 0;}\n";
} else { } else {
$code = "$prelude\n\n$precode\n\n$code\n"; $code = "$prelude\n\n$precode\n\n$code\n";
} }
@ -61,6 +61,10 @@ if(not $has_main) {
$code =~ s/\|n/\n/g; $code =~ s/\|n/\n/g;
$code =~ s/^\s+//; $code =~ s/^\s+//;
$code =~ s/\s+$//; $code =~ s/\s+$//;
$code =~ s/;\s*;\n/;\n/gs;
$code =~ s/;(\s*\/\*.*?\*\/\s*);\n/;$1/gs;
$code =~ s/;(\s*\/\/.*?\s*);\n/;$1/gs;
$code =~ s/({|})\n\s*;\n/$1\n/gs;
chdir "$ENV{HOME}/blackshell/msmud/babel-buster/code" or die "Could not chdir: $!"; chdir "$ENV{HOME}/blackshell/msmud/babel-buster/code" or die "Could not chdir: $!";
@ -68,6 +72,67 @@ open my $fh, '>', 'code.c' or die "Could not write code: $!";
print $fh $code; print $fh $code;
close $fh; close $fh;
my ($ret, $result) = execute(10, "gcc -std=c89 -pedantic -Werror -Wno-unused -c code.c");
if($ret != 0) {
$output = $result;
$output =~ s/code\.c:\d+:\d+://g;
$output =~ s/code\.c://g;
$output =~ s/error=edantic/error=pedantic/g;
$output =~ s/(\d+:\d+:\s*)*cc1: all warnings being treated as errors//;
$output =~ s/(\d+:\d+:\s*)* \(first use in this function\)//g;
$output =~ s/(\d+:\d+:\s*)*error: \(Each undeclared identifier is reported only once.*?\)//msg;
$output =~ s/(\d+:\d+:\s*)*ld: warning: cannot find entry symbol _start; defaulting to [^ ]+//;
#$output =~ s/(\d+:\d+:\s*)*error: (.*?) error/error: $1; error/msg;
$output =~ s/(\d+:\d+:\s*)*\/tmp\/.*\.o://g;
$output =~ s/(\d+:\d+:\s*)*collect2: ld returned \d+ exit status//g;
$output =~ s/\(\.text\+[^)]+\)://g;
$output =~ s/\[ In/[In/;
$output =~ s/(\d+:\d+:\s*)*warning: Can't read pathname for load map: Input.output error.//g;
my $left_quote = chr(226) . chr(128) . chr(152);
my $right_quote = chr(226) . chr(128) . chr(153);
$output =~ s/$left_quote/'/msg;
$output =~ s/$right_quote/'/msg;
$output =~ s/`/'/msg;
$output =~ s/\t/ /g;
$output =~ s/(\d+:\d+:\s*)*\s*In function .main.:\s*//g;
$output =~ s/(\d+:\d+:\s*)*warning: unknown conversion type character 'b' in format \[-Wformat\]\s+(\d+:\d+:\s*)*warning: too many arguments for format \[-Wformat-extra-args\]/info: %b is a candide extension/g;
$output =~ s/(\d+:\d+:\s*)*warning: unknown conversion type character 'b' in format \[-Wformat\]//g;
$output =~ s/\s\(core dumped\)/./;
# $output =~ s/\[\s+/[/g;
$output =~ s/ \[enabled by default\]//g;
$output =~ s/initializer\s+warning: \(near/initializer (near/g;
$output =~ s/(\d+:\d+:\s*)*note: each undeclared identifier is reported only once for each function it appears in//g;
$output =~ s/\(gdb\)//g;
$output =~ s/", '\\(\d{3})' <repeats \d+ times>,? ?"/\\$1/g;
$output =~ s/, '\\(\d{3})' <repeats \d+ times>\s*//g;
$output =~ s/(\\000)+/\\0/g;
$output =~ s/\\0[^">']+/\\0/g;
$output =~ s/= (\d+) '\\0'/= $1/g;
$output =~ s/\\0"/"/g;
$output =~ s/"\\0/"/g;
$output =~ s/\.\.\.>/>/g;
$output =~ s/(\\\d{3})+//g;
$output =~ s/<\s*included at \/home\/compiler\/>\s*//g;
$output =~ s/\s*compilation terminated due to -Wfatal-errors\.//g;
$output =~ s/^======= Backtrace.*\[vsyscall\]\s*$//ms;
$output =~ s/glibc detected \*\*\* \/home\/compiler\/code: //;
$output =~ s/: \/home\/compiler\/code terminated//;
$output =~ s/<Defined at \/home\/compiler\/>/<Defined at \/home\/compiler\/code.c:0>/g;
$output =~ s/\s*In file included from\s+\/usr\/include\/.*?:\d+:\d+:\s*/, /g;
$output =~ s/\s*collect2: error: ld returned 1 exit status//g;
$output =~ s/In function\s*`main':\s*\/home\/compiler\/ undefined reference to/error: undefined reference to/g;
$output =~ s/\/home\/compiler\///g;
$output =~ s/compilation terminated.//;
$output =~ s/<'(.*)' = char>/<'$1' = int>/g;
$output =~ s/= (-?\d+) ''/= $1/g;
$output =~ s/, <incomplete sequence >//g;
print "$output\n";
exit 0;
}
$output = `./c2e 2>/dev/null code.c` if not defined $output; $output = `./c2e 2>/dev/null code.c` if not defined $output;
if(not $has_main) { if(not $has_main) {
@ -98,3 +163,36 @@ if($output eq " ") {
} }
print "$output\n"; print "$output\n";
sub execute {
my $timeout = shift @_;
my ($cmdline) = @_;
my ($ret, $result);
($ret, $result) = eval {
my $result = '';
my $pid = open(my $fh, '-|', "$cmdline 2>&1");
local $SIG{ALRM} = sub { kill 'TERM', $pid; die "$result [Timed-out]\n"; };
alarm($timeout);
while(my $line = <$fh>) {
$result .= $line;
}
close $fh;
my $ret = $? >> 8;
alarm 0;
return ($ret, $result);
};
alarm 0;
if($@ =~ /Timed-out/) {
return (-1, $@);
}
return ($ret, $result);
}