Abstracted copy string
This commit is contained in:
parent
2ce9aadd56
commit
87cc3e816f
|
@ -1,3 +1,4 @@
|
|||
<> (?_ import) (?_)
|
||||
<> (?_ import) (?(?: ?:) (?_ \n) )
|
||||
|
||||
(examples/postcard.modal import)
|
||||
(examples/import.modal import)
|
||||
(examples/missing.modal import)
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
(this (file (gets (imported by file.modal))))
|
4
makefile
4
makefile
|
@ -10,13 +10,13 @@ dest:
|
|||
run: all bin/modal
|
||||
@ bin/modal -q examples/hello.modal
|
||||
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
|
||||
@ bin/modal -v
|
||||
@ bin/modal-debug -q examples/fizzbuzz.modal
|
||||
@ bin/modal-debug -q examples/sierpinski.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
|
||||
cp bin/modal ~/bin/
|
||||
uninstall:
|
||||
|
|
60
src/modal.c
60
src/modal.c
|
@ -14,6 +14,23 @@ static char *regs[0x100], stack[0x10], *stack_ = stack;
|
|||
|
||||
#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 *
|
||||
walk(char *s)
|
||||
{
|
||||
|
@ -30,16 +47,6 @@ walk(char *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
|
||||
device_write(char *s)
|
||||
{
|
||||
|
@ -87,12 +94,9 @@ static void
|
|||
device_read(void)
|
||||
{
|
||||
char c, *origin = dst_;
|
||||
while(fread(&c, 1, 1, stdin) && c >= ' ')
|
||||
*dst_++ = c;
|
||||
if(feof(stdin))
|
||||
*dst_++ = 'E', *dst_++ = 'O', *dst_++ = 'F';
|
||||
if(dst_ - origin == 0)
|
||||
dst_--;
|
||||
while(fread(&c, 1, 1, stdin) && c >= ' ') *dst_++ = c;
|
||||
if(feof(stdin)) dst_ = copy("EOF", dst_, 3);
|
||||
if(dst_ - origin == 0) dst_--;
|
||||
}
|
||||
|
||||
static void
|
||||
|
@ -116,7 +120,7 @@ file_import(char *path)
|
|||
fclose(f);
|
||||
if(pr) fprintf(stderr, "Modal program imbalanced.\n");
|
||||
} else
|
||||
*dst_++ = 'N', *dst_++ = 'A', *dst_++ = 'F';
|
||||
dst_ = copy("NAF", dst_, 3);
|
||||
}
|
||||
|
||||
static void
|
||||
|
@ -126,8 +130,8 @@ write_reg(char r, char *reg)
|
|||
case ':': device_write(reg); return;
|
||||
case '~': device_read(); return;
|
||||
case '_': {
|
||||
char filepath[0x80], *path = filepath, *cap = walk(reg);
|
||||
while(reg < cap) *path++ = *reg++;
|
||||
char filepath[0x80];
|
||||
copy(reg, filepath, walk(reg) - reg);
|
||||
file_import(filepath);
|
||||
return;
|
||||
}
|
||||
|
@ -141,7 +145,7 @@ write_reg(char r, char *reg)
|
|||
case '.': { /* op: unwrap */
|
||||
char *cap = walk(reg);
|
||||
if(*reg == '(') reg++, --cap;
|
||||
while(reg < cap) *dst_++ = *reg++;
|
||||
dst_ = copy(reg, dst_, cap - reg);
|
||||
return;
|
||||
}
|
||||
case '*': { /* op: explode */
|
||||
|
@ -162,8 +166,7 @@ write_reg(char r, char *reg)
|
|||
return;
|
||||
}
|
||||
default: {
|
||||
char *cap = walk(reg);
|
||||
while(reg < cap) *dst_++ = *reg++;
|
||||
dst_ = copy(reg, dst_, walk(reg) - reg);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -233,7 +236,7 @@ parse_frag(char **side, char *src)
|
|||
}
|
||||
*side = dict_, cap = walk(src), wrapped = c == '(';
|
||||
if(wrapped) src++, cap--;
|
||||
while(src < cap) c = *src, *dict_++ = *src++;
|
||||
while(src < cap) *dict_++ = *src++;
|
||||
src += wrapped, *dict_++ = 0;
|
||||
return src;
|
||||
}
|
||||
|
@ -328,14 +331,9 @@ main(int argc, char **argv)
|
|||
while(rewrite() && ++rw)
|
||||
if(!cycles--) return !fprintf(stderr, "Modal rewrites exceeded.\n");
|
||||
if(!quiet) {
|
||||
while(rules_-- > rules) {
|
||||
if(rules_->a) {
|
||||
if(!rules_->refs)
|
||||
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);
|
||||
}
|
||||
}
|
||||
while(rules_-- > rules)
|
||||
if(rules_->a && !rules_->refs)
|
||||
fprintf(stderr, "-- Unused rule: %d <> (%s) (%s)\n", rules_->id, rules_->a, rules_->b);
|
||||
if(rw)
|
||||
fprintf(stderr, ".. %s\nCompleted in %d rewrites.\n", src_, rw);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue