Starting rewrite

This commit is contained in:
Devine Lu Linvega 2024-04-04 11:54:31 -07:00
parent d80725f4c8
commit 921cb53e2a
2 changed files with 12 additions and 11 deletions

View File

@ -6,8 +6,9 @@ typedef struct {
static int rules_len; static int rules_len;
static Rule rules[0x100]; static Rule rules[0x100];
static char dict[0x8000], *next = dict; static char dict[0x8000], *dict_ = dict;
static char prog[0x1000], *prog_ = prog; static char prog[0x1000], *prog_ = prog;
static char outp[0x1000], *outp_ = outp;
static char *regs[0x100]; static char *regs[0x100];
static char * static char *
@ -38,7 +39,7 @@ match(char *p, Rule *r)
} }
static int static int
reduce(void) rewrite(void)
{ {
char c, *p = prog; char c, *p = prog;
while((c = *p)) { while((c = *p)) {
@ -47,6 +48,7 @@ reduce(void)
Rule *r = &rules[i]; Rule *r = &rules[i];
match(p, r); match(p, r);
} }
*outp_++ = c;
p++; p++;
} }
return 1; return 1;
@ -60,23 +62,23 @@ display(void)
Rule *r = &rules[i]; Rule *r = &rules[i];
printf("Rule #%d: %s -> %s\n", i, r->a, r->b); printf("Rule #%d: %s -> %s\n", i, r->a, r->b);
} }
printf("Program: %s\n", prog); printf("\nEval: %s -- %s\n", prog, outp);
} }
static char * static char *
parse_rulefrag(FILE *f) parse_rulefrag(FILE *f)
{ {
int depth = 0; int depth = 0;
char c, *origin = next; char c, *origin = dict_;
while(f && fread(&c, 1, 1, f) && c && c != 0xa) { while(f && fread(&c, 1, 1, f) && c && c != 0xa) {
if(c == '(') depth++; if(c == '(') depth++;
if(c == ')') --depth; if(c == ')') --depth;
if(c == ' ' && !depth) if(c == ' ' && !depth)
break; break;
else else
*next++ = c; *dict_++ = c;
} }
*next++ = 0; *dict_++ = 0;
return origin; return origin;
} }
@ -122,6 +124,7 @@ main(int argc, char **argv)
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, 3 Apr 2024.\n"); return !printf("Modal - Modal Interpreter, 3 Apr 2024.\n");
parse(argv[1]); parse(argv[1]);
rewrite();
display(); display();
return !reduce(); return 0;
} }

View File

@ -1,5 +1,3 @@
<> (?x world) (?x whurl) <> bar baz
<> (?b bar) (?b baz)
(hello world) foo bar
((foo baz) bar)