Skip to content

Commit

Permalink
Code cleanup in replace.c
Browse files Browse the repository at this point in the history
* move replace.h into tree.h
* replace // comments by /* */ comments
* remove TRUE/FALSE
* define both string_of_text_list and string_of_token_list
  • Loading branch information
lefessan committed Jul 5, 2023
1 parent 9b1ab30 commit 8bdfd9e
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 106 deletions.
16 changes: 8 additions & 8 deletions cobc/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
2023-07-05 Fabrice Le Fessant <[email protected]>

* replace.c: rewrite the code for preprocessing with a two-phase
algorithm. The first phase performs COPY REPLACING on the stream
of tokens, while the second phase perform REPLACE on the resulting
stream of tokens. This rewriting is closer to the COBOL standard
and fixes bug #831 partially.
algorithm. The first phase performs COPY REPLACING on the stream
of tokens, while the second phase perform REPLACE on the resulting
stream of tokens. This rewriting is closer to the COBOL standard
and fixes bug #831 partially.

2023-07-02 Fabrice Le Fessant <[email protected]>

* pplex.l/replace.c: move the preprocessing code performing
COPY REPLACING and REPLACE from pplex.l to replace.c
COPY REPLACING and REPLACE from pplex.l to replace.c

2023-07-02 Fabrice Le Fessant <[email protected]>

* pplex.l (ppecho, ppecho_direct): replace alt_space by passing a
second equivalent token
second equivalent token

2023-07-05 Fabrice Le Fessant <[email protected]>

Expand Down Expand Up @@ -97,8 +97,8 @@
* typeck.c (cb_build_expr): remove cb_ prefix from static functions
and comment algorithm

Bugs #875 V IS ZERO AND, #880 error compiling abbreviated conditions
and #887 error compiling parenthesized relation
Bugs #875 V IS ZERO AND, #880 error compiling abbreviated conditions
and #887 error compiling parenthesized relation
* typeck.c (cb_build_expr): additional generate 'invalid conditional
expression' error", translate NOT comparison to inversed comparison
* typeck.c (cb_build_expr), tree.c (cb_build_binary_op): give more
Expand Down
6 changes: 5 additions & 1 deletion cobc/pplex.l
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ static int ppwrap (void) {
#include "cobc.h"
#include "tree.h"
#include "ppparse.h"
#include "replace.h"

#ifdef _WIN32
#include <io.h> /* for access */
Expand Down Expand Up @@ -1970,6 +1969,11 @@ next_word_is_comment_paragraph_name (const char *buff)
return 1;
}

static COB_INLINE COB_A_INLINE int
is_space_or_nl (const char c)
{
return c == ' ' || c == '\n';
}

/* FIXME: try to optimize as this function used 25-10% (according to callgrind)
of the complete time spent in a sample run with
Expand Down
115 changes: 63 additions & 52 deletions cobc/replace.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@

#include "cobc.h"
#include "tree.h"
#include "replace.h"

#define TRUE 1
#define FALSE 0

/* This is an implementation of the *two* phases of COPY-REPLACING and
REPLACE on a stream of tokens: the stream of tokens generated by the
Expand All @@ -54,9 +50,9 @@
been applied.
The general entry point is `add_text_to_replace(stream, prequeue,
token)`, it adds `token` to `stream`, `prequeue` is TRUE if the
token)`, it adds `token` to `stream`, `prequeue` is 1 if the
token should not be treated immediately (because it may be merged
with other following tokens if they are of the same kind), FALSE
with other following tokens if they are of the same kind), 0
otherwise.
Initially, `pp_echo()` in `pplex.l` will use
Expand Down Expand Up @@ -153,42 +149,51 @@ struct cb_replacement_state {
char depth_buffer[MAX_DEPTH+1];
#define DEPTH depth_buffer + ( MAX_DEPTH-depth )

#else // DEBUG_REPLACE_TRACE
#else /* DEBUG_REPLACE_TRACE */

#define WITH_DEPTH
#define DEPTH
#define INIT_DEPTH
#define MORE_DEPTH

#endif // DEBUG_REPLACE_TRACE
#endif /* DEBUG_REPLACE_TRACE */


#ifdef DEBUG_REPLACE

#define MAX_TEXT_LIST_STRING 10000
char text_list_string[MAX_TEXT_LIST_STRING];

