Display rewrites count

This commit is contained in:
Devine Lu Linvega 2024-04-27 08:46:31 -07:00
parent 8b618d2425
commit e83ac9ce02
1 changed files with 20 additions and 20 deletions

View File

@ -87,7 +87,7 @@ write_reg(char r, char *reg)
}
static int
write_rule(Rule *r, char *s)
write_tail(char *s)
{
while((*dst_++ = *s++))
;
@ -96,7 +96,6 @@ write_rule(Rule *r, char *s)
src_ = bank_b, dst_ = bank_a;
else
src_ = bank_a, dst_ = bank_b;
if(!quiet && r) fprintf(stderr, "%02d %s\n", r->id, src_), ++r->refs;
return 1;
}
@ -105,7 +104,7 @@ apply_rule(Rule *r, char *s)
{
unsigned int i, id;
char c, *a = r->a, *b = r->b, *origin = dst_, *reg, *regs[0x8];
/* phase: clean registers */
/* phase: clean regs */
for(i = 0; i < r->ptr; i++)
regs[i] = NULL;
/* phase: match rule */
@ -140,11 +139,12 @@ apply_rule(Rule *r, char *s)
while(*s == ' ') s++;
if(*s == ')' && *(dst_ - 1) == ' ') dst_--;
}
return write_rule(r, s);
if(!quiet && r) fprintf(stderr, "%02d %s\n", r->id, src_), ++r->refs;
return write_tail(s);
}
static int
find_register(Rule *r, char reg)
find_reg(Rule *r, char reg)
{
int i;
for(i = 0; i < (int)r->ptr; i++)
@ -166,7 +166,7 @@ compile_rule(Rule *r, int id, char *src)
while(src < cap) {
c = *src, *dict_++ = *src++;
if(c == '?') {
c = *src++, reg = find_register(r, c);
c = *src++, reg = find_reg(r, c);
if(reg == -1 && c != '(')
r->reg[r->ptr] = c, reg = r->ptr++;
*dict_++ = '0' + reg;
@ -181,7 +181,7 @@ compile_rule(Rule *r, int id, char *src)
while(src < cap) {
c = *src, *dict_++ = *src++;
if(c == '?') {
c = *src++, reg = find_register(r, c);
c = *src++, reg = find_reg(r, c);
*dict_++ = reg != -1 ? '0' + reg : c;
}
}
@ -212,20 +212,13 @@ rewrite(void)
while((c = *s)) {
if(c == '(' || spacer(last)) {
Rule *r = NULL;
/* phase: lambda */
if(c == '?' && s[1] == '(') {
cap = walk(s + 1), compile_rule(&lambda, -1, s + 2), s = cap;
while(*s == ' ') s++;
if(!apply_rule(&lambda, s)) write_rule(&lambda, s);
return 1;
}
/* phase: define */
if(c == '<' && s[1] == '>') {
r = rules_++;
s = compile_rule(r, rules_ - rules - 1, s + 2);
if(!quiet && r->a) fprintf(stderr, "<> (%s) (%s)\n", r->a, r->b);
while(*s == ' ') s++;
return write_rule(NULL, s);
return write_tail(s);
}
/* phase: undefine */
if(c == '>' && s[1] == '<') {
@ -234,7 +227,14 @@ rewrite(void)
cap = walk(s), r = find_rule(s, cap), r->a = 0;
if(!quiet && r->a) fprintf(stderr, ">< (%s)\n", r->a);
while(*cap == ' ') cap++;
return write_rule(NULL, cap);
return write_tail(cap);
}
/* phase: lambda */
if(c == '?' && s[1] == '(') {
cap = walk(s + 1), compile_rule(&lambda, -1, s + 2), s = cap;
while(*s == ' ') s++;
if(!apply_rule(&lambda, s)) write_tail(s);
return 1;
}
/* phase: match */
for(r = rules; r < rules_; r++)
@ -251,13 +251,13 @@ int
main(int argc, char **argv)
{
FILE *f;
int i, pl = 0, pr = 0;
int i, pl = 0, pr = 0, rw = 0;
char c, last = 0, *w = bank_a;
if(argc < 2)
return !printf("usage: modal [-vqn] source.modal\n");
for(i = 1; i < argc && *argv[i] == '-'; i++) {
switch(argv[i][1]) {
case 'v': /* version */ return !printf("Modal Interpreter, 26 Apr 2024.\n");
case 'v': /* version */ return !printf("Modal Interpreter, 27 Apr 2024.\n");
case 'q': /* quiet */ quiet = 1; break;
case 'n': /* infinite */ cycles = 0xffffffff; break;
}
@ -279,10 +279,10 @@ main(int argc, char **argv)
fclose(f);
if(pr != pl)
return !fprintf(stdout, "Modal program imbalanced.\n");
while(rewrite())
while(rewrite() && ++rw)
if(!cycles--) return !fprintf(stdout, "Modal rewrites exceeded.\n");
while(rules_-- > rules && !quiet)
if(!rules_->refs && rules_->a) printf("-- Unused rule: %d <> (%s) (%s)\n", rules_->refs, rules_->a, rules_->b);
if(!quiet) printf(".. %s\n", src_);
if(!quiet) printf(".. %s\nCompleted in %d rewrites.\n", src_, rw);
return 0;
}