Blame


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