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