Print unused rules

This commit is contained in:
Devine Lu Linvega 2024-04-22 15:29:54 -07:00
parent 6fb9f96b0c
commit e153eb797b
1 changed files with 11 additions and 7 deletions

View File

@ -1,11 +1,11 @@
#include <stdio.h> #include <stdio.h>
typedef struct { typedef struct {
int id; int id, refs;
char *a, *b; char *a, *b;
} Rule; } Rule;
static int flip, rmin = 0xff, rmax = 0x00, cycles = 0x10000; static int flip, quiet, rmin = 0xff, rmax = 0x00, cycles = 0x10000;
static Rule rules[0x1000], lambda, *rules_ = rules; static Rule rules[0x1000], lambda, *rules_ = rules;
static char dict[0x8000], *dict_ = dict; static char dict[0x8000], *dict_ = dict;
static char bank_a[0x4000], *src_ = bank_a; static char bank_a[0x4000], *src_ = bank_a;
@ -122,10 +122,12 @@ commit_rule(Rule *r, char *s, int create)
src_ = bank_b, dst_ = bank_a; src_ = bank_b, dst_ = bank_a;
else else
src_ = bank_a, dst_ = bank_b; src_ = bank_a, dst_ = bank_b;
if(create) if(!quiet) {
fprintf(stderr, "<> (%s) (%s)\n", r->a, r->b); if(create)
else fprintf(stderr, "<> (%s) (%s)\n", r->a, r->b);
fprintf(stderr, "%02d %s\n", r->id, src_); else
fprintf(stderr, "%02d %s\n", r->id, src_), ++r->refs;
}
return 1; return 1;
} }
@ -209,7 +211,7 @@ main(int argc, char **argv)
for(i = 1; i < argc && *argv[i] == '-'; i++) { for(i = 1; i < argc && *argv[i] == '-'; i++) {
switch(argv[i][1]) { switch(argv[i][1]) {
case 'v': /* version */ return !printf("Modal Interpreter, 22 Apr 2024.\n"); case 'v': /* version */ return !printf("Modal Interpreter, 22 Apr 2024.\n");
case 'q': /* quiet */ fclose(stderr); break; case 'q': /* quiet */ quiet = 1; break;
case 'n': /* infinite */ cycles = 0xffffffff; break; case 'n': /* infinite */ cycles = 0xffffffff; break;
} }
} }
@ -232,5 +234,7 @@ main(int argc, char **argv)
return !fprintf(stdout, "Modal program imbalanced.\n"); return !fprintf(stdout, "Modal program imbalanced.\n");
while(rewrite()) while(rewrite())
if(!cycles--) return !fprintf(stdout, "Modal rewrites exceeded.\n"); if(!cycles--) return !fprintf(stdout, "Modal rewrites exceeded.\n");
while(rules_-- > rules && !quiet)
if(!rules_->refs) printf("-- Unused rule: %d <> (%s) (%s)\n", rules_->refs, rules_->a, rules_->b);
return 0; return 0;
} }