diff --git a/examples/hello.modal b/examples/hello.modal index 1e92471..2b3e220 100644 --- a/examples/hello.modal +++ b/examples/hello.modal @@ -1,4 +1,5 @@ <> (?x dup) (?x ?x) <> (?x ?y swap) (?y ?x) +<> ( ?x pop) () -(A B swap) dup \ No newline at end of file +(1 2 3) (4 5 6) swap pop dup diff --git a/examples/test.modal b/examples/test.modal index 2b3e220..6f157a8 100644 --- a/examples/test.modal +++ b/examples/test.modal @@ -1,5 +1,4 @@ -<> (?x dup) (?x ?x) -<> (?x ?y swap) (?y ?x) -<> ( ?x pop) () +<> (eq ?x ?x) (#t) +<> (eq ?x ?y) (#f) -(1 2 3) (4 5 6) swap pop dup +(eq fox fox) (eq fox owl) \ No newline at end of file diff --git a/makefile b/makefile index 02ffa3b..1216357 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 +.PHONY: all debug dest run test install uninstall format clean archive all: dest bin/modal @@ -13,13 +13,15 @@ test: all @ bin/modal -v @ bin/modal examples/test.modal install: all - @ cp bin/modal ~/bin/ + cp bin/modal ~/bin/ uninstall: - @ rm -f ~/bin/modal + rm -f ~/bin/modal format: @ clang-format -i src/modal.c clean: - @ rm -fr bin + rm -fr bin +archive: all + cp src/modal.c ../oscean/etc/modal.c.txt bin/modal: src/modal.c @ cc ${DEBUG_flags} ${CFLAGS} src/modal.c -o bin/modal diff --git a/src/modal.c b/src/modal.c index 2e097cf..ae9fd75 100644 --- a/src/modal.c +++ b/src/modal.c @@ -30,12 +30,33 @@ walk(char *s) return s; } +static int +compare(char *a, char *b) +{ + int i = 0, al = walk(a) - a, bl = walk(b) - b; + if(al != bl) return 0; + while(a[i] == b[i]) + if(!a[i] || ++i >= al) return 1; + return 0; +} + static char * match(char *p, Rule *r) { + int i; char c, *a = r->a, *b = p; + for(i = 0x21; i < 0x7f; i++) + regs[i] = 0; while((c = *a)) { - if(c == '?') regs[(int)*(++a)] = b, a++, b = walk(b), c = *b; + if(c == '?') { + int id = (int)*(++a); + if(regs[id]) { + if(!compare(regs[id], b)) + return NULL; + } else + regs[id] = b; + a++, b = walk(b), c = *b; + } if(!a[0]) return b; if(c != *b) return NULL; a++, b++; @@ -68,7 +89,7 @@ save(int rule) /* todo: change pointer instead of copying memory */ for(i = 0; i <= end; i++) prog[i] = outp[i]; prog_ = prog, outp_ = outp; - printf("..%02d %s\n", rule, prog); + printf("%02d %s\n", rule, prog); } static int @@ -107,7 +128,7 @@ print_rules(void) { int i; for(i = 0; i < rules_len; i++) - printf("Rule #%d: <%s> -> <%s>\n", i, rules[i].a, rules[i].b); + printf("<> (%s) (%s)\n", rules[i].a, rules[i].b); printf("\n"); } @@ -180,10 +201,10 @@ 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 - Modal Interpreter, 3 Apr 2024.\n"); + return !printf("Modal - Modal Interpreter, 4 Apr 2024.\n"); parse(argv[1]); print_rules(); - printf(".... %s\n", prog); + printf(".. %s\n", prog); while(rewrite()) ; return 0;