Inline rule parsing

This commit is contained in:
Devine Lu Linvega 2024-04-05 13:19:34 -07:00
parent 7147e8e5ad
commit 3c99fb47f6
2 changed files with 18 additions and 3 deletions

View File

@ -1,4 +1,3 @@
<> (eq ?x ?x) (#t) <> ((?x -> ?y)) (<> ?x ?y)
<> (eq ?x ?y) (#f)
(eq fox fox) (eq fox owl) (foo -> bar) foo

View File

@ -13,6 +13,8 @@ static char *regs[0x100];
#define spacer(c) (c == ' ' || c == '(' || c == ')') #define spacer(c) (c == ' ' || c == '(' || c == ')')
static char *parse_rulefrag(char *line);
static char * static char *
walk(char *s) walk(char *s)
{ {
@ -92,12 +94,26 @@ save(int rule)
printf("%02d %s\n", rule, prog); printf("%02d %s\n", rule, prog);
} }
static char *
addrule(char *s)
{
Rule *r = &rules[rules_len++];
s += 3;
r->a = parse_rulefrag(s);
s = walk(s) + 1;
r->b = parse_rulefrag(s);
s = walk(s);
return s;
}
static int static int
rewrite(void) rewrite(void)
{ {
char c, *p = prog; char c, *p = prog;
while((c = *p)) { while((c = *p)) {
int i; int i;
if(p[0] == '<' && p[1] == '>')
p = addrule(p) + 1, c = *p;
for(i = 0; i < rules_len; i++) { for(i = 0; i < rules_len; i++) {
Rule *r = &rules[i]; Rule *r = &rules[i];
char *res = match(p, r); char *res = match(p, r);