Removed rules compilation step
This commit is contained in:
parent
d520ab8489
commit
dd1fddb20c
69
src/modal.c
69
src/modal.c
|
@ -1,15 +1,17 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
unsigned int id, refs, ptr;
|
unsigned int id, refs;
|
||||||
char *a, *b, reg[8];
|
char *a, *b;
|
||||||
} Rule;
|
} Rule;
|
||||||
|
|
||||||
|
static unsigned char rmin = 0xff, rmax = 0x00;
|
||||||
static int flip, quiet, cycles = 0x10000;
|
static int flip, quiet, cycles = 0x10000;
|
||||||
static Rule rules[0x1000], *rules_ = rules, lambda;
|
static Rule rules[0x1000], *rules_ = rules, lambda;
|
||||||
static char dict[0x8000], *dict_ = dict, empty;
|
static char dict[0x8000], *dict_ = dict, empty;
|
||||||
static char bank_a[0x4000], *src_ = bank_a;
|
static char bank_a[0x4000], *src_ = bank_a;
|
||||||
static char bank_b[0x4000], *dst_ = bank_b;
|
static char bank_b[0x4000], *dst_ = bank_b;
|
||||||
|
static char *regs[0x100];
|
||||||
|
|
||||||
#define spacer(c) (c <= ' ' || c == '(' || c == ')')
|
#define spacer(c) (c <= ' ' || c == '(' || c == ')')
|
||||||
|
|
||||||
|
@ -103,21 +105,27 @@ static int
|
||||||
apply_rule(Rule *r, char *s)
|
apply_rule(Rule *r, char *s)
|
||||||
{
|
{
|
||||||
unsigned int i, rid;
|
unsigned int i, rid;
|
||||||
char c, *a = r->a, *b = r->b, *origin = dst_, *reg, *regs[8];
|
char c, *a = r->a, *b = r->b, *origin = dst_, *reg;
|
||||||
/* phase: clean regs */
|
/* phase: clean regs */
|
||||||
for(i = 0; i < r->ptr; i++)
|
if(rmax) {
|
||||||
regs[i] = NULL;
|
for(i = 0; i <= rmax; i++)
|
||||||
|
regs[i] = 0;
|
||||||
|
rmin = 0xff, rmax = 0x00;
|
||||||
|
}
|
||||||
/* phase: match rule */
|
/* phase: match rule */
|
||||||
while((c = *a++)) {
|
while((c = *a++)) {
|
||||||
if(c == '?') {
|
if(c == '?') {
|
||||||
char *pcap = walk(s);
|
char *pcap = walk(s);
|
||||||
rid = *a++ - '0';
|
rid = *a++;
|
||||||
if((reg = regs[rid])) { /* 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[rid] = s;
|
regs[rid] = s;
|
||||||
|
if(rid < rmin) rmin = rid;
|
||||||
|
if(rid > rmax) rmax = rid;
|
||||||
|
}
|
||||||
s = pcap;
|
s = pcap;
|
||||||
} else if(c != *s++)
|
} else if(c != *s++)
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -127,9 +135,9 @@ apply_rule(Rule *r, char *s)
|
||||||
/* phase: write rule */
|
/* phase: write rule */
|
||||||
while((c = *b++)) {
|
while((c = *b++)) {
|
||||||
if(c == '?') {
|
if(c == '?') {
|
||||||
rid = *b - '0';
|
rid = *b;
|
||||||
if(rid < 9 && (reg = regs[rid]))
|
if((reg = regs[rid]))
|
||||||
b++, write_reg(r->reg[rid], reg);
|
b++, write_reg(rid, reg);
|
||||||
else
|
else
|
||||||
*dst_++ = c;
|
*dst_++ = c;
|
||||||
} else
|
} else
|
||||||
|
@ -143,21 +151,12 @@ apply_rule(Rule *r, char *s)
|
||||||
return write_tail(s);
|
return write_tail(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
|
||||||
find_reg(Rule *r, char reg)
|
|
||||||
{
|
|
||||||
int rid;
|
|
||||||
for(rid = 0; rid < (int)r->ptr; rid++)
|
|
||||||
if(r->reg[rid] == reg) return rid;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
compile_rule(Rule *r, int id, char *src)
|
compile_rule(Rule *r, int id, char *src)
|
||||||
{
|
{
|
||||||
char c, *cap;
|
char c, *cap;
|
||||||
int wrapped, reg;
|
int wrapped;
|
||||||
r->id = id, r->ptr = 0, r->a = &empty, r->b = ∅
|
r->id = id, r->a = &empty, r->b = ∅
|
||||||
/* phase: compile left */
|
/* phase: compile left */
|
||||||
while((c = *src) && c == ' ') src++;
|
while((c = *src) && c == ' ') src++;
|
||||||
if(c == ')' || (c == '<' && src[1] == '>')) return src;
|
if(c == ')' || (c == '<' && src[1] == '>')) return src;
|
||||||
|
@ -165,12 +164,6 @@ compile_rule(Rule *r, int id, char *src)
|
||||||
if(wrapped) src++, cap--;
|
if(wrapped) src++, cap--;
|
||||||
while(src < cap) {
|
while(src < cap) {
|
||||||
c = *src, *dict_++ = *src++;
|
c = *src, *dict_++ = *src++;
|
||||||
if(c == '?') {
|
|
||||||
c = *src++, reg = find_reg(r, c);
|
|
||||||
if(reg == -1 && c != '(')
|
|
||||||
r->reg[r->ptr] = c, reg = r->ptr++;
|
|
||||||
*dict_++ = '0' + reg;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
src += wrapped, *dict_++ = 0;
|
src += wrapped, *dict_++ = 0;
|
||||||
/* phase: compile right */
|
/* phase: compile right */
|
||||||
|
@ -180,10 +173,6 @@ compile_rule(Rule *r, int id, char *src)
|
||||||
if(wrapped) src++, cap--;
|
if(wrapped) src++, cap--;
|
||||||
while(src < cap) {
|
while(src < cap) {
|
||||||
c = *src, *dict_++ = *src++;
|
c = *src, *dict_++ = *src++;
|
||||||
if(c == '?') {
|
|
||||||
c = *src++, reg = find_reg(r, c);
|
|
||||||
*dict_++ = reg != -1 ? '0' + reg : c;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
src += wrapped, *dict_++ = 0;
|
src += wrapped, *dict_++ = 0;
|
||||||
return src;
|
return src;
|
||||||
|
@ -204,20 +193,6 @@ find_rule(char *s, char *cap)
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
|
||||||
echo_rule(Rule *r, char *s)
|
|
||||||
{
|
|
||||||
char c;
|
|
||||||
putc('(', stdout);
|
|
||||||
while((c = *s++)) {
|
|
||||||
unsigned int rid;
|
|
||||||
putc(c, stdout);
|
|
||||||
if(c == '?')
|
|
||||||
c = *s++, rid = c - '0', putc(rid < 9 ? r->reg[rid] : c, stdout);
|
|
||||||
}
|
|
||||||
putc(')', stdout), putc(' ', stdout);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
rewrite(void)
|
rewrite(void)
|
||||||
{
|
{
|
||||||
|
@ -232,7 +207,7 @@ rewrite(void)
|
||||||
s = compile_rule(r, rules_ - rules - 1, s + 2);
|
s = compile_rule(r, rules_ - rules - 1, s + 2);
|
||||||
if(*r->a) {
|
if(*r->a) {
|
||||||
if(!quiet && r->a)
|
if(!quiet && r->a)
|
||||||
printf("<> "), echo_rule(r, r->a), echo_rule(r, r->b), putc('\n', stdout);
|
printf("<> (%s) (%s)\n", r->a, r->b);
|
||||||
while(*s == ' ') s++;
|
while(*s == ' ') s++;
|
||||||
rules_++;
|
rules_++;
|
||||||
}
|
}
|
||||||
|
@ -244,7 +219,7 @@ rewrite(void)
|
||||||
while(*s == ' ') s++;
|
while(*s == ' ') s++;
|
||||||
cap = walk(s), r = find_rule(s, cap);
|
cap = walk(s), r = find_rule(s, cap);
|
||||||
if(!quiet && r->a)
|
if(!quiet && r->a)
|
||||||
printf(">< "), echo_rule(r, r->a), echo_rule(r, r->b), putc('\n', stdout);
|
printf(">< (%s) (%s)\n", r->a, r->b);
|
||||||
r->a = 0;
|
r->a = 0;
|
||||||
while(*cap == ' ') cap++;
|
while(*cap == ' ') cap++;
|
||||||
return write_tail(cap);
|
return write_tail(cap);
|
||||||
|
|
Loading…
Reference in New Issue