Use walk during parse_rule

This commit is contained in:
Devine Lu Linvega 2024-04-08 09:42:37 -07:00
parent 0dc32f04f9
commit 8e22324ce2
1 changed files with 8 additions and 24 deletions

View File

@ -122,30 +122,14 @@ save(int rule, char *s)
} }
static char * static char *
parse_rulefrag(char *line) parse_rule(char *s)
{ {
int depth = 0; char *ss = walk(s), *d = dict_;
char c, *s = line, *res = dict_; if(*s == '(') s++, ss--;
if(s[0] == '(') { while((s < ss) && (*dict_++ = *s++))
while((c = *s++)) {
if(c == '(') {
depth++;
if(depth == 1) continue;
}
if(c == ')') {
--depth;
if(!depth) {
*dict_++ = 0;
return res;
}
}
*dict_++ = c;
}
}
while(!spacer(s[0]) && (*dict_++ = *s++))
; ;
*dict_++ = 0; *dict_++ = 0;
return res; return d;
} }
static int static int
@ -158,8 +142,8 @@ rewrite(void)
if(p[0] == '<' && p[1] == '>') { if(p[0] == '<' && p[1] == '>') {
Rule *r = &rules[rules_len++]; Rule *r = &rules[rules_len++];
p += 3; p += 3;
r->a = parse_rulefrag(p), p = walk(p) + 1; r->a = parse_rule(p), p = walk(p) + 1;
r->b = parse_rulefrag(p), p = walk(p); r->b = parse_rule(p), p = walk(p);
return save(-1, p); return save(-1, p);
} }
if(p == o || spacer(*(p - 1))) { if(p == o || spacer(*(p - 1))) {
@ -204,7 +188,7 @@ main(int argc, char **argv)
if(argc < 2) if(argc < 2)
return !printf("usage: modal [-v] source.modal\n"); return !printf("usage: modal [-v] source.modal\n");
if(argc < 3 && argv[1][0] == '-' && argv[1][1] == 'v') if(argc < 3 && argv[1][0] == '-' && argv[1][1] == 'v')
return !printf("Modal - Modal Interpreter, 7 Apr 2024.\n"); return !printf("Modal - Modal Interpreter, 8 Apr 2024.\n");
if(!(f = fopen(argv[1], "r"))) if(!(f = fopen(argv[1], "r")))
return !printf("Invalid Modal file: %s.\n", argv[1]); return !printf("Invalid Modal file: %s.\n", argv[1]);
while(fread(&c, 1, 1, f)) { while(fread(&c, 1, 1, f)) {