From 56bb5969ab1f1f519c86949bd0a818fa4f9ada51 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Wed, 24 Apr 2024 15:55:48 -0700 Subject: [PATCH] Added comments --- src/modal.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/modal.c b/src/modal.c index d52abfd..ab22cb8 100644 --- a/src/modal.c +++ b/src/modal.c @@ -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; }