From 9dd3439986ca97d4ce78b9b271dc9f0c0a2a8c7e Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Wed, 17 Apr 2024 08:47:39 -0700 Subject: [PATCH 1/7] Added unpack register --- src/modal.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/modal.c b/src/modal.c index 71e6569..77ea3c8 100644 --- a/src/modal.c +++ b/src/modal.c @@ -65,6 +65,10 @@ put_reg(char r) for(i = 0; i < depth; i++) *dst_++ = ')'; } + } else if(r == '.') { /* unpack */ + if(*s == '(') + s++, --ss; + while(s < ss) *dst_++ = *s++; } else if(r == ':') { /* special stdout */ if(*s == '(') s++, --ss; while(s < ss) { @@ -206,7 +210,7 @@ main(int argc, char **argv) if(argc < 2) return !printf("usage: modal [-v] source.modal\n"); if(argc < 3 && argv[1][0] == '-' && argv[1][1] == 'v') - return !printf("Modal Interpreter, 16 Apr 2024.\n"); + return !printf("Modal Interpreter, 17 Apr 2024.\n"); if(!(f = fopen(argv[1], "r"))) return !fprintf(stderr, "Invalid Modal file: %s.\n", argv[1]); while(fread(&c, 1, 1, f)) { From 3bce3dab79293769ddd4bb85f7530a2102f1be1b Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Wed, 17 Apr 2024 09:17:31 -0700 Subject: [PATCH 2/7] Added align register --- src/modal.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/modal.c b/src/modal.c index 77ea3c8..6d34bdc 100644 --- a/src/modal.c +++ b/src/modal.c @@ -65,10 +65,22 @@ put_reg(char r) for(i = 0; i < depth; i++) *dst_++ = ')'; } - } else if(r == '.') { /* unpack */ + } else if(r == '.') { /* special unpack */ if(*s == '(') s++, --ss; while(s < ss) *dst_++ = *s++; + } else if(r == '^') { /* special align */ + int i, depth = 0; + s++; + while(s < ss) { + *dst_++ = '('; + while((c = *s) && !spacer(c)) + *dst_++ = c, s++; + if(s < ss - 1) *dst_++ = ' '; + s++, depth++; + } + for(i = 0; i < depth; i++) + *dst_++ = ')'; } else if(r == ':') { /* special stdout */ if(*s == '(') s++, --ss; while(s < ss) { From 6e0d3bb9e0f6ca8d5ed57239f463a499c8799bbf Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Wed, 17 Apr 2024 10:35:40 -0700 Subject: [PATCH 3/7] New explode/join registers --- src/modal.c | 44 ++++++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/src/modal.c b/src/modal.c index 6d34bdc..8bd4db2 100644 --- a/src/modal.c +++ b/src/modal.c @@ -55,32 +55,31 @@ put_reg(char r) } ss = walk(s); if(r == '*') { - if(*s == '(') { /* special implode */ - while(s < ss && (c = *s++)) - if(!spacer(c)) *dst_++ = c; - } else { /* special explode */ - int i, depth = 0; + int i, depth = 0; + if(*s == '(') { /* special explode tuple */ + s++; + while(s < ss) { + while((c = *s) && !spacer(c)) + *dst_++ = c, s++; + *dst_++ = ' '; + *dst_++ = '(', s++, depth++; + } + } else { /* special explode token */ while((c = *s++) && !spacer(c)) *dst_++ = c, *dst_++ = ' ', *dst_++ = '(', depth++; - for(i = 0; i < depth; i++) - *dst_++ = ')'; - } - } else if(r == '.') { /* special unpack */ - if(*s == '(') - s++, --ss; - while(s < ss) *dst_++ = *s++; - } else if(r == '^') { /* special align */ - int i, depth = 0; - s++; - while(s < ss) { - *dst_++ = '('; - while((c = *s) && !spacer(c)) - *dst_++ = c, s++; - if(s < ss - 1) *dst_++ = ' '; - s++, depth++; } for(i = 0; i < depth; i++) *dst_++ = ')'; + } else if(r == '.') { /* special unpack */ + if(*s == '(') s++, --ss; + while(s < ss) *dst_++ = *s++; + } else if(r == '^') { /* special join */ + if(*s == '(') s++, --ss; + while(s < ss && (c = *s++)) + if(!spacer(c)) *dst_++ = c; + } else if(r == '~') { /* special stdin */ + while(fread(&c, 1, 1, stdin) && c >= ' ') + *dst_++ = c; } else if(r == ':') { /* special stdout */ if(*s == '(') s++, --ss; while(s < ss) { @@ -94,9 +93,6 @@ put_reg(char r) } else putc(c, stdout); } - } else if(r == '~') { /* special stdin */ - while(fread(&c, 1, 1, stdin) && c >= ' ') - *dst_++ = c; } else while(s < ss) *dst_++ = *s++; } From 44e809f092fa6d0bef7b2a0b5dd219b45c5ff1a3 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Wed, 17 Apr 2024 10:56:51 -0700 Subject: [PATCH 4/7] Updated tests to match new registers --- examples/string_join.modal | 2 +- examples/string_reverse.modal | 2 +- examples/tests.modal | 6 ++++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/examples/string_join.modal b/examples/string_join.modal index 8f4b8ea..eb3193e 100644 --- a/examples/string_join.modal +++ b/examples/string_join.modal @@ -1,7 +1,7 @@ ?(?-) (This example joins two tokens.) <> (join (?a) (?*)) (join reverse (?a) List (?*)) -<> (join List () List ?*) (?*) +<> (join List () List ?^) (?^) <> (join List (?x ?y) List ?z) (join List ?y List (?x ?z)) <> (reverse List ()) (List) diff --git a/examples/string_reverse.modal b/examples/string_reverse.modal index 4e14c75..a973a80 100644 --- a/examples/string_reverse.modal +++ b/examples/string_reverse.modal @@ -1,6 +1,6 @@ ?(?-) (This example reverses the string modal, into ladom.) -<> (reverse List () ?*) (?*) +<> (reverse List () ?^) (?^) <> (reverse (?*)) (reverse List (?*) ()) <> (reverse List (?x ?y) ?z) (reverse List ?y (?x ?z)) diff --git a/examples/tests.modal b/examples/tests.modal index 9d6d20a..4fb6d80 100644 --- a/examples/tests.modal +++ b/examples/tests.modal @@ -57,16 +57,18 @@ abc ?(?x) def = abc test <> (explode ?*) ((?*) =) explode cow (c (o (w ()))) test +explode (12 34 45) (12 (34 (45 ()))) test ?(?-) (Implode) -<> (implode ?*) (?* =) +<> (implode ?^) (?^ =) implode (b (a (t ()))) bat test +implode (12 (34 (56 ()))) 123456 test ?(?-) (List reversal) -<> (reverse List () ?*) (?*) +<> (reverse List () ?^) (?^) <> (reverse (?*)) (reverse List (?*) ()) <> (reverse List (?x ?y) ?z) (reverse List ?y (?x ?z)) From 88649fce129c9ff3d874c0d2878cd90c2f8f8c50 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Wed, 17 Apr 2024 11:18:37 -0700 Subject: [PATCH 5/7] Fixes whitespace bug --- src/modal.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/modal.c b/src/modal.c index 8bd4db2..0db8882 100644 --- a/src/modal.c +++ b/src/modal.c @@ -146,8 +146,10 @@ write_rule(Rule *r, char last, char *res) put_reg(*b++); else *dst_++ = c, last = c; - if(dst_ == origin) + if(dst_ == origin) { + if(*res == ')') dst_--; while(*res == ' ') res++; + } return commit_rule(r, res, 0); } From 7a93de35ad2ea7f6aefd2a8bb33193b0ecdfbf1c Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Wed, 17 Apr 2024 11:40:47 -0700 Subject: [PATCH 6/7] Fixes issue with trailing ws --- examples/tests.modal | 2 ++ makefile | 4 +++- src/modal.c | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/tests.modal b/examples/tests.modal index 4fb6d80..30b66ba 100644 --- a/examples/tests.modal +++ b/examples/tests.modal @@ -15,9 +15,11 @@ nap tap test <> (?x pop-plain) <> (?x pop) () +<> (ghost) () abc def pop-plain = abc test abc def pop = abc test +(ghost) = () ?(?-) (Basic replacements) diff --git a/makefile b/makefile index eaf3ae3..ab918b7 100644 --- a/makefile +++ b/makefile @@ -1,7 +1,7 @@ RELEASE_flags=-DNDEBUG -O2 -g0 -s DEBUG_flags=-std=c89 -D_POSIX_C_SOURCE=199309L -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined -.PHONY: all debug dest run test install uninstall format clean archive +.PHONY: all debug dest run debug test install uninstall format clean archive all: dest @@ -9,6 +9,8 @@ dest: @ mkdir -p bin run: all bin/modal @ bin/modal examples/hello.modal 2> /dev/null +debug: bin/modal-debug + @ bin/modal-debug examples/hello.modal test: bin/modal-debug bin/modal @ bin/modal -v @ bin/modal-debug examples/tests.modal diff --git a/src/modal.c b/src/modal.c index 0db8882..6b1cb0a 100644 --- a/src/modal.c +++ b/src/modal.c @@ -147,8 +147,8 @@ write_rule(Rule *r, char last, char *res) else *dst_++ = c, last = c; if(dst_ == origin) { - if(*res == ')') dst_--; while(*res == ' ') res++; + if(*res == ')' && *(dst_ - 1) == ' ') dst_--; } return commit_rule(r, res, 0); } From 21782ba7bacb559867e37e1e4caad6d5b7c068bd Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Wed, 17 Apr 2024 11:49:32 -0700 Subject: [PATCH 7/7] Added a test for self-erasing token --- examples/tests.modal | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/examples/tests.modal b/examples/tests.modal index 30b66ba..f8e329e 100644 --- a/examples/tests.modal +++ b/examples/tests.modal @@ -1,9 +1,5 @@ ?(?-) (This example tests various aspects of the implementation.) -<> (?: print) (?:) -<> (?x = ?x test) (#ok) -<> (?x = ?y test) (#fail) - ?(?-) (Inline rules) <> ((?x -> ?y)) (<> ?x ?y) @@ -19,7 +15,7 @@ nap tap test abc def pop-plain = abc test abc def pop = abc test -(ghost) = () +(ghost) = () test ?(?-) (Basic replacements) @@ -68,6 +64,12 @@ explode (12 34 45) (12 (34 (45 ()))) test implode (b (a (t ()))) bat test implode (12 (34 (56 ()))) 123456 test +?(?-) (Test Primitives) + +<> (?: print) (?:) +<> (?x = ?x test) (#ok) +<> (?x = ?y test) (#fail) + ?(?-) (List reversal) <> (reverse List () ?^) (?^) @@ -75,3 +77,4 @@ implode (12 (34 (56 ()))) 123456 test <> (reverse List (?x ?y) ?z) (reverse List ?y (?x ?z)) reverse (modal) = ladom test +