commit dcbb4baab5e50adb7ab0fcc821a6032dbcd34116
parent 18235b39b49c0dc17ab926563ee36369c7175e77
Author: Pavel Renev <an2qzavok@gmail.com>
Date: Thu, 15 Aug 2024 22:00:10 +0000
src/fltp: a small redesign
Diffstat:
4 files changed, 146 insertions(+), 156 deletions(-)
diff --git a/src/fltp/example b/src/fltp/example
@@ -1,10 +1,10 @@
-[uuid] {
-names {
-.l Cool Story Bro
-.l Everyone Clapped
+{ cool-story
+{ names
+.n Cool Story Bro
+.n Everyone Clapped
}
-tags .l fantasy romance drama
-type .l novel
-status .l ch 76
-extra .l not real
+{ tags fantasy romance drama }
+{ type novel }
+{ status ch 76 }
+{ extra not a real novel }
}
diff --git a/src/fltp/fltp.c b/src/fltp/fltp.c
@@ -7,166 +7,113 @@
#include "fltp.h"
-enum {
- StackSize = 64,
- TokenBufSize = 4096,
- WordSize = 16,
+FLTWord dict[] = {
+ {&dict[1], "{", fltpstartlist},
+ {&dict[2], "}", fltpendlist},
+ {nil, ".n", fltpgetline},
};
-typedef struct _context _context;
-typedef struct _stack _stack;
-typedef struct Word Word;
-
-struct _stack {
- int d;
- int i;
- FLTP * s[StackSize];
-};
-
-struct _context {
- void (*fp)(void *);
- char *buf;
- char *tbuf;
- _stack st;
- FLTP *fltp;
- long i;
- long nbytes; // in ref to buf
- int ti;
- Word *wp;
-};
-
-struct Word {
- void *prev;
- char str[WordSize];
- void (*fp)(void *);
-};
+FLTP *
+initfltp(void){
+ FLTP *new;
+ new = mallocz(sizeof(FLTP), 1);
+ new->fp = new->rs;
+ *(new->fp) = fltpmain; // TODO: should be actual func
+ new->tbuf = malloc(FLTTokenBufSize);
+ new->wp = dict;
+ return new;
+}
-Word * getword(Word *wp, char *buf, long nbytes);
-void _buildtoken(void *);
-void _buildline(void *);
-void _beginlist(void *);
-void _endlist(void *);
+void
+evalfltp(FLTP *fltp, char *buf, long nbytes){
+ fltp->bp = buf;
+ fltp->nbytes = nbytes;
+ (*fltp->fp)(fltp);
+}
-Word dict[] = {
- {nil, ".l", _buildline},
- {&(dict[0]), "{", _beginlist},
- {&(dict[1]), "}", _endlist},
-};
+FLTNode *
+createfltnode(FLTNode *prev, FLTNode *parent, char *buf, long nbytes)
+{
+ FLTNode *new;
+ new = malloc(sizeof(FLTNode) + nbytes);
+ new->prev = prev;
+ new->next = nil;
+ new->parent = parent;
+ new->nbytes = nbytes;
+ memcpy((char *)new + sizeof(FLTNode), buf, nbytes);
+ if(prev != nil) prev->next = new;
+ return new;
+}
-Word *
-getword(Word *wp, char *buf, long nbytes)
+FLTWord *
+getfltword(char *str, long nbytes, FLTWord *wp)
{
- for (; wp != nil; wp = wp->prev) {
- if (nbytes != strlen(wp->str)) continue;
- if (memcmp(buf, wp->str, nbytes) == 0) break;
+ if(nbytes >= FLTWordSize) return nil;
+ for(; wp != nil; wp = wp->prev){
+ if(strlen(wp->str) != nbytes) continue;
+ if(memcmp(wp->str, str, nbytes) == 0) return wp;
}
- return wp;
+ return nil;
}
void
-_buildtoken(void *v)
-{
- char c;
- _context *con;
- Word *wp;
-
- con = v;
- if (con->i >= con->nbytes) {
- con->fp = nil;
- return;
- }
- c = con->buf[con->i];
- con->i++;
- if (strchr(" \t\n", c) == nil) {
- con->tbuf[con->ti] = c;
- con->ti++;
- } else {
- con->fltp = createfltp(con->fltp, con->st.s[con->st.i], con->tbuf, con->ti);
- wp = getword(con->wp, con->tbuf, con->ti);
- if ((wp != nil) && (wp->fp != nil)) {
- con->fp = wp->fp;
+fltpmain(FLTP *fltp){
+ FLTWord *wp;
+ for(;;){
+ fltpgettoken(fltp);
+ if(fltp->ti > 0){
+ fltp->np = createfltnode(fltp->np, fltp->pnp, fltp->tbuf, fltp->ti);
+ wp = getfltword(fltp->tbuf, fltp->ti, fltp->wp);
+ if(wp != nil) wp->fp(fltp); // TODO: should utilise fltp->rs ???
}
- con->ti = 0;
+ if(fltp->nbytes == 0) break;
}
}
void
-_buildline(void *v)
-{
- char c;
- _context *con;
-
- con = v;
- if (con->i >= con->nbytes) {
- con->fp = nil;
- return;
- }
- c = con->buf[con->i];
- con->i++;
- if (c != '\n') {
- con->tbuf[con->ti] = c;
- con->ti++;
- } else {
- con->fltp = createfltp(con->fltp, con->fltp, con->tbuf, con->ti);
- con->fp = _buildtoken;
- con->ti = 0;
+fltpgettoken(FLTP *fltp){
+ int c;
+ fltp->ti = 0;
+ for(;;){
+ if(fltp->ti >= FLTTokenBufSize) sysfatal("fltp: token buffer overflow");
+ c = *fltp->bp;
+ fltp->bp++;
+ fltp->nbytes--;
+ if(strchr(" \t\n", c) != nil) break;
+ fltp->tbuf[fltp->ti] = c;
+ fltp->ti++;
+ if(fltp->nbytes == 0) sysfatal("fltp: input buffer overflow (unterminated token");
}
}
void
-_beginlist(void *v)
-{
- _context *con;
- con = v;
- con->fp = _buildtoken;
- con->st.i++;
- con->st.s[con->st.i] = con->fltp->prev;
- con->fltp->parent = con->fltp->prev;
+fltpstartlist(FLTP *fltp){
+ fltp->pnp = fltp->np;
}
void
-_endlist(void *v)
-{
- _context *con;
- con = v;
- con->fp = _buildtoken;
- con->st.i--;
+fltpendlist(FLTP *fltp){
+ fltp->pnp = fltp->pnp->parent;
}
-FLTP *
-createfltp(void *prev, void *parent, char *buf, long nbytes)
-{
- FLTP *new;
- new = malloc(sizeof(FLTP) + nbytes);
- *new = (FLTP){prev, parent, nbytes};
- memcpy((char *)new + sizeof(FLTP), buf, nbytes);
- return new;
-}
-
-FLTP *
-fltpparse(char *buf, long nbytes)
-{
- _stack st;
- _context con;
- char *tbuf;
- st.d = 1;
- st.i = 0;
- st.s[0] = nil;
- tbuf = malloc(TokenBufSize);
- memset(&con, 0, sizeof(con));
- con.fp = _buildtoken;
- con.buf = buf;
- con.tbuf = tbuf;
- con.st = st;
- con.nbytes = nbytes;
- con.wp = &(dict[2]);
- while (con.fp != nil) {
- if (con.ti >= TokenBufSize)
- sysfatal("fltpparse: token buf overflow");
- if (st.i >= StackSize)
- sysfatal("fltpparse: stack overflow");
- con.fp(&con);
+void
+fltpgetline(FLTP *fltp){
+ fprint(2,"getline\n");
+ FLTNode *new;
+ int c;
+ fltp->ti = 0;
+ for(;;){
+ if(fltp->ti >= FLTTokenBufSize) sysfatal("fltp: token buffer overflow");
+ c = *fltp->bp;
+ fltp->bp++;
+ fltp->nbytes--;
+ if(c == '\n') break;
+ fltp->tbuf[fltp->ti] = c;
+ fltp->ti++;
+ if(fltp->nbytes == 0) sysfatal("fltp: input buffer overflow (unterminated token");
+ }
+ if (fltp->ti > 0) {
+ new = createfltnode(fltp->np, fltp->np, fltp->tbuf, fltp->ti);
+ fltp->np = new;
}
- free(tbuf);
- return con.fltp;
}
diff --git a/src/fltp/fltp.h b/src/fltp/fltp.h
@@ -1,10 +1,47 @@
+enum{
+ FLTStackSize = 64,
+ FLTTokenBufSize = 4096,
+ FLTWordSize = 16,
+};
+
+typedef struct FLTWord FLTWord;
+typedef struct FLTNode FLTNode;
typedef struct FLTP FLTP;
-struct FLTP {
+struct FLTWord{
void *prev;
- void *parent;
+ char str[FLTWordSize];
+ void (*fp)(FLTP *);
+};
+
+struct FLTNode{
+ FLTNode *prev;
+ FLTNode *next;
+ FLTNode *parent;
long nbytes;
};
-FLTP * createfltp(void *prev, void *parent, char *buf, long nbytes);
-FLTP * fltpparse(char *buf, long nbytes);
+struct FLTP{
+ void (*rs[FLTStackSize])(FLTP *);
+ void (**fp)(FLTP *);
+ int st[FLTStackSize];
+ int *sp;
+ FLTWord *wp;
+ FLTNode *np;
+ FLTNode *pnp;
+ char *bp;
+ char *tbuf;
+ long nbytes;
+ int ti;
+};
+
+FLTP * initfltp(void);
+void evalfltp(FLTP *, char *buf, long nbytes);
+FLTNode * createfltnode(FLTNode *prev, FLTNode *parent, char *buf, long nbytes);
+FLTWord * getfltword(char *str, long nbytes, FLTWord *wp);
+
+void fltpmain(FLTP *);
+void fltpgettoken(FLTP *);
+void fltpstartlist(FLTP *);
+void fltpendlist(FLTP *);
+void fltpgetline(FLTP *);
diff --git a/src/fltp/test.c b/src/fltp/test.c
@@ -2,14 +2,20 @@
#include <libc.h>
#include "fltp.h"
+enum{
+ BufSize = 8192,
+};
+
void
-main(void)
-{
- char *b = malloc(4096);
- long n = read(0, b, 4096);
- FLTP *f = fltpparse(b, n);
- for (; f != nil; f = f->prev) {
+main(void){
+ char *b = malloc(BufSize);
+ long n = read(0, b, BufSize);
+ FLTP *f = initfltp();
+ evalfltp(f, b, n);
+ FLTNode *N = f->np;
+ while(N->prev != nil) N = N->prev;
+ for(; N != nil; N = N->next) {
fprint(2, "%.11p %.11p %.*s\n",
- f, f->parent, f->nbytes, (char*)f + sizeof(FLTP));
+ N, N->parent, N->nbytes, (char*)N + sizeof(FLTNode));
}
}