protoverse

A metaverse protocol
git clone git://jb55.com/protoverse
Log | Files | Refs | README | LICENSE

commit 407da600814524ae5a4dc502d569323b0c09a32c
parent d6e45e4301acfcacdba728dd1ec2868d0a8a8b9b
Author: William Casarin <jb55@jb55.com>
Date:   Thu,  2 Jul 2020 08:17:57 -0700

print scene tree description

Diffstat:
MMakefile | 3+++
Mparse.c | 31++++++++++++++++++++++---------
Mparse.h | 1+
Mprotoverse.c | 62+++++++++++++++++++++++++++++++++++++++++++++-----------------
Msatoshis-citadel.space | 19+++++++++----------
5 files changed, 80 insertions(+), 36 deletions(-)

diff --git a/Makefile b/Makefile @@ -6,6 +6,9 @@ OBJS = io.o parse.o protoverse: protoverse.c $(OBJS) $(CC) $(CFLAGS) $^ $(LDFLAGS) -o $@ +libprotoverse.a: $(OBJS) + ar rcs $@ $^ + clean: rm -f protoverse *.o diff --git a/parse.c b/parse.c @@ -99,6 +99,25 @@ static const char *token_error_string(enum token_error err) return "unknown"; } +void print_cell(struct cursor *attributes, struct cell *cell) +{ + const char *name; + int name_len; + + if (cell->type == C_GROUP) { + printf("---\n"); + return; + } + + cell_name(attributes, cell, &name, &name_len); + + printf("%.*s%s%s\n", name_len, name, name_len > 0?" ":"", + cell->type == C_OBJECT + ? object_type_str(cell->obj_type) + : cell_type_str(cell->type)); +} + + static const char *token_type_str(enum token_type type) { switch (type) { @@ -1024,16 +1043,9 @@ static int push_cell(struct cursor *cells, struct cell *cell, u16 *cell_index) int index; int ok; - static int group_count = 0; - - if (cell->type == C_GROUP) - group_count++; - - assert(group_count != 2); - - tokdebug("push_cell %s\n", cell_type_str(cell->type)); + index = cursor_index(cells, sizeof(*cell)); - index = cursor_index(cells, sizeof(cell)); + tokdebug("push_cell %d (%zu) %s\n", index, cells->p - cells->start, cell_type_str(cell->type)); if (index > 0xFFFF) { /* TODO: actual error message here */ @@ -1280,6 +1292,7 @@ static int parse_object(struct parser *parser, u16 *index) ok = parse_cell_attrs(&backtracked, &ind, &cell); if (!ok) return 0; + assert(ind < 10); if (index) *index = ind; diff --git a/parse.h b/parse.h @@ -124,6 +124,7 @@ struct parser { void make_cursor(u8 *start, u8 *end, struct cursor *cursor); +void print_cell(struct cursor *attributes, struct cell *cell); int tokenize_cells(unsigned char *buf, int buf_size, struct cursor *tokens); int parse_cell(struct parser *parser, u16 *index); void print_token_error(struct cursor *cursor); diff --git a/protoverse.c b/protoverse.c @@ -4,6 +4,48 @@ #include <assert.h> +static void print_all_cells(struct parser *parser) +{ + struct cell *cell; + int i, j; + int ncells; + + ncells = cursor_index(parser->cells, sizeof(struct cell)); + printf("ncells %d\n", ncells); + for (i = 0; i < ncells; i++) { + cell = get_cell(parser->cells, i); + print_cell(parser->attributes, cell); + + for (j = 0; j < cell->n_children; j++) { + cell = get_cell(parser->cells, cell->children[j]); + assert(cell); + printf(" "); + print_cell(parser->attributes, cell); + } + } +} + +static int print_cell_tree(struct parser *parser, u16 root, int depth) +{ + int i; + + struct cell *cell = get_cell(parser->cells, root); + if (!cell) return 0; + + for (i = 0; i < depth; i++) { + printf(" "); + } + + print_cell(parser->attributes, cell); + + for (i = 0; i < cell->n_children; i++) { + print_cell_tree(parser, cell->children[i], depth+1); + } + + + return 1; +} + int main(int argc, const char *argv[]) { static u8 file_buf[4096]; static u8 token_buf[2048]; @@ -14,17 +56,12 @@ int main(int argc, const char *argv[]) { struct cursor attributes; struct cursor cells; - struct cell *cell; - struct parser parser; size_t count; const char *space; - int ok, i; + int ok; u16 root; - int ncells; - const char *name; - int name_len; parser.tokens = &tokens; parser.attributes = &attributes; @@ -58,17 +95,8 @@ int main(int argc, const char *argv[]) { print_token_error(&tokens); } - ncells = cursor_index(&cells, sizeof(struct cell)); - printf("ncells %d\n", ncells); - for (i = 0; i < ncells; i++) { - name_len = 0; - cell = get_cell(&cells, i); - cell_name(&attributes, cell, &name, &name_len); - printf("cell %s %.*s\n", - cell->type == C_OBJECT - ? object_type_str(cell->obj_type) - : cell_type_str(cell->type), name_len, name); - } + print_cell_tree(&parser, root, 0); return 0; } + diff --git a/satoshis-citadel.space b/satoshis-citadel.space @@ -1,18 +1,17 @@ -(room (shape rectangle) - (name "\"Satoshi's\" Citadel") +(room (shape rectangle) + (name "Satoshi's Citadel") (width 10) (depth 10) (height 100) (group (table (id welcome-desk) - (name "Welcome desk") + (name "welcome desk") (material marble) (condition clean) (condition new) (width 1) (depth 2) (height 1) - (location center)) - (table - (name "main") - (light (name "lamp"))) - ) -) - + (location center) + (light (name "desk"))) + (chair (id welcome-desk-chair) + (name "welcome desk chair")) + (light (location ceiling) + (shape round))))