Return lambda to its own memory
This commit is contained in:
parent
7a27c2ca5b
commit
9c95b4567b
42
src/modal.c
42
src/modal.c
|
@ -6,7 +6,7 @@ typedef struct {
|
|||
} Rule;
|
||||
|
||||
static int flip, quiet, rmin = 0xff, rmax = 0x00, cycles = 0x10000;
|
||||
static Rule rules[0x1000], *rules_ = rules + 1;
|
||||
static Rule rules[0x1000], lambda, *rules_ = rules;
|
||||
static char dict[0x8000], *dict_ = dict;
|
||||
static char bank_a[0x4000], *src_ = bank_a;
|
||||
static char bank_b[0x4000], *dst_ = bank_b;
|
||||
|
@ -99,47 +99,46 @@ write_rule(Rule *r, char *s, int create)
|
|||
static int
|
||||
run_rule(Rule *r, char *s)
|
||||
{
|
||||
int i;
|
||||
char c, *a = r->a, *p = s;
|
||||
if(rmax) {
|
||||
char c, *a = r->a;
|
||||
if(rmax) { /* clean registers */
|
||||
int i;
|
||||
for(i = rmin; i <= rmax; i++)
|
||||
regs[i] = 0;
|
||||
rmin = 0xff, rmax = 0x00;
|
||||
}
|
||||
while((c = *a)) {
|
||||
while((c = *a)) { /* find match */
|
||||
if(c == '?') {
|
||||
int regid = (int)*(++a);
|
||||
char *pcap = walk(p), *reg = regs[regid];
|
||||
char *pcap = walk(s), *reg = regs[regid];
|
||||
if(reg) { /* reg cmp */
|
||||
char *rcap = walk(reg), *pp = p;
|
||||
char *rcap = walk(reg), *pp = s;
|
||||
while(reg < rcap || pp < pcap)
|
||||
if(*reg++ != *pp++) return 0;
|
||||
} else { /* reg set */
|
||||
regs[regid] = p;
|
||||
regs[regid] = s;
|
||||
if(regid < rmin) rmin = regid;
|
||||
if(regid > rmax) rmax = regid;
|
||||
}
|
||||
a++, p = pcap;
|
||||
a++, s = pcap;
|
||||
if(!spacer(*a))
|
||||
while((c = *a) && !spacer(c)) a++;
|
||||
continue;
|
||||
}
|
||||
if(c != *p) return 0;
|
||||
a++, p++;
|
||||
if(c != *s) return 0;
|
||||
a++, s++;
|
||||
}
|
||||
c = *p;
|
||||
if(spacer(c)) {
|
||||
if((c = *s) && spacer(c)) { /* write match */
|
||||
char *b = r->b, *reg, *origin = dst_;
|
||||
while((c = *b++))
|
||||
if(c == '?' && (reg = regs[(int)*b]))
|
||||
write_reg(*b++, reg);
|
||||
else
|
||||
*dst_++ = c;
|
||||
if(dst_ == origin) {
|
||||
while(*p == ' ') p++;
|
||||
if(*p == ')' && *(dst_ - 1) == ' ') dst_--;
|
||||
if(dst_ == origin) { /* trim */
|
||||
while(*s == ' ') s++;
|
||||
if(*s == ')' && *(dst_ - 1) == ' ') dst_--;
|
||||
}
|
||||
return write_rule(r, p, 0);
|
||||
return write_rule(r, s, 0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -179,14 +178,13 @@ rewrite(void)
|
|||
}
|
||||
if(c == '?' && s[1] == '(') { /* lambda */
|
||||
cap = walk(s + 1);
|
||||
r = &rules[0], r->id = -1;
|
||||
r = &lambda, r->id = -1;
|
||||
r->a = dict_, s = parse_frag(s + 2);
|
||||
r->b = dict_, parse_frag(s), s = cap;
|
||||
while(*s == ' ') s++;
|
||||
r = rules;
|
||||
} else
|
||||
r = rules + 1;
|
||||
for(; r < rules_; r++)
|
||||
if(run_rule(r, s)) return 1;
|
||||
}
|
||||
for(r = rules; r < rules_; r++)
|
||||
if(run_rule(r, s)) return 1;
|
||||
}
|
||||
*dst_++ = last = c;
|
||||
|
|
Loading…
Reference in New Issue