protoverse

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

commit d6e45e4301acfcacdba728dd1ec2868d0a8a8b9b
parent 6ca1543204da40e7a8d07d0b498c490717c5c12c
Author: William Casarin <jb55@jb55.com>
Date:   Wed,  1 Jul 2020 15:33:03 -0700

more object types

Diffstat:
Mparse.c | 83++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
Mparse.h | 9+++++++++
Mprotoverse.c | 4+++-
Msatoshis-citadel.space | 8++++++--
4 files changed, 88 insertions(+), 16 deletions(-)

diff --git a/parse.c b/parse.c @@ -1076,6 +1076,17 @@ static int push_cell_child(struct cell *parent, u16 child_ind) return 1; } +const char *object_type_str(enum object_type type) +{ + switch (type) { + case O_DOOR: return "door"; + case O_TABLE: return "table"; + case O_LIGHT: return "light"; + } + + return "unknown"; +} + const char *cell_type_str(enum cell_type type) { switch (type) { @@ -1088,22 +1099,19 @@ const char *cell_type_str(enum cell_type type) return "unknown"; } -static int parse_cell_attrs(struct parser *parser, u16 *index, enum cell_type type) +static int parse_cell_attrs(struct parser *parser, u16 *index, struct cell *cell) { struct cursor cell_attr_inds; struct cell *child_cell; - struct cell cell; u16 child_cell_index; int attr_inds[2] = {0}; int i, ok; - init_cell(&cell); - - make_cursor((u8*)cell.attributes, - (u8*)cell.attributes + sizeof(cell.attributes), + make_cursor((u8*)cell->attributes, + (u8*)cell->attributes + sizeof(cell->attributes), &cell_attr_inds); - cell_attr_inds.p += cell.n_attributes * sizeof(cell.attributes[0]); + cell_attr_inds.p += cell->n_attributes * sizeof(cell->attributes[0]); /* 0 attributes returns 1, 1 attrs returns 2, etc 0 is a real error, an attribute push overflow */ @@ -1114,7 +1122,7 @@ static int parse_cell_attrs(struct parser *parser, u16 *index, enum cell_type ty for (i = attr_inds[0]; i <= attr_inds[1]; i++) { ok = push_int(&cell_attr_inds, i); - cell.n_attributes++; + cell->n_attributes++; if (!ok) return 0; } @@ -1125,7 +1133,7 @@ static int parse_cell_attrs(struct parser *parser, u16 *index, enum cell_type ty child_cell = get_cell(parser->cells, child_cell_index); if (!child_cell) return 0; tokdebug("parse_cell_attrs push child cell\n"); - ok = push_cell_child(&cell, child_cell_index); + ok = push_cell_child(cell, child_cell_index); if (!ok) return 0; } @@ -1133,8 +1141,7 @@ static int parse_cell_attrs(struct parser *parser, u16 *index, enum cell_type ty tokdebug("no child cells found\n"); } - cell.type = type; - ok = push_cell(parser->cells, &cell, index); + ok = push_cell(parser->cells, cell, index); if (!ok) return 0; return 1; @@ -1148,16 +1155,20 @@ static int parse_cell_by_name(struct parser *parser, int ok; struct cursor temp; struct parser backtracked; + struct cell cell; u16 ind; + init_cell(&cell); copy_cursor(parser->tokens, &temp); copy_parser(parser, &backtracked); backtracked.tokens = &temp; + cell.type = type; + ok = parse_symbol(&temp, name); if (!ok) return 0; - ok = parse_cell_attrs(&backtracked, &ind, type); + ok = parse_cell_attrs(&backtracked, &ind, &cell); if (!ok) return 0; if (index) @@ -1226,9 +1237,55 @@ static int parse_group(struct parser *parser, u16 *index) return ncells; } +struct object_def { + const char *name; + enum object_type type; +}; + +static struct object_def object_defs[] = { + {"table", O_TABLE}, + {"door", O_DOOR}, + {"light", O_LIGHT}, +}; + static int parse_object(struct parser *parser, u16 *index) { - return parse_cell_by_name(parser, index, "table", C_OBJECT); + int ok, i; + struct cursor temp; + struct parser backtracked; + struct tok_str str; + struct object_def *def; + struct cell cell; + u16 ind; + + init_cell(&cell); + cell.type = C_OBJECT; + + copy_cursor(parser->tokens, &temp); + copy_parser(parser, &backtracked); + backtracked.tokens = &temp; + + ok = pull_symbol_token(&temp, &str); + if (!ok) return 0; + + for (i = 0; i < ARRAY_SIZE(object_defs); i++) { + def = &object_defs[i]; + + if (symbol_eq(&str, def->name, strlen(def->name))) { + cell.obj_type = def->type; + break; + } + } + + ok = parse_cell_attrs(&backtracked, &ind, &cell); + if (!ok) return 0; + + if (index) + *index = ind; + + copy_cursor(&temp, parser->tokens); + + return 1; } int parse_cell(struct parser *parser, u16 *index) diff --git a/parse.h b/parse.h @@ -6,6 +6,7 @@ #define MAX_ATTRIBUTES 24 #define MAX_CHILDREN 24 +#define ARRAY_SIZE(x) ((int)(sizeof(x) / sizeof((x)[0]))) enum token_error { TE_OK, @@ -26,6 +27,12 @@ enum cell_type { C_OBJECT, }; +enum object_type { + O_TABLE, + O_DOOR, + O_LIGHT, +}; + enum attribute_type { A_ID, A_TYPE, @@ -106,6 +113,7 @@ struct cell { int n_children; enum cell_type type; + enum object_type obj_type; }; struct parser { @@ -121,6 +129,7 @@ int parse_cell(struct parser *parser, u16 *index); void print_token_error(struct cursor *cursor); int cursor_index(struct cursor *cursor, int elem_size); const char *cell_type_str(enum cell_type); +const char *object_type_str(enum object_type); int cell_name(struct cursor *attributes, struct cell *cell, const char** name, int *len); struct cell *get_cell(struct cursor *cells, u16 index); diff --git a/protoverse.c b/protoverse.c @@ -65,7 +65,9 @@ int main(int argc, const char *argv[]) { cell = get_cell(&cells, i); cell_name(&attributes, cell, &name, &name_len); printf("cell %s %.*s\n", - cell_type_str(cell->type), name_len, name); + cell->type == C_OBJECT + ? object_type_str(cell->obj_type) + : cell_type_str(cell->type), name_len, name); } return 0; diff --git a/satoshis-citadel.space b/satoshis-citadel.space @@ -9,6 +9,10 @@ (condition clean) (condition new) (width 1) (depth 2) (height 1) - (location center) - ))) + (location center)) + (table + (name "main") + (light (name "lamp"))) + ) +)