3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-23 12:29:27 +01:00

CGrammar: Add more types; improve translation of comma operator; track identifiers

This commit is contained in:
Pragmatic Software 2014-06-25 08:19:56 +00:00
parent a232919dca
commit b38c6c91c1
3 changed files with 41 additions and 18 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 => 676, BUILD_REVISION => 677,
BUILD_DATE => "2014-06-24", BUILD_DATE => "2014-06-25",
}; };
1; 1;

View File

@ -2,8 +2,7 @@
# Pragmatic Software # Pragmatic Software
{ {
my @defined_types = ('`FILE`'); my ($rule_name, @macros, @typedefs, @identifiers);
my ($rule_name, @macros);
} }
startrule: startrule:
@ -322,7 +321,7 @@ jump_statement:
} }
| 'continue' ';' | 'continue' ';'
{ $return = "Return to the top of the current loop.\n"; } { $return = "Return to the top of the current loop.\n"; }
| 'return' <commit> expression[context => 'statement'](?) ';' | 'return' <commit> expression[context => 'return statement'](?) ';'
{ {
my $expression = join('', @{$item{'expression(?)'}}); my $expression = join('', @{$item{'expression(?)'}});
@ -364,7 +363,7 @@ labeled_statement:
expression: expression:
<leftop: assignment_expression[context => $arg{context}] ',' assignment_expression[context => $arg{context}]> <leftop: assignment_expression[context => $arg{context}] ',' assignment_expression[context => $arg{context}]>
{ {
$return = join(", then ^L",@{$item[-1]}); $return = join(" and then discard the result and evaluate ^L",@{$item[-1]});
} }
assignment_expression: assignment_expression:
@ -392,7 +391,7 @@ conditional_expression:
my $op1 = $item{conditional_ternary_expression}->[0]; my $op1 = $item{conditional_ternary_expression}->[0];
my $op2 = $item{conditional_ternary_expression}->[1]; my $op2 = $item{conditional_ternary_expression}->[1];
my $expression = ::istrue $item{logical_OR_AND_expression}; my $expression = ::istrue $item{logical_OR_AND_expression};
$return = "$op1 if $expression otherwise to $op2"; $return = "$op1 if $expression otherwise $op2";
} else { } else {
$return = $item{logical_OR_AND_expression}; $return = $item{logical_OR_AND_expression};
} }
@ -603,7 +602,7 @@ declaration:
$return = "Let $init_declaration_list be another name for $item{declaration_specifiers}.\n"; $return = "Let $init_declaration_list be another name for $item{declaration_specifiers}.\n";
push @defined_types, @init_list; push @typedefs, @init_list;
} else { } else {
my $inits = 0; my $inits = 0;
while(@init_list) { while(@init_list) {
@ -749,7 +748,7 @@ declaration:
$comma = ', '; $comma = ', ';
$and = ' and '; $and = ' and ';
} else { } else {
$initializer =~ s/^initialized to //; $initializer =~ s/^initialized to \^L//;
$return .= "$and$identifier to $initializer"; $return .= "$and$identifier to $initializer";
if($i < @initializers - 2) { if($i < @initializers - 2) {
$and = $comma = ', '; $and = $comma = ', ';
@ -781,7 +780,7 @@ init_declarator:
my $init = join('',@{$item[-1]}); my $init = join('',@{$item[-1]});
if (length $init) { if (length $init) {
$return = [$item{declarator}, "initialized to $init"]; $return = [$item{declarator}, "initialized to ^L$init"];
} }
} }
@ -987,7 +986,6 @@ postfix_productions:
# having done the simplest cases, we go to the catch all for left recursions. # having done the simplest cases, we go to the catch all for left recursions.
| primary_expression postfix_suffix(s) | primary_expression postfix_suffix(s)
{ {
# is this ever reached?
print STDERR "Untested code!\n"; print STDERR "Untested code!\n";
$return = $item{primary_expression} . "'s " . join('',@{$item{'postfix_suffix(s)'}}); $return = $item{primary_expression} . "'s " . join('',@{$item{'postfix_suffix(s)'}});
} }
@ -1008,7 +1006,7 @@ postfix_expression:
} }
postfix_suffix: postfix_suffix:
('[' expression ']')(s) '[' expression ']'
| '.' identifier | '.' identifier
| '->' identifier | '->' identifier
| '++' | '++'
@ -1229,6 +1227,12 @@ DAD: # macro for direct_abstract_declarator
identifier: identifier:
...!reserved identifier_word ...!reserved identifier_word
{
if(not grep { $_ eq $item{identifier_word} } @identifiers) {
push @identifiers, $item{identifier_word};
}
$return = $item{identifier_word};
}
pointer: pointer:
'*' type_qualifier_list(s) pointer(?) '*' type_qualifier_list(s) pointer(?)
@ -1318,6 +1322,25 @@ type_specifier:
| 'void' | 'void'
| 'signed' | 'signed'
| 'unsigned' | 'unsigned'
| 'FILE' | 'fpos_t'
| 'bool' | '_Bool'
| '_Complex' | '_Imaginary'
| 'int_fast8_t' | 'int_fast16_t' | 'int_fast24_t' | 'int_fast32_t' | 'int_fast64_t' | 'int_fast128_t'
| 'uint_fast8_t' | 'uint_fast16_t' | 'uint_fast24_t' | 'uint_fast32_t' | 'uint_fast64_t' | 'uint_fast128_t'
| 'int_least8_t' | 'int_least16_t' | 'int_least24_t' | 'int_least32_t' | 'int_least64_t' | 'int_least128_t'
| 'uint_least8_t' | 'uint_least16_t' | 'uint_least24_t' | 'uint_least32_t' | 'uint_least64_t' | 'uint_least128_t'
| 'int8_t' | 'int16_t' | 'int24_t' | 'int32_t' | 'int64_t' | 'int128_t'
| 'uint8_t' | 'uint16_t' | 'uint24_t' | 'uint32_t' | 'uint64_t' | 'uint128_t'
| 'intmax_t' | 'uintmax_t'
| 'intptr_t' | 'uintptr_t' | 'ptrdiff_t'
| 'sig_atomic_t'
| 'wint_t' | 'wchar_t'
| 'size_t' | 'rsize_t' | 'max_align_t'
| 'mbstate_t' | 'char16_t' | 'char32_t'
| 'fenv_t' | 'fexcept_t'
| 'div_t' | 'ldiv_t' | 'lldiv_t' | 'imaxdiv_t'
| 'cnd_t' | 'thrd_t' | 'tss_t' | 'mtx_t' | 'tss_dtor_t' | 'thrd_start_t' | 'once_flag'
| 'clock_t' | 'time_t'
| struct_or_union_specifier | struct_or_union_specifier
| enum_specifier | enum_specifier
| typedef_name | typedef_name
@ -1326,7 +1349,7 @@ typedef_name:
identifier identifier
{ {
my $answer = 0; my $answer = 0;
foreach (@defined_types) { foreach (@typedefs) {
if ($item{identifier} eq $_) { if ($item{identifier} eq $_) {
$answer = 1; $answer = 1;
$return = ($item{identifier} =~ m/^`[aeiouy]/ ? 'an ' : 'a ') . $item{identifier}; $return = ($item{identifier} =~ m/^`[aeiouy]/ ? 'an ' : 'a ') . $item{identifier};

View File

@ -93,7 +93,7 @@ sub istrue {
my ($result, $and) = ('', ''); my ($result, $and) = ('', '');
foreach my $part (@parts) { foreach my $part (@parts) {
$result .= $and; $result .= $and;
if($part !~ /(greater|less|equal|false$)/) { if($part !~ /(discard the result|greater|less|equal|false$)/) {
$result .= "$part is nonzero"; $result .= "$part is nonzero";
} else { } else {
$result .= $part; $result .= $part;