Abstracted copy string

This commit is contained in:
Devine Lu Linvega 2024-05-09 16:45:34 -07:00
parent 2ce9aadd56
commit 87cc3e816f
4 changed files with 35 additions and 35 deletions

View File

@ -1,3 +1,4 @@
<> (?_ import) (?_) <> (?_ import) (?(?: ?:) (?_ \n) )
(examples/postcard.modal import) (examples/import.modal import)
(examples/missing.modal import)

1
examples/import.modal Normal file
View File

@ -0,0 +1 @@
(this (file (gets (imported by file.modal))))

View File

@ -10,13 +10,13 @@ dest:
run: all bin/modal run: all bin/modal
@ bin/modal -q examples/hello.modal @ bin/modal -q examples/hello.modal
debug: all bin/modal-debug debug: all bin/modal-debug
@ bin/modal-debug -a examples/file.modal @ bin/modal-debug -a examples/hello.modal
test: all bin/modal-debug bin/modal test: all bin/modal-debug bin/modal
@ bin/modal -v @ bin/modal -v
@ bin/modal-debug -q examples/fizzbuzz.modal @ bin/modal-debug -q examples/fizzbuzz.modal
@ bin/modal-debug -q examples/sierpinski.modal @ bin/modal-debug -q examples/sierpinski.modal
@ bin/modal-debug -q examples/tests.modal @ bin/modal-debug -q examples/tests.modal
@ bin/modal-debug -a examples/file.modal @ bin/modal-debug -q -a examples/file.modal
install: bin/modal install: bin/modal
cp bin/modal ~/bin/ cp bin/modal ~/bin/
uninstall: uninstall:

View File

@ -14,6 +14,23 @@ static char *regs[0x100], stack[0x10], *stack_ = stack;
#define spacer(c) (c <= ' ' || c == '(' || c == ')') #define spacer(c) (c <= ' ' || c == '(' || c == ')')
static char *
copy(char *src, char *dst, int length)
{
while(length--) *dst++ = *src++;
return dst;
}
static int
sint(char *s)
{
char c;
int r = 0, n = 1;
if(*s == '-') { n = -1, s++; }
while((c = *s++) && !spacer(c)) r = r * 10 + c - '0';
return r * n;
}
static char * static char *
walk(char *s) walk(char *s)
{ {
@ -30,16 +47,6 @@ walk(char *s)
return s; return s;
} }
static int
sint(char *s)
{
char c;
int r = 0, n = 1;
if(*s == '-') { n = -1, s++; }
while((c = *s++) && !spacer(c)) r = r * 10 + c - '0';
return r * n;
}
static void static void
device_write(char *s) device_write(char *s)
{ {
@ -87,12 +94,9 @@ static void
device_read(void) device_read(void)
{ {
char c, *origin = dst_; char c, *origin = dst_;
while(fread(&c, 1, 1, stdin) && c >= ' ') while(fread(&c, 1, 1, stdin) && c >= ' ') *dst_++ = c;
*dst_++ = c; if(feof(stdin)) dst_ = copy("EOF", dst_, 3);
if(feof(stdin)) if(dst_ - origin == 0) dst_--;
*dst_++ = 'E', *dst_++ = 'O', *dst_++ = 'F';
if(dst_ - origin == 0)
dst_--;
} }
static void static void
@ -116,7 +120,7 @@ file_import(char *path)
fclose(f); fclose(f);
if(pr) fprintf(stderr, "Modal program imbalanced.\n"); if(pr) fprintf(stderr, "Modal program imbalanced.\n");
} else } else
*dst_++ = 'N', *dst_++ = 'A', *dst_++ = 'F'; dst_ = copy("NAF", dst_, 3);
} }
static void static void
@ -126,8 +130,8 @@ write_reg(char r, char *reg)
case ':': device_write(reg); return; case ':': device_write(reg); return;
case '~': device_read(); return; case '~': device_read(); return;
case '_': { case '_': {
char filepath[0x80], *path = filepath, *cap = walk(reg); char filepath[0x80];
while(reg < cap) *path++ = *reg++; copy(reg, filepath, walk(reg) - reg);
file_import(filepath); file_import(filepath);
return; return;
} }
@ -141,7 +145,7 @@ write_reg(char r, char *reg)
case '.': { /* op: unwrap */ case '.': { /* op: unwrap */
char *cap = walk(reg); char *cap = walk(reg);
if(*reg == '(') reg++, --cap; if(*reg == '(') reg++, --cap;
while(reg < cap) *dst_++ = *reg++; dst_ = copy(reg, dst_, cap - reg);
return; return;
} }
case '*': { /* op: explode */ case '*': { /* op: explode */
@ -162,8 +166,7 @@ write_reg(char r, char *reg)
return; return;
} }
default: { default: {
char *cap = walk(reg); dst_ = copy(reg, dst_, walk(reg) - reg);
while(reg < cap) *dst_++ = *reg++;
return; return;
} }
} }
@ -233,7 +236,7 @@ parse_frag(char **side, char *src)
} }
*side = dict_, cap = walk(src), wrapped = c == '('; *side = dict_, cap = walk(src), wrapped = c == '(';
if(wrapped) src++, cap--; if(wrapped) src++, cap--;
while(src < cap) c = *src, *dict_++ = *src++; while(src < cap) *dict_++ = *src++;
src += wrapped, *dict_++ = 0; src += wrapped, *dict_++ = 0;
return src; return src;
} }
@ -328,14 +331,9 @@ main(int argc, char **argv)
while(rewrite() && ++rw) while(rewrite() && ++rw)
if(!cycles--) return !fprintf(stderr, "Modal rewrites exceeded.\n"); if(!cycles--) return !fprintf(stderr, "Modal rewrites exceeded.\n");
if(!quiet) { if(!quiet) {
while(rules_-- > rules) { while(rules_-- > rules)
if(rules_->a) { if(rules_->a && !rules_->refs)
if(!rules_->refs) fprintf(stderr, "-- Unused rule: %d <> (%s) (%s)\n", rules_->id, rules_->a, rules_->b);
fprintf(stderr, "-- Unused rule: %d <> (%s) (%s)\n", rules_->id, rules_->a, rules_->b);
if(debug)
fprintf(stderr, " (%s) (%s), %d times.\n", rules_->a, rules_->b, rules_->refs);
}
}
if(rw) if(rw)
fprintf(stderr, ".. %s\nCompleted in %d rewrites.\n", src_, rw); fprintf(stderr, ".. %s\nCompleted in %d rewrites.\n", src_, rw);
} }