Initial lambda support

This commit is contained in:
Devine Lu Linvega 2024-04-10 12:19:44 -07:00
parent d255500447
commit 3c5e3de38f
2 changed files with 34 additions and 11 deletions

View File

@ -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:

View File

@ -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 = &lambda;
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) {