commit e168181e64907d62c30af2bb6feb0a82f13a0a33
parent 4601dd1827700297b525884f95f444c5ff252d0b
Author: rpa <rpa@laika>
Date: Sat, 22 Jul 2023 23:13:18 +0000
src/tablist: some fixes to encoder, decoder is still no good
Diffstat:
3 files changed, 28 insertions(+), 20 deletions(-)
diff --git a/src/tablist/example.tl b/src/tablist/example.tl
@@ -1,10 +1,9 @@
Uno
-Duo uno
-Tres
uno
- duo 1
- tres
-\A a
- \a a
- \b a
- \g a
+ 1
+Duo
+ duo
+ 2
+\Uno
+ \uno
+ \1
diff --git a/src/tablist/tablist.c b/src/tablist/tablist.c
@@ -65,10 +65,10 @@ TLdecode(TLdecoder *r, char *buf, usize count)
for (i = 0; i < count;) {
switch (r->state) {
case Start:
+ r->sp = 0;
r->state = Begin;
case Begin:
- r->sp = 0;
r->indent = 0;
if (buf[i] == '\t') r->state = Indent;
else r->stack[0] = r->nodes, r->state = Node;
@@ -85,18 +85,21 @@ TLdecode(TLdecoder *r, char *buf, usize count)
r->acc = sliceappendp(r->acc, 1, buf+i), i++;
break;
}
- else r->state = FlushName;
+ r->state = FlushName;
case FlushName:
new = allocTLnode();
new->name = slicecopy(r->acc);
sliceappendp(r->stack[r->sp], 1, &new);
- if (r->indent == r->sp + 1) r->sp++;
- else if (r->indent <= r->sp) r->sp = r->indent;
- else sysfatal("TLdecode: indent is no good");
+
+ print("indent %d\n", r->indent);
+
+ if (r->indent <= r->sp + 1) r->sp = r->indent;
+ else sysfatal("TLdecode/FlushName: indent is no good");
+
r->stack[r->sp] = new->children;
if (buf[i] == ' ') r->state = Node, i++;
- else if (buf[i] == '\n') r->state = Begin, i++;
+ else r->state = Begin, i++;
break;
case Value:
@@ -109,9 +112,10 @@ TLdecode(TLdecoder *r, char *buf, usize count)
new = allocTLnode();
new->value = slicecopy(r->acc);
sliceappendp(r->stack[r->sp], 1, &new);
- if (r->indent == r->sp + 1) r->sp++;
- else if (r->indent <= r->sp) r->sp = r->indent;
- else sysfatal("TLdecode: indent is no good");
+
+ if (r->indent <= r->sp + 1) r->sp = r->indent;
+ else sysfatal("TLdecode/FlushValue: indent is no good");
+
r->stack[r->sp] = new->children;
r->state = Begin, i++;
break;
@@ -188,11 +192,12 @@ void *
_encnext(TLencoder *w, char *c)
{
TLnode *n = ENCTOS(w);
+ TLnode *fc = nil;
+ if (n->children->len > 0) fc = *(TLnode **)slicegetp(n->children, 0);
if (n->children->len == 0) {
w->cs[w->sp]++;
return _encnl(w, c);
- } else if (n->children->len == 1) {
- w->sp++;
+ } else if ((n->children->len == 1) && (fc->name != nil)) {
w->ns[w->sp] = n->children;
w->cs[w->sp] = 0;
return _encsp(w, c);
@@ -245,7 +250,6 @@ _encend(TLencoder *, char *c)
int
TLencode(TLencoder *w, char *buf, usize count)
{
-
usize i;
for (i = 0; i < count; i++) {
if (w->state == _encend) return i;
diff --git a/src/tablist/test.c b/src/tablist/test.c
@@ -31,17 +31,22 @@ test(char *path)
char *buf = malloc(BufSize);
int fd = open(path, OREAD);
if (fd < 0) sysfatal("%r");
+
usize n = read(fd, buf, BufSize);
print("READ %d bytes\n", n);
write(1, buf, n);
+
+ print("DECODE\n");
TLdecoder *tld = initTLdecoder(path);
TLdecode(tld, buf, n);
print("DEBUGPRINT\n");
printnodes(tld->nodes, 0);
+
print("ENCODE\n");
TLencoder *tle = initTLencoder(tld->nodes);
n = TLencode(tle, buf, BufSize);
+
print("WRITE %d bytes\n", n);
write(1, buf, n);
}