# HG changeset patch # User HackBot # Date 1387577930 0 # Node ID ac04036869593b2585f6353a569419d358072436 # Parent b0f3e267bb1e597315dab929e1ae1ee11c470530 rm -rf src/ploki; mv ploki src diff -r b0f3e267bb1e -r ac0403686959 ploki/Compile --- a/ploki/Compile Fri Dec 20 22:15:50 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,3 +0,0 @@ -#!/bin/sh -echo cc -o ploki *.c -lm -cc -o ploki *.c -lm diff -r b0f3e267bb1e -r ac0403686959 ploki/GNUmakefile --- a/ploki/GNUmakefile Fri Dec 20 22:15:50 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,22 +0,0 @@ -.PHONY: all clean realclean distclean dist remake - -include MakeSkel - -$(OBJ:.o=.depend): %.depend: %.c %.h - $(DEPEND) $< >$@ - --include $(OBJ:.o=.depend) - -Makefile: MakeSkel GNUmakefile $(OBJ:.o=.depend) - $(RMF) Makefile - $(CP) MakeSkel Makefile - $(CAT) *.depend >>Makefile - -.PHONY: dist -dist: Makefile tags distclean - plokidir="`$(BASENAME) \"\`$(PWD)\`\"`" \ - version="`$(CAT) VERSION`" && \ - cd .. && \ - $(RMF) "ploki-$$version.tar$(ZIP_EXT)" && \ - $(TAR) -cf "ploki-$$version.tar" --exclude-from="$$plokidir/IGNORE" "$$plokidir" && \ - $(ZIP) "ploki-$$version.tar" diff -r b0f3e267bb1e -r ac0403686959 ploki/IGNORE --- a/ploki/IGNORE Fri Dec 20 22:15:50 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -.git diff -r b0f3e267bb1e -r ac0403686959 ploki/IO.c --- a/ploki/IO.c Fri Dec 20 22:15:50 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,515 +0,0 @@ -#include "config.h" -#include "IO.h" -#include "Str.h" -#include "main.h" -#include "main_io.h" -#include "xmalloc.h" -#include "zz.h" - -#include -#include -#include -#include - -struct IO { - struct IO *prev, *next; - enum io_flags mode; - FILE *fp; - char *name; - size_t refs; - String *buf; - long told; - enum { - DI_NONE, - DI_RD, - DI_WR - } dirct; -}; - -static IO *Root; - -static void sanitycheck(enum io_flags m) { - #if !DEBUG_P - (void)m; - #endif - assert( - ( - m & IO_READ || - m & IO_WRITE || - m & IO_APPEND - ) && - !( - m & IO_WRITE && - m & IO_APPEND - ) && - ( - !(m & IO_BUFFERED) || m & IO_READ - ) && - ( - !(m & IO_AUTOFLUSH) || - ( - m & IO_WRITE || - m & IO_APPEND - ) - ) && - ( - !(m & IO_WRITE) || (m & IO_TRUNCATE || m & IO_READ) - ) && - ( - !(m & IO_TRUNCATE) || m & IO_WRITE - ) - ); -} - -void io_init(void) { -} - -static void io_delete(IO *io) { - if (io->mode & IO_BUFFERED) { - St_clear(io->buf); - xfree(io->buf); - } - if (io->fp && io->fp != stderr) { - if (fclose(io->fp)) { - fprintf(io_fp(Err), "%s: %s: %s\n", Prog, io->name, strerror(errno)); - } - } - xfree(io->name); - if (io->prev) { - io->prev->next = io->next; - } else { - assert(io == Root); - Root = io->next; - } - if (io->next) { - io->next->prev = io->prev; - } - xfree(io); -} - -void io_end(void) { - while (Root) { - io_delete(Root); - } -} - -static char *xstrdup(const char *s) { - const size_t len = strlen(s) + 1; - char *const tmp = xmalloc(len, sizeof *tmp); - memcpy(tmp, s, len); - return tmp; -} - -IO *io_enter(const char *name, FILE *fp, enum io_flags mode) { - IO *io; - sanitycheck(mode); - io = xmalloc(1, sizeof *io); - io->fp = fp; - io->mode = mode; - if (mode & IO_BUFFERED) { - io->buf = xmalloc(1, sizeof *io->buf); - St_init(io->buf); - io->told = -1; - } - io->name = xstrdup(name); - io->refs = 1; - - io->dirct = DI_NONE; - - io->prev = NULL; - io->next = Root; - if (Root) { - Root->prev = io; - } - Root = io; - return io; -} - -const char *io_name(const IO *io, String *s) { - if (s) { - St_cpy_s(s, io->name); - } - return io->name; -} - -IO *io_open(const char *name, enum io_flags mode) { - FILE *fp; - char mbuf[4], *p = mbuf; - sanitycheck(mode); - - if (mode & IO_APPEND) { - *p++ = 'a'; - if (mode & IO_READ) { - *p++ = '+'; - } - } else if (mode & IO_WRITE) { - if (mode & IO_TRUNCATE) { - *p++ = 'w'; - if (mode & IO_READ) { - *p++ = '+'; - } - } else { - assert(mode & IO_READ); - *p++ = 'r'; - *p++ = '+'; - } - } else { - assert(mode & IO_READ); - *p++ = 'r'; - } - - if (mode & IO_BINARY) { - *p++ = 'b'; - } - *p = '\0'; - - if (!(fp = fopen(name, mbuf))) { - return NULL; - } - return io_enter(name, fp, mode); -} - -IO *io_incr(IO *io) { - ++io->refs; - return io; -} - -void io_decr(IO *io) { - if (!--io->refs) { - io_delete(io); - } -} - -int io_close(IO *io) { - int ret; - assert(io->fp != NULL); - ret = fclose(io->fp); - io->fp = NULL; - if (io->mode & IO_BUFFERED) { - St_clear(io->buf); - xfree(io->buf); - io->mode &= ~IO_BUFFERED; - } - return ret; -} - -int io_bufred(const IO *io) { - return !!(io->mode & IO_BUFFERED); -} - -void io_unbuffer(IO *io) { - assert(io->mode & IO_BUFFERED); - if (St_len(io->buf) && io->told != -1) { - io_seek(io, io->told, SEEK_SET); - } - St_clear(io->buf); - xfree(io->buf); - io->mode &= ~IO_BUFFERED; -} - -FILE *io_fp(const IO *io) { - return io->fp; -} - -static void bufk(IO *f, size_t n) { - String tmp; - assert(f->mode & IO_BUFFERED); - - if (St_len(f->buf) >= n || feof(f->fp) || ferror(f->fp)) { - return; - } - - assert(f->dirct != DI_WR); - #if 0 - if (f->dirct == DI_WR) { - io_seek(f, 0, SEEK_CUR); - } - f->dirct = DI_RD; - #endif - - if (!(St_len(f->buf) || f->mode & IO_BINARY)) { - f->told = ftell(f->fp); - } - - St_init(&tmp); - while ( - St_len(f->buf) < n && - !feof(f->fp) && !ferror(f->fp) && - St_read(&tmp, f->fp, n - St_len(f->buf)) - ) { - St_cat(f->buf, &tmp); - } - St_clear(&tmp); -} - -#ifdef EBADF - #define BADF ((void)(errno = EBADF)) -#else - #define BADF ((void)0) -#endif - -#define XMODE(c, x) \ -do { \ - if (!(c)) { \ - BADF; \ - return (x); \ - } \ -} while (0) - -#define WMODE(f, x) XMODE((f)->mode & IO_WRITE || (f)->mode & IO_APPEND, (x)) -#define PMODE(f, x) XMODE((f)->mode & IO_BUFFERED, (x)) -#define RMODE(f, x) XMODE((f)->mode & IO_READ, (x)) - -const char *io_bufptr(IO *io) { - PMODE(io, NULL); - return St_ptr(io->buf); -} - -int io_flush(IO *f) { - WMODE(f, EOF); - return fflush(f->fp); -} - -int io_err(const IO *f) { - return ferror(f->fp); -} - -int io_eof(const IO *f) { - return feof(f->fp); -} - -int io_peek(IO *f, size_t pos) { - PMODE(f, EOF); - bufk(f, pos + 1); - if (St_len(f->buf) <= pos) { - return EOF; - } - return ST_INDEX(f->buf, pos); -} - -int io_cmppeek(IO *f, size_t o, const void *p, size_t n) { - size_t i; - PMODE(f, -1); - - for (i = 0; i < n; ++i) { - bufk(f, o + i + 1u); - if (ST_INDEX(f->buf, o + i) != i[(const unsigned char *)p]) { - return 1; - } - } - return 0; -} - -int io_xcmp(IO *f, size_t a, size_t b, size_t n) { - const size_t max = a > b ? a : b; - PMODE(f, -1); - bufk(f, max + n); - if (St_len(f->buf) < max + n) { - return 1; - } - return memcmp(St_ptr(f->buf) + a, St_ptr(f->buf) + b, n) != 0; -} - -size_t io_read(IO *f, String *s, size_t n) { - RMODE(f, -1); - - if (f->mode & IO_BUFFERED) { - String null; - size_t old; - - bufk(f, n); - old = St_len(f->buf); - St_init(&null); - St_substr(s, f->buf, 0, n, &null); - St_clear(&null); - old -= St_len(f->buf); - if (St_len(f->buf) && old && !(f->mode & IO_BINARY) && f->told != -1) { - const long keep = ftell(f->fp); - if (fseek(f->fp, f->told, SEEK_SET) != -1) { - size_t i; - for (i = 0; i < old; ++i) { - getc(f->fp); - } - f->told = ftell(f->fp); - f->dirct = DI_RD; - fseek(f->fp, keep, SEEK_SET); - } - } - return old; - } else { - if (f->dirct == DI_WR) { - fseek(f->fp, 0, SEEK_CUR); - } - f->dirct = DI_RD; - return St_read(s, f->fp, n); - } -} - -int io_getc(IO *f) { - RMODE(f, EOF); - - if (f->mode & IO_BUFFERED) { - int c; - bufk(f, 1); - c = St_shift(f->buf); - if (St_len(f->buf) && c != EOF && !(f->mode & IO_BINARY) && f->told != -1) { - const long keep = ftell(f->fp); - if (fseek(f->fp, f->told, SEEK_SET) != -1) { - getc(f->fp); - f->told = ftell(f->fp); - f->dirct = DI_RD; - fseek(f->fp, keep, SEEK_SET); - } - } - return c; - } - - if (f->dirct == DI_WR) { - fseek(f->fp, 0, SEEK_CUR); - } - f->dirct = DI_RD; - return getc(f->fp); -} - -size_t io_getline(IO *f, String *s) { - RMODE(f, -1); - - if (f->mode & IO_BUFFERED) { - size_t p; - size_t old; - - p = St_chr(f->buf, '\n') + 1u; - if (p) { - old = St_len(f->buf); - if (s) { - St_cpy_m(s, St_ptr(f->buf), p); - } - St_del(f->buf, 0, p); - } else { - for (p = St_len(f->buf); io_peek(f, p) != EOF; ++p) { - if (ST_LASTCHAR(f->buf) == '\n') { - break; - } - } - old = St_len(f->buf); - if (s) { - St_cpy(s, f->buf); - } - St_zero(f->buf); - } - old -= St_len(f->buf); - if (St_len(f->buf) && old && !(f->mode & IO_BINARY) && f->told != -1) { - const long keep = ftell(f->fp); - if (fseek(f->fp, f->told, SEEK_SET) != -1) { - size_t i; - for (i = 0; i < old; ++i) { - getc(f->fp); - } - f->dirct = DI_RD; - f->told = ftell(f->fp); - fseek(f->fp, keep, SEEK_SET); - } - } - return old; - } else { - int c; - size_t n; - - if (f->dirct == DI_WR) { - fseek(f->fp, 0, SEEK_CUR); - } - f->dirct = DI_RD; - - if (s) { - St_zero(s); - } - n = 0; - while ((c = getc(f->fp)) != EOF) { - if (s) { - St_cat_c(s, c); - } - ++n; - if (c == '\n') { - break; - } - } - if (!n && ferror(f->fp)) { - return -1; - } - return n; - } -} - -size_t io_write(IO *f, const String *s) { - size_t ret; - WMODE(f, -1); - if (f->dirct == DI_RD) { - fseek(f->fp, 0, SEEK_CUR); - } - f->dirct = DI_WR; - ret = ST_WRITE(s, f->fp); - if (f->mode & IO_AUTOFLUSH) { - fflush(f->fp); - } - return ret; -} - -size_t io_write_m(IO *f, const void *p, size_t n) { - size_t ret; - WMODE(f, -1); - if (f->dirct == DI_RD) { - fseek(f->fp, 0, SEEK_CUR); - } - f->dirct = DI_WR; - ret = fwrite(p, 1, n, f->fp); - if (f->mode & IO_AUTOFLUSH) { - fflush(f->fp); - } - return ret; -} - -size_t io_write_s(IO *f, const char *s) { - return io_write_m(f, s, strlen(s)); -} - -int io_putc(IO *f, int c) { - int ret; - WMODE(f, EOF); - if (f->dirct == DI_RD) { - fseek(f->fp, 0, SEEK_CUR); - } - f->dirct = DI_WR; - ret = putc(c, f->fp); - if (f->mode & IO_AUTOFLUSH) { - fflush(f->fp); - } - return ret; -} - -long io_tell(IO *f) { - if (f->mode & IO_BUFFERED) { - if (f->mode & IO_BINARY) { - const long ret = ftell(f->fp); - if (ret == -1) { - return ret; - } - return ret - St_len(f->buf); - } - if (St_len(f->buf)) { - return f->told; - } - } - return ftell(f->fp); -} - -int io_seek(IO *f, long off, enum io_whence w) { - if (f->mode & IO_BUFFERED) { - St_zero(f->buf); - } - f->dirct = DI_NONE; - return fseek(f->fp, off, w); -} - -void io_clearerr(IO *f) { - clearerr(f->fp); -} diff -r b0f3e267bb1e -r ac0403686959 ploki/IO.depend --- a/ploki/IO.depend Fri Dec 20 22:15:50 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -IO.o: IO.c config.h IO.h Str.h main.h main_io.h xmalloc.h zz.h diff -r b0f3e267bb1e -r ac0403686959 ploki/IO.h --- a/ploki/IO.h Fri Dec 20 22:15:50 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,65 +0,0 @@ -#ifndef IO_H_ -#define IO_H_ - -#include "config.h" -#include "Str.h" - -#include - -typedef struct IO IO; - -enum io_flags { - IO_BINARY = 1, - IO_READ = 2 * IO_BINARY, - IO_WRITE = 2 * IO_READ, - IO_APPEND = 2 * IO_WRITE, - IO_TRUNCATE = 2 * IO_APPEND, - IO_BUFFERED = 2 * IO_TRUNCATE, - IO_AUTOFLUSH = 2 * IO_BUFFERED -}; - -enum io_whence { - IOS_START = SEEK_SET, - IOS_CUR = SEEK_CUR, - IOS_END = SEEK_END -}; - -void io_init(void); -void io_end(void); -const char *io_name(const IO *, String *); -IO *io_enter(const char *, FILE *, enum io_flags); -IO *io_open(const char *, enum io_flags); -IO *io_incr(IO *); -void io_decr(IO *); -int io_close(IO *); - -ATTR_PURE -int io_bufred(const IO *); -void io_unbuffer(IO *); -const char *io_bufptr(IO *); - -ATTR_PURE -FILE *io_fp(const IO *); - -int io_flush(IO *); -ATTR_PURE -int io_err(const IO *); -ATTR_PURE -int io_eof(const IO *); -int io_peek(IO *, size_t); -int io_cmppeek(IO *, size_t, const void *, size_t); -int io_xcmp(IO *, size_t, size_t, size_t); - -size_t io_read(IO *, String *, size_t); -int io_getc(IO *); -size_t io_getline(IO *, String *); -size_t io_write(IO *, const String *); -size_t io_write_m(IO *, const void *, size_t); -size_t io_write_s(IO *, const char *); -int io_putc(IO *, int); - -long io_tell(IO *); -int io_seek(IO *, long, enum io_whence); -void io_clearerr(IO *); - -#endif /* IO_H_ */ diff -r b0f3e267bb1e -r ac0403686959 ploki/MakeSkel --- a/ploki/MakeSkel Fri Dec 20 22:15:50 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,73 +0,0 @@ -# vi: set ft=make: -PREFIX = /usr/local -INSTDIR = $(PREFIX)/bin - -CC = gcc -#CC = cc - -#COPT = -DDEBUGGING -g -COPT = -g -O2 -#COPT = -xO4 -xalias_level=std -xbuiltin=%all -xdepend -xinline=%auto -xlibmil -xtarget=native -#COPT = -O -#COPT = - -LDOPT = -#LDOPT = -s - -CPP = $(CC) -E -#CPP = cpp - -DEPEND = $(CPP) -MM -#DEPEND = $(CC) -xM1 - -CFLAGS = $(COPT) -W -Wall -Wundef -Wshadow -Wpointer-arith -Wcast-align -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs -Winline -std=gnu9x -pedantic -fstrict-aliasing -pipe -#CFLAGS = $(COPT) -W -Wall -Wundef -Wendif-labels -Wshadow -Wpointer-arith -Wcast-align -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs -Winline -Wdisabled-optimization -std=gnu99 -pedantic -fstrict-aliasing -pipe -#CFLAGS = $(COPT) -Xc -#CFLAGS = $(COPT) - -LDFLAGS = $(LDOPT) -lm - -ECHO = echo -CAT = cat -CP = cp -RMF = rm -f -TAR = tar -CTAGS = ctags -#CTAGS = ctags --langmap=c:.c.h -ZIP = bzip2 -ZIP_EXT = .bz2 -BASENAME = basename -PWD = pwd - -# # # # # - -OBJ = IO.o Str.o atechit.o compile.o deparse.o expr.o hang.o hash.o inc.o \ - kork.o list.o main.o mars.o match.o op.o opt.o parse.o pp.o random.o \ - re.o run.o strhash.o strutil.o sub.o text.o transmogrify.o val.o \ - variable.o venus.o version.o xmalloc.o zz.o - -all: tags ploki - -tags: *.c *.h - $(CTAGS) *.c *.h - -distclean: clean - $(RMF) core examples/core a.out -clean: - $(RMF) *.o ploki -realclean: clean - $(RMF) *.depend Makefile - -remake: clean all - -ploki: $(OBJ) - $(CC) -o ploki $(OBJ) $(LDFLAGS) - -install: ploki - $(CP) ploki '$(INSTDIR)/' - -version.c: version.c.in VERSION - $(CP) version.c.in version.c - $(ECHO) '"'"`$(CAT) VERSION`"'";' >>version.c - -######################################################################## diff -r b0f3e267bb1e -r ac0403686959 ploki/Makefile --- a/ploki/Makefile Fri Dec 20 22:15:50 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,135 +0,0 @@ -# vi: set ft=make: -PREFIX = /usr/local -INSTDIR = $(PREFIX)/bin - -CC = gcc -#CC = cc - -#COPT = -DDEBUGGING -g -COPT = -g -O2 -#COPT = -xO4 -xalias_level=std -xbuiltin=%all -xdepend -xinline=%auto -xlibmil -xtarget=native -#COPT = -O -#COPT = - -LDOPT = -#LDOPT = -s - -CPP = $(CC) -E -#CPP = cpp - -DEPEND = $(CPP) -MM -#DEPEND = $(CC) -xM1 - -CFLAGS = $(COPT) -W -Wall -Wundef -Wshadow -Wpointer-arith -Wcast-align -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs -Winline -std=gnu9x -pedantic -fstrict-aliasing -pipe -#CFLAGS = $(COPT) -W -Wall -Wundef -Wendif-labels -Wshadow -Wpointer-arith -Wcast-align -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs -Winline -Wdisabled-optimization -std=gnu99 -pedantic -fstrict-aliasing -pipe -#CFLAGS = $(COPT) -Xc -#CFLAGS = $(COPT) - -LDFLAGS = $(LDOPT) -lm - -ECHO = echo -CAT = cat -CP = cp -RMF = rm -f -TAR = tar -CTAGS = ctags -#CTAGS = ctags --langmap=c:.c.h -ZIP = bzip2 -ZIP_EXT = .bz2 -BASENAME = basename -PWD = pwd - -# # # # # - -OBJ = IO.o Str.o atechit.o compile.o deparse.o expr.o hang.o hash.o inc.o \ - kork.o list.o main.o mars.o match.o op.o opt.o parse.o pp.o random.o \ - re.o run.o strhash.o strutil.o sub.o text.o transmogrify.o val.o \ - variable.o venus.o version.o xmalloc.o zz.o - -all: tags ploki - -tags: *.c *.h - $(CTAGS) *.c *.h - -distclean: clean - $(RMF) core examples/core a.out -clean: - $(RMF) *.o ploki -realclean: clean - $(RMF) *.depend Makefile - -remake: clean all - -ploki: $(OBJ) - $(CC) -o ploki $(OBJ) $(LDFLAGS) - -install: ploki - $(CP) ploki '$(INSTDIR)/' - -version.c: version.c.in VERSION - $(CP) version.c.in version.c - $(ECHO) '"'"`$(CAT) VERSION`"'";' >>version.c - -######################################################################## -IO.o: IO.c config.h IO.h Str.h main.h main_io.h xmalloc.h zz.h -Str.o: Str.c config.h Str.h strutil.h xmalloc.h -atechit.o: atechit.c zz.h config.h atechit.h main.h -compile.o: compile.c config.h Str.h compile.h text.h op.h IO.h expr.h \ - re.h stack.h xmalloc.h strhash.h val.h kork.h list.h sub.h variable.h \ - main_var.h zz.h -deparse.o: deparse.c config.h Str.h deparse.h text.h op.h IO.h expr.h \ - re.h stack.h xmalloc.h strhash.h val.h kork.h list.h sub.h variable.h \ - main_io.h main_label.h mars.h venus.h hash.h zz.h -expr.o: expr.c config.h Str.h expr.h op.h IO.h re.h stack.h xmalloc.h \ - strhash.h val.h kork.h list.h sub.h variable.h hang.h main_io.h \ - main_label.h mars.h venus.h hash.h main_var.h match.h pp.h random.h \ - run.h text.h zz.h -hang.o: hang.c config.h hang.h -hash.o: hash.c config.h hash.h main.h main_opt.h random.h xmalloc.h -inc.o: inc.c config.h inc.h -kork.o: kork.c config.h IO.h Str.h kork.h strutil.h xmalloc.h -list.o: list.c list.h config.h val.h IO.h Str.h kork.h sub.h xmalloc.h -main.o: main.c IO.h config.h Str.h atechit.h compile.h text.h op.h expr.h \ - re.h stack.h xmalloc.h strhash.h val.h kork.h list.h sub.h variable.h \ - deparse.h inc.h main.h main_io.h main_label.h mars.h venus.h hash.h \ - main_opt.h main_var.h opt.h parse.h random.h run.h transmogrify.h \ - version.h zz.h -mars.o: mars.c Str.h config.h mars.h op.h IO.h expr.h re.h stack.h \ - xmalloc.h strhash.h val.h kork.h list.h sub.h variable.h -match.o: match.c IO.h config.h Str.h main_io.h main_opt.h match.h re.h \ - val.h kork.h list.h sub.h run.h op.h expr.h stack.h xmalloc.h strhash.h \ - variable.h text.h -op.o: op.c IO.h config.h Str.h expr.h op.h re.h stack.h xmalloc.h \ - strhash.h val.h kork.h list.h sub.h variable.h main_label.h mars.h \ - venus.h hash.h -opt.o: opt.c opt.h -parse.o: parse.c IO.h config.h Str.h inc.h main.h main_label.h mars.h \ - op.h expr.h re.h stack.h xmalloc.h strhash.h val.h kork.h list.h sub.h \ - variable.h venus.h hash.h parse.h text.h -pp.o: pp.c IO.h config.h Str.h kork.h list.h main_io.h main_opt.h match.h \ - re.h val.h sub.h pp.h run.h op.h expr.h stack.h xmalloc.h strhash.h \ - variable.h text.h -random.o: random.c config.h random.h -re.o: re.c config.h IO.h Str.h hash.h main_io.h main_opt.h re.h xmalloc.h \ - zz.h re_block.c.h -run.o: run.c config.h IO.h Str.h atechit.h expr.h op.h re.h stack.h \ - xmalloc.h strhash.h val.h kork.h list.h sub.h variable.h hang.h main.h \ - main_io.h main_label.h mars.h venus.h hash.h main_opt.h run.h text.h \ - zz.h -strhash.o: strhash.c hash.h strhash.h config.h strutil.h xmalloc.h -strutil.o: strutil.c strutil.h config.h -sub.o: sub.c expr.h config.h Str.h op.h IO.h re.h stack.h xmalloc.h \ - strhash.h val.h kork.h list.h sub.h variable.h main_opt.h \ - transmogrify.h text.h -text.o: text.c text.h op.h IO.h config.h Str.h expr.h re.h stack.h \ - xmalloc.h strhash.h val.h kork.h list.h sub.h variable.h -transmogrify.o: transmogrify.c config.h Str.h expr.h op.h IO.h re.h \ - stack.h xmalloc.h strhash.h val.h kork.h list.h sub.h variable.h \ - main_label.h mars.h venus.h hash.h text.h transmogrify.h zz.h -val.o: val.c config.h IO.h Str.h val.h kork.h list.h sub.h xmalloc.h -variable.o: variable.c config.h Str.h strhash.h variable.h xmalloc.h -venus.o: venus.c Str.h config.h hash.h op.h IO.h expr.h re.h stack.h \ - xmalloc.h strhash.h val.h kork.h list.h sub.h variable.h venus.h zz.h -version.o: version.c version.h -xmalloc.o: xmalloc.c config.h main.h xmalloc.h -zz.o: zz.c zz.h config.h main.h diff -r b0f3e267bb1e -r ac0403686959 ploki/README --- a/ploki/README Fri Dec 20 22:15:50 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,32 +0,0 @@ -DESCRIPTION - -This is the ploki package. ploki is a programming language defined -by its implementation and designed by accident. There are short example -scripts in the examples/ subdirectory. You might want to copy -syntax/ploki.vim and indent/ploki.vim into your ~/.vim/ directory if you're -using vim; to get automatic filetype detection, create filetype.vim as -described in :help new-filetype and add the following entry: - au BufNewFile,BufRead *.pk setf ploki - -See the files in doc/ for a description of the language. - -BUILDING - -You need a C compiler. Compile all *.c files into a single executable called -'ploki'. That's it. - -If you have make, typing 'make ploki' should invoke your C compiler with the -right arguments. You may need to adjust some configuration variables: CC is -the C compiler used; CFLAGS are default arguments for the compiler; LDFLAGS -are default arguments for the linker. - -You should edit MakeSkel if you're using GNU make, Makefile otherwise. Note -that Makefile is autogenerated by GNU make from MakeSkel and *.depend. - -There is no 'configure' script; ploki is almost completely standard C and -should Just Work(TM). But have a look at config.h if you want to tweak the -autodetection of gcc, C99, presence of /dev/urandom, etc. - - -Have fun! -Lukas Mai diff -r b0f3e267bb1e -r ac0403686959 ploki/Str.c --- a/ploki/Str.c Fri Dec 20 22:15:50 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,794 +0,0 @@ -#include "config.h" -#include "Str.h" -#include "strutil.h" -#include "xmalloc.h" - -#include -#include -#include -#include -#include -#include -#include -#include - -enum {MAGIC = 4}; - -#define OFFSET_OFF(s) \ -do { \ - if ((s)->offset) { \ - memmove((s)->buf - (s)->offset, (s)->buf, (s)->length + 1); \ - (s)->buf -= (s)->offset; \ - (s)->offset = 0; \ - } \ -} while (0) - - -void -St_init(String *s) { - s->buf = xmalloc(s->size = MAGIC, sizeof *s->buf); - s->buf[s->offset = s->length = 0] = '\0'; -} - -void -St_fake(String *s, char *p, size_t n) { - assert(s != NULL); - assert(p != NULL); - s->buf = p; - s->offset = 0; - s->length = s->size = n; -} - -void -St_clear(String *s) { - assert(s != NULL); - if (!s->buf) { - return; - } - xfree(s->buf - s->offset); - s->buf = NULL; - DEBUG(s->offset = s->length = s->size = 0;) -} - -#if 0 -void -(St_zero)(String *s) { - St_zero(s); -} - -char * -(St_ptr)(const String *s) { - return St_ptr(s); -} - -size_t -(St_len)(const String *s) { - return St_len(s); -} -#endif - -static void -St_grow(String *s, size_t n) { - if (s->size - s->offset <= n) { - OFFSET_OFF(s); - if (s->size <= n) { - s->buf = xrealloc(s->buf, n + 1u); - s->size = n + 1u; - } - } -} - -void -St_trunc(String *s, size_t n) { - if (s->length > n) { - s->buf[s->length = n] = '\0'; - } - if (s->size > n + 1 && n >= MAGIC) { - OFFSET_OFF(s); - s->buf = xrealloc(s->buf, n + 1); - s->size = n + 1; - } -} - -#if 0 -int -St_lastchar(const String *s) { - return ST_LASTCHAR(s); -} - -int -St_firstchar(const String *s) { - return ST_FIRSTCHAR(s); -} - -int -St_chop(String *s) { - int tmp; - - if (!s->length) { - return EOF; - } - tmp = (unsigned char)s->buf[s->length - 1]; - s->buf[--s->length] = '\0'; - return tmp; -} -#endif - -int -St_shift(String *s) { - int tmp; - - if (!s->length) { - return EOF; - } - tmp = (unsigned char)s->buf[0]; - ++s->offset; - ++s->buf; - --s->length; - return tmp; -} - - -size_t -St_shiftws(String *s) { - size_t n; - - for (n = 0; n < s->length && isspace((unsigned char)s->buf[n]); ++n) - ; - s->offset += n; - s->buf += n; - s->length -= n; - - return n; -} - -#if 0 -int -St_index(const String *s, size_t n) { - return ST_INDEX(s, n); -} -#endif - -size_t -St_chr(const String *s, int c) { - const char *tmp; - if (!(tmp = memchr(s->buf, c, s->length))) { - return -1; - } - return tmp - s->buf; -} - -#if 0 -size_t -St_rchr(const String *s, int c) { - size_t i; - - for (i = s->length; i; --i) { - if (s->buf[i - 1] == c) - return i - 1; - } - return -1; -} -#endif - -int -St_cmp(const String *s, const String *t) { - return u_cmp(St_ptr(s), St_len(s), St_ptr(t), St_len(t)); -} - -int -St_cmp_m(const String *s, const void *m, size_t n) { - return u_cmp(St_ptr(s), St_len(s), m, n); -} - -#if 0 -int -St_cmp_s(const String *s, const char *tz) { - return u_cmp(St_ptr(s), St_len(s), tz, strlen(tz)); -} -#endif - -#define NCMP(s, m, n) \ -do { \ - return memcmp((s)->buf, m, (s)->length < (n) ? (s)->length : (n)); \ -} while (0) - -#if 0 -int -St_ncmp(const String *s, const String *t) { - NCMP(s, t->buf, t->length); -} -#endif - -int -St_ncmp_m(const String *s, const void *m, size_t n) { - NCMP(s, m, n); -} - -#if 0 -int -St_ncmp_s(const String *s, const char *tz) { - size_t length = strlen(tz); - - NCMP(s, tz, length); -} - -static int -my_memcasecmp(const void *p, const void *q, size_t n) { - const unsigned char *s = p, *t = q; - size_t i; - - for (i = 0; i < n; ++i) { - if (tolower(s[i]) < tolower(t[i])) { - return -1; - } - if (tolower(s[i]) > tolower(t[i])) { - return 1; - } - } - return 0; -} - -#define CASECMP(s, m, n) \ -do { \ - int tmp__; \ - if (!(tmp__ = my_memcasecmp((s)->buf, m, (s)->length < (n) ? (s)->length : (n)))) { \ - if ((s)->length < (n)) \ - return -1; \ - if ((s)->length > (n)) \ - return 1; \ - } \ - return tmp__; \ -} while (0) - -int -St_casecmp(const String *s, const String *t) { - CASECMP(s, t->buf, t->length); -} - -int -St_casecmp_m(const String *s, const void *m, size_t n) { - CASECMP(s, m, n); -} - -int -St_casecmp_s(const String *s, const char *tz) { - size_t length = strlen(tz); - - CASECMP(s, tz, length); -} - -#define NCASECMP(s, m, n) \ -do { \ - return my_memcasecmp((s)->buf, m, n); \ -} while (0) - -int -St_ncasecmp(const String *s, const String *t) { - NCASECMP(s, t->buf, t->length); -} - -int -St_ncasecmp_m(const String *s, const void *m, size_t n) { - NCASECMP(s, m, n); -} - -int -St_ncasecmp_s(const String *s, const char *tz) { - size_t length = strlen(tz); - - NCASECMP(s, tz, length); -} -#endif - -#define STR(s, m, n) \ -do { \ - size_t i__; \ - if ((n) == 0) \ - return 0; \ - if ((s)->length < (n)) \ - return -1; \ - for (i__ = 0; i__ <= (s)->length - (n); ++i__) { \ - if (!memcmp((s)->buf + i__, m, n)) \ - return i__; \ - } \ - return -1; \ -} while (0) - -size_t -St_str(const String *s, const String *t) { - STR(s, t->buf, t->length); -} - -size_t -St_str_m(const String *s, const void *m, size_t n) { - STR(s, m, n); -} - -#if 0 -size_t -St_str_s(const String *s, const char *tz) { - size_t length = strlen(tz); - - STR(s, tz, length); -} -#endif - -#define RSTR(s, m, n) \ -do { \ - size_t i__; \ - if ((n) == 0) \ - return (s)->length; \ - if ((s)->length < (n)) \ - return -1; \ - for (i__ = (s)->length - (n) + 1; i__; --i__) { \ - if (!memcmp((s)->buf + i__ - 1, m, n)) \ - return i__ - 1; \ - } \ - return -1; \ -} while (0) - -#if 0 -size_t -St_rstr(const String *s, const String *t) { - RSTR(s, t->buf, t->length); -} -#endif - -size_t -St_rstr_m(const String *s, const void *m, size_t n) { - RSTR(s, m, n); -} - -size_t -St_rstr_s(const String *s, const char *tz) { - size_t length = strlen(tz); - - RSTR(s, tz, length); -} - -size_t -St_stro_m(const String *s, size_t off, const void *m, size_t n) { - size_t i; - - assert(off <= s->length); - - if (n == 0) { - return off; - } - - if (n > s->length - off) { - return -1; - } - - for (i = off; i < s->length - n; ++i) { - if (memcmp(s->buf + i, m, n) == 0) { - return i; - } - } - return -1; -} - -size_t -St_rstro_m(const String *s, size_t off, const void *m, size_t n) { - size_t i; - - assert(off <= s->length); - - if (n == 0) { - return off; - } - - if (n > s->length) { - return -1; - } - - i = off; - if (i > s->length - n) { - i = s->length - n; - } - - for (; i + 1u; --i) { - if (memcmp(s->buf + i, m, n) == 0) { - return i; - } - } - return -1; -} - -#define CPY(s, m, n) \ -do { \ - if ((s)->size - (s)->offset <= (n)) { \ - OFFSET_OFF(s); \ - if ((s)->size <= (n)) { \ - (s)->buf = xrealloc((s)->buf, (n) + 1); \ - (s)->size = (n) + 1; \ - } \ - } \ - memcpy((s)->buf, m, (s)->length = (n)); \ - (s)->buf[n] = '\0'; \ -} while (0) - -void -St_cpy(String *s, const String *t) { - CPY(s, t->buf, t->length); -} - -void -St_cpy_m(String *s, const void *m, size_t n) { - CPY(s, m, n); -} - -void -St_cpy_s(String *s, const char *tz) { - size_t length = strlen(tz); - - CPY(s, tz, length); -} - -void -St_cpy_c(String *s, int c) { - unsigned char tmp = c; - CPY(s, &tmp, 1); -} - -static void cat(String *s, const void *m, size_t n) { - if (s->size - s->offset <= s->length + n) { - OFFSET_OFF(s); - if (s->size <= s->length + n) { - do { - s->size = s->size / 2 * 3 + 1; - } while (s->size <= s->length + n); - s->buf = xrealloc(s->buf, s->size); - } - } - memcpy(s->buf + s->length, m, n); - s->buf[s->length += n] = '\0'; -} - -void -St_cat(String *s, const String *t) { - cat(s, t->buf, t->length); -} - -void -St_cat_m(String *s, const void *m, size_t n) { - cat(s, m, n); -} - -void -St_cat_s(String *s, const char *tz) { - cat(s, tz, strlen(tz)); -} - -void -St_cat_c(String *s, int c) { - unsigned char tmp = c; - cat(s, &tmp, 1); -} - -#define TAC(s, m, n) \ -do { \ - if ((s)->offset >= (n)) { \ - (s)->buf -= (n); \ - (s)->offset -= (n); \ - } else if ((s)->size <= (s)->length + (n)) { \ - (s)->buf -= (s)->offset; \ - (s)->buf = xrealloc((s)->buf, (s)->length + (n) + 1); \ - (s)->size = (s)->length + (n) + 1; \ - memmove((s)->buf + (n), (s)->buf + (s)->offset, (s)->length + 1); \ - (s)->offset = 0; \ - } else { \ - memmove((s)->buf + (n), (s)->buf, (s)->length + 1); \ - } \ - memcpy((s)->buf, m, n); \ - (s)->length += (n); \ -} while (0) - -#if 0 -void -St_tac(String *s, const String *t) { - TAC(s, t->buf, t->length); -} -#endif - -void -St_tac_m(String *s, const void *m, size_t n) { - TAC(s, m, n); -} - -void -St_tac_s(String *s, const char *tz) { - size_t length = strlen(tz); - - TAC(s, tz, length); -} - -void -St_tac_c(String *s, int c) { - unsigned char tmp = c; - TAC(s, &tmp, 1); -} - -void -St_reverse(String *s) { - size_t i; - char tmp; - - for (i = 0; i < s->length / 2; ++i) { - tmp = s->buf[i]; - s->buf[i] = s->buf[s->length - i - 1]; - s->buf[s->length - i - 1] = tmp; - } -} - -#if 0 -void -St_map(String *s, int (*better)(int)) { - size_t i; - int tmp; - - for (i = 0; i < s->length; ++i) { - tmp = better((unsigned char)s->buf[i]); - if (tmp == EOF) { - s->buf[s->length = i] = '\0'; - return; - } - s->buf[i] = tmp; - } - - while ((tmp = better(EOF)) != EOF) { - if (s->size <= s->length + 1) { - OFFSET_OFF(s); - XREALLOC(s->buf, s->size * 2); - s->size *= 2; - } - s->buf[s->length++] = tmp; - s->buf[s->length] = '\0'; - } -} -#endif - -void -St_grep(String *s, int (*good)(int)) { - size_t r, w; - - for (r = w = 0; r < s->length; ++r) { - if (good((unsigned char)s->buf[r])) { - s->buf[w++] = s->buf[r]; - } - } - s->buf[s->length = w] = '\0'; -} - -void -St_upper(String *s) { - size_t i; - - for (i = 0; i < s->length; ++i) { - s->buf[i] = toupper((unsigned char)s->buf[i]); - } -} - -void -St_lower(String *s) { - size_t i; - - for (i = 0; i < s->length; ++i) { - s->buf[i] = tolower((unsigned char)s->buf[i]); - } -} - -void -St_del(String *s, size_t p, size_t n) { - if (n == 0 || s->length < p) - return; - if (s->length < n || s->length < p + n) { - n = s->length - p; - } - if (p + n == s->length) { - s->buf[p] = '\0'; - } else if (p == 0) { - s->offset += n; - s->buf += n; - } else { - memmove(s->buf + p, s->buf + n + p, s->length - p - n); - } - s->length -= n; -} - -#if 0 -#define INS(s, p, m, n) \ -do { \ - if ((n) == 0 || (s)->length < (p)) \ - return; \ - if ((n) <= (s)->offset) { \ - (s)->offset -= (n); \ - (s)->buf -= (n); \ - memmove((s)->buf, (s)->buf + (n), p); \ - } else { \ - if ((s)->size - (s)->offset <= (s)->length + (n)) { \ - OFFSET_OFF(s); \ - XREALLOC((s)->buf, (s)->length + (n) + 1); \ - (s)->size = (s)->length + (n) + 1; \ - } \ - memmove((s)->buf + (p) + (n), (s)->buf + (p), (s)->length - (p) + 1); \ - } \ - memcpy((s)->buf + (p), m, n); \ - (s)->length += (n); \ -} while (0) - -void -St_ins(String *s, size_t p, const String *t) { - INS(s, p, t->buf, t->length); -} - -void -St_ins_m(String *s, size_t p, const void *m, size_t n) { - INS(s, p, m, n); -} - -void -St_ins_s(String *s, size_t p, const char *tz) { - size_t length = strlen(tz); - - INS(s, p, tz, length); -} - -void -St_ins_c(String *s, size_t p, int c) { - unsigned char tmp = c; - INS(s, p, &tmp, 1); -} -#endif - -void -St_substr(String *l, String *s, size_t p, size_t n, const String *r) { - if (l) { - if (p >= s->length) { - St_zero(l); - } else { - size_t length = n; - - if (p + length > s->length) { - length = s->length - p; - } - St_cpy_m(l, s->buf + p, length); - } - } - - if (r) { - size_t gap = 0; - - if (p + n > s->length) { - if (p > s->length) { - n = 0; - gap = p - s->length; - } else { - n = s->length - p; - } - } - St_grow(s, s->length + r->length - n + gap); - if (gap) { - memset(s->buf + s->length, '\0', gap); - } else if (r->length != n && s->length - p - n) { - memmove(s->buf + p + r->length, s->buf + p + n, s->length - p - n); - } - memcpy(s->buf + p, r->buf, r->length); - s->buf[s->length += r->length - n + gap] = '\0'; - } -} - -#if HAVE_VSNPRINTF_P -size_t -St_xprintf(String *s, const char *fmt, ...) { - va_list ap; - int tmp; - - va_start(ap, fmt); - tmp = vsnprintf(s->buf, s->size, fmt, ap); - va_end(ap); - - if (tmp < 0) { - do { - St_grow(s, s->size * 2); - - va_start(ap, fmt); - tmp = vsnprintf(s->buf, s->size, fmt, ap); - va_end(ap); - } while (tmp < 0); - } else if (tmp + 1u >= s->size) { - St_grow(s, tmp); - s->length = 0; - - va_start(ap, fmt); - tmp = vsnprintf(s->buf, s->size, fmt, ap); - va_end(ap); - - if (tmp < 0) { - s->buf[0] = '\0'; - return -1; - } - } - - return s->length = tmp; -} -#endif - -void -St_num(String *s, double d) { - #if HAVE_VSNPRINTF_P - St_xprintf(s, "%.*g", DBL_DIG, d); - #else - St_grow(s, 255); - s->length = sprintf(s->buf, "%.*g", DBL_DIG, d); - #endif -} - -#if 0 -size_t -St_write(const String *s, FILE *fp) { - return ST_WRITE(s, fp); -} -#endif - -#define MIN(a, b) ((a) < (b) ? (a) : (b)) - -size_t -St_read(String *s, FILE *fp, size_t n) { - char buf[4096]; - size_t red; - - St_zero(s); - while (n && !feof(fp) && (red = fread(buf, sizeof *buf, MIN(n, sizeof buf / sizeof *buf), fp))) { - St_cat_m(s, buf, red); - n -= red; - } - return s->length; -} - -#if 0 -int -St_getline(String *s, FILE *fp, int n) { - int c; - - OFFSET_OFF(s); - for (s->length = 0; (c = getc(fp)) != EOF; ++s->length) { - if (s->size <= s->length + 1) { - XREALLOC(s->buf, s->size * 2); - s->size *= 2; - } - s->buf[s->length] = c; - if (c == n) - break; - } - s->buf[s->length] = '\0'; - - return s->length != 0; -} - -void -St_getfile(String *s, FILE *fp) { - size_t tmp; - - OFFSET_OFF(s); - if (s->size <= BUFSIZ) { - XREALLOC(s->buf, BUFSIZ + 1); - s->size = BUFSIZ + 1; - } - - for (s->length = 0; (tmp = fread(s->buf + s->length, 1, BUFSIZ, fp)); ) { - s->length += tmp; - if (s->size <= s->length + BUFSIZ + 1) { - XREALLOC(s->buf, s->size * 2); - s->size *= 2; - } - } - s->buf[s->length] = '\0'; -} -#endif - -size_t -St_hash(const String *s, size_t h) { - return u_hash(St_ptr(s), St_len(s), h); -} diff -r b0f3e267bb1e -r ac0403686959 ploki/Str.depend --- a/ploki/Str.depend Fri Dec 20 22:15:50 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -Str.o: Str.c config.h Str.h strutil.h xmalloc.h diff -r b0f3e267bb1e -r ac0403686959 ploki/Str.h --- a/ploki/Str.h Fri Dec 20 22:15:50 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,144 +0,0 @@ -#ifndef STR_H_ -#define STR_H_ - -#include "config.h" - -#include - -typedef struct { - char *buf; - size_t length; - size_t size; - size_t offset; -} String; - -void St_init (String *); -void St_fake (String *, char *, size_t); -void St_clear (String *); -#if 0 -void St_zero (String *); -ATTR_PURE -char *St_ptr (const String *); -ATTR_PURE -size_t St_len (const String *); -void St_grow (String *, size_t); -#endif -void St_trunc (String *, size_t); -#if 0 -ATTR_PURE -int St_lastchar (const String *); -ATTR_PURE -int St_firstchar (const String *); -int St_chop (String *); -#endif -int St_shift (String *); -size_t St_shiftws (String *); -#if 0 -ATTR_PURE -int St_index (const String *, size_t); -#endif -ATTR_PURE -size_t St_chr (const String *, int); -#if 0 -size_t St_rchr (const String *, int); -#endif -ATTR_PURE -int St_cmp (const String *, const String *); -ATTR_PURE -int St_cmp_m (const String *, const void *, size_t); -#if 0 -ATTR_PURE -int St_cmp_s (const String *, const char *); -ATTR_PURE -int St_ncmp (const String *, const String *); -#endif -ATTR_PURE -int St_ncmp_m (const String *, const void *, size_t); -#if 0 -ATTR_PURE -int St_ncmp_s (const String *, const char *); -ATTR_PURE -int St_casecmp (const String *, const String *); -ATTR_PURE -int St_casecmp_m (const String *, const void *, size_t); -ATTR_PURE -int St_casecmp_s (const String *, const char *); -ATTR_PURE -int St_ncasecmp (const String *, const String *); -ATTR_PURE -int St_ncasecmp_m (const String *, const void *, size_t); -ATTR_PURE -int St_ncasecmp_s (const String *, const char *); -#endif -ATTR_PURE -size_t St_str (const String *, const String *); -ATTR_PURE -size_t St_str_m (const String *, const void *, size_t); -#if 0 -ATTR_PURE -size_t St_str_s (const String *, const char *); -ATTR_PURE -size_t St_rstr (const String *, const String *); -#endif -ATTR_PURE -size_t St_rstr_m (const String *, const void *, size_t); -ATTR_PURE -size_t St_rstr_s (const String *, const char *); -ATTR_PURE -size_t St_stro_m (const String *, size_t, const void *, size_t); -ATTR_PURE -size_t St_rstro_m (const String *, size_t, const void *, size_t); -void St_cpy (String *, const String *); -void St_cpy_m (String *, const void *, size_t); -void St_cpy_s (String *, const char *); -void St_cpy_c (String *, int); -void St_cat (String *, const String *); -void St_cat_m (String *, const void *, size_t); -void St_cat_s (String *, const char *); -void St_cat_c (String *, int); -#if 0 -void St_tac (String *, const String *); -#endif -void St_tac_m (String *, const void *, size_t); -void St_tac_s (String *, const char *); -void St_tac_c (String *, int); -void St_reverse (String *); -#if 0 -void St_map (String *, int (*)(int)); -#endif -void St_grep (String *, int (*)(int)); -void St_upper (String *); -void St_lower (String *); -void St_del (String *, size_t, size_t); -#if 0 -void St_ins (String *, size_t, const String *); -void St_ins_m (String *, size_t, const void *, size_t); -void St_ins_s (String *, size_t, const char *); -void St_ins_c (String *, size_t, int); -#endif -void St_substr (String *, String *, size_t, size_t, const String *); -#if HAVE_VSNPRINTF_P -ATTR_SPRINTF -size_t St_xprintf (String *, const char *, ...); -#endif -void St_num (String *, double); -#if 0 -size_t St_write (const String *, FILE *); -#endif -size_t St_read (String *, FILE *, size_t); -#if 0 -int St_getline (String *, FILE *, int); -void St_getfile (String *, FILE *); -#endif -ATTR_PURE -size_t St_hash (const String *, size_t); - -#define St_ptr(s) (0 + (s)->buf) -#define St_len(s) (0 + (s)->length) -#define St_zero(s) ((void)((s)->buf[(s)->length = 0] = '\0')) -#define ST_LASTCHAR(s) ((s)->length ? (int)(unsigned char)(s)->buf[(s)->length - 1] : EOF) -#define ST_FIRSTCHAR(s) ((s)->length ? (int)(unsigned char)(s)->buf[0] : EOF) -#define ST_INDEX(s, n) ((size_t)(n) < (s)->length ? (int)(unsigned char)(s)->buf[(size_t)(n)] : EOF) -#define ST_WRITE(s, fp) fwrite((s)->buf, 1, (s)->length, fp) - -#endif /* STR_H_ */ diff -r b0f3e267bb1e -r ac0403686959 ploki/TODO --- a/ploki/TODO Fri Dec 20 22:15:50 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,29 +0,0 @@ -fix write/seek semantics for Z buffers - -add sort function - -add new value type: hashes -@TYPE OF hash : "hash" -- create: -@TAN list; # -@SIN list; # -@COS list; # -- decompose: -@ATAN hash; -> # -@ASIN hash; -> # -@ACOS hash; -> # -- push entries: -hash + hashmod -- pop entries: -hash - hashmod -- replace (-+) entries: -hash _ hashmod -- index: -hash . key -- slice: -hash . # -- remove: -hash % key -hash % # -- get depth: -hash / key diff -r b0f3e267bb1e -r ac0403686959 ploki/VERSION --- a/ploki/VERSION Fri Dec 20 22:15:50 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -0.6.5.1 diff -r b0f3e267bb1e -r ac0403686959 ploki/atechit.c --- a/ploki/atechit.c Fri Dec 20 22:15:50 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,27 +0,0 @@ -#include "zz.h" -#include "atechit.h" -#include "main.h" - -#include -#include - -enum {MAGIC = 23}; -static size_t Aused; -static void (*Afunc[MAGIC])(void); - -static void bah(void) { - for (; Aused; Afunc[--Aused]()) - ; -} - -void atechit(void (*f)(void)) { - if (Aused >= sizeof Afunc / sizeof *Afunc) { - NOTREACHED; - } - Afunc[Aused++] = f; - if (Aused == 1 && atexit(bah)) { - fprintf(stderr, "%s: atexit(): error message\n", Prog); - bah(); - exit(EXIT_FAILURE); - } -} diff -r b0f3e267bb1e -r ac0403686959 ploki/atechit.depend --- a/ploki/atechit.depend Fri Dec 20 22:15:50 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -atechit.o: atechit.c zz.h config.h atechit.h main.h diff -r b0f3e267bb1e -r ac0403686959 ploki/atechit.h --- a/ploki/atechit.h Fri Dec 20 22:15:50 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,6 +0,0 @@ -#ifndef ATECHIT_H_ -#define ATECHIT_H_ - -void atechit(void (*)(void)); - -#endif /* ATECHIT_H_ */ diff -r b0f3e267bb1e -r ac0403686959 ploki/compile.c --- a/ploki/compile.c Fri Dec 20 22:15:50 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,171 +0,0 @@ -#include "config.h" -#include "Str.h" -#include "compile.h" -#include "expr.h" -#include "main_var.h" -#include "op.h" -#include "text.h" -#include "zz.h" - -#include - -static struct op *end_if(struct text *code, size_t *n) { - for (; *n < code->length; ++*n) { - struct op *const p = code->start[*n]; - switch (p->type) { - case OP_IF: - ++*n; - p->arh.op = code->start[*n]; - p->next = end_if(code, n); - break; - - case OP_ELSE: { - size_t tmp; - tmp = ++*n; - p->next = end_if(code, n); - p->type = OP_NOP; - if (tmp < code->length) { - return code->start[tmp]; - } - return p; - } - - case OP_FI: - p->type = OP_NOP; - return p; - - default: - break; - } - } - - return code->start[0]; -} - -static void resolve(struct expr *e) { - if (!e) { - return; - } - - switch (e->type) { - case literE: - break; - - case varE: - e->v.val = vr_data(Var_plain, e->v.tent); - assert(e->v.val != NULL); - break; - - case varhashE: - e->v.hash = vr_data(Var_hash, e->v.tent); - resolve(e->right); - break; - - case symbolE: - if (e->op == S_ARGV) { - resolve(e->right); - } - break; - - case unopE: - resolve(e->right); - break; - - case binopE: - resolve(e->left.expr); - resolve(e->right); - break; - - case listE: - resolve(e->right); - resolve(e->left.expr); - break; - } -} - -static void op_resolve(struct op *o) { - switch (o->type) { - case OP_NOP: - case OP_GOBACK: - case OP_GOTO: - case OP_HANG: - break; - - case OP_ASSIGN: - case OP_CALL_BACK: - case OP_MODIFY: - case OP_PRINT: - case OP_PUTC: - case OP_TEMP: - resolve(o->arh.expr); - resolve(o->arg); - break; - - case OP_CALL: - case OP_CALL_DYN: - case OP_CLOSE: - case OP_EXIT: - case OP_IF: - case OP_RETURN: - case OP_SYSTEM: - case OP_THROW: - resolve(o->arg); - break; - - default: - NOTREACHED; - break; - } -} - -void compile(struct text *code) { - size_t i; - - for (i = 0; i < code->length; ++i) { - struct op *const p = code->start[i]; - - op_getop(p); - - if (!p->next && p->type != OP_EXIT && i + 1 < code->length) { - p->next = code->start[i + 1]; - } - } - - if (!i) { - struct op tmp; - op_init(&tmp); - text_push(code, &tmp); - } - - vr_freeze(Var_plain); - vr_freeze(Var_hash); - - for (i = 0; i < code->length; ++i) { - struct op *const p = code->start[i]; - - switch (p->type) { - case OP_IF: - ++i; - p->arh.op = code->start[i]; - p->next = end_if(code, &i); - break; - - case OP_ELSE: - ++i; - p->next = end_if(code, &i); - p->type = OP_NOP; - break; - - case OP_FI: - p->type = OP_NOP; - break; - - default: - break; - } - } - - for (i = 0; i < code->length; ++i) { - op_resolve(code->start[i]); - } -} diff -r b0f3e267bb1e -r ac0403686959 ploki/compile.depend --- a/ploki/compile.depend Fri Dec 20 22:15:50 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,3 +0,0 @@ -compile.o: compile.c config.h Str.h compile.h text.h op.h IO.h expr.h \ - re.h stack.h xmalloc.h strhash.h val.h kork.h list.h sub.h variable.h \ - main_var.h zz.h diff -r b0f3e267bb1e -r ac0403686959 ploki/compile.h --- a/ploki/compile.h Fri Dec 20 22:15:50 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,8 +0,0 @@ -#ifndef COMPILE_H_ -#define COMPILE_H_ - -#include "text.h" - -void compile(struct text *); - -#endif /* COMPILE_H_ */ diff -r b0f3e267bb1e -r ac0403686959 ploki/config.h --- a/ploki/config.h Fri Dec 20 22:15:50 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,114 +0,0 @@ -#ifndef CONFIG_H_ -#define CONFIG_H_ - -#ifdef DEBUGGING - #define DEBUG(x) x - #define DEBUG_P 1 -#else - #define DEBUG(x) - #define DEBUG_P 0 - #define NDEBUG 1 -#endif - -#if defined _WIN32 || defined __WIN32__ || defined WIN32 - #define DIR_END "\\/" -#elif defined __unix__ || defined __linux__ || defined __unix || defined unix - #define DIR_END "/" -#else - /* XXX */ - #define DIR_END 0 -#endif - -#if defined __POSIX__ || defined __unix__ || defined __linux__ || defined __unix || defined unix - #define HAVE_SLEEP_P 1 - #define SLEEP_HEADER - #define DO_SLEEP pause() -#elif defined _WIN32 || defined __WIN32__ || defined WIN32 - #define HAVE_SLEEP_P 1 - #define SLEEP_HEADER - #define DO_SLEEP Sleep(INFINITE) -#else - #define HAVE_SLEEP_P 0 - #define DO_SLEEP ((void)0) -#endif - -#if defined __linux__ || defined __sun__ || defined __sun - #define HAVE_DEV_URANDOM_P 1 -#else - #define HAVE_DEV_URANDOM_P 0 -#endif - -#ifndef INC_PREFIX - #if defined __unix__ || defined __linux__ || defined __unix || defined unix - #define INC_PREFIX "/usr/local/lib/ploki/" - #else - #define INC_PREFIX 0 - #endif -#endif - -#define INC_PREFIX_LIST \ -{ \ - "", \ - INC_PREFIX, \ - 0 \ -} - -#if defined __STDC_VERSION__ && __STDC_VERSION__ + 0 >= 199901L - #define HAVE_C99_P 1 - #define HAVE_C99(x) x -#else - #define HAVE_C99_P 0 - #define HAVE_C99(x) -#endif - -#ifdef __GNUC__ - #define HAVE_GCC_P 1 - #define HAVE_GCC(x) x -#else - #define HAVE_GCC_P 0 - #define HAVE_GCC(x) -#endif - -#if HAVE_GCC_P - #if __GNUC__ == 2 && __GNUC_MINOR__ >= 7 || __GNUC__ > 2 - #define ATTR(x) __attribute__(x) - #else - #define ATTR(x) - #endif - - #if __GNUC__ == 2 && __GNUC_MINOR__ >= 96 || __GNUC__ > 2 - #define ATTR_MALLOC __attribute__((__malloc__)) - #define ATTR_PURE __attribute__((__pure__)) - #else - #define ATTR_MALLOC - #define ATTR_PURE - #endif -#else - #define ATTR(x) - #define ATTR_MALLOC - #define ATTR_PURE -#endif - -#define ATTR_UNUSED ATTR((__unused__)) -#define ATTR_NORETURN ATTR((__noreturn__)) -#define ATTR_SPRINTF ATTR((__format__(__printf__, 2, 3))) -#define ATTR_CONST ATTR((__const__)) - -#if HAVE_C99_P - #define HAVE_VSNPRINTF(x) x - #define HAVE_VSNPRINTF_P 1 -#elif defined __GLIBC__ - #include - #if defined __USE_BSD || defined __USE_ISOC99 || defined __USE_UNIX98 - #define HAVE_VSNPRINTF(x) x - #define HAVE_VSNPRINTF_P 1 - #else - #define HAVE_VSNPRINTF(x) - #define HAVE_VSNPRINTF_P 0 - #endif -#else - #define HAVE_VSNPRINTF(x) - #define HAVE_VSNPRINTF_P 0 -#endif - -#endif /* CONFIG_H_ */ diff -r b0f3e267bb1e -r ac0403686959 ploki/deparse.c --- a/ploki/deparse.c Fri Dec 20 22:15:50 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,688 +0,0 @@ -#include "config.h" -#include "Str.h" -#include "deparse.h" -#include "expr.h" -#include "main_io.h" -#include "main_label.h" -#include "text.h" -#include "val.h" -#include "venus.h" -#include "xmalloc.h" -#include "zz.h" - -#include -#include -#include - -#if HAVE_VSNPRINTF_P -#define MAKE_LABEL(s, c) \ -do { \ - static unsigned long seq__; \ - St_init(s); \ - St_xprintf(s, "%c%lu", c, seq__++); \ -} while (0) -#else -#define MAKE_LABEL(s, c) \ -do { \ - static unsigned long seq__; \ - St_init(s); \ - St_num(s, seq__++); \ - St_tac_c(s, c); \ -} while (0) -#endif - -ATTR_CONST -static int display(enum t_binop b) { - switch (b) { - case B_SPARK_SPOT: return '!'; - case B_DOUBLE_OH_SEVEN: return '%'; - case B_AMPERSAND: return '&'; - case B_SPARK: return '\''; - case B_SPLAT: return '*'; - case B_INTERSECTION: return '+'; - case B_TAIL: return ','; - case B_WORM: return '-'; - case B_SPOT: return '.'; - case B_SLAT: return '/'; - case B_TWO_SPOT: return ':'; - case B_HYBRID: return ';'; - case B_ANGLE: return '<'; - case B_HALF_MESH: return '='; - case B_RIGHT_ANGLE: return '>'; - case B_U_TURN: return '['; - case B_U_TURN_BACK: return ']'; - case B_SHARK_FIN: return '^'; - case B_FLATWORM: return '_'; - case B_BACKSPARK: return '`'; - case B_EMBRACE: return '{'; - case B_SPIKE: return '|'; - case B_BRACELET: return '}'; - case B_SQIGGLE: return '~'; - default: break; - } - NOTREACHED; -} - -static void dump_str(const String *s) { - size_t i; - - io_write_m(Out, "\"", 1); - for (i = 0; i < St_len(s); ++i) { - const unsigned char c = ST_INDEX(s, i); - if (c != '\\' && c != '"' && isprint(c)) { - io_write_m(Out, &c, 1); - } else { - io_write_m(Out, "\\", 1); - switch (c) { - case '"': - case '\\': io_write_m(Out, &c , 1); break; - case '\a': io_write_m(Out, "a", 1); break; - case '\b': io_write_m(Out, "b", 1); break; - case '\f': io_write_m(Out, "f", 1); break; - case '\n': io_write_m(Out, "n", 1); break; - case '\r': io_write_m(Out, "r", 1); break; - case '\t': io_write_m(Out, "t", 1); break; - case '\v': io_write_m(Out, "v", 1); break; - default: fprintf(io_fp(Out), "%03o", c); break; - } - } - } - io_write_m(Out, "\"", 1); -} - -static void dump_ko(const struct kork *k) { - String tmp; - St_fake(&tmp, (char *)ko_ptr(k), ko_length(k)); - dump_str(&tmp); -} - -static void to_id(struct kork *k, size_t n) { - static char id[] = - "abcdefghijklmnopqrstuvwxyz" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "$" - ; - const size_t base = sizeof id - 1; - do { - ko_cat_c(k, id[n % base]); - n /= base; - } while (n); -} - -static void make_var(struct val *v) { - static size_t seq; - - ko_decouple(v->ko); - to_id(v->ko, seq++); - v->type = V_STR_K; -} - -static void dump_expr(const struct expr *e, int inlist) { - assert(e != NULL); - - switch (e->type) { - case literE: - if (V_EXT_P(e->v.val)) { - fprintf(io_fp(Out), "?%s?", io_name(e->v.val->magic.ext, NULL)); - } else if (V_SUB_P(e->v.val)) { - fprintf(io_fp(Out), "?CODE(%p)?", (void *)e->v.val->magic.sub); - } else if (V_STR_P(e->v.val)) { - dump_ko(e->v.val->ko); - } else if (V_NUM_P(e->v.val)) { - if (e->v.val->num < 0.0) { - fprintf(io_fp(Out), "@NEG %g", -e->v.val->num); - } else { - fprintf(io_fp(Out), "%g", e->v.val->num); - } - } else { - #if 0 - io_write_m(Out, "()", 2); - #endif - } - break; - - case varE: - if (!V_STR_P(e->v.val)) { - assert(e->v.val->type == V_UNDEF); - make_var(e->v.val); - } - io_write_m(Out, ko_ptr(e->v.val->ko), ko_length(e->v.val->ko)); - break; - - case varhashE: { - struct val *tmp; - if (!(tmp = sh_get(e->v.hash, "name", 4))) { - tmp = v_undef(); - make_var(tmp); - sh_put(e->v.hash, "name", 4, tmp); - } - io_write_m(Out, ko_ptr(tmp->ko), ko_length(tmp->ko)); - io_write_m(Out, "(", 1); - dump_expr(e->right, 0); - io_write_m(Out, ")", 1); - break; - } - - case symbolE: - io_write_m(Out, "\\", 1); - switch (e->op) { - case S_NUL: break; - case S_ARG: io_write_m(Out, "@", 1); break; - case S_ARGC: io_write_m(Out, "ARG", 3); break; - - case S_ARGV: - io_write_m(Out, "ARG:", 4); - if (e->right->type == binopE) { - io_write_m(Out, "(", 1); - dump_expr(e->right, 0); - io_write_m(Out, ")", 1); - } else { - dump_expr(e->right, 0); - } - break; - - case S_ERR: io_write_m(Out, "!", 1); break; - case S_EULER: io_write_m(Out, "E", 1); break; - case S_LUDOLF: io_write_m(Out, "PI", 2); break; - case S_MATCH: - fprintf(io_fp(Out), "%lu", (unsigned long)e->left.bonus); - break; - case S_RAND: io_write_m(Out, "?", 1); break; - case S_RESULT: io_write_m(Out, "_", 1); break; - case S_STDIN: io_write_m(Out, "EING", 4); break; - case S_STDOUT: io_write_m(Out, "AUSG", 4); break; - case S_STDERR: io_write_m(Out, "FEHL", 4); break; - - default: NOTREACHED; break; - } - break; - - case unopE: - switch (e->op) { - case F_EXP: io_write_m(Out, "\\E^", 3); break; - case F_LOWER: io_write_m(Out, "\\L" , 2); break; - case F_QUOTE: io_write_m(Out, "\\Q" , 2); break; - case F_RE_ESC: io_write_m(Out, "\\R" , 2); break; - case F_UPPER: io_write_m(Out, "\\U" , 2); break; - case F_MATCH: { - String tmp; - io_write_m(Out, "(", 1); - dump_expr(e->right, 0); - io_write_m(Out, " ~ ", 3); - St_init(&tmp); - re_decompile(e->left.rx, &tmp); - dump_str(&tmp); - St_clear(&tmp); - io_write_m(Out, ")", 1); - return; - } - - default: - io_write_m(Out, "@", 1); - switch (e->op) { - case F_CALL: io_write(Out, &e->left.op->txt); - case F_NUL: break; - case F_ABS: io_write_m(Out, "ABS", 3); break; - case F_ACOS: io_write_m(Out, "ACOS", 4); break; - case F_ASIN: io_write_m(Out, "ASIN", 4); break; - case F_ATAN: io_write_m(Out, "ATAN", 4); break; - case F_ATAN2: io_write_m(Out, "ATAN2", 5); break; - case F_CATCH: io_write_m(Out, "EVAL", 4); break; - case F_CHR: io_write_m(Out, "CHR", 3); break; - case F_COS: io_write_m(Out, "COS", 3); break; - case F_DEFINED: io_write_m(Out, "DEF-P", 5); break; - case F_EOF: io_write_m(Out, "EDD-P", 5); break; - case F_ERROR: io_write_m(Out, "ERR-P", 5); break; - case F_FREEZE: io_write_m(Out, "OMFG", 4); break; - case F_GETC: io_write_m(Out, "GET", 3); break; - case F_GETENV: io_write_m(Out, "ENV", 3); break; - case F_GETS: io_write_m(Out, "LEGS", 4); break; - case F_HANG: { - size_t i; - String tmp; - - St_init(&tmp); - for (St_num(&tmp, i = 1); - ve_findnext(&Venus, &tmp, 0); - St_num(&tmp, ++i)) - ; - fprintf(io_fp(Out), "(%s)", St_ptr(&tmp)); - St_clear(&tmp); - break; - } - case F_INT: io_write_m(Out, "INT", 3); break; - case F_IO: io_write_m(Out, "IO-P", 4); break; - case F_LENGTH: io_write_m(Out, "LENGTH", 6); break; - case F_LOG10: io_write_m(Out, "LG", 2); break; - case F_LOG: io_write_m(Out, "LN", 2); break; - case F_MOEND: io_write_m(Out, "+", 1); break; - case F_MOSTART: io_write_m(Out, "-", 1); break; - case F_NEG: io_write_m(Out, "NEG", 3); break; - case F_NOT: io_write_m(Out, "NOT", 3); break; - case F_NUM: io_write_m(Out, "NUM", 3); break; - case F_OPEN: io_write_m(Out, "APERS", 5); break; - case F_OPENR: io_write_m(Out, "LAPERS", 6); break; - case F_OPENW: io_write_m(Out, "SAPERS", 6); break; - case F_ORD: io_write_m(Out, "ORD", 3); break; - case F_REMOVE: io_write_m(Out, "REMOVE", 6); break; - case F_RENAME: io_write_m(Out, "RENAEM", 6); break; - case F_REVERSE: io_write_m(Out, "REVERSE", 7); break; - case F_SEEK: io_write_m(Out, "SUCH", 4); break; - case F_SIN: io_write_m(Out, "SIN", 3); break; - case F_SQRT: io_write_m(Out, "SPQR", 4); break; - case F_STR: io_write_m(Out, "STR", 3); break; - case F_TAN: io_write_m(Out, "TAN", 3); break; - case F_TELL: io_write_m(Out, "SAG", 3); break; - case F_TYPEOF: io_write_m(Out, "TYPE OF", 7); break; - default: - NOTREACHED; - break; - } - break; - } - if (e->right->type != unopE || e->op == F_NUL) { - io_write_m(Out, "(", 1); - dump_expr(e->right, 0); - io_write_m(Out, ")", 1); - } else { - io_write_m(Out, " ", 1); - dump_expr(e->right, 0); - } - break; - - case binopE: - dump_expr(e->left.expr, 0); - - if (e->op == B_XMATCH) { - io_write_m(Out, " ?o~ ", 5); - } else { - fprintf(io_fp(Out), "%s%c ", &" "[e->op == B_TAIL], display(e->op)); - } - - if (e->right->type == binopE) { - io_write_m(Out, "(", 1); - dump_expr(e->right, 0); - io_write_m(Out, ")", 1); - } else { - dump_expr(e->right, 0); - } - break; - - case listE: - if (!inlist) { - io_write_m(Out, "#<", 2); - } - if (e->right) { - io_write_m(Out, "(", 1); - dump_expr(e->right, 0); - io_write_m(Out, ")", 1); - if (e->left.expr) { - io_write_m(Out, " ", 1); - dump_expr(e->left.expr, 1); - } - } - if (!inlist) { - io_write_m(Out, "#>", 2); - } - break; - - } -} - -static void dump_op(struct op *op) { - switch (op->type) { - case OP_NOP: - io_write_m(Out, "REM", 3); - break; - - case OP_SET_VAL: - case OP_ASSIGN: - io_write_m(Out, "LET ", 4); - if (op->arh.expr) { - dump_expr(op->arh.expr, 0); - io_write_m(Out, " ", 1); - dump_expr(op->arg, 0); - } else { - io_write_m(Out, "(", 1); - dump_expr(op->arg, 0); - io_write_m(Out, ")", 1); - } - break; - - case OP_CALL: - if (op->arh.op) { - fprintf(io_fp(Out), "%s ", St_ptr(&op->arh.op->txt)); - dump_expr(op->arg, 0); - } else { - io_write_m(Out, "LET () ", 7); - dump_expr(op->arg, 0); - if (op->next) { - io_write_m(Out, "\nEND", 4); - } - } - break; - - case OP_CALL_BACK: - io_write_m(Out, "ABRUF (", 7); - dump_expr(op->arh.expr, 0); - io_write_m(Out, ") ", 2); - dump_expr(op->arg, 0); - break; - - case OP_CALL_DYN: - io_write_m(Out, "ANRUF ", 6); - dump_expr(op->arg, 0); - break; - - case OP_CLOSE: - io_write_m(Out, "CLAUDS ", 7); - dump_expr(op->arg, 0); - break; - - case OP_ELSE: - NOTREACHED; - break; - - case OP_EXIT: - if (op->arg->type == literE && !op->arg->v.val->type) { - io_write_m(Out, "END", 3); - } else { - io_write_m(Out, "END ", 4); - dump_expr(op->arg, 0); - } - break; - - case OP_FI: - NOTREACHED; - break; - - case OP_FLUSH: - io_write_m(Out, "FLUSH ", 6); - dump_expr(op->arg, 0); - break; - - case OP_GOBACK: - io_write_m(Out, "GOFOR ", 6); - dump_expr(op->arg, 0); - break; - - case OP_GOTO: - io_write_m(Out, "GOTO ", 5); - dump_expr(op->arg, 0); - break; - - case OP_HANG: - if (St_ptr(&op->txt)) { - fprintf(io_fp(Out), "NEXT %s", St_ptr(&op->txt)); - } else { - String tmp; - MAKE_LABEL(&tmp, '*'); - fprintf(io_fp(Out), "FOR %s NEXT %s", St_ptr(&tmp), St_ptr(&tmp)); - St_clear(&tmp); - } - break; - - case OP_IF: - if (op->arg->type == unopE && op->arg->op == F_NOT) { - io_write_m(Out, "IF ", 3); - dump_expr(op->arg->right, 0); - } else { - io_write_m(Out, "IF @NOT ", 8); - if (op->arg->type == binopE) { - io_write_m(Out, "(", 1); - dump_expr(op->arg, 0); - io_write_m(Out, ")", 1); - } else { - dump_expr(op->arg, 0); - } - } - - if (op->next) { - fprintf(io_fp(Out), "\n NEXT %s\nFI", St_ptr(&op->next->txt)); - } else { - io_write_m(Out, "\n END\nFI", 9); - } - break; - - case OP_MODIFY: - io_write_m(Out, "LET ", 4); - dump_expr(op->arh.expr, 0); - if (op->arh.expr->op == B_XMATCH) { - io_write_m(Out, " ?o~= ", 6); - } else { - fprintf(io_fp(Out), " %c= ", display(op->arh.expr->op)); - } - dump_expr(op->arg, 0); - break; - - case OP_PRINT: - io_write_m(Out, "WUNT ", 5); - if (op->arh.expr) { - dump_expr(op->arh.expr, 0); - io_write_m(Out, " ", 1); - dump_expr(op->arg, 0); - } else if (op->arg->type != binopE) { - dump_expr(op->arg, 0); - } else { - io_write_m(Out, "(", 1); - dump_expr(op->arg, 0); - io_write_m(Out, ")", 1); - } - break; - - case OP_PUTC: - io_write_m(Out, "SET ", 4); - dump_expr(op->arg, 0); - break; - - case OP_RESET: - io_write_m(Out, "RESET ", 6); - dump_expr(op->arg, 0); - break; - - case OP_RETURN: - io_write_m(Out, "KTHX ", 5); - dump_expr(op->arg, 0); - break; - - case OP_SYSTEM: - io_write_m(Out, "# ", 2); - dump_expr(op->arg, 0); - break; - - case OP_TEMP: - io_write_m(Out, "LEET ", 5); - dump_expr(op->arh.expr, 0); - io_write_m(Out, " ", 1); - dump_expr(op->arg, 0); - break; - - case OP_THROW: - io_write_m(Out, "IACS ", 5); - dump_expr(op->arg, 0); - break; - - default: - NOTREACHED; - break; - } -} - -static struct { - size_t length; - size_t size; - struct op **ops; -} seen; - -#define PUSH_SEEN(p) do { \ - if (seen.length >= seen.size) { \ - seen.ops = xrealloc(seen.ops, seen.size *= 2); \ - } \ - seen.ops[seen.length++] = (p); \ -} while (0) - -static int walk(const struct expr *e) { - for (;;) { - switch (e->type) { - case literE: - case symbolE: - return 0; - - case varE: - return 0; - - case varhashE: - e = e->right; - continue; - - case unopE: - if (e->op == F_NUL) { - return 1; - } else if (e->op == F_CALL && !St_ptr(&e->left.op->txt)) { - MAKE_LABEL(&e->left.op->txt, 'f'); - PUSH_SEEN(e->left.op); - } - e = e->right; - continue; - - case binopE: - return walk(e->left.expr) | walk(e->right); - - case listE: - return e->right && (walk(e->right) | (e->left.expr && walk(e->left.expr))); - } - } -} - -static void do_stuff(struct op *op) { - for (; op && !op->line; op = op->next) { - op->line = 1; - - if (St_ptr(&op->txt)) { - fprintf(io_fp(Out), "FOR %s ", St_ptr(&op->txt)); - if (op->type != OP_NOP) { - dump_op(op); - } else { - io_write_m(Out, "REM", 4); - } - io_write_m(Out, "\n", 1); - } else { - dump_op(op); - #if 0 - if (op->type != OP_NOP) - #endif - io_write_m(Out, "\n", 1); - } - - if (op->type == OP_IF) { - if (op->arh.op) { - if (op->arh.op->line) { - fprintf(io_fp(Out), "NEXT %s\n", St_ptr(&op->arh.op->txt)); - } else { - do_stuff(op->arh.op); - } - } - } else if (op->type == OP_CALL) { - if (op->arh.op && !op->arh.op->line) { - PUSH_SEEN(op->arh.op); - } - } else if (op->type == OP_HANG) { - return; - } - - if (op->next) { - if (op->next->line && op->type != OP_IF && op->type != OP_RETURN) { - fprintf(io_fp(Out), "NEXT %s\n", St_ptr(&op->next->txt)); - } - } else if ( - op->type != OP_EXIT && - op->type != OP_CALL && - op->type != OP_CALL_BACK && - op->type != OP_CALL_DYN && - op->type != OP_HANG && - op->type != OP_RETURN - ) { - io_write_m(Out, "END\n", 4); - } - } -} - -enum {MAGIC = 23}; -void deparse(const struct text *t) { - size_t i; - int computed_jumps; - - computed_jumps = 0; - seen.ops = xmalloc(seen.size = MAGIC, sizeof *seen.ops); - seen.length = 0; - - for (i = 0; i < t->length; ++i) { - struct op *op = t->start[i]; - - op->line = 0; - - if ( - op->type == OP_GOBACK || - op->type == OP_GOTO || - op->type == OP_CALL_DYN - ) { - walk(op->arg); - computed_jumps |= 1; - } else if (op->type == OP_CALL_BACK) { - walk(op->arh.expr); - walk(op->arg); - computed_jumps |= 1; - } else if (OP_1ARG_P(op->type)) { - computed_jumps |= walk(op->arg); - if (op->type == OP_CALL && op->arh.op) { - if (!St_ptr(&op->arh.op->txt)) { - MAKE_LABEL(&op->arh.op->txt, 'c'); - } - } - } else if (OP_2ARG_P(op->type)) { - if (op->arh.expr) { - computed_jumps |= walk(op->arh.expr); - } - computed_jumps |= walk(op->arg); - } - - if ( - op->next && - (op->next != t->start[i + 1] || op->type == OP_CALL) && - !St_ptr(&op->next->txt) - ) { - MAKE_LABEL(&op->next->txt, 'x'); - } - } - - if (!computed_jumps) { - do_stuff(t->start[0]); - for (i = 0; i < seen.length; ++i) { - do_stuff(seen.ops[i]); - } - } else { - for (i = 0; i < t->length; ++i) { - struct op *op = t->start[i]; - const String *tmp; - - if ((tmp = ve_label(&Venus, op))) { - if (St_ptr(&op->txt)) { - fprintf(io_fp(Out), " FOR %s\n", St_ptr(&op->txt)); - } - fprintf(io_fp(Out), "%s%s", St_ptr(tmp), &" "[!St_len(tmp)]); - } else { - if (St_ptr(&op->txt)) { - fprintf(io_fp(Out), "FOR %s ", St_ptr(&op->txt)); - } - } - - dump_op(op); - io_write_m(Out, "\n", 1); - - if (op->next && op->next != t->start[i + 1] && op->type != OP_IF) { - fprintf(io_fp(Out), "NEXT %s\n", St_ptr(&op->next->txt)); - } - } - } - xfree(seen.ops); -} diff -r b0f3e267bb1e -r ac0403686959 ploki/deparse.depend --- a/ploki/deparse.depend Fri Dec 20 22:15:50 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,3 +0,0 @@ -deparse.o: deparse.c config.h Str.h deparse.h text.h op.h IO.h expr.h \ - re.h stack.h xmalloc.h strhash.h val.h kork.h list.h sub.h variable.h \ - main_io.h main_label.h mars.h venus.h hash.h zz.h diff -r b0f3e267bb1e -r ac0403686959 ploki/deparse.h --- a/ploki/deparse.h Fri Dec 20 22:15:50 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,8 +0,0 @@ -#ifndef DEPARSE_H_ -#define DEPARSE_H_ - -#include "text.h" - -void deparse(const struct text *); - -#endif /* DEPARSE_H_ */ diff -r b0f3e267bb1e -r ac0403686959 ploki/doc/perl-ploki-regex.txt --- a/ploki/doc/perl-ploki-regex.txt Fri Dec 20 22:15:50 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,157 +0,0 @@ -COMPARISON BETWEEN PERL AND PLOKI REGEXES - - -General: - -ploki doesn't support perl's /cgimosx regex switches: -/c and /g don't make sense in ploki (strings don't have an associated pos()); -/i can be emulated by writing \L str ~ "lowercaseregex"; -/m and /s aren't needed (^, A!, $, Z! match at beginning-of-string, -beginning-of-line, end-of-string, end-of-line respectively; . matches any -character (use '^\n' if you want any character except newline)); -/o is mostly equivalent to ploki's ?o~ operator; -/x is the default in ploki: comments and whitespace are mostly ignored -(except when it's escaped, in a character class, in a quantifier (like "*?" -or ":2,:") or inside "^]", "&]", "?]" and "[number]"). - - - -[Perl] [ploki] - - -Escaping: - -\x x! -No word character is special. No escaped non-word character is special. - - -Alternation: - -foo|bar foo|bar - - -Repetition: - -x* x* -x+ x+ -x? x? -x*? x*? -x+? x+? -x?? x?? -x{n} x:n: -x{n,} x:n,: -x{n,m} x:n,m: - - -Grouping: - -(?:foo) (foo) - - -Character classes: - -[ab0-9\-\]'] 'ab0-9-!]'!' -[^a-z] '^a-z' -. '^\n' -. (with /s) . -[[:alnum:]] '[:alnum:]' - -Ploki provides the following POSIXish character classes (inside an ordinary -character class only): -[:alnum:] alphanumeric char -[:alpha:] alphabetic char -[:cntrl:] control char -[:digit:] digit -[:graph:] printable char (except space) -[:lower:] lowercase char -[:print:] printable char (including space) -[:punct:] punctuation char ([:graph:] without [:alnum:]) -[:space:] whitespace char -[:upper:] uppercase char -[:xdigit:] hex digit -Every POSIXish subclass [:foo:] can be negated by writing [:^foo:] (this is -compatible with Perl). - -In addition there are the following built-in character classes (inside and -outside of user-defined character classes): - - q! (equivalent to '[:alpha:]') - c! (equivalent to '[:cntrl:]') -\d d! (equivalent to '[:digit:]') - l! (equivalent to '[:lower:]') - p! (equivalent to '[:print:]') -\s s! (equivalent to '[:space:]') [1] - u! (equivalent to '[:upper:]') - x! (equivalent to '[:xdigit:]') -\w w! (equivalent to '_[:alnum:]') - -They can be negated by using the corresponding uppercase letter, e.g. D! -matches a non-digit character. - -[1] Perl's \s does not include \v (vertical tab). - - -Independent groups: - -(?>foo) - - -Capturing: - -(foo) {foo} - - -Backreferences: - -\1, \2, ... 0!, 1!, ... - -Note that ploki's backreferences start with 0 *counting from the right*, -i.e. after a successful match against "{{f}o{o}}", \0 is "foo", \1 is "o" -and \2 is "f". - - -Assertions: - -^ (without /m) ^ -\A ^ -^ (with /m) A! -\z $ -$ (with /m) Z! (roughly) -\b b! -\B B! -(?=foo) [foo&] -(?!foo) [foo^] -(?<=foo) NOT IMPLEMENTED -(? and undef are false. Any other value is true. - - -Conversions - -Conversions happen as follows: - * number -> string: decimal floating point, the "normal" way - * IO handle -> string: filename - * list -> string: concatenation of the stringified list elements - * bound expression -> string: undefined results - * undef -> string: "" (the empty string) - - * string -> number: initial whitespace is skipped, the rest of the string - is interpreted as a decimal floating point number - (as done by strtod()) - * IO handle -> number: undefined results - * list -> number: sum of the numified list elements - * bound expression -> number: undefined results - * undef -> number: 0.0 - - -Constructors - -Anything starting with a digit or a period followed by a digit is taken to -be the beginning of a number (again, as parsed by strtod(), which means that -scientific notation (e.g. 1e-23) is allowed). String literals start with '"' -and end with '"' or end-of-line. The following escape sequences are -recognized: - - \OOO octal char (where OOO matches [0-7]{1,3}) - \xXX hex char (where XX matches [0-9a-fA-F]{1,2}) - \a alarm - \b backspace - \f form feed - \n newline - \r carriage return - \t tab - \v vertical tab - - \cX the character resulting from (toupper(X) + 64) % 128 - (this depends on the character set used) - - \V - The stringified result of evaluating . In other words, this - can be used to interpolate expressions in strings. - Example: "2 + 2 = \V(2 + 2)" yields "2 + 2 = 4". - - -Lists start with #< and end with #> or end-of-line. Example: #<2 +1 (3 +4)#> -is equivalent to #<2 1 7#>. - -The empty value yields undef: `-2' is parsed as ` - 2', undef is converted -to 0.0, and the result is 0.0 - 2.0, which is -2. - - -Special symbols - -There are the following special symbols, similar to Perl's punctuation -variables: - - * \N (where N is a non-empty sequence of digits) - Contains the substring that was matched by the Nth capturing group in - the previous pattern match. - * \! - If used numerically, yields the current value of C's errno variable, or - in other words, if a system or library call fails, it sets this - variable. If used as a string, yields the corresponding system error - string (i.e. strerror(errno)). - * \? - Yields a random number in the range [0,1). - * \_ - Contains the return value of the last command. - * \@ - The argument the current function was called with. - * \ARG - Number of command line options (C's argc). - * \AUSG - The standard output IO handle (open mode: "WF"). - * \EING - The standard input IO handle (open mode: "RZ"). - * \E - Euler's number: 2.718281828... - * \FEHL - The standard error IO handle (open mode: "WF"). - * \PI - pi: 3.14159265... - * \ - undef - - -VARIABLES - -There are two kinds of variables: plain variables and subscripted variables. -A plain variable has the form [A-Za-z$]+, i.e. one or more alphabetic -characters (where '$' is considered alphabetic). A subscripted variable -looks like a plain variable immediately followed by an opening paren "(", an -expression (the result of which is converted to a string) and possibly a -closing paren ")". - - -UNARY OPERATORS - -Each unary operator takes a value and returns a value. The only exception is -@OMFG, which is more a constructor than an operator. - - * \ARG: - Takes an integer and returns the corresponding command line argument - (like C's argv[]). - * \L - Returns a lowercased version of its operand (like Perl's lc). - * \Q - Returns its operand with all non-word characters backslashed, i.e. any - character not matching [a-zA-Z0-9_] will be preceded by a \ in the - returned string (like Perl's quotemeta). - * \R - Returns its operand with all non-word characters regex-escaped, i.e. any - character not matching [a-zA-Z0-9_] will be followed by a ! in the - returned string (like s/(\W)/$1!/g in Perl). - * \U - Returns an uppercased version of its operand (like Perl's uc). - * @+ - For an operand N, returns the offset of the character after the portion - of the string matched by the Nth capturing group in the last pattern - matching. - * @- - For an operand N, returns the offset of the portion of the string - matched by the Nth capturing group in the last pattern matching. - * @ABS - Returns the absolute value of its operand. - * @ACOS - Returns the arc cosine of its operand. - * @APERS - Takes a list of two elements, a filename and a mode string. Opens the - specified file and returns an IO handle or undef on error. If the mode - string contains "A", the file is created if necessary and opened for - appending (i.e. every write goes to the end of the file); if the mode - string contains "W", the file is truncated/created and opened for - writing; otherwise the file is opened for reading, and the open fails if - the file doesn't exist. If the mode string contains "+", reading is - added for "A" and "W", and writing otherwise. "R+" is almost always - preferred for read/write access; "W+" would clobber the file first. The - resulting IO handle defaults to text mode; add "B" to open the file in - binary mode. Adding "F" turns on autoflush mode, i.e. the stream is - flushed after every write. If the file is opened for reading and the - mode string contains "Z", the resulting stream can be used with the - regex match operator (~). - * @ASIN - Returns the arc sine of its operand. - * @ATAN - Returns the arc tangent of its operand. - * @ATAN2 - Takes a list of two elements # and returns the arc tangent of x - and y. It is similar to @ATAN (y / x), except that the signs of both - arguments are used to determine the quadrant of the result. - * @CHR - Takes an integer and returns the character represented by that number in - the character set. - * @COS - Returns the cosine of its operand. - * @DEF-P - Returns "" if its operand is undef, 1 otherwise. - * @EDD-P - Returns 1 if the end-of-file indicator for its operand (which must be an - IO handle) is set, "" otherwise. - * @ENV or \ENV - Returns the value of the specified environment variable. - * @ERR-P - Returns 1 if the error indicator for its operand (which must be an IO - handle) is set, "" otherwise. - * @EVAL - Evaluates and returns its operand. If an exception is thrown during the - evaluation of its operand, assigns the exception value to \_ and returns - undef. - * @GET - Takes an IO handle, reads one character, and returns its numeric value or - -1 on end-of-file or error. - * @INT - Returns the integer portion of its operand. - * @IO-P - Returns 1 if its operand is an IO handle, "" otherwise. - * @LAPERS - Takes a filename, opens it for reading, and returns the resulting IO - handle or undef on failure. Equivalent to @APERS #. - * @LEGS - Takes an IO handle, reads a line and returns it. Returns "" on - end-of-file and undef on error. - * @LENGTH - If passed a list, returns the number of elements; otherwise, returns the - length of its operand in characters. - * @LG - Returns the base-10 logarithm of its operand. - * @LN - Returns the natural logarithm (base e) of its operand. - * @NEG - Performs arithmetic negation. - * @NOT - Returns 1 if its operand is false, "" otherwise. - * @NUM - Converts its operand to a number. - * @ORD - Returns the numeric value of the first character of its operand. - * @OMFG - Does not evaluate its operand but wraps it up in a bound expression by - replacing all variables by their current value. - * @REMOVE - Tries to remove the specified file. Returns 1 for success, "" for - failure. - * @RENAEM - Takes a list of two filenames # and tries to rename OLD to - NEW. Returns 1 for success, "" for failure. - * @REVERSE - If passed a list, returns a list consisting of the elements of the - original list in the opposite order. Otherwise, returns a string with - all the characters in the opposite order. - * @SAG - Returns a value representing the current file position of its operand - (which must be an IO handle) suitable as the second argument of @SUCH. - If the file was opened in binary mode, this is the number of bytes from - the beginning of the file. - * @SAPERS - Takes a filename, opens it for writing, and returns the resulting IO - handle or undef on failure. The file is created if it doesn't exist and - truncated to length 0 otherwise. Equivalent to @APERS #. - * @SIN - Returns the sine of its operand. - * @SQRT - Returns the square root of its operand. - * @STR - Converts its operand to a string. - * @SUCH - Takes a list of two or three elements, #. WHENCE - defaults to 0 if omitted. Sets the current file position of IO (which - must be an IO handle) to POS (relative to WHENCE). WHENCE must be one of - 0 (beginning of the file), 1 (current position) or 2 (end of file). If - IO is in text mode, POS must be a value returned by @SAG and WHENCE must - be 0, or POS must be 0. If IO is in binary mode, the new position - (measured in bytes from the beginning of the file) is obtained by adding - POS to the position specified by WHENCE. In this case POS=0 and WHENCE=2 - may not work, depending on your C library. - * @TAN - Returns the tangent of its operand. - * @TYPE OF - Returns a string describing the type of its operand: "string" for - strings, "number" for numbers, "stream" for IO handles, "list" for - lists, "stuff" for bound expressions, "nothing" for undef. - * @