Blob


1 BEGIN {
2 count = 0;
3 obj = "";
4 if (pass == "h") {
5 print "#ifndef OBJECT_H";
6 print "#define OBJECT_H";
7 print "";
8 }
9 if (pass == "c1") {
10 print "#include \"adventure.h\"\n";
11 print "";
12 print "static int always_true(struct object *o) { return 1; }";
13 print "";
14 }
15 if (pass == "c2") {
16 print "\nstruct object objs[] = {";
17 }
18 }
20 /^- / {
21 output_record(",");
22 obj = $2;
23 prop["condition"] = "always_true";
24 prop["description"] = "NULL";
25 prop["tags"] = "";
26 prop["location"] = "NULL";
27 prop["destination"] = "NULL";
28 prop["prospect"] = "";
29 prop["details"] = "\"You see nothing special.\"";
30 prop["contents"] = "\"You see\"";
31 prop["text_go"] = "\"You can't get any closer than this.\"";
32 prop["weight"] = "99";
33 prop["capacity"] = "9999";
34 prop["health"] = "0";
35 prop["open"] = "cannot_be_opened";
36 prop["close"] = "cannot_be_closed";
37 prop["lock"] = "cannot_be_locked";
38 prop["unlock"] = "cannot_be_unlocked";
39 }
41 obj && /^[ \t]+[a-z]/ {
42 name = $1;
43 $1 = "";
44 if (name in prop) {
45 prop[name] = $0;
46 if (/^[ \t]*\{/) {
47 prop[name] = name count;
48 if (pass == "c1")
49 print "static int " prop[name] "(struct object *obj) " $0;
50 }
51 } else if (pass == "c2") {
52 print "#error \"" FILENAME " line " NR ": unknown attribute '" name "'\"";
53 }
54 }
56 !obj && pass == (/^#include/ ? "c1" : "h") {
57 print;
58 }
60 END {
61 output_record("\n};");
62 if (pass == "h") {
63 print "";
64 print "#define obj_end\t(objs + " count ")";
65 print "#define valid_obj(obj)\t" \
66 "((obj) != NULL && (*(obj)->condition)((obj)))";
67 print "#define foreach_obj(obj)\t" \
68 "for (obj = objs; obj < obj_end; ++obj) if (valid_obj(obj))";
69 print "";
70 print "#endif";
71 }
72 }
74 function output_record(separator) {
75 if (obj) {
76 if (pass == "h") {
77 print "#define " obj "\t(objs + " count ")";
78 } else if (pass == "c1") {
79 print "static const char *tags" count "[] = {" prop["tags"] ", NULL};";
80 } else if (pass == "c2") {
81 print "\t{\t/* " count " = " obj " */";
82 print "\t\t" prop["condition"] ",";
83 print "\t\t" prop["description"] ",";
84 print "\t\ttags" count ",";
85 print "\t\t" prop["location"] ",";
86 print "\t\t" prop["destination"] ",";
87 print "\t\t" prop[prop["prospect"] ? "prospect" : "destination"] ",";
88 print "\t\t" prop["details"] ",";
89 print "\t\t" prop["contents"] ",";
90 print "\t\t" prop["text_go"] ",";
91 print "\t\t" prop["weight"] ",";
92 print "\t\t" prop["capacity"] ",";
93 print "\t\t" prop["health"] ",";
94 print "\t\t" prop["open"] ",";
95 print "\t\t" prop["close"] ",";
96 print "\t\t" prop["lock"] ",";
97 print "\t\t" prop["unlock"] ",";
98 print "\t}" separator;
99 delete prop;
102 count++;