From 44d5893139a096b74bb75b4311f19cfc98b76afb Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Sat, 6 Apr 2024 15:48:17 -0700 Subject: [PATCH] Added I/O --- examples/console_write.modal | 3 +++ examples/test.modal | 4 +++- makefile | 4 ++-- src/modal.c | 12 +++++++----- 4 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 examples/console_write.modal diff --git a/examples/console_write.modal b/examples/console_write.modal new file mode 100644 index 0000000..4035038 --- /dev/null +++ b/examples/console_write.modal @@ -0,0 +1,3 @@ +<> ((send ?:)) () + +(send (hello world)) \ No newline at end of file diff --git a/examples/test.modal b/examples/test.modal index 4035038..0821fd9 100644 --- a/examples/test.modal +++ b/examples/test.modal @@ -1,3 +1,5 @@ +<> ((read)) (?:) <> ((send ?:)) () +<> (?x ?y catswap) ((?y ?x)) -(send (hello world)) \ No newline at end of file +(send (You said: )) (read) send catswap \ No newline at end of file diff --git a/makefile b/makefile index f7c6007..af2582e 100644 --- a/makefile +++ b/makefile @@ -8,10 +8,10 @@ all: dest bin/modal dest: @ mkdir -p bin run: all - @ bin/modal examples/test.modal 2> /dev/null + @ bin/modal examples/test.modal "(arg1)" "(arg2)" "(arg3)" test: all @ bin/modal -v - @ bin/modal examples/test.modal + @ bin/modal examples/test.modal 2> /dev/null install: all cp bin/modal ~/bin/ uninstall: diff --git a/src/modal.c b/src/modal.c index ea9a5b5..51c749e 100644 --- a/src/modal.c +++ b/src/modal.c @@ -4,7 +4,7 @@ typedef struct { char *a, *b; } Rule; -static int rules_len, direction = 0; +static int rules_len, direction, queue; static Rule rules[0x100]; static char dict[0x8000], *dict_ = dict; static char bank_a[0x4000], *prog_ = bank_a; @@ -77,10 +77,11 @@ match(char *p, Rule *r) } static int -bind(char r) +bind(char r, char *incoming) { int depth = 0; char c, *s = regs[(int)r]; + if(r == ':' && incoming != NULL) s = incoming, queue++; if(!s) return !fprintf(stderr, "?%c Empty\n", r); if(s[0] == '(') { while((c = *s++)) { @@ -147,7 +148,7 @@ addrule(char *s) } static int -rewrite(void) +rewrite(char *incoming) { char c, *p = direction ? bank_b : bank_a; while((c = *p)) { @@ -167,7 +168,7 @@ rewrite(void) char cc, *b = r->b; while((cc = *b++)) { if(cc == '?') - bind(*b++); + bind(*b++, incoming); else *outp_++ = cc; } @@ -206,8 +207,9 @@ main(int argc, char **argv) return !printf("Modal - Modal Interpreter, 4 Apr 2024.\n"); if(!(f = fopen(argv[1], "r"))) return !printf("Invalid Modal file: %s.\n", argv[1]); + queue = 2; fread(bank_a, 1, 0x1000, f), fclose(f); - while(rewrite()) + while(rewrite(argv[queue])) ; print_rules(); return 0;