From bb72d27e2de3e0c133a301fe0dd09c104193414f Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Sat, 6 Apr 2024 09:10:19 -0700 Subject: [PATCH] Only try-match during spacers --- examples/arithmetic.modal | 22 +++++++++++----------- examples/test.modal | 7 +++---- src/modal.c | 34 ++++++++++++++++++---------------- 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/examples/arithmetic.modal b/examples/arithmetic.modal index 776bc74..c73796b 100644 --- a/examples/arithmetic.modal +++ b/examples/arithmetic.modal @@ -3,20 +3,20 @@ <> (add (0) (s ?y)) (s ?y) <> (add (0) (0)) (0) -<> (subtract (s ?x) (s ?y)) (subtract ?x ?y) -<> (subtract (s ?x) (0)) (s ?x) -<> (subtract (0) (s ?y)) (s ?y) -<> (subtract (0) (0)) (0) +<> (sub (s ?x) (s ?y)) (sub ?x ?y) +<> (sub (s ?x) (0)) (s ?x) +<> (sub (0) (s ?y)) (s ?y) +<> (sub (0) (0)) (0) -<> (multiply (s ?x) (s ?y)) (add (s ?x) (multiply (s ?x) (subtract (s ?y) (s (0))))) -<> (multiply (s ?x) (s (0)) (s ?x) -<> (multiply (s (0)) (s ?y) (s ?y) -<> (multiply (s ?x) (0)) (0) -<> (multiply (0) (s ?x)) (0) +<> (mul (s ?x) (s ?y)) (add (s ?x) (mul (s ?x) (sub (s ?y) (s (0))))) +<> (mul (s ?x) (s (0)) (s ?x) +<> (mul (s (0)) (s ?y) (s ?y) +<> (mul (s ?x) (0)) (0) +<> (mul (0) (s ?x)) (0) <> (?x + ?y) (add ?x ?y) -<> (?x - ?y) (subtract ?x ?y) -<> (?x * ?y) (multiply ?x ?y) +<> (?x - ?y) (sub ?x ?y) +<> (?x * ?y) (mul ?x ?y) <> (factorial (s (0))) ((s (0))) <> (factorial (s ?x)) (((s ?x) * factorial ((s ?x) - (s (0))))) diff --git a/examples/test.modal b/examples/test.modal index 2b3e220..a34e4aa 100644 --- a/examples/test.modal +++ b/examples/test.modal @@ -1,5 +1,4 @@ -<> (?x dup) (?x ?x) -<> (?x ?y swap) (?y ?x) -<> ( ?x pop) () +<> ([b]) () +<> (b) ([b]) -(1 2 3) (4 5 6) swap pop dup +(abc) \ No newline at end of file diff --git a/src/modal.c b/src/modal.c index e04ae04..a01e71e 100644 --- a/src/modal.c +++ b/src/modal.c @@ -59,7 +59,7 @@ match(char *p, Rule *r) regs[id] = b; a++, b = walk(b), c = *b; } - if(!a[0]) return b; + if(!a[0] && spacer(*(b + 1))) return b; if(c != *b) return NULL; a++, b++; } @@ -143,22 +143,24 @@ rewrite(void) int i; if(p[0] == '<' && p[1] == '>') p = addrule(p) + 1, c = *p; - for(i = 0; i < rules_len; i++) { - Rule *r = &rules[i]; - char *res = match(p, r); - if(res != NULL) { - char cc, *b = r->b; - while((cc = *b++)) { - if(cc == '?') - bind(*b++); - else - *outp_++ = cc; + if(p == bank_a || p == bank_b || spacer(*(p - 1))) { + for(i = 0; i < rules_len; i++) { + Rule *r = &rules[i]; + char *res = match(p, r); + if(res != NULL) { + char cc, *b = r->b; + while((cc = *b++)) { + if(cc == '?') + bind(*b++); + else + *outp_++ = cc; + } + while((*outp_++ = *res++)) + ; + *outp_++ = 0; + save(i); + return 1; } - while((*outp_++ = *res++)) - ; - *outp_++ = 0; - save(i); - return 1; } } *outp_++ = c;