Initial implementation of undefine rules

This commit is contained in:
Devine Lu Linvega 2024-04-26 20:50:22 -07:00
parent a7abb9475a
commit 49614f1a61
2 changed files with 41 additions and 22 deletions

View File

@ -8,8 +8,8 @@
?(?-) (Formatter) ?(?-) (Formatter)
?((?x ?y) one) aaa(bbb) = one (formatter 1) test ?((?x ?y) two) aaa(bbb) = two (formatter 1) test
?((?x ?y) one) (bbb)aaa = one (formatter 2) test ?((?x ?y) two) (bbb)aaa = two (formatter 2) test
(a b c ) = (a b c) (formatter 3) test (a b c ) = (a b c) (formatter 3) test
( a b c) = (a b c) (formatter 4) test ( a b c) = (a b c) (formatter 4) test
( a b c ) = (a b c) (formatter 5) test ( a b c ) = (a b c) (formatter 5) test

View File

@ -2,7 +2,7 @@
typedef struct { typedef struct {
unsigned int id, refs, ptr; unsigned int id, refs, ptr;
char *a, *b, reg[0x10]; char *a, *b, reg[0x8];
} Rule; } Rule;
static int flip, quiet, cycles = 0x10000; static int flip, quiet, cycles = 0x10000;
@ -87,7 +87,7 @@ write_reg(char r, char *reg)
} }
static int static int
write_rule(Rule *r, char *s, int create) write_rule(Rule *r, char *s)
{ {
while((*dst_++ = *s++)) while((*dst_++ = *s++))
; ;
@ -96,12 +96,7 @@ write_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(!quiet) { if(!quiet && r) fprintf(stderr, "%02d %s\n", r->id, src_), ++r->refs;
if(create)
fprintf(stderr, "<> (%s) (%s)\n", r->a, r->b);
else
fprintf(stderr, "%02d %s\n", r->id, src_), ++r->refs;
}
return 1; return 1;
} }
@ -109,7 +104,7 @@ static int
apply_rule(Rule *r, char *s) 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[0x08]; char c, *a = r->a, *b = r->b, *origin = dst_, *reg, *regs[0x8];
/* phase: clean registers */ /* phase: clean registers */
for(i = 0; i < r->ptr; i++) for(i = 0; i < r->ptr; i++)
regs[i] = NULL; regs[i] = NULL;
@ -145,7 +140,7 @@ 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, 0); return write_rule(r, s);
} }
static int static int
@ -194,6 +189,19 @@ compile_rule(Rule *r, int id, char *src)
return src; return src;
} }
static Rule *
find_rule(char *s)
{
Rule *r = rules;
while(r < rules_) {
char *ss = s, *a = r->a;
while(*ss++ == *a++)
if(!*a) return r;
r++;
}
return NULL;
}
static int static int
rewrite(void) rewrite(void)
{ {
@ -202,23 +210,34 @@ rewrite(void)
while((c = *s)) { while((c = *s)) {
if(c == '(' || spacer(last)) { if(c == '(' || spacer(last)) {
Rule *r = NULL; Rule *r = NULL;
/* phase: rule */
if(c == '<' && s[1] == '>') {
r = rules_++;
s = compile_rule(r, rules_ - rules - 1, s + 2);
while(*s == ' ') s++;
return write_rule(r, s, 1);
}
/* phase: lambda */ /* phase: lambda */
if(c == '?' && s[1] == '(') { if(c == '?' && s[1] == '(') {
cap = walk(s + 1), compile_rule(&lambda, -1, s + 2), s = cap; cap = walk(s + 1), compile_rule(&lambda, -1, s + 2), s = cap;
while(*s == ' ') s++; while(*s == ' ') s++;
if(!apply_rule(&lambda, s)) write_rule(&lambda, s, 0); if(!apply_rule(&lambda, s)) write_rule(&lambda, s);
return 1; 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);
}
/* phase: undefine */
if(c == '>' && s[1] == '<') {
s += 2;
while(*s == ' ') s++;
r = find_rule(s), r->a = 0;
if(!quiet && r->a) fprintf(stderr, ">< (%s)\n", r->a);
s = walk(s);
while(*s == ' ') s++;
return write_rule(NULL, s);
}
/* phase: match */ /* phase: match */
for(r = rules; r < rules_; r++) for(r = rules; r < rules_; r++)
if(apply_rule(r, s)) return 1; if(r->a && apply_rule(r, s)) return 1;
} }
*dst_++ = last = c; *dst_++ = last = c;
s++; s++;
@ -263,6 +282,6 @@ main(int argc, char **argv)
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) printf("-- Unused rule: %d <> (%s) (%s)\n", rules_->refs, rules_->a, rules_->b); if(!rules_->refs) printf("-- Unused rule: %d <> (%s) (%s)\n", rules_->refs, rules_->a, rules_->b);
if(!quiet) printf(".. %s\n", src_); if(!quiet) printf(".. [%s]\n", src_);
return 0; return 0;
} }