xref: /original-bsd/games/battlestar/parse.c (revision 1f2fb9e6)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)parse.c	5.1 (Berkeley) 05/08/88";
15 #endif /* not lint */
16 
17 #include "externs.h"
18 
19 wordinit()
20 {
21 	register struct wlist *w;
22 
23 	for (w = wlist; w->string; w++)
24 		install(w);
25 }
26 
27 hash(s)
28 	register char *s;
29 {
30 	register hashval = 0;
31 
32 	while (*s) {
33 		hashval += *s++;
34 		hashval *= HASHMUL;
35 		hashval &= HASHMASK;
36 	}
37 	return hashval;
38 }
39 
40 struct wlist *
41 lookup(s)
42 	char *s;
43 {
44 	register struct wlist *wp;
45 
46 	for (wp = hashtab[hash(s)]; wp != NULL; wp = wp->next)
47 		if (*s == *wp->string && strcmp(s, wp->string) == 0)
48 			return wp;
49 	return NULL;
50 }
51 
52 install(wp)
53 	register struct wlist *wp;
54 {
55 	int hashval;
56 
57 	if (lookup(wp->string) == NULL) {
58 		hashval = hash(wp->string);
59 		wp->next = hashtab[hashval];
60 		hashtab[hashval] = wp;
61 	} else
62 		printf("Multiply defined %s.\n", wp->string);
63 }
64 
65 parse()
66 {
67 	register struct wlist *wp;
68 	register n;
69 
70 	wordnumber = 0;           /* for cypher */
71 	for (n = 0; n <= wordcount; n++) {
72 		if ((wp = lookup(words[n])) == NULL) {
73 			wordvalue[n] = -1;
74 			wordtype[n] = -1;
75 		} else {
76 			wordvalue[n] = wp -> value;
77 			wordtype[n] = wp -> article;
78 		}
79 	}
80 }
81