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