This commit is contained in:
Devine Lu Linvega 2024-04-04 11:35:58 -07:00
parent 511053b4ca
commit f9dbae56bb
2 changed files with 10 additions and 14 deletions

View File

@ -8,23 +8,20 @@ static int rules_len;
static Rule rules[0x100]; static Rule rules[0x100];
static char dict[0x8000], *next = dict; static char dict[0x8000], *next = dict;
static char prog[0x1000], *prog_ = prog; static char prog[0x1000], *prog_ = prog;
static char *regs[0x100]; static char *regs[0x100];
static void
bind(int id, char *addr)
{
printf("bind register %02x -> %s\n", id, addr);
regs[id] = addr;
}
static char * static char *
walk(char *s) walk(char *s)
{ {
int depth = 0;
char c; char c;
while((c = *s++)) while((c = *s++)) {
if(*s == ')' || *s == ' ') break; if(c == '(') depth++;
return s; if(c == ')') --depth;
if(c == ' ' && !depth)
break;
}
return s - 1;
} }
static int static int
@ -32,7 +29,7 @@ match(char *p, Rule *r)
{ {
char c, *a = r->a, *b = p; char c, *a = r->a, *b = p;
while((c = *a)) { while((c = *a)) {
if(c == '?') bind(*(++a), b), a++, b = walk(b), c = *b; if(c == '?') regs[(int)*(++a)] = b, a++, b = walk(b), c = *b;
if(c != *b) return 0; if(c != *b) return 0;
a++, b++; a++, b++;
} }

View File

@ -1,4 +1,3 @@
<> (foo ?a) (fuu ?a)
<> (?b bar) (?b baz) <> (?b bar) (?b baz)
(foo bar) ((foo baz) bar)