xref: /dragonfly/games/atc/grammar.y (revision 26720ae0)
1 /*-
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Ed James.
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  * @(#)grammar.y	8.1 (Berkeley) 5/31/93
33  */
34 
35 /*
36  * Copyright (c) 1987 by Ed James, UC Berkeley.  All rights reserved.
37  *
38  * Copy permission is hereby granted provided that this notice is
39  * retained on all partial or complete copies.
40  *
41  * For more info on this and all of my stuff, mail edjames@berkeley.edu.
42  */
43 
44 %token <ival>	HeightOp
45 %token <ival>	WidthOp
46 %token <ival>	UpdateOp
47 %token <ival>	NewplaneOp
48 %token <cval>	DirOp
49 %token <ival>	ConstOp
50 %token <ival>	LineOp
51 %token <ival>	AirportOp
52 %token <ival>	BeaconOp
53 %token <ival>	ExitOp
54 %union {
55 	int	ival;
56 	char	cval;
57 }
58 
59 %{
60 #include "include.h"
61 
62 #if YYPATCH < 20180510
63 extern int	yylex(void);
64 #endif
65 
66 static void	check_edge(int, int);
67 static void	check_point(int, int);
68 static void	check_linepoint(int, int);
69 static void	check_line(int, int, int, int);
70 static int	yyerror(const char *);
71 static void	check_edir(int, int, int);
72 static int	checkdefs(void);
73 
74 int	errors = 0;
75 int	line = 1;
76 %}
77 
78 %%
79 file:
80 	bunch_of_defs { if (checkdefs() < 0) return (errors); } bunch_of_lines
81 		{
82 		if (sp->num_exits + sp->num_airports < 2)
83 			yyerror("Need at least 2 airports and/or exits.");
84 		return (errors);
85 		}
86 	;
87 
88 bunch_of_defs:
89 	def bunch_of_defs
90 	| def
91 	;
92 
93 def:
94 	udef
95 	| ndef
96 	| wdef
97 	| hdef
98 	;
99 
100 udef:
101 	UpdateOp '=' ConstOp ';'
102 		{
103 		if (sp->update_secs != 0)
104 			return (yyerror("Redefinition of 'update'."));
105 		else if ($3 < 1)
106 			return (yyerror("'update' is too small."));
107 		else
108 			sp->update_secs = $3;
109 		}
110 	;
111 
112 ndef:
113 	NewplaneOp '=' ConstOp ';'
114 		{
115 		if (sp->newplane_time != 0)
116 			return (yyerror("Redefinition of 'newplane'."));
117 		else if ($3 < 1)
118 			return (yyerror("'newplane' is too small."));
119 		else
120 			sp->newplane_time = $3;
121 		}
122 	;
123 
124 hdef:
125 	HeightOp '=' ConstOp ';'
126 		{
127 		if (sp->height != 0)
128 			return (yyerror("Redefinition of 'height'."));
129 		else if ($3 < 3)
130 			return (yyerror("'height' is too small."));
131 		else
132 			sp->height = $3;
133 		}
134 	;
135 
136 wdef:
137 	WidthOp '=' ConstOp ';'
138 		{
139 		if (sp->height != 0)
140 			return (yyerror("Redefinition of 'width'."));
141 		else if ($3 < 3)
142 			return (yyerror("'width' is too small."));
143 		else
144 			sp->width = $3;
145 		}
146 	;
147 
148 bunch_of_lines:
149 	line bunch_of_lines
150 		{}
151 	| line
152 		{}
153 	;
154 
155 line:
156 	BeaconOp ':' Bpoint_list ';'
157 		{}
158 	| ExitOp ':' Epoint_list ';'
159 		{}
160 	| LineOp ':' Lline_list ';'
161 		{}
162 	| AirportOp ':' Apoint_list ';'
163 		{}
164 	;
165 
166 Bpoint_list:
167 	Bpoint Bpoint_list
168 		{}
169 	| Bpoint
170 		{}
171 	;
172 
173 Bpoint:
174 	'(' ConstOp ConstOp ')'
175 		{
176 		if (sp->num_beacons % REALLOC == 0) {
177 			if (sp->beacon == NULL)
178 				sp->beacon = malloc((sp->num_beacons + REALLOC)
179 					* sizeof(BEACON));
180 			else
181 				sp->beacon = realloc(sp->beacon,
182 					(sp->num_beacons + REALLOC) *
183 					sizeof(BEACON));
184 			if (sp->beacon == NULL)
185 				return (yyerror("No memory available."));
186 		}
187 		sp->beacon[sp->num_beacons].x = $2;
188 		sp->beacon[sp->num_beacons].y = $3;
189 		check_point($2, $3);
190 		sp->num_beacons++;
191 		}
192 	;
193 
194 Epoint_list:
195 	Epoint Epoint_list
196 		{}
197 	| Epoint
198 		{}
199 	;
200 
201 Epoint:
202 	'(' ConstOp ConstOp DirOp ')'
203 		{
204 		int	dir;
205 
206 		if (sp->num_exits % REALLOC == 0) {
207 			if (sp->exit == NULL)
208 				sp->exit = malloc((sp->num_exits + REALLOC) *
209 					sizeof(EXIT));
210 			else
211 				sp->exit = realloc(sp->exit, (sp->num_exits +
212 					REALLOC) * sizeof(EXIT));
213 			if (sp->exit == NULL)
214 				return (yyerror("No memory available."));
215 		}
216 		dir = dir_no($4);
217 		sp->exit[sp->num_exits].x = $2;
218 		sp->exit[sp->num_exits].y = $3;
219 		sp->exit[sp->num_exits].dir = dir;
220 		check_edge($2, $3);
221 		check_edir($2, $3, dir);
222 		sp->num_exits++;
223 		}
224 	;
225 
226 Apoint_list:
227 	Apoint Apoint_list
228 		{}
229 	| Apoint
230 		{}
231 	;
232 
233 Apoint:
234 	'(' ConstOp ConstOp DirOp ')'
235 		{
236 		int	dir;
237 
238 		if (sp->num_airports % REALLOC == 0) {
239 			if (sp->airport == NULL)
240 				sp->airport = malloc((sp->num_airports +
241 					REALLOC) * sizeof(AIRPORT));
242 			else
243 				sp->airport = realloc(sp->airport,
244 					(sp->num_airports + REALLOC) *
245 					sizeof(AIRPORT));
246 			if (sp->airport == NULL)
247 				return (yyerror("No memory available."));
248 		}
249 		dir = dir_no($4);
250 		sp->airport[sp->num_airports].x = $2;
251 		sp->airport[sp->num_airports].y = $3;
252 		sp->airport[sp->num_airports].dir = dir;
253 		check_point($2, $3);
254 		sp->num_airports++;
255 		}
256 	;
257 
258 Lline_list:
259 	Lline Lline_list
260 		{}
261 	| Lline
262 		{}
263 	;
264 
265 Lline:
266 	'[' '(' ConstOp ConstOp ')' '(' ConstOp ConstOp ')' ']'
267 		{
268 		if (sp->num_lines % REALLOC == 0) {
269 			if (sp->line == NULL)
270 				sp->line = malloc((sp->num_lines + REALLOC) *
271 					sizeof(LINE));
272 			else
273 				sp->line = realloc(sp->line, (sp->num_lines +
274 					REALLOC) * sizeof(LINE));
275 			if (sp->line == NULL)
276 				return (yyerror("No memory available."));
277 		}
278 		sp->line[sp->num_lines].p1.x = $3;
279 		sp->line[sp->num_lines].p1.y = $4;
280 		sp->line[sp->num_lines].p2.x = $7;
281 		sp->line[sp->num_lines].p2.y = $8;
282 		check_line($3, $4, $7, $8);
283 		sp->num_lines++;
284 		}
285 	;
286 %%
287 
288 static void
289 check_edge(int x, int y)
290 {
291 	if (!(x == 0) && !(x == sp->width - 1) &&
292 	    !(y == 0) && !(y == sp->height - 1))
293 		yyerror("edge value not on edge.");
294 }
295 
296 static void
297 check_point(int x, int y)
298 {
299 	if (x < 1 || x >= sp->width - 1)
300 		yyerror("X value out of range.");
301 	if (y < 1 || y >= sp->height - 1)
302 		yyerror("Y value out of range.");
303 }
304 
305 static void
306 check_linepoint(int x, int y)
307 {
308 	if (x < 0 || x >= sp->width)
309 		yyerror("X value out of range.");
310 	if (y < 0 || y >= sp->height)
311 		yyerror("Y value out of range.");
312 }
313 
314 static void
315 check_line(int x_1, int y_1, int x_2, int y_2)
316 {
317 	int	d1, d2;
318 
319 	check_linepoint(x_1, y_1);
320 	check_linepoint(x_2, y_2);
321 
322 	d1 = ABS(x_2 - x_1);
323 	d2 = ABS(y_2 - y_1);
324 
325 	if (!(d1 == d2) && !(d1 == 0) && !(d2 == 0))
326 		yyerror("Bad line endpoints.");
327 }
328 
329 static int
330 yyerror(const char *s)
331 {
332 	fprintf(stderr, "\"%s\": line %d: %s\n", filename, line, s);
333 	errors++;
334 
335 	return (errors);
336 }
337 
338 static void
339 check_edir(int x, int y, int dir)
340 {
341 	int	bad = 0;
342 
343 	if (x == sp->width - 1)
344 		x = 2;
345 	else if (x != 0)
346 		x = 1;
347 	if (y == sp->height - 1)
348 		y = 2;
349 	else if (y != 0)
350 		y = 1;
351 
352 	switch (x * 10 + y) {
353 	case 00: if (dir != 3) bad++; break;
354 	case 01: if (dir < 1 || dir > 3) bad++; break;
355 	case 02: if (dir != 1) bad++; break;
356 	case 10: if (dir < 3 || dir > 5) bad++; break;
357 	case 11: break;
358 	case 12: if (dir > 1 && dir < 7) bad++; break;
359 	case 20: if (dir != 5) bad++; break;
360 	case 21: if (dir < 5) bad++; break;
361 	case 22: if (dir != 7) bad++; break;
362 	default:
363 		yyerror("Unknown value in checkdir!  Get help!");
364 		break;
365 	}
366 	if (bad)
367 		yyerror("Bad direction for entrance at exit.");
368 }
369 
370 static int
371 checkdefs(void)
372 {
373 	int	err = 0;
374 
375 	if (sp->width == 0) {
376 		yyerror("'width' undefined.");
377 		err++;
378 	}
379 	if (sp->height == 0) {
380 		yyerror("'height' undefined.");
381 		err++;
382 	}
383 	if (sp->update_secs == 0) {
384 		yyerror("'update' undefined.");
385 		err++;
386 	}
387 	if (sp->newplane_time == 0) {
388 		yyerror("'newplane' undefined.");
389 		err++;
390 	}
391 	if (err)
392 		return (-1);
393 	else
394 		return (0);
395 }
396