Unwrap rules on reading

This commit is contained in:
Devine Lu Linvega 2024-04-05 09:12:49 -07:00
parent 0557b06faf
commit 373137a054
2 changed files with 13 additions and 9 deletions

View File

@ -1,5 +1,4 @@
<> (?x dup) (?x ?x)
<> (?x ?y swap) (?y ?x)
((A B swap) dup)
A B dup

View File

@ -107,7 +107,7 @@ print_rules(void)
{
int i;
for(i = 0; i < rules_len; i++)
printf("Rule #%d: %s -> %s\n", i, rules[i].a, rules[i].b);
printf("Rule #%d: <%s> -> <%s>\n", i, rules[i].a, rules[i].b);
printf("\n");
}
@ -118,13 +118,18 @@ parse_rulefrag(char *line)
char c, *s = line, *res = dict_;
if(s[0] == '(') {
while((c = *s++)) {
if(c == '(') depth++;
*dict_++ = c;
if(c == ')') --depth;
if(!depth) {
*dict_++ = 0;
return res;
if(c == '(') {
depth++;
if(depth == 1) continue;
}
if(c == ')') {
--depth;
if(!depth) {
*dict_++ = 0;
return res;
}
}
*dict_++ = c;
}
}
while(!spacer(s[0]) && (*dict_++ = *s++))