From 3c5e3de38f7b54af6cc30ce1908c12d7fb988e50 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Wed, 10 Apr 2024 12:19:44 -0700 Subject: [PATCH] Initial lambda support --- makefile | 6 +++--- src/modal.c | 39 +++++++++++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/makefile b/makefile index 297bc15..7b6e7ed 100644 --- a/makefile +++ b/makefile @@ -10,9 +10,9 @@ dest: run: all bin/modal @ bin/modal examples/hello.modal 2> /dev/null test: bin/modal-debug bin/modal - @ bin/modal -v - @ bin/modal-debug examples/test.modal "(arg1) (arg2 (arg3))" - @ time bin/modal examples/test.modal "(arg1) (arg2 (arg3))" +# @ bin/modal -v +# @ bin/modal-debug examples/test.modal "(arg1) (arg2 (arg3))" + @ bin/modal examples/test.modal install: bin/modal cp bin/modal ~/bin/ uninstall: diff --git a/src/modal.c b/src/modal.c index 71276c2..1d58ba4 100644 --- a/src/modal.c +++ b/src/modal.c @@ -149,15 +149,38 @@ rewrite(void) char c, last = 0, *p = direction ? bank_b : bank_a; while((c = *p) && c <= ' ') p++; while((c = *p)) { - if(p[0] == '<' && p[1] == '>') { - Rule *r = rules_++; - r->id = rules_ - rules - 1, p += 2; - r->a = dict_, p = parse_frag(p); - r->b = dict_, p = parse_frag(p); - return commit_rule(r, p, 1); - } if(spacer(last)) { - Rule *r; + Rule *r = NULL, lambda; + 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); + return commit_rule(r, p, 1); + } + if(p[0] == '?' && p[1] == '(') { + char *res; + p += 2; + lambda.a = dict_, p = parse_frag(p); + lambda.b = dict_, p = parse_frag(p); + p++; + while((c = *p) && c <= ' ') p++; + r = λ + res = match_rule(p, r); + if(res != NULL) { + char cc, *b = r->b; + printf("LAMBDA [%s][%s] > %s\n", r->a, r->b, p); + if(!*b && last == ' ') outp_--; + while((cc = *b++)) { + if(cc == '?') + put_reg(*b++); + else + *outp_++ = cc; + } + return commit_rule(r, res, 0); + } + } for(r = rules; r < rules_; r++) { char *res = match_rule(p, r); if(res != NULL) {