]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - battlestar/parse.c
Rename com#.c to command#.c to avoid conflicts with those file basenames
[bsdgames-darwin.git] / battlestar / parse.c
1 /* $NetBSD: parse.c,v 1.12 2001/06/19 13:42:09 wiz Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)parse.c 8.2 (Berkeley) 4/28/95";
40 #else
41 __RCSID("$NetBSD: parse.c,v 1.12 2001/06/19 13:42:09 wiz Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include "extern.h"
46
47 #define HASHSIZE 256
48 #define HASHMUL 81
49 #define HASHMASK (HASHSIZE - 1)
50
51 static int hash __P((const char *));
52 static void install __P((struct wlist *));
53 static struct wlist *lookup __P((const char *));
54
55 static struct wlist *hashtab[HASHSIZE];
56
57 void
58 wordinit()
59 {
60 struct wlist *w;
61
62 for (w = wlist; w->string; w++)
63 install(w);
64 }
65
66 static int
67 hash(s)
68 const char *s;
69 {
70 int hashval = 0;
71
72 while (*s) {
73 hashval += *s++;
74 hashval *= HASHMUL;
75 hashval &= HASHMASK;
76 }
77 return hashval;
78 }
79
80 static struct wlist *
81 lookup(s)
82 const char *s;
83 {
84 struct wlist *wp;
85
86 for (wp = hashtab[hash(s)]; wp != NULL; wp = wp->next)
87 if (*s == *wp->string && strcmp(s, wp->string) == 0)
88 return wp;
89 return NULL;
90 }
91
92 static void
93 install(wp)
94 struct wlist *wp;
95 {
96 int hashval;
97
98 if (lookup(wp->string) == NULL) {
99 hashval = hash(wp->string);
100 wp->next = hashtab[hashval];
101 hashtab[hashval] = wp;
102 } else
103 printf("Multiply defined %s.\n", wp->string);
104 }
105
106 void
107 parse()
108 {
109 struct wlist *wp;
110 int n;
111 int flag;
112
113 wordnumber = 0; /* for cypher */
114 for (n = 0; n <= wordcount; n++) {
115 if ((wp = lookup(words[n])) == NULL) {
116 wordvalue[n] = -1;
117 wordtype[n] = -1;
118 } else {
119 wordvalue[n] = wp->value;
120 wordtype[n] = wp->article;
121 }
122 }
123 /* We never use adjectives for anything, so yank them all. */
124 for (n = 1; n < wordcount; n++)
125 if (wordtype[n] == ADJS) {
126 int i;
127 for (i = n + 1; i < wordcount; i++) {
128 wordtype[i - 1] = wordtype[i];
129 wordvalue[i - 1] = wordvalue[i];
130 strcpy(words[i - 1], words[i]);
131 }
132 wordcount--;
133 }
134 /* Don't let a comma mean AND if followed by a verb. */
135 for (n = 0; n < wordcount; n++)
136 if (wordvalue[n] == AND && words[n][0] == ','
137 && wordtype[n + 1] == VERB) {
138 wordvalue[n] = -1;
139 wordtype[n] = -1;
140 }
141 /* Trim "AND AND" which can happen naturally at the end of a
142 * comma-delimited list.
143 */
144 for (n = 1; n < wordcount; n++)
145 if (wordvalue[n - 1] == AND && wordvalue[n] == AND) {
146 int i;
147 for (i = n + 1; i < wordcount; i++) {
148 wordtype[i - 1] = wordtype[i];
149 wordvalue[i - 1] = wordvalue[i];
150 strcpy(words[i - 1], words[i]);
151 }
152 wordcount--;
153 }
154
155 /* If there is a sequence (NOUN | OBJECT) AND EVERYTHING
156 * then move all the EVERYTHINGs to the beginning, since that's where
157 * they're expected. We can't get rid of the NOUNs and OBJECTs in
158 * case they aren't in EVERYTHING (i.e. not here or nonexistent).
159 */
160 flag = 1;
161 while (flag) {
162 flag = 0;
163 for (n = 1; n < wordcount; n++)
164 if ((wordtype[n - 1] == NOUNS || wordtype[n - 1] == OBJECT) &&
165 wordvalue[n] == AND && wordvalue[n + 1] == EVERYTHING) {
166 char tmpword[WORDLEN];
167 wordvalue[n + 1] = wordvalue[n - 1];
168 wordvalue[n - 1] = EVERYTHING;
169 wordtype[n + 1] = wordtype[n - 1];
170 wordtype[n - 1] = OBJECT;
171 strcpy(tmpword, words[n - 1]);
172 strcpy(words[n - 1], words[n + 1]);
173 strcpy(words[n + 1], tmpword);
174 flag = 1;
175 }
176 /* And trim EVERYTHING AND EVERYTHING. */
177 for (n = 1; n < wordcount; n++)
178 if (wordvalue[n - 1] == EVERYTHING &&
179 wordvalue[n] == AND && wordvalue[n + 1] == EVERYTHING) {
180 int i;
181 for (i = n + 1; i < wordcount; i++) {
182 wordtype[i - 1] = wordtype[i + 1];
183 wordvalue[i - 1] = wordvalue[i + 1];
184 strcpy(words[i - 1], words[i + 1]);
185 }
186 wordcount--;
187 wordcount--;
188 flag = 1;
189 }
190 }
191 }