Starting plode register

This commit is contained in:
Devine Lu Linvega 2024-04-07 09:08:58 -07:00
parent 84531bcfc3
commit 2831040581
2 changed files with 24 additions and 7 deletions

View File

@ -1,5 +1,4 @@
<> (?x dup) (?x ?x) <> (plode ?*) (?*)
<> (?x ?y swap) (?y ?x)
<> ( ?x pop) () (plode hello)
(1 2 3) (4 5 6) swap pop dup

View File

@ -76,14 +76,32 @@ match(char *p, Rule *r)
return spacer(*b) ? b : NULL; return spacer(*b) ? b : NULL;
} }
static char *
plode(char *s)
{
char c;
if(s[0] == '(') { /* implode */
} else { /* explode */
int i, j = 0;
while((c = *s++) && !spacer(c))
*outp_++ = '(', *outp_++ = c, j++;
for(i = 0; i < j; i++)
*outp_++ = ')';
}
return s;
}
static int static int
bind(char r, char *incoming) commit(char r, char *incoming)
{ {
int depth = 0; int depth = 0;
char c, *s = regs[(int)r]; char c, *s = regs[(int)r];
if(r == ':' && incoming != NULL) s = incoming, queue++; if(r == ':' && incoming != NULL) s = incoming, queue++;
if(!s) return !fprintf(stderr, "?%c Empty\n", r); if(!s) return !fprintf(stderr, "?%c Empty\n", r);
if(s[0] == '(') { if(r == '*')
s = plode(s);
else if(s[0] == '(') {
while((c = *s++)) { while((c = *s++)) {
if(c == '(') depth++; if(c == '(') depth++;
*outp_++ = c; *outp_++ = c;
@ -161,7 +179,7 @@ rewrite(char *incoming)
char cc, *b = r->b; char cc, *b = r->b;
while((cc = *b++)) { while((cc = *b++)) {
if(cc == '?') if(cc == '?')
bind(*b++, incoming); commit(*b++, incoming);
else else
*outp_++ = cc; *outp_++ = cc;
} }