]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - atc/grammar.y
cgram: conform to lint's strict bool mode, KNF
[bsdgames-darwin.git] / atc / grammar.y
1 /* $NetBSD: grammar.y,v 1.12 2015/06/19 06:02:31 dholland Exp $ */
2
3 /*-
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Ed James.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
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 <sys/cdefs.h>
61 #ifndef lint
62 #if 0
63 static char sccsid[] = "@(#)grammar.y 8.1 (Berkeley) 5/31/93";
64 #else
65 __RCSID("$NetBSD: grammar.y,v 1.12 2015/06/19 06:02:31 dholland Exp $");
66 #endif
67 #endif /* not lint */
68
69 #include <stdio.h>
70 #include <stdlib.h>
71
72 #include "def.h"
73 #include "struct.h"
74 #include "extern.h"
75 #include "tunable.h"
76
77 int line = 1;
78
79 static int errors = 0;
80
81 static int yyerror(const char *);
82 static int checkdefs(void);
83 static void check_point(int x, int y);
84 static void check_edir(int x, int y, int dir);
85 static void check_line(int px1, int py1, int px2, int py2);
86 static void check_edge(int x, int y);
87 %}
88
89 %%
90 file:
91 bunch_of_defs { if (checkdefs() < 0) return (errors); } bunch_of_lines
92 {
93 if (sp->num_exits + sp->num_airports < 2)
94 yyerror("Need at least 2 airports and/or exits.");
95 return (errors);
96 }
97 ;
98
99 bunch_of_defs:
100 def bunch_of_defs
101 | def
102 ;
103
104 def:
105 udef
106 | ndef
107 | wdef
108 | hdef
109 ;
110
111 udef:
112 UpdateOp '=' ConstOp ';'
113 {
114 if (sp->update_secs != 0)
115 return (yyerror("Redefinition of 'update'."));
116 else if ($3 < 1)
117 return (yyerror("'update' is too small."));
118 else
119 sp->update_secs = $3;
120 }
121 ;
122
123 ndef:
124 NewplaneOp '=' ConstOp ';'
125 {
126 if (sp->newplane_time != 0)
127 return (yyerror("Redefinition of 'newplane'."));
128 else if ($3 < 1)
129 return (yyerror("'newplane' is too small."));
130 else
131 sp->newplane_time = $3;
132 }
133 ;
134
135 hdef:
136 HeightOp '=' ConstOp ';'
137 {
138 if (sp->height != 0)
139 return (yyerror("Redefinition of 'height'."));
140 else if ($3 < 3)
141 return (yyerror("'height' is too small."));
142 else
143 sp->height = $3;
144 }
145 ;
146
147 wdef:
148 WidthOp '=' ConstOp ';'
149 {
150 if (sp->width != 0)
151 return (yyerror("Redefinition of 'width'."));
152 else if ($3 < 3)
153 return (yyerror("'width' is too small."));
154 else
155 sp->width = $3;
156 }
157 ;
158
159 bunch_of_lines:
160 line bunch_of_lines
161 {}
162 | line
163 {}
164 ;
165
166 line:
167 BeaconOp ':' Bpoint_list ';'
168 {}
169 | ExitOp ':' Epoint_list ';'
170 {}
171 | LineOp ':' Lline_list ';'
172 {}
173 | AirportOp ':' Apoint_list ';'
174 {}
175 ;
176
177 Bpoint_list:
178 Bpoint Bpoint_list
179 {}
180 | Bpoint
181 {}
182 ;
183
184 Bpoint:
185 '(' ConstOp ConstOp ')'
186 {
187 if (sp->num_beacons % REALLOC == 0) {
188 if (sp->beacon == NULL)
189 sp->beacon = malloc((sp->num_beacons
190 + REALLOC) * sizeof (BEACON));
191 else
192 sp->beacon = realloc(sp->beacon,
193 (sp->num_beacons + REALLOC) *
194 sizeof (BEACON));
195 if (sp->beacon == NULL)
196 return (yyerror("No memory available."));
197 }
198 sp->beacon[sp->num_beacons].x = $2;
199 sp->beacon[sp->num_beacons].y = $3;
200 check_point($2, $3);
201 sp->num_beacons++;
202 }
203 ;
204
205 Epoint_list:
206 Epoint Epoint_list
207 {}
208 | Epoint
209 {}
210 ;
211
212 Epoint:
213 '(' ConstOp ConstOp DirOp ')'
214 {
215 int dir;
216
217 if (sp->num_exits % REALLOC == 0) {
218 if (sp->exit == NULL)
219 sp->exit = malloc((sp->num_exits +
220 REALLOC) * sizeof (EXIT));
221 else
222 sp->exit = realloc(sp->exit,
223 (sp->num_exits + REALLOC) *
224 sizeof (EXIT));
225 if (sp->exit == NULL)
226 return (yyerror("No memory available."));
227 }
228 dir = dir_no($4);
229 sp->exit[sp->num_exits].x = $2;
230 sp->exit[sp->num_exits].y = $3;
231 sp->exit[sp->num_exits].dir = dir;
232 check_edge($2, $3);
233 check_edir($2, $3, dir);
234 sp->num_exits++;
235 }
236 ;
237
238 Apoint_list:
239 Apoint Apoint_list
240 {}
241 | Apoint
242 {}
243 ;
244
245 Apoint:
246 '(' ConstOp ConstOp DirOp ')'
247 {
248 int dir;
249
250 if (sp->num_airports % REALLOC == 0) {
251 if (sp->airport == NULL)
252 sp->airport = malloc((sp->num_airports
253 + REALLOC) * sizeof(AIRPORT));
254 else
255 sp->airport = realloc(sp->airport,
256 (sp->num_airports + REALLOC) *
257 sizeof(AIRPORT));
258 if (sp->airport == NULL)
259 return (yyerror("No memory available."));
260 }
261 dir = dir_no($4);
262 sp->airport[sp->num_airports].x = $2;
263 sp->airport[sp->num_airports].y = $3;
264 sp->airport[sp->num_airports].dir = dir;
265 check_point($2, $3);
266 sp->num_airports++;
267 }
268 ;
269
270 Lline_list:
271 Lline Lline_list
272 {}
273 | Lline
274 {}
275 ;
276
277 Lline:
278 '[' '(' ConstOp ConstOp ')' '(' ConstOp ConstOp ')' ']'
279 {
280 if (sp->num_lines % REALLOC == 0) {
281 if (sp->line == NULL)
282 sp->line = malloc((sp->num_lines +
283 REALLOC) * sizeof (LINE));
284 else
285 sp->line = realloc(sp->line,
286 (sp->num_lines + REALLOC) *
287 sizeof (LINE));
288 if (sp->line == NULL)
289 return (yyerror("No memory available."));
290 }
291 sp->line[sp->num_lines].p1.x = $3;
292 sp->line[sp->num_lines].p1.y = $4;
293 sp->line[sp->num_lines].p2.x = $7;
294 sp->line[sp->num_lines].p2.y = $8;
295 check_line($3, $4, $7, $8);
296 sp->num_lines++;
297 }
298 ;
299 %%
300
301 static void
302 check_edge(int x, int y)
303 {
304 if (!(x == 0) && !(x == sp->width - 1) &&
305 !(y == 0) && !(y == sp->height - 1))
306 yyerror("edge value not on edge.");
307 }
308
309 static void
310 check_point(int x, int y)
311 {
312 if (x < 1 || x >= sp->width - 1)
313 yyerror("X value out of range.");
314 if (y < 1 || y >= sp->height - 1)
315 yyerror("Y value out of range.");
316 }
317
318 static void
319 check_linepoint(int x, int y)
320 {
321 if (x < 0 || x >= sp->width)
322 yyerror("X value out of range.");
323 if (y < 0 || y >= sp->height)
324 yyerror("Y value out of range.");
325 }
326
327 static void
328 check_line(int px1, int py1, int px2, int py2)
329 {
330 int d1, d2;
331
332 check_linepoint(px1, py1);
333 check_linepoint(px2, py2);
334
335 d1 = ABS(px2 - px1);
336 d2 = ABS(py2 - py1);
337
338 if (!(d1 == d2) && !(d1 == 0) && !(d2 == 0))
339 yyerror("Bad line endpoints.");
340 }
341
342 static int
343 yyerror(const char *s)
344 {
345 fprintf(stderr, "\"%s\": line %d: %s\n", filename, line, s);
346 errors++;
347
348 return (errors);
349 }
350
351 static void
352 check_edir(int x, int y, int dir)
353 {
354 int bad = 0;
355
356 if (x == sp->width - 1)
357 x = 2;
358 else if (x != 0)
359 x = 1;
360 if (y == sp->height - 1)
361 y = 2;
362 else if (y != 0)
363 y = 1;
364
365 switch (x * 10 + y) {
366 case 00: if (dir != 3) bad++; break;
367 case 01: if (dir < 1 || dir > 3) bad++; break;
368 case 02: if (dir != 1) bad++; break;
369 case 10: if (dir < 3 || dir > 5) bad++; break;
370 case 11: break;
371 case 12: if (dir > 1 && dir < 7) bad++; break;
372 case 20: if (dir != 5) bad++; break;
373 case 21: if (dir < 5) bad++; break;
374 case 22: if (dir != 7) bad++; break;
375 default:
376 yyerror("Unknown value in checkdir! Get help!");
377 break;
378 }
379 if (bad)
380 yyerror("Bad direction for entrance at exit.");
381 }
382
383 static int
384 checkdefs(void)
385 {
386 int error = 0;
387
388 if (sp->width == 0) {
389 yyerror("'width' undefined.");
390 error++;
391 }
392 if (sp->height == 0) {
393 yyerror("'height' undefined.");
394 error++;
395 }
396 if (sp->update_secs == 0) {
397 yyerror("'update' undefined.");
398 error++;
399 }
400 if (sp->newplane_time == 0) {
401 yyerror("'newplane' undefined.");
402 error++;
403 }
404 if (error)
405 return (-1);
406 else
407 return (0);
408 }