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