Added implode

This commit is contained in:
Devine Lu Linvega 2024-04-07 09:13:35 -07:00
parent 2831040581
commit 135204131a
2 changed files with 11 additions and 6 deletions

View File

@ -1,4 +1,4 @@
<> (plode ?*) (?*) <> (plode (String ?*)) (String ?*)
(plode hello) (plode (plode (String hello)))

View File

@ -79,14 +79,19 @@ match(char *p, Rule *r)
static char * static char *
plode(char *s) plode(char *s)
{ {
int i, depth = 0;
char c; char c;
if(s[0] == '(') { /* implode */ if(s[0] == '(') { /* implode */
while((c = *s++)) {
if(c == '(') depth++;
if(!spacer(c)) *outp_++ = c;
if(c == ')') --depth;
if(!depth) return s;
}
} else { /* explode */ } else { /* explode */
int i, j = 0;
while((c = *s++) && !spacer(c)) while((c = *s++) && !spacer(c))
*outp_++ = '(', *outp_++ = c, j++; *outp_++ = '(', *outp_++ = c, depth++;
for(i = 0; i < j; i++) for(i = 0; i < depth; i++)
*outp_++ = ')'; *outp_++ = ')';
} }
return s; return s;