CGrammar: Add istrue to static assertion expression; add ability to customize istrue truthiness result, add isfalse

This commit is contained in:
Pragmatic Software 2014-09-19 04:17:37 +00:00
parent 9d3c8c0285
commit 79796f9710
4 changed files with 15 additions and 6 deletions

View File

@ -13,7 +13,7 @@ use warnings;
# These are set automatically by the build/commit script
use constant {
BUILD_NAME => "PBot",
BUILD_REVISION => 788,
BUILD_REVISION => 789,
BUILD_DATE => "2014-09-18",
};

View File

@ -19,7 +19,7 @@ if($code =~ s/^-f\s+//) {
}
my ($has_function, $has_main, $got_nomain);
my $prelude_base = "#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 <stdnoreturn.h>\n#include <stdbool.h>\n#include <stdalign.h>\n#include <time.h>\n#define _Atomic\n#define _Static_assert(a, b)\n\n";
my $prelude_base = "#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 <stdnoreturn.h>\n#include <stdbool.h>\n#include <stdalign.h>\n#include <time.h>\n#include <stddef.h>\n#define _Atomic\n#define _Static_assert(a, b)\n\n";
my $prelude = $prelude_base;
print "code before: [$code]\n" if $debug;
@ -309,6 +309,9 @@ my ($ret, $result) = execute(10, "gcc -std=c11 -pedantic -Werror -Wno-unused -fs
if(not $force and $ret != 0) {
$output = $result;
#print STDERR "output: [$output]\n";
$output =~ s/\s*In file included from\s+.*?:\d+:\d+:\s*//g;
$output =~ s/code\.c:\d+:\d+://g;
$output =~ s/code\.c://g;
$output =~ s/error=edantic/error=pedantic/g;

View File

@ -695,9 +695,10 @@ Static_assert:
| 'static_assert'
static_assert_declaration:
Static_assert '(' constant_expression[context => 'static_assert'] ',' string ')' ';'
Static_assert '(' constant_expression[context => 'static assert'] ',' string ')' ';'
{
$return = "Halt compilation and produce the diagnostic $item{string} unless $item{constant_expression}.\n";
my $expression = ::istrue $item{constant_expression};
$return = "Halt compilation and produce the diagnostic $item{string} unless $expression.\n";
}
declaration_list:

View File

@ -101,19 +101,24 @@ sub flatten {
map { ref eq 'ARRAY' ? flatten(@$_) : $_ } @_
}
sub isfalse {
return istrue($_[0], 'zero');
}
sub istrue {
my @parts = split /(?<!,) and /, $_[0];
my $truthy = defined $_[1] ? $_[1] : 'nonzero';
my ($result, $and) = ('', '');
foreach my $part (@parts) {
$result .= $and;
if($part !~ /(discard the result|result discarded|greater|less|equal|false$)/) {
$result .= "$part is nonzero";
$result .= "$part is $truthy";
} else {
$result .= $part;
}
$and = ' and ';
}
$result =~ s/is nonzero and the result discarded/is evaluated and the result discarded/g;
$result =~ s/is $truthy and the result discarded/is evaluated and the result discarded/g;
$result =~ s/is ((?:(?!evaluated).)+) and the result discarded/is evaluated to be $1 and the result discarded/g;
return $result;
}