Housekeeping

This commit is contained in:
Devine Lu Linvega 2024-04-06 08:17:58 -07:00
parent 2322f51749
commit 0d9cd63829
1 changed files with 59 additions and 33 deletions

View File

@ -66,15 +66,13 @@ match(char *p, Rule *r)
return b; return b;
} }
static void static int
bind(char r) bind(char r)
{ {
int depth = 0; int depth = 0;
char c, *s = regs[(int)r]; char c, *s = regs[(int)r];
if(!s) { if(!s)
printf("!! Reading from invalid register: ?%c\n", r); return !printf("!! Reading from invalid register: ?%c\n", r);
return;
}
if(s[0] == '(') { if(s[0] == '(') {
while((c = *s++)) { while((c = *s++)) {
if(c == '(') depth++; if(c == '(') depth++;
@ -85,7 +83,7 @@ bind(char r)
} }
while(!spacer(s[0]) && (*outp_++ = *s++)) while(!spacer(s[0]) && (*outp_++ = *s++))
; ;
return; return 1;
} }
static void static void
@ -98,6 +96,33 @@ save(int rule)
printf("%02d %s\n", rule, prog); printf("%02d %s\n", rule, prog);
} }
static char *
parse_rulefrag(char *line)
{
int depth = 0;
char c, *s = line, *res = dict_;
if(s[0] == '(') {
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;
return res;
}
static char * static char *
addrule(char *s) addrule(char *s)
{ {
@ -152,44 +177,45 @@ print_rules(void)
printf("\n"); printf("\n");
} }
static char * static int
parse_rulefrag(char *line) parse_line(char *line)
{ {
int depth = 0; char c;
char c, *s = line, *res = dict_; if(line[0] == 0) return 1;
if(s[0] == '(') { if(line[0] == '<' && line[1] == '>')
while((c = *s++)) { return !!addrule(line);
if(c == '(') { while((c = *line++))
depth++; *prog_++ = c;
if(depth == 1) continue; return 1;
}
static int
parse(char *path)
{
FILE *f;
char c, line[0x400], *line_ = line;
if(!(f = fopen(path, "r")))
return 0;
while(f && fread(&c, 1, 1, f)) {
if(c == 0xa)
*line_++ = 0x00, parse_line(line), line_ = line;
else if(line_ - line < 0x400)
*line_++ = c;
} }
if(c == ')') { *line_++ = 0x00, parse_line(line), line_ = line;
--depth; fclose(f);
if(!depth) { return 1;
*dict_++ = 0;
return res;
}
}
*dict_++ = c;
}
}
while(!spacer(s[0]) && (*dict_++ = *s++))
;
*dict_++ = 0;
return res;
} }
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
FILE *f;
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, 4 Apr 2024.\n"); return !printf("Modal - Modal Interpreter, 4 Apr 2024.\n");
if(!(f = fopen(argv[1], "r"))) if(!parse(argv[1]))
return !printf("Invalid file: %s\n", argv[1]); return !printf("Invalid Modal file: %s.\n", argv[1]);
fread(&prog, 1, 0x1000, f), fclose(f);
print_rules(); print_rules();
printf(".. %s\n", prog); printf(".. %s\n", prog);
while(rewrite()) while(rewrite())