BEGIN { count = 0; obj = ""; if (pass == "h") { print "#ifndef OBJECT_H"; print "#define OBJECT_H"; print ""; } if (pass == "c1") { print "#include \"adventure.h\"\n"; print ""; print "static int always_true(struct object *o) { return 1; }"; print ""; } if (pass == "c2") { print "\nstruct object objs[] = {"; } } /^- / { output_record(","); obj = $2; prop["condition"] = "always_true"; prop["description"] = "NULL"; prop["tags"] = ""; prop["location"] = "NULL"; prop["destination"] = "NULL"; prop["prospect"] = ""; prop["details"] = "\"You see nothing special.\""; prop["contents"] = "\"You see\""; prop["text_go"] = "\"You can't get any closer than this.\""; prop["weight"] = "99"; prop["capacity"] = "9999"; prop["health"] = "0"; prop["open"] = "cannot_be_opened"; prop["close"] = "cannot_be_closed"; prop["lock"] = "cannot_be_locked"; prop["unlock"] = "cannot_be_unlocked"; } obj && /^[ \t]+[a-z]/ { name = $1; $1 = ""; if (name in prop) { prop[name] = $0; if (/^[ \t]*\{/) { prop[name] = name count; if (pass == "c1") print "static int " prop[name] "(struct object *obj) " $0; } } else if (pass == "c2") { print "#error \"" FILENAME " line " NR ": unknown attribute '" name "'\""; } } !obj && pass == (/^#include/ ? "c1" : "h") { print; } END { output_record("\n};"); if (pass == "h") { print ""; print "#define obj_end\t(objs + " count ")"; print "#define valid_obj(obj)\t" \ "((obj) != NULL && (*(obj)->condition)((obj)))"; print "#define foreach_obj(obj)\t" \ "for (obj = objs; obj < obj_end; ++obj) if (valid_obj(obj))"; print ""; print "#endif"; } } function output_record(separator) { if (obj) { if (pass == "h") { print "#define " obj "\t(objs + " count ")"; } else if (pass == "c1") { print "static const char *tags" count "[] = {" prop["tags"] ", NULL};"; } else if (pass == "c2") { print "\t{\t/* " count " = " obj " */"; print "\t\t" prop["condition"] ","; print "\t\t" prop["description"] ","; print "\t\ttags" count ","; print "\t\t" prop["location"] ","; print "\t\t" prop["destination"] ","; print "\t\t" prop[prop["prospect"] ? "prospect" : "destination"] ","; print "\t\t" prop["details"] ","; print "\t\t" prop["contents"] ","; print "\t\t" prop["text_go"] ","; print "\t\t" prop["weight"] ","; print "\t\t" prop["capacity"] ","; print "\t\t" prop["health"] ","; print "\t\t" prop["open"] ","; print "\t\t" prop["close"] ","; print "\t\t" prop["lock"] ","; print "\t\t" prop["unlock"] ","; print "\t}" separator; delete prop; } count++; } }