style: move to C99 to reduce scopes

* src/symtab.c, src/reader.c: Freely mix statements and variable
definitions.  And use for-loops with initializers.
This commit is contained in:
Akim Demaille
2018-07-09 13:51:31 +02:00
parent 9502b2ab48
commit 4c7377f499
2 changed files with 39 additions and 74 deletions

View File

@@ -78,13 +78,13 @@ grammar_start_symbol_set (symbol *sym, location loc)
static int static int
get_merge_function (uniqstr name) get_merge_function (uniqstr name)
{ {
if (! glr_parser)
return 0;
merger_list *syms; merger_list *syms;
merger_list head; merger_list head;
int n; int n;
if (! glr_parser)
return 0;
head.next = merge_functions; head.next = merge_functions;
for (syms = &head, n = 1; syms->next; syms = syms->next, n += 1) for (syms = &head, n = 1; syms->next; syms = syms->next, n += 1)
if (UNIQSTR_EQ (name, syms->next->name)) if (UNIQSTR_EQ (name, syms->next->name))
@@ -111,12 +111,12 @@ get_merge_function (uniqstr name)
static void static void
record_merge_function_type (int merger, uniqstr type, location declaration_loc) record_merge_function_type (int merger, uniqstr type, location declaration_loc)
{ {
int merger_find;
merger_list *merge_function;
if (merger <= 0) if (merger <= 0)
return; return;
int merger_find;
merger_list *merge_function;
if (type == NULL) if (type == NULL)
type = uniqstr_new (""); type = uniqstr_new ("");
@@ -227,13 +227,11 @@ void
grammar_current_rule_begin (symbol *lhs, location loc, grammar_current_rule_begin (symbol *lhs, location loc,
named_ref *lhs_name) named_ref *lhs_name)
{ {
symbol_list* p;
/* Start a new rule and record its lhs. */ /* Start a new rule and record its lhs. */
++nrules; ++nrules;
previous_rule_end = grammar_end; previous_rule_end = grammar_end;
p = grammar_symbol_append (lhs, loc); symbol_list *p = grammar_symbol_append (lhs, loc);
if (lhs_name) if (lhs_name)
assign_named_ref (p, named_ref_copy (lhs_name)); assign_named_ref (p, named_ref_copy (lhs_name));
@@ -314,9 +312,8 @@ grammar_rule_check (const symbol_list *r)
/* Check that symbol values that should be used are in fact used. */ /* Check that symbol values that should be used are in fact used. */
{ {
symbol_list const *l = r;
int n = 0; int n = 0;
for (; l && l->content.sym; l = l->next, ++n) for (symbol_list const *l = r; l && l->content.sym; l = l->next, ++n)
{ {
bool midrule_warning = false; bool midrule_warning = false;
if (!l->action_props.is_value_used if (!l->action_props.is_value_used
@@ -512,10 +509,9 @@ void
grammar_current_rule_symbol_append (symbol *sym, location loc, grammar_current_rule_symbol_append (symbol *sym, location loc,
named_ref *name) named_ref *name)
{ {
symbol_list *p;
if (current_rule->action_props.code) if (current_rule->action_props.code)
grammar_midrule_action (); grammar_midrule_action ();
p = grammar_symbol_append (sym, loc); symbol_list *p = grammar_symbol_append (sym, loc);
if (name) if (name)
assign_named_ref (p, name); assign_named_ref (p, name);
if (sym->content->status == undeclared || sym->content->status == used) if (sym->content->status == undeclared || sym->content->status == used)
@@ -556,7 +552,6 @@ packgram (void)
{ {
unsigned int itemno = 0; unsigned int itemno = 0;
rule_number ruleno = 0; rule_number ruleno = 0;
symbol_list *p;
ritem = xnmalloc (nritems + 1, sizeof *ritem); ritem = xnmalloc (nritems + 1, sizeof *ritem);
@@ -565,7 +560,7 @@ packgram (void)
rules = xnmalloc (nrules, sizeof *rules); rules = xnmalloc (nrules, sizeof *rules);
for (p = grammar; p; p = p->next) for (symbol_list *p = grammar; p; p = p->next)
{ {
symbol *ruleprec = p->ruleprec; symbol *ruleprec = p->ruleprec;
record_merge_function_type (p->merger, p->content.sym->content->type_name, record_merge_function_type (p->merger, p->content.sym->content->type_name,
@@ -751,7 +746,7 @@ check_and_convert_grammar (void)
for (node = node->next; for (node = node->next;
node != NULL && node->content.sym != NULL; node != NULL && node->content.sym != NULL;
node = node->next) node = node->next)
; continue;
} }
aver (node != NULL); aver (node != NULL);
grammar_start_symbol_set (node->content.sym, grammar_start_symbol_set (node->content.sym,
@@ -792,11 +787,8 @@ check_and_convert_grammar (void)
rule. For the same reason, all the 'used' flags must be set before rule. For the same reason, all the 'used' flags must be set before
checking whether to remove '$' from any midrule symbol name (also in checking whether to remove '$' from any midrule symbol name (also in
packgram). */ packgram). */
{ for (symbol_list *sym = grammar; sym; sym = sym->next)
symbol_list *sym; code_props_translate_code (&sym->action_props);
for (sym = grammar; sym; sym = sym->next)
code_props_translate_code (&sym->action_props);
}
/* Convert the grammar into the format described in gram.h. */ /* Convert the grammar into the format described in gram.h. */
packgram (); packgram ();

View File

@@ -70,11 +70,8 @@ sym_content_new (symbol *s)
res->symbol = s; res->symbol = s;
res->type_name = NULL; res->type_name = NULL;
{ for (int i = 0; i < CODE_PROPS_SIZE; ++i)
int i; code_props_none_init (&res->props[i]);
for (i = 0; i < CODE_PROPS_SIZE; ++i)
code_props_none_init (&res->props[i]);
}
res->number = NUMBER_UNDEFINED; res->number = NUMBER_UNDEFINED;
res->prec = 0; res->prec = 0;
@@ -204,11 +201,8 @@ semantic_type_new (uniqstr tag, const location *loc)
res->tag = tag; res->tag = tag;
res->location = loc ? *loc : empty_location; res->location = loc ? *loc : empty_location;
res->status = undeclared; res->status = undeclared;
{ for (int i = 0; i < CODE_PROPS_SIZE; ++i)
int i; code_props_none_init (&res->props[i]);
for (i = 0; i < CODE_PROPS_SIZE; ++i)
code_props_none_init (&res->props[i]);
}
return res; return res;
} }
@@ -410,9 +404,9 @@ symbol_code_props_get (symbol *sym, code_props_type kind)
void void
symbol_precedence_set (symbol *sym, int prec, assoc a, location loc) symbol_precedence_set (symbol *sym, int prec, assoc a, location loc)
{ {
sym_content *s = sym->content;
if (a != undef_assoc) if (a != undef_assoc)
{ {
sym_content *s = sym->content;
if (s->prec) if (s->prec)
symbol_redeclaration (sym, assoc_to_string (a), symbol_redeclaration (sym, assoc_to_string (a),
s->prec_location, loc); s->prec_location, loc);
@@ -511,11 +505,8 @@ symbol_check_defined (symbol *sym)
s->number = nvars++; s->number = nvars++;
} }
{ for (int i = 0; i < 2; ++i)
int i; symbol_code_props_get (sym, i)->is_used = true;
for (i = 0; i < 2; ++i)
symbol_code_props_get (sym, i)->is_used = true;
}
/* Set the semantic type status associated to the current symbol to /* Set the semantic type status associated to the current symbol to
'declared' so that we could check semantic types unnecessary uses. */ 'declared' so that we could check semantic types unnecessary uses. */
@@ -537,8 +528,7 @@ semantic_type_check_defined (semantic_type *sem_type)
|| !*sem_type->tag || !*sem_type->tag
|| STREQ (sem_type->tag, "*")) || STREQ (sem_type->tag, "*"))
{ {
int i; for (int i = 0; i < 2; ++i)
for (i = 0; i < 2; ++i)
if (sem_type->props[i].kind != CODE_PROPS_NONE if (sem_type->props[i].kind != CODE_PROPS_NONE
&& ! sem_type->props[i].is_used) && ! sem_type->props[i].is_used)
complain (&sem_type->location, Wother, complain (&sem_type->location, Wother,
@@ -586,14 +576,11 @@ symbol_merge_properties (symbol *sym, symbol *str)
} }
{ for (int i = 0; i < CODE_PROPS_SIZE; ++i)
int i; if (str->content->props[i].code)
for (i = 0; i < CODE_PROPS_SIZE; ++i) symbol_code_props_set (sym, i, &str->content->props[i]);
if (str->content->props[i].code) else if (sym->content->props[i].code)
symbol_code_props_set (sym, i, &str->content->props[i]); symbol_code_props_set (str, i, &sym->content->props[i]);
else if (sym->content->props[i].code)
symbol_code_props_set (str, i, &sym->content->props[i]);
}
if (sym->content->prec || str->content->prec) if (sym->content->prec || str->content->prec)
{ {
@@ -787,10 +774,9 @@ symbol *
symbol_from_uniqstr (const uniqstr key, location loc) symbol_from_uniqstr (const uniqstr key, location loc)
{ {
symbol probe; symbol probe;
symbol *entry;
probe.tag = key; probe.tag = key;
entry = hash_lookup (symbol_table, &probe); symbol *entry = hash_lookup (symbol_table, &probe);
if (!entry) if (!entry)
{ {
@@ -813,10 +799,9 @@ semantic_type *
semantic_type_from_uniqstr (const uniqstr key, const location *loc) semantic_type_from_uniqstr (const uniqstr key, const location *loc)
{ {
semantic_type probe; semantic_type probe;
semantic_type *entry;
probe.tag = key; probe.tag = key;
entry = hash_lookup (semantic_type_table, &probe); semantic_type *entry = hash_lookup (semantic_type_table, &probe);
if (!entry) if (!entry)
{ {
@@ -865,10 +850,8 @@ dummy_symbol_get (location loc)
static int dummy_count = 0; static int dummy_count = 0;
static char buf[256]; static char buf[256];
symbol *sym;
sprintf (buf, "$@%d", ++dummy_count); sprintf (buf, "$@%d", ++dummy_count);
sym = symbol_get (buf, loc); symbol *sym = symbol_get (buf, loc);
sym->content->class = nterm_sym; sym->content->class = nterm_sym;
sym->content->number = nvars++; sym->content->number = nvars++;
return sym; return sym;
@@ -923,11 +906,8 @@ symbols_do (Hash_processor processor, void *processor_data,
hash_get_entries (table, (void**)*sorted, count); hash_get_entries (table, (void**)*sorted, count);
qsort (*sorted, count, sizeof **sorted, symbols_cmp_qsort); qsort (*sorted, count, sizeof **sorted, symbols_cmp_qsort);
} }
{ for (size_t i = 0; i < count; ++i)
size_t i; processor ((*sorted)[i], processor_data);
for (i = 0; i < count; ++i)
processor ((*sorted)[i], processor_data);
}
} }
/*--------------------------------------------------------------. /*--------------------------------------------------------------.
@@ -953,12 +933,11 @@ static void
symbols_token_translations_init (void) symbols_token_translations_init (void)
{ {
bool num_256_available_p = true; bool num_256_available_p = true;
int i;
/* Find the highest user token number, and whether 256, the POSIX /* Find the highest user token number, and whether 256, the POSIX
preferred user token number for the error token, is used. */ preferred user token number for the error token, is used. */
max_user_token_number = 0; max_user_token_number = 0;
for (i = 0; i < ntokens; ++i) for (int i = 0; i < ntokens; ++i)
{ {
sym_content *this = symbols[i]->content; sym_content *this = symbols[i]->content;
if (this->user_token_number != USER_NUMBER_UNDEFINED) if (this->user_token_number != USER_NUMBER_UNDEFINED)
@@ -979,7 +958,7 @@ symbols_token_translations_init (void)
if (max_user_token_number < 256) if (max_user_token_number < 256)
max_user_token_number = 256; max_user_token_number = 256;
for (i = 0; i < ntokens; ++i) for (int i = 0; i < ntokens; ++i)
{ {
sym_content *this = symbols[i]->content; sym_content *this = symbols[i]->content;
if (this->user_token_number == USER_NUMBER_UNDEFINED) if (this->user_token_number == USER_NUMBER_UNDEFINED)
@@ -993,7 +972,7 @@ symbols_token_translations_init (void)
/* Initialize all entries for literal tokens to the internal token /* Initialize all entries for literal tokens to the internal token
number for $undefined, which represents all invalid inputs. */ number for $undefined, which represents all invalid inputs. */
for (i = 0; i < max_user_token_number + 1; i++) for (int i = 0; i < max_user_token_number + 1; i++)
token_translations[i] = undeftoken->content->number; token_translations[i] = undeftoken->content->number;
symbols_do (symbol_translation_processor, NULL, symbols_do (symbol_translation_processor, NULL,
symbol_table, &symbols_sorted); symbol_table, &symbols_sorted);
@@ -1013,10 +992,8 @@ symbols_pack (void)
/* Aliases leave empty slots in symbols, so remove them. */ /* Aliases leave empty slots in symbols, so remove them. */
{ {
int writei;
int readi;
int nsyms_old = nsyms; int nsyms_old = nsyms;
for (writei = 0, readi = 0; readi < nsyms_old; readi += 1) for (int writei = 0, readi = 0; readi < nsyms_old; readi += 1)
{ {
if (symbols[readi] == NULL) if (symbols[readi] == NULL)
{ {
@@ -1052,9 +1029,8 @@ symbols_pack (void)
static void static void
init_prec_nodes (void) init_prec_nodes (void)
{ {
int i;
prec_nodes = xcalloc (nsyms, sizeof *prec_nodes); prec_nodes = xcalloc (nsyms, sizeof *prec_nodes);
for (i = 0; i < nsyms; ++i) for (int i = 0; i < nsyms; ++i)
{ {
prec_nodes[i] = xmalloc (sizeof *prec_nodes[i]); prec_nodes[i] = xmalloc (sizeof *prec_nodes[i]);
symgraph *s = prec_nodes[i]; symgraph *s = prec_nodes[i];
@@ -1145,8 +1121,7 @@ linkedlist_free (symgraphlink *node)
static void static void
assoc_free (void) assoc_free (void)
{ {
int i; for (int i = 0; i < nsyms; ++i)
for (i = 0; i < nsyms; ++i)
{ {
linkedlist_free (prec_nodes[i]->pred); linkedlist_free (prec_nodes[i]->pred);
linkedlist_free (prec_nodes[i]->succ); linkedlist_free (prec_nodes[i]->succ);
@@ -1162,9 +1137,8 @@ assoc_free (void)
static void static void
init_assoc (void) init_assoc (void)
{ {
graphid i;
used_assoc = xcalloc (nsyms, sizeof *used_assoc); used_assoc = xcalloc (nsyms, sizeof *used_assoc);
for (i = 0; i < nsyms; ++i) for (graphid i = 0; i < nsyms; ++i)
used_assoc[i] = false; used_assoc[i] = false;
} }
@@ -1201,12 +1175,11 @@ register_assoc (graphid i, graphid j)
void void
print_precedence_warnings (void) print_precedence_warnings (void)
{ {
int i;
if (!prec_nodes) if (!prec_nodes)
init_prec_nodes (); init_prec_nodes ();
if (!used_assoc) if (!used_assoc)
init_assoc (); init_assoc ();
for (i = 0; i < nsyms; ++i) for (int i = 0; i < nsyms; ++i)
{ {
symbol *s = symbols[i]; symbol *s = symbols[i];
if (s if (s