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