aboutsummaryrefslogtreecommitdiff
path: root/sys/libbio/io/newick.c
diff options
context:
space:
mode:
Diffstat (limited to 'sys/libbio/io/newick.c')
-rw-r--r--sys/libbio/io/newick.c76
1 files changed, 40 insertions, 36 deletions
diff --git a/sys/libbio/io/newick.c b/sys/libbio/io/newick.c
index 82ea0d7..8b02446 100644
--- a/sys/libbio/io/newick.c
+++ b/sys/libbio/io/newick.c
@@ -65,20 +65,20 @@ tokstr(struct Token tok)
// TODO: Bounds checking on buffer
static
struct Token
-lex(io·Peeker s)
+lex(io·Peeker s, void* impl)
{
byte *c;
struct Token tok;
static byte b[1024];
c = b;
- *c = s.get();
+ *c = s.get(impl);
if (isspace(*c)) {
while (isspace(*c)) {
- *(++c) = s.get();
+ *(++c) = s.get(impl);
}
- s.unget(*c);
+ s.unget(impl, *c);
Assert(c - b < 1024);
*c = 0;
@@ -101,11 +101,11 @@ lex(io·Peeker s)
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
while (isdigit(*c)) {
- NUM: *(++c) = s.get();
+ NUM: *(++c) = s.get(impl);
}
if (*c == '.') goto NUM;
- s.unget(*c);
+ s.unget(impl, *c);
Assert(c - b < 1024);
*c = 0;
@@ -115,10 +115,10 @@ lex(io·Peeker s)
default:
while (isalnum(*c)) {
- *(++c) = s.get();
+ *(++c) = s.get(impl);
}
- s.unget(*c);
+ s.unget(impl, *c);
Assert(c - b < 1024);
*c = '\0';
@@ -130,12 +130,12 @@ lex(io·Peeker s)
static
struct Token
-lex_nospace(io·Peeker s)
+lex_nospace(io·Peeker s, void *impl)
{
struct Token tok;
- tok = lex(s);
+ tok = lex(s, impl);
if (tok.kind == tok·space) {
- tok = lex_nospace(s);
+ tok = lex_nospace(s, impl);
}
return tok;
@@ -147,7 +147,9 @@ struct Parser
bio·Node *root;
struct Token tok;
+ void *fimpl;
io·Peeker file;
+ void *himpl;
mem·Allocator heap;
};
@@ -162,7 +164,7 @@ parse(struct Parser *p)
node = p->root;
for (;;) {
- tok = lex_nospace(p->file);
+ tok = lex_nospace(p->file, p->fimpl);
switch (tok.kind) {
case tok·lparen:
@@ -171,7 +173,7 @@ parse(struct Parser *p)
goto ERROR;
}
- node = p->heap.alloc(sizeof(*node));
+ node = p->heap.alloc(p->himpl, 1, sizeof(*node));
memset(node, 0, sizeof(*node));
if (p->root) {
@@ -208,7 +210,7 @@ parse(struct Parser *p)
}
node->comment = str·new("");
while (tok.kind != tok·rbrak) {
- tok = lex_nospace(p->file);
+ tok = lex_nospace(p->file, p->fimpl);
if (tok.kind == tok·eof || tok.kind == tok·nil) {
errorf("incorrect format: unmatched comment bracket '['");
goto ERROR;
@@ -223,7 +225,7 @@ parse(struct Parser *p)
break;
case tok·colon:
- tok = lex_nospace(p->file);
+ tok = lex_nospace(p->file, p->fimpl);
if (tok.kind != tok·number) {
errorf("incorrect format: expected number after colon");
goto ERROR;
@@ -258,7 +260,7 @@ parse(struct Parser *p)
goto ERROR;
}
- node = p->heap.alloc(sizeof(*node));
+ node = p->heap.alloc(p->himpl, 1, sizeof(*node));
memset(node, 0, sizeof(*node));
node->name = str·new(tok.lit.s);
@@ -280,7 +282,7 @@ parse(struct Parser *p)
break;
case tok·semi:
- p->file.unget(';');
+ p->file.unget(p->fimpl, ';');
if (p->lev) {
errorf("format error: uneven number of parentheses found at ';'");
goto ERROR;
@@ -307,18 +309,20 @@ ERROR:
}
bio·Tree
-bio·readnewick(io·Peeker stream, mem·Allocator heap)
+bio·readnewick(io·Peeker stream, void *si, mem·Allocator heap, void *hi)
{
error err;
struct Parser p;
bio·Tree tree;
p = (struct Parser){
- .lev = 0,
- .root = nil,
- .tok = (struct Token){ 0 },
- .file = stream,
- .heap = heap,
+ .lev = 0,
+ .root = nil,
+ .tok = (struct Token){ 0 },
+ .fimpl = si,
+ .file = stream,
+ .himpl = hi,
+ .heap = heap,
};
err = parse(&p);
if (err) {
@@ -334,7 +338,7 @@ bio·readnewick(io·Peeker stream, mem·Allocator heap)
// Write
error
-dump(bio·Node *node, io·Putter out)
+dump(bio·Node *node, void *impl, io·Putter out)
{
byte b[24];
@@ -343,35 +347,35 @@ dump(bio·Node *node, io·Putter out)
}
bio·Node *child;
if (node->nchild) {
- out.put('(');
+ out.put(impl, '(');
- dump(node->child[0], out);
+ dump(node->child[0], impl, out);
for (child = node->child[1]; child != nil; child = child->sibling) {
- out.put(',');
- dump(child, out);
+ out.put(impl, ',');
+ dump(child, impl, out);
}
- out.put(')');
+ out.put(impl, ')');
}
if (node->name) {
- out.putstr(node->name);
+ out.putstr(impl, node->name);
}
if (node->parent) {
- out.put(':');
+ out.put(impl, ':');
snprintf(b, arrlen(b), "%f", node->dist);
- out.putstr(b);
+ out.putstr(impl, b);
}
return 0;
}
error
-bio·writenewick(bio·Tree tree, io·Putter out)
+bio·writenewick(bio·Tree tree, io·Putter out, void* impl)
{
- dump(tree.root, out);
- out.put(';');
- out.put('\n');
+ dump(tree.root, impl, out);
+ out.put(impl, ';');
+ out.put(impl, '\n');
return 0;
}