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 dup) (?x ?x)
<> (?x ?y swap) (?y ?x) <> (?x ?y swap) (?y ?x)
((A B swap) dup) A B dup

View File

@ -107,7 +107,7 @@ print_rules(void)
{ {
int i; int i;
for(i = 0; i < rules_len; 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"); printf("\n");
} }
@ -118,13 +118,18 @@ parse_rulefrag(char *line)
char c, *s = line, *res = dict_; char c, *s = line, *res = dict_;
if(s[0] == '(') { if(s[0] == '(') {
while((c = *s++)) { while((c = *s++)) {
if(c == '(') depth++; if(c == '(') {
*dict_++ = c; depth++;
if(c == ')') --depth; if(depth == 1) continue;
if(!depth) {
*dict_++ = 0;
return res;
} }
if(c == ')') {
--depth;
if(!depth) {
*dict_++ = 0;
return res;
}
}
*dict_++ = c;
} }
} }
while(!spacer(s[0]) && (*dict_++ = *s++)) while(!spacer(s[0]) && (*dict_++ = *s++))