Store rule id in struct

This commit is contained in:
Devine Lu Linvega 2024-04-08 11:46:24 -07:00
parent b0663fc236
commit 418bf81c5a
1 changed files with 11 additions and 12 deletions

View File

@ -1,10 +1,11 @@
#include <stdio.h> #include <stdio.h>
typedef struct { typedef struct {
int id;
char *a, *b; char *a, *b;
} Rule; } Rule;
static int rules_len, direction; static int direction;
static Rule rules[0x1000], *rules_ = rules; static Rule rules[0x1000], *rules_ = rules;
static char dict[0x8000], *dict_ = dict; static char dict[0x8000], *dict_ = dict;
static char bank_a[0x4000], *prog_ = bank_a; static char bank_a[0x4000], *prog_ = bank_a;
@ -108,7 +109,7 @@ match_rule(char *p, Rule *r)
} }
static int static int
commit_rule(int rule, char *s) commit_rule(Rule *r, char *s)
{ {
while((*outp_++ = *s++)) while((*outp_++ = *s++))
; ;
@ -117,8 +118,8 @@ commit_rule(int rule, char *s)
prog_ = bank_b, outp_ = bank_a; prog_ = bank_b, outp_ = bank_a;
else else
prog_ = bank_a, outp_ = bank_b; prog_ = bank_a, outp_ = bank_b;
if(rule >= 0) if(r != NULL)
fprintf(stderr, "%02d %s\n", rule, prog_); fprintf(stderr, "%02d %s\n", r->id, prog_);
return 1; return 1;
} }
@ -138,13 +139,12 @@ add_rule(char *p)
{ {
char c; char c;
Rule *r = rules_++; Rule *r = rules_++;
rules_len++;
p += 2; p += 2;
while((c = *p) && c <= ' ') p++; while((c = *p) && c <= ' ') p++;
r->a = parse_rule(p), p = walk(p); r->a = parse_rule(p), p = walk(p);
while((c = *p) && c <= ' ') p++; while((c = *p) && c <= ' ') p++;
r->b = parse_rule(p), p = walk(p); r->b = parse_rule(p), p = walk(p);
return commit_rule(-1, p); return commit_rule(NULL, p);
} }
static int static int
@ -156,9 +156,8 @@ rewrite(void)
if(p[0] == '<' && p[1] == '>') if(p[0] == '<' && p[1] == '>')
return add_rule(p); return add_rule(p);
if(spacer(last)) { if(spacer(last)) {
int i; Rule *r;
for(i = 0; i < rules_len; i++) { for(r = rules; r < rules_; r++) {
Rule *r = &rules[i];
char *res = match_rule(p, r); char *res = match_rule(p, r);
if(res != NULL) { if(res != NULL) {
char cc, *b = r->b; char cc, *b = r->b;
@ -169,7 +168,7 @@ rewrite(void)
else else
*outp_++ = cc; *outp_++ = cc;
} }
return commit_rule(i, res); return commit_rule(r, res);
} }
} }
} }
@ -183,9 +182,9 @@ rewrite(void)
static void static void
print_rules(void) print_rules(void)
{ {
Rule *r = rules; Rule *r;
fprintf(stderr, "\n"); fprintf(stderr, "\n");
while(r++ < rules_) for(r = rules; r < rules_; r++)
fprintf(stderr, "<> (%s) (%s)\n", r->a, r->b); fprintf(stderr, "<> (%s) (%s)\n", r->a, r->b);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }