Added comments

This commit is contained in:
Devine Lu Linvega 2024-04-24 15:55:48 -07:00
parent bd6a194e24
commit 56bb5969ab
1 changed files with 13 additions and 10 deletions

View File

@ -105,15 +105,16 @@ write_rule(Rule *r, char *s, int create)
static int
apply_rule(Rule *r, char *s)
{
unsigned int i;
unsigned int i, id;
char c, *a = r->a, *regs[0x08], *reg;
/* phase: clean registers */
for(i = 0; i < r->ptr; i++) regs[i] = NULL;
for(i = 0; i < r->ptr; i++)
regs[i] = NULL;
/* phase: match rule */
while((c = *a)) {
if(c == '?') {
char *pcap = walk(s);
unsigned int id = *(++a) - '0';
id = *(++a) - '0';
if((reg = regs[id])) { /* reg cmp */
char *rcap = walk(reg), *pp = s;
while(reg < rcap || pp < pcap)
@ -133,11 +134,10 @@ apply_rule(Rule *r, char *s)
char *b = r->b, *origin = dst_;
while((c = *b++)) {
if(c == '?') {
unsigned int id = *b - '0';
if(id < 9 && (reg = regs[id])) {
b++;
if(!write_reg(r->reg[id], reg) && dst_ != origin) dst_--;
} else
id = *b - '0';
if(id < 9 && (reg = regs[id]))
b++, write_reg(r->reg[id], reg);
else
*dst_++ = c;
} else
*dst_++ = c;
@ -206,19 +206,22 @@ rewrite(void)
while((c = *s)) {
if(spacer(last)) {
Rule *r = NULL;
if(c == '<' && s[1] == '>') { /* rule */
/* 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);
}
if(c == '?' && s[1] == '(') { /* lambda */
/* phase: lambda */
if(c == '?' && s[1] == '(') {
cap = walk(s + 1);
compile_rule(&lambda, -1, s + 2);
s = cap;
while(*s == ' ') s++;
return apply_rule(&lambda, s);
}
/* phase: match */
for(r = rules; r < rules_; r++)
if(apply_rule(r, s)) return 1;
}