From 2250f9231e172907f57a9d531e3c22b43f2e2e02 Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Sun, 7 Apr 2024 10:56:43 -0700 Subject: [PATCH] Compress program on load --- examples/test.modal | 7 ++++--- src/modal.c | 25 ++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/examples/test.modal b/examples/test.modal index 01a9a30..2b3e220 100644 --- a/examples/test.modal +++ b/examples/test.modal @@ -1,4 +1,5 @@ -<> (plode (String ?*)) (String ?*) - -(plode (plode (String hello))) +<> (?x dup) (?x ?x) +<> (?x ?y swap) (?y ?x) +<> ( ?x pop) () +(1 2 3) (4 5 6) swap pop dup diff --git a/src/modal.c b/src/modal.c index 7e2a69e..3812bdd 100644 --- a/src/modal.c +++ b/src/modal.c @@ -213,18 +213,37 @@ print_rules(void) fprintf(stderr, "\n"); } +static int +load(char *path) +{ + char c, *w = bank_a; + FILE *f = fopen(path, "r"); + if(!f) return 0; + while(fread(&c, 1, 1, f)) { + if(prog_ > bank_a) { + if(c == ')' && *(w - 1) == ' ') w--; + if(c == '(' && *(w - 1) == ' ') w--; + if(c == ' ' && *(w - 1) == ' ') w--; + if(c == ' ' && *(w - 1) == '(') continue; + if(c == ' ' && *(w - 1) == ')') continue; + } + *w++ = c; + } + *w++ = 0; + fclose(f); + return 1; +} + int main(int argc, char **argv) { - FILE *f; if(argc < 2) return !printf("usage: modal [-v] source.modal\n"); if(argc < 3 && argv[1][0] == '-' && argv[1][1] == 'v') return !printf("Modal - Modal Interpreter, 4 Apr 2024.\n"); - if(!(f = fopen(argv[1], "r"))) + if(!load(argv[1])) return !printf("Invalid Modal file: %s.\n", argv[1]); queue = 2; - fread(bank_a, 1, 0x1000, f), fclose(f); while(rewrite(argv[queue])) ; print_rules();