Skip to content

Commit

Permalink
Track shortest string in each among in analyser
Browse files Browse the repository at this point in the history
This refactor eliminates duplicate code from the C and Ada generators,
and means this data is available to other generators that want to use
it.
  • Loading branch information
ojwb committed Jan 30, 2024
1 parent f6a2ca9 commit f0ad585
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 18 deletions.
4 changes: 4 additions & 0 deletions compiler/analyser.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <assert.h>
#include <limits.h> /* for INT_MAX */
#include <stdio.h> /* printf etc */
#include <stdlib.h> /* exit */
#include <string.h> /* memmove */
Expand Down Expand Up @@ -760,6 +761,7 @@ static struct node * make_among(struct analyser * a, struct node * p, struct nod
x->nocommand_count = 0;
x->amongvar_needed = false;
x->always_matches = false;
x->shortest_size = INT_MAX;

if (q->type == c_bra) { starter = q; q = q->right; }

Expand Down Expand Up @@ -847,6 +849,8 @@ static struct node * make_among(struct analyser * a, struct node * p, struct nod
int size = w0->size;
struct amongvec * w;

if (size && size < x->shortest_size) x->shortest_size = size;

for (w = w0 - 1; w >= v; w--) {
if (w->size < size && memcmp(w->b, b, w->size * sizeof(symbol)) == 0) {
w0->i = w - v; /* fill in index of longest substring */
Expand Down
10 changes: 1 addition & 9 deletions compiler/generator.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <assert.h>
#include <limits.h> /* for INT_MAX */
#include <stdio.h> /* for fprintf etc */
#include <stdlib.h> /* for free etc */
#include <string.h> /* for strlen */
Expand Down Expand Up @@ -1460,7 +1459,7 @@ static void generate_substring(struct generator * g, struct node * p) {
int empty_case = -1;
int n_cases = 0;
symbol cases[2];
int shortest_size = INT_MAX;
int shortest_size = x->shortest_size;
int shown_comment = 0;

g->S[0] = p->mode == m_forward ? "" : "_b";
Expand All @@ -1474,13 +1473,6 @@ static void generate_substring(struct generator * g, struct node * p) {
* In backward mode, we can't match if there are fewer characters before
* the current position than the minimum length.
*/
for (c = 0; c < x->literalstring_count; ++c) {
int size = among_cases[c].size;
if (size != 0 && size < shortest_size) {
shortest_size = size;
}
}

for (c = 0; c < x->literalstring_count; ++c) {
symbol ch;
if (among_cases[c].size == 0) {
Expand Down
10 changes: 1 addition & 9 deletions compiler/generator_ada.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <string.h> /* for strlen */
#include <stdio.h> /* for fprintf etc */
#include <ctype.h>
#include <limits.h>
#include "header.h"

/* prototypes */
Expand Down Expand Up @@ -1164,7 +1163,7 @@ static void generate_substring(struct generator * g, struct node * p) {
int empty_case = -1;
int n_cases = 0;
symbol cases[2];
int shortest_size = INT_MAX;
int shortest_size = x->shortest_size;
int call_done = 0;
int need_among_handler = (x->function_count > 0);

Expand All @@ -1180,13 +1179,6 @@ static void generate_substring(struct generator * g, struct node * p) {
* In backward mode, we can't match if there are fewer characters before
* the current position than the minimum length.
*/
for (c = 0; c < x->literalstring_count; ++c) {
int size = among_cases[c].size;
if (size != 0 && size < shortest_size) {
shortest_size = size;
}
}

for (c = 0; c < x->literalstring_count; ++c) {
symbol ch;
if (among_cases[c].size == 0) {
Expand Down
1 change: 1 addition & 0 deletions compiler/header.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ struct among {
int function_count; /* in this among */
int amongvar_needed; /* do we need to set among_var? */
int always_matches; /* will this among always match? */
int shortest_size; /* smallest non-zero string length in this among */
struct node * substring; /* i.e. substring ... among ( ... ) */
struct node ** commands; /* array with command_count entries */
};
Expand Down

0 comments on commit f0ad585

Please sign in to comment.