Pretty print rules
This commit is contained in:
parent
392e05d19e
commit
a2ea2a6ac2
44
src/modal.c
44
src/modal.c
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
unsigned int id, refs, ptr;
|
unsigned int id, refs, ptr;
|
||||||
char *a, *b, reg[0x8];
|
char *a, *b, reg[8];
|
||||||
} Rule;
|
} Rule;
|
||||||
|
|
||||||
static int flip, quiet, cycles = 0x10000;
|
static int flip, quiet, cycles = 0x10000;
|
||||||
|
@ -102,8 +102,8 @@ write_tail(char *s)
|
||||||
static int
|
static int
|
||||||
apply_rule(Rule *r, char *s)
|
apply_rule(Rule *r, char *s)
|
||||||
{
|
{
|
||||||
unsigned int i, id;
|
unsigned int i, rid;
|
||||||
char c, *a = r->a, *b = r->b, *origin = dst_, *reg, *regs[0x8];
|
char c, *a = r->a, *b = r->b, *origin = dst_, *reg, *regs[8];
|
||||||
/* phase: clean regs */
|
/* phase: clean regs */
|
||||||
for(i = 0; i < r->ptr; i++)
|
for(i = 0; i < r->ptr; i++)
|
||||||
regs[i] = NULL;
|
regs[i] = NULL;
|
||||||
|
@ -111,13 +111,13 @@ apply_rule(Rule *r, char *s)
|
||||||
while((c = *a++)) {
|
while((c = *a++)) {
|
||||||
if(c == '?') {
|
if(c == '?') {
|
||||||
char *pcap = walk(s);
|
char *pcap = walk(s);
|
||||||
id = *a++ - '0';
|
rid = *a++ - '0';
|
||||||
if((reg = regs[id])) { /* reg cmp */
|
if((reg = regs[rid])) { /* reg cmp */
|
||||||
char *rcap = walk(reg), *pp = s;
|
char *rcap = walk(reg), *pp = s;
|
||||||
while(reg < rcap || pp < pcap)
|
while(reg < rcap || pp < pcap)
|
||||||
if(*reg++ != *pp++) return 0;
|
if(*reg++ != *pp++) return 0;
|
||||||
} else /* reg set */
|
} else /* reg set */
|
||||||
regs[id] = s;
|
regs[rid] = s;
|
||||||
s = pcap;
|
s = pcap;
|
||||||
} else if(c != *s++)
|
} else if(c != *s++)
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -127,9 +127,9 @@ apply_rule(Rule *r, char *s)
|
||||||
/* phase: write rule */
|
/* phase: write rule */
|
||||||
while((c = *b++)) {
|
while((c = *b++)) {
|
||||||
if(c == '?') {
|
if(c == '?') {
|
||||||
id = *b - '0';
|
rid = *b - '0';
|
||||||
if(id < 9 && (reg = regs[id]))
|
if(rid < 9 && (reg = regs[rid]))
|
||||||
b++, write_reg(r->reg[id], reg);
|
b++, write_reg(r->reg[rid], reg);
|
||||||
else
|
else
|
||||||
*dst_++ = c;
|
*dst_++ = c;
|
||||||
} else
|
} else
|
||||||
|
@ -146,9 +146,9 @@ apply_rule(Rule *r, char *s)
|
||||||
static int
|
static int
|
||||||
find_reg(Rule *r, char reg)
|
find_reg(Rule *r, char reg)
|
||||||
{
|
{
|
||||||
int i;
|
int rid;
|
||||||
for(i = 0; i < (int)r->ptr; i++)
|
for(rid = 0; rid < (int)r->ptr; rid++)
|
||||||
if(r->reg[i] == reg) return i;
|
if(r->reg[rid] == reg) return rid;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -204,6 +204,20 @@ find_rule(char *s, char *cap)
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
echo_rule(Rule *r, char *field)
|
||||||
|
{
|
||||||
|
char c, *s = field;
|
||||||
|
putc('(', stdout);
|
||||||
|
while((c = *s++)) {
|
||||||
|
int rid;
|
||||||
|
putc(c, stdout);
|
||||||
|
if(c == '?')
|
||||||
|
c = *s++, rid = c - '0', putc(rid < 8 ? r->reg[rid] : c, stdout);
|
||||||
|
}
|
||||||
|
putc(')', stdout), putc(' ', stdout);
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
rewrite(void)
|
rewrite(void)
|
||||||
{
|
{
|
||||||
|
@ -216,7 +230,8 @@ rewrite(void)
|
||||||
if(c == '<' && s[1] == '>') {
|
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);
|
||||||
if(!quiet && r->a) printf("<> (%s) (%s)\n", r->a, r->b);
|
if(!quiet && r->a)
|
||||||
|
printf("<> "), echo_rule(r, r->a), echo_rule(r, r->b), putc('\n', stdout);
|
||||||
while(*s == ' ') s++;
|
while(*s == ' ') s++;
|
||||||
return write_tail(s);
|
return write_tail(s);
|
||||||
}
|
}
|
||||||
|
@ -225,7 +240,8 @@ rewrite(void)
|
||||||
s += 2;
|
s += 2;
|
||||||
while(*s == ' ') s++;
|
while(*s == ' ') s++;
|
||||||
cap = walk(s), r = find_rule(s, cap), r->a = 0;
|
cap = walk(s), r = find_rule(s, cap), r->a = 0;
|
||||||
if(!quiet && r->a) printf(">< (%s)\n", r->a);
|
if(!quiet && r->a)
|
||||||
|
printf(">< "), echo_rule(r, r->a), putc('\n', stdout);
|
||||||
while(*cap == ' ') cap++;
|
while(*cap == ' ') cap++;
|
||||||
return write_tail(cap);
|
return write_tail(cap);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue