From 5f70f09ce3455cb8a27178420aa10908baa58124 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Thu, 11 Apr 2024 08:53:49 -0700 Subject: [PATCH] Abstracted create_rule --- src/modal.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/modal.c b/src/modal.c index d566e11..758242f 100644 --- a/src/modal.c +++ b/src/modal.c @@ -6,7 +6,7 @@ typedef struct { } Rule; static int direction; -static Rule rules[0x1000], *rules_ = rules; +static Rule rules[0x1000], lambda, *rules_ = rules; static char dict[0x8000], *dict_ = dict; static char bank_a[0x4000], *prog_ = bank_a; static char bank_b[0x4000], *outp_ = bank_b; @@ -155,6 +155,16 @@ write(Rule *r, char last, char *res) return commit_rule(r, res, 0); } +static char * +create_rule(Rule *r, int id, char *s) +{ + char c; + r->id = id, s += 2; + r->a = dict_, s = parse_frag(s), r->b = dict_, s = parse_frag(s); + while((c = *s) && c <= ' ') s++; + return s; +} + static int rewrite(void) { @@ -165,16 +175,12 @@ rewrite(void) Rule *r; if(p[0] == '<' && p[1] == '>') { r = rules_++; - r->id = rules_ - rules - 1; - p += 2, r->a = dict_, p = parse_frag(p), r->b = dict_, p = parse_frag(p); + p = create_rule(r, rules_ - rules - 1, p); return commit_rule(r, p, 1); } if(p[0] == '?' && p[1] == '(') { - Rule lambda; - lambda.id = -1; - p += 2, lambda.a = dict_, p = parse_frag(p), lambda.b = dict_, p = parse_frag(p); - p++; - while((c = *p) && c <= ' ') p++; + r = λ + p = create_rule(&lambda, -1, p) + 1; if((res = match_rule(&lambda, p)) != NULL) return write(&lambda, last, res); }