static
char * string_of_list(const struct cb_text_list *list)
{
int pos = 1;
text_list_string[0] = '[';

for(; list != NULL; list = list->next){
size_t len = strlen(list->text);
text_list_string[pos++] = '"';
memcpy( text_list_string + pos, list->text, len );
pos += len;
text_list_string[pos++] = '"';
text_list_string[pos++] = ',';
text_list_string[pos++] = ' ';
}

text_list_string[pos] = ']';
text_list_string[pos+1]=0;
return text_list_string;
/* In debugging mode only, stores a list of text/tokens into a
preallocated string for easy display */
#define STRING_OF_LIST(kind) \
static \
char * string_of_##kind##_list(const struct cb_##kind##_list *list) \
{ \
int pos = 1; \
text_list_string[0] = '['; \
\
for(; list != NULL; list = list->next){ \
size_t len = strlen(list->text); \
text_list_string[pos++] = '"'; \
memcpy( text_list_string + pos, list->text, len ); \
pos += len; \
text_list_string[pos++] = '"'; \
text_list_string[pos++] = ','; \
text_list_string[pos++] = ' '; \
} \
\
text_list_string[pos] = ']'; \
text_list_string[pos+1]=0; \
return text_list_string; \
}
#endif // DEBUG_REPLACE

/* string_of_token_list (...) */
STRING_OF_LIST(token)
/* string_of_text_list (...) */
STRING_OF_LIST(text)

#endif /* DEBUG_REPLACE */

/* global state */
static struct cb_replacement_state * replace_repls;
Expand Down Expand Up @@ -222,7 +227,7 @@ token_list_add (WITH_DEPTH struct cb_token_list *list,
{
#ifdef DEBUG_REPLACE_TRACE
fprintf(stderr, "%stoken_list_add(%s,'%s')\n",
DEPTH, string_of_list(list), text);
DEPTH, string_of_token_list(list), text);
#endif
struct cb_token_list *p;

Expand Down Expand Up @@ -286,7 +291,7 @@ void ppecho_switch_text_list (WITH_DEPTH struct cb_replacement_state *repls,
{
#ifdef DEBUG_REPLACE_TRACE
fprintf(stderr, "%sppecho_switch_text_list(%s, %s)\n",
DEPTH, repls->name, string_of_list(p));
DEPTH, repls->name, string_of_text_list(p));
#endif

for (;p;p=p->next){
Expand All @@ -301,7 +306,7 @@ void ppecho_switch_token_list (WITH_DEPTH struct cb_replacement_state *repls,
{
#ifdef DEBUG_REPLACE_TRACE
fprintf(stderr, "%sppecho_switch_token_list(%s, %s)\n",
DEPTH, repls->name, string_of_list(p));
DEPTH, repls->name, string_of_token_list(p));
#endif

for (;p;p=p->next){
Expand All @@ -316,18 +321,18 @@ int is_leading_or_trailing (WITH_DEPTH int leading,
int strict)
{

size_t src_len = strlen (src_text);
size_t text_len = strlen(text);
const size_t src_len = strlen (src_text);
const size_t text_len = strlen(text);
int result ;
if( text_len > src_len || ( !strict && text_len == src_len ) ){
int pos = leading ? 0 : text_len - src_len ;
if( strncasecmp (src_text, text+pos, src_len) ){
result = FALSE;
result = 0;
} else {
result = TRUE;
result = 1;
}
} else {
result = FALSE;
result = 0;
}
#ifdef DEBUG_REPLACE_TRACE
fprintf(stderr,
Expand Down Expand Up @@ -453,6 +458,12 @@ void check_replace (WITH_DEPTH struct cb_replacement_state* repls,
}
}

static COB_INLINE COB_A_INLINE int
is_space_or_nl (const char c)
{
return c == ' ' || c == '\n';
}

/* `check_replace_all( repls, new_text, texts, src, replace_list )`:
* checks whether a particular replacement is possible on the current
* list of texts.
Expand All @@ -474,11 +485,11 @@ void check_replace_all (WITH_DEPTH
fprintf(stderr, "%scheck_replace_all(%s,",
DEPTH, repls->name);
fprintf(stderr, "%s new_text = %s,\n", DEPTH,
string_of_list(new_text));
string_of_text_list(new_text));
fprintf(stderr, "%s texts = %s,\n", DEPTH,
string_of_list(texts));
string_of_token_list(texts));
fprintf(stderr, "%s src = %s,\n", DEPTH,
string_of_list(src));
string_of_text_list(src));
fprintf(stderr, "%s)\n", DEPTH);
#endif

Expand Down Expand Up @@ -608,15 +619,15 @@ int is_word (WITH_DEPTH const char* s){

} else {
#ifdef DEBUG_REPLACE_TRACE
fprintf(stderr, "%sis_word('%s') -> FALSE\n", DEPTH, s);
fprintf(stderr, "%sis_word('%s') -> 0\n", DEPTH, s);
#endif
return FALSE;
return 0;
}
}
#ifdef DEBUG_REPLACE_TRACE
fprintf(stderr, "%sis_word('%s') -> TRUE\n", DEPTH, s);
fprintf(stderr, "%sis_word('%s') -> 1\n", DEPTH, s);
#endif
return TRUE;
return 1;
}

static void add_text_to_replace (WITH_DEPTH struct cb_replacement_state *repls,
Expand Down Expand Up @@ -649,7 +660,7 @@ static void add_text_to_replace (WITH_DEPTH struct cb_replacement_state *repls,
if( repls->text_prequeue == NULL ){
/* not a word, and empty prequeue,
* just perform replacements */
add_text_to_replace(MORE_DEPTH repls, FALSE, text, token);
add_text_to_replace(MORE_DEPTH repls, 0, text, token);
} else {
/* not a word, one word in the
* prequeue, flush the word from the
Expand All @@ -658,9 +669,9 @@ static void add_text_to_replace (WITH_DEPTH struct cb_replacement_state *repls,
const char* pretext = repls->text_prequeue;
repls->text_prequeue = NULL;
add_text_to_replace(MORE_DEPTH repls,
FALSE, pretext, NULL);
0, pretext, NULL);
add_text_to_replace(MORE_DEPTH repls,
FALSE, text, token);
0, text, token);
}
}
}
Expand All @@ -684,27 +695,27 @@ static void add_text_to_replace (WITH_DEPTH struct cb_replacement_state *repls,
}

/* pass a text to the replace stream (called from the copy-replacing
stream). Use prequeue = TRUE so that texts of the same kind are
stream). Use prequeue = 1 so that texts of the same kind are
merged into a single text.
*/
static void ppecho_replace (WITH_DEPTH const char *text, const char *token)
{
#ifdef DEBUG_REPLACE
fprintf(stderr, "%sppecho_replace('%s')\n", DEPTH, text);
#endif
add_text_to_replace(MORE_DEPTH replace_repls, TRUE, text, token);
add_text_to_replace(MORE_DEPTH replace_repls, 1, text, token);
}

/* pass a text to the copy-replacing stream (called from ppecho() in
pplex.l). Use prequeue = FALSE as texts of the same kind from the
pplex.l). Use prequeue = 0 as texts of the same kind from the
source file should not be merged.
*/
void cb_ppecho_copy_replace (const char *text, const char *token)
{
#ifdef DEBUG_REPLACE
fprintf(stderr, "cb_ppecho_copy_replace('%s')\n", text);
#endif
add_text_to_replace(INIT_DEPTH copy_repls, FALSE, text, token);
add_text_to_replace(INIT_DEPTH copy_repls, 0, text, token);
}


Expand Down Expand Up @@ -790,11 +801,11 @@ void cb_set_copy_replacing_list (struct cb_replace_list *list)
for(;list != NULL; list=list->next){
fprintf(stderr, " repl = {\n");
fprintf(stderr, " src = %s\n",
string_of_list(list->src->text_list));
string_of_text_list(list->src->text_list));
fprintf(stderr, " leading = %d\n",
list->src->lead_trail);
fprintf(stderr, " new_text = %s\n",
string_of_list(list->new_text));
string_of_text_list(list->new_text));
fprintf(stderr, " };\n");
}
fprintf(stderr, " )\n");
Expand Down
45 changes: 0 additions & 45 deletions cobc/replace.h

This file was deleted.

15 changes: 15 additions & 0 deletions cobc/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -2572,6 +2572,21 @@ extern cb_tree cobc_tree_cast_check (const cb_tree, const char *,
const int, const enum cb_tag);
#endif

/* pplex.l */
extern void cb_ppecho_direct (const char *text, const char *token );
extern struct cb_text_list *cb_pp_text_list_add (struct cb_text_list *,
const char *, const size_t);
/* replace.c */
extern void cb_ppecho_copy_replace (const char *text, const char *token );
extern void cb_free_replace (void);

/* For COPY-REPLACING */
extern void cb_set_copy_replacing_list (struct cb_replace_list *list);
extern struct cb_replace_list * cb_get_copy_replacing_list (void);

extern void
cb_set_print_replace_list (struct cb_replace_list *);

/* codeoptim.c */
extern void cob_gen_optim (const enum cb_optim);

Expand Down

0 comments on commit 8bdfd9e

Please sign in to comment.