Register reassignment

This commit is contained in:
Devine Lu Linvega 2024-04-05 10:34:46 -07:00
parent 976749c292
commit 7147e8e5ad
4 changed files with 37 additions and 14 deletions

View File

@ -1,4 +1,5 @@
<> (?x dup) (?x ?x)
<> (?x ?y swap) (?y ?x)
<> ( ?x pop) ()
(A B swap) dup
(1 2 3) (4 5 6) swap pop dup

View File

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

View File

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

View File

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