This commit is contained in:
Devine Lu Linvega 2024-04-04 14:48:04 -07:00
parent 91b68104d0
commit 557dfcbecf
2 changed files with 18 additions and 1 deletions

View File

@ -1,4 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <string.h>
typedef struct { typedef struct {
char *a, *b; char *a, *b;
@ -56,6 +57,7 @@ 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++) {
@ -63,6 +65,7 @@ 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++);
@ -78,6 +81,7 @@ rewrite(void)
p++; p++;
} }
} }
*outp_++ = 0;
return 1; return 1;
} }
@ -143,6 +147,15 @@ 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)
{ {
@ -153,5 +166,8 @@ main(int argc, char **argv)
parse(argv[1]); parse(argv[1]);
rewrite(); rewrite();
display(); display();
save();
rewrite();
display();
return 0; return 0;
} }

View File

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