Skip empty rules

This commit is contained in:
Devine Lu Linvega 2024-04-04 14:55:51 -07:00
parent 557dfcbecf
commit c174832c8e
2 changed files with 19 additions and 17 deletions

View File

@ -53,11 +53,22 @@ writereg(char r)
return 1; return 1;
} }
static void
save(void)
{
int i, end = outp_ - outp;
for(i = 0; i < end; i++)
prog[i] = outp[i];
prog[i] = 0;
prog_ = prog;
outp_ = outp;
printf(".. %s\n", prog);
}
static int static int
rewrite(void) rewrite(void)
{ {
char c, *p = prog; char c, *p = prog;
printf("STEP ---------\n");
while((c = *p)) { while((c = *p)) {
int i, found = 0; int i, found = 0;
for(i = 0; i < rules_len; i++) { for(i = 0; i < rules_len; i++) {
@ -65,7 +76,6 @@ rewrite(void)
char *res = match(p, r); char *res = match(p, r);
if(res != NULL) { if(res != NULL) {
char cc, *b = r->b; char cc, *b = r->b;
printf("MATCH: %s -> %s\n", r->a, r->b);
while((cc = *b++)) { while((cc = *b++)) {
if(cc == '?') if(cc == '?')
writereg(*b++); writereg(*b++);
@ -82,18 +92,19 @@ rewrite(void)
} }
} }
*outp_++ = 0; *outp_++ = 0;
save();
return 1; return 1;
} }
static void static void
display(void) print_rules(void)
{ {
int i; int i;
for(i = 0; i < rules_len; i++) { for(i = 0; i < rules_len; i++) {
Rule *r = &rules[i]; Rule *r = &rules[i];
printf("Rule #%d: %s -> %s\n", i, r->a, r->b); printf("Rule #%d: %s -> %s\n", i, r->a, r->b);
} }
printf("\nStep %s -- %s\n", prog, outp); printf("\n");
} }
static char * static char *
@ -117,6 +128,7 @@ static void
tokenize(char *t, FILE *f) tokenize(char *t, FILE *f)
{ {
char c; char c;
if(!t[0]) return;
if(t[0] == '<' && t[1] == '>') { if(t[0] == '<' && t[1] == '>') {
Rule *r = &rules[rules_len++]; Rule *r = &rules[rules_len++];
r->a = parse_rulefrag(f), r->b = parse_rulefrag(f); r->a = parse_rulefrag(f), r->b = parse_rulefrag(f);
@ -147,15 +159,6 @@ parse(char *path)
return 1; return 1;
} }
static void
save(void)
{
int i, end = outp_ - outp;
for(i = 0; i < end; i++)
prog[i] = outp[i];
prog[i] = 0;
}
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
@ -164,10 +167,9 @@ main(int argc, char **argv)
if(argc < 3 && argv[1][0] == '-' && argv[1][1] == 'v') if(argc < 3 && argv[1][0] == '-' && argv[1][1] == 'v')
return !printf("Modal - Modal Interpreter, 3 Apr 2024.\n"); return !printf("Modal - Modal Interpreter, 3 Apr 2024.\n");
parse(argv[1]); parse(argv[1]);
print_rules();
printf(".. %s\n", prog);
rewrite(); rewrite();
display();
save();
rewrite(); rewrite();
display();
return 0; return 0;
} }

View File

@ -1,4 +1,4 @@
<> (?x ?y swap) (?y ?x)
<> (?x dup) (?x ?x) <> (?x dup) (?x ?x)
<> (?x ?y swap) (?y ?x)
foo ((A B swap) dup) foo ((A B swap) dup)