Annotation of 43BSDReno/games/atc/grammar.y, revision 1.1.1.1

1.1       root        1: /*-
                      2:  * Copyright (c) 1990 The Regents of the University of California.
                      3:  * 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 are permitted
                      9:  * provided that: (1) source distributions retain this entire copyright
                     10:  * notice and comment, and (2) distributions including binaries display
                     11:  * the following acknowledgement:  ``This product includes software
                     12:  * developed by the University of California, Berkeley and its contributors''
                     13:  * in the documentation or other materials provided with the distribution
                     14:  * and in all advertising materials mentioning features or use of this
                     15:  * software. Neither the name of the University nor the names of its
                     16:  * contributors may be used to endorse or promote products derived
                     17:  * from this software without specific prior written permission.
                     18:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
                     19:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
                     20:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     21:  */
                     22: 
                     23: /*
                     24:  * Copyright (c) 1987 by Ed James, UC Berkeley.  All rights reserved.
                     25:  *
                     26:  * Copy permission is hereby granted provided that this notice is
                     27:  * retained on all partial or complete copies.
                     28:  *
                     29:  * For more info on this and all of my stuff, mail [email protected].
                     30:  */
                     31: 
                     32: %token <ival>  HeightOp
                     33: %token <ival>  WidthOp
                     34: %token <ival>  UpdateOp
                     35: %token <ival>  NewplaneOp
                     36: %token <cval>  DirOp
                     37: %token <ival>  ConstOp
                     38: %token <ival>  LineOp
                     39: %token <ival>  AirportOp
                     40: %token <ival>  BeaconOp
                     41: %token <ival>  ExitOp
                     42: %union {
                     43:        int     ival;
                     44:        char    cval;
                     45: }
                     46: 
                     47: %{
                     48: #include "include.h"
                     49: 
                     50: #ifndef lint
                     51: static char sccsid[] = "@(#)grammar.y  5.2 (Berkeley) 4/30/90";
                     52: #endif /* not lint */
                     53: 
                     54: int    errors = 0;
                     55: int    line = 1;
                     56: %}
                     57: 
                     58: %%
                     59: file:
                     60:        bunch_of_defs { if (checkdefs() < 0) return (errors); } bunch_of_lines
                     61:                { 
                     62:                if (sp->num_exits + sp->num_airports < 2)
                     63:                        yyerror("Need at least 2 airports and/or exits.");
                     64:                return (errors);
                     65:                }
                     66:        ;
                     67: 
                     68: bunch_of_defs:
                     69:        def bunch_of_defs
                     70:        | def
                     71:        ;
                     72: 
                     73: def:
                     74:        udef
                     75:        | ndef
                     76:        | wdef
                     77:        | hdef
                     78:        ;
                     79: 
                     80: udef:
                     81:        UpdateOp '=' ConstOp ';'
                     82:                {
                     83:                if (sp->update_secs != 0)
                     84:                        return (yyerror("Redefinition of 'update'."));
                     85:                else if ($3 < 1)
                     86:                        return (yyerror("'update' is too small."));
                     87:                else
                     88:                        sp->update_secs = $3;
                     89:                }
                     90:        ;
                     91: 
                     92: ndef:
                     93:        NewplaneOp '=' ConstOp ';'
                     94:                {
                     95:                if (sp->newplane_time != 0)
                     96:                        return (yyerror("Redefinition of 'newplane'."));
                     97:                else if ($3 < 1)
                     98:                        return (yyerror("'newplane' is too small."));
                     99:                else
                    100:                        sp->newplane_time = $3;
                    101:                }
                    102:        ;
                    103: 
                    104: hdef:
                    105:        HeightOp '=' ConstOp ';'
                    106:                {
                    107:                if (sp->height != 0)
                    108:                        return (yyerror("Redefinition of 'height'."));
                    109:                else if ($3 < 3)
                    110:                        return (yyerror("'height' is too small."));
                    111:                else
                    112:                        sp->height = $3; 
                    113:                }
                    114:        ;
                    115: 
                    116: wdef:
                    117:        WidthOp '=' ConstOp ';'
                    118:                {
                    119:                if (sp->height != 0)
                    120:                        return (yyerror("Redefinition of 'width'."));
                    121:                else if ($3 < 3)
                    122:                        return (yyerror("'width' is too small."));
                    123:                else
                    124:                        sp->width = $3; 
                    125:                }
                    126:        ;
                    127: 
                    128: bunch_of_lines:
                    129:        line bunch_of_lines
                    130:                {}
                    131:        | line
                    132:                {}
                    133:        ;
                    134: 
                    135: line:
                    136:        BeaconOp ':' Bpoint_list ';'
                    137:                {}
                    138:        | ExitOp ':' Epoint_list ';'
                    139:                {}
                    140:        | LineOp ':' Lline_list ';'
                    141:                {}
                    142:        | AirportOp ':' Apoint_list ';'
                    143:                {}
                    144:        ;
                    145: 
                    146: Bpoint_list:
                    147:        Bpoint Bpoint_list
                    148:                {}
                    149:        | Bpoint
                    150:                {}
                    151:        ;
                    152: 
                    153: Bpoint:
                    154:        '(' ConstOp ConstOp ')'
                    155:                {
                    156:                if (sp->num_beacons % REALLOC == 0) {
                    157:                        if (sp->beacon == NULL)
                    158:                                sp->beacon = (BEACON *) malloc((sp->num_beacons
                    159:                                        + REALLOC) * sizeof (BEACON));
                    160:                        else
                    161:                                sp->beacon = (BEACON *) realloc(sp->beacon,
                    162:                                        (sp->num_beacons + REALLOC) * 
                    163:                                        sizeof (BEACON));
                    164:                        if (sp->beacon == NULL)
                    165:                                return (yyerror("No memory available."));
                    166:                }
                    167:                sp->beacon[sp->num_beacons].x = $2;
                    168:                sp->beacon[sp->num_beacons].y = $3;
                    169:                check_point($2, $3);
                    170:                sp->num_beacons++;
                    171:                }
                    172:        ;
                    173: 
                    174: Epoint_list:
                    175:        Epoint Epoint_list
                    176:                {}
                    177:        | Epoint
                    178:                {}
                    179:        ;
                    180: 
                    181: Epoint:
                    182:        '(' ConstOp ConstOp DirOp ')'
                    183:                {
                    184:                int     dir;
                    185: 
                    186:                if (sp->num_exits % REALLOC == 0) {
                    187:                        if (sp->exit == NULL)
                    188:                                sp->exit = (EXIT *) malloc((sp->num_exits + 
                    189:                                        REALLOC) * sizeof (EXIT));
                    190:                        else
                    191:                                sp->exit = (EXIT *) realloc(sp->exit,
                    192:                                        (sp->num_exits + REALLOC) * 
                    193:                                        sizeof (EXIT));
                    194:                        if (sp->exit == NULL)
                    195:                                return (yyerror("No memory available."));
                    196:                }
                    197:                dir = dir_no($4);
                    198:                sp->exit[sp->num_exits].x = $2;
                    199:                sp->exit[sp->num_exits].y = $3;
                    200:                sp->exit[sp->num_exits].dir = dir;
                    201:                check_edge($2, $3);
                    202:                check_edir($2, $3, dir);
                    203:                sp->num_exits++;
                    204:                }
                    205:        ;
                    206: 
                    207: Apoint_list:
                    208:        Apoint Apoint_list
                    209:                {}
                    210:        | Apoint
                    211:                {}
                    212:        ;
                    213: 
                    214: Apoint:
                    215:        '(' ConstOp ConstOp DirOp ')'
                    216:                {
                    217:                int     dir;
                    218: 
                    219:                if (sp->num_airports % REALLOC == 0) {
                    220:                        if (sp->airport == NULL)
                    221:                                sp->airport=(AIRPORT *)malloc((sp->num_airports
                    222:                                        + REALLOC) * sizeof(AIRPORT));
                    223:                        else
                    224:                                sp->airport = (AIRPORT *) realloc(sp->airport,
                    225:                                        (sp->num_airports + REALLOC) * 
                    226:                                        sizeof(AIRPORT));
                    227:                        if (sp->airport == NULL)
                    228:                                return (yyerror("No memory available."));
                    229:                }
                    230:                dir = dir_no($4);
                    231:                sp->airport[sp->num_airports].x = $2;
                    232:                sp->airport[sp->num_airports].y = $3;
                    233:                sp->airport[sp->num_airports].dir = dir;
                    234:                check_point($2, $3);
                    235:                check_adir($2, $3, dir);
                    236:                sp->num_airports++;
                    237:                }
                    238:        ;
                    239: 
                    240: Lline_list:
                    241:        Lline Lline_list
                    242:                {}
                    243:        | Lline
                    244:                {}
                    245:        ;
                    246: 
                    247: Lline:
                    248:        '[' '(' ConstOp ConstOp ')' '(' ConstOp ConstOp ')' ']'
                    249:                {
                    250:                if (sp->num_lines % REALLOC == 0) {
                    251:                        if (sp->line == NULL)
                    252:                                sp->line = (LINE *) malloc((sp->num_lines + 
                    253:                                        REALLOC) * sizeof (LINE));
                    254:                        else
                    255:                                sp->line = (LINE *) realloc(sp->line,
                    256:                                        (sp->num_lines + REALLOC) *
                    257:                                        sizeof (LINE));
                    258:                        if (sp->line == NULL)
                    259:                                return (yyerror("No memory available."));
                    260:                }
                    261:                sp->line[sp->num_lines].p1.x = $3;
                    262:                sp->line[sp->num_lines].p1.y = $4;
                    263:                sp->line[sp->num_lines].p2.x = $7;
                    264:                sp->line[sp->num_lines].p2.y = $8;
                    265:                check_line($3, $4, $7, $8);
                    266:                sp->num_lines++;
                    267:                }
                    268:        ;
                    269: %%
                    270: 
                    271: check_edge(x, y)
                    272: {
                    273:        if (!(x == 0) && !(x == sp->width - 1) && 
                    274:            !(y == 0) && !(y == sp->height - 1))
                    275:                yyerror("edge value not on edge.");
                    276: }
                    277: 
                    278: check_point(x, y)
                    279: {
                    280:        if (x < 1 || x >= sp->width - 1)
                    281:                yyerror("X value out of range.");
                    282:        if (y < 1 || y >= sp->height - 1)
                    283:                yyerror("Y value out of range.");
                    284: }
                    285: 
                    286: check_linepoint(x, y)
                    287: {
                    288:        if (x < 0 || x >= sp->width)
                    289:                yyerror("X value out of range.");
                    290:        if (y < 0 || y >= sp->height)
                    291:                yyerror("Y value out of range.");
                    292: }
                    293: 
                    294: check_line(x1, y1, x2, y2)
                    295: {
                    296:        int     d1, d2;
                    297: 
                    298:        check_linepoint(x1, y1);
                    299:        check_linepoint(x2, y2);
                    300: 
                    301:        d1 = ABS(x2 - x1);
                    302:        d2 = ABS(y2 - y1);
                    303: 
                    304:        if (!(d1 == d2) && !(d1 == 0) && !(d2 == 0))
                    305:                yyerror("Bad line endpoints.");
                    306: }
                    307: 
                    308: yyerror(s)
                    309: {
                    310:        fprintf(stderr, "\"%s\": line %d: %s\n", file, line, s);
                    311:        errors++;
                    312: 
                    313:        return (errors);
                    314: }
                    315: 
                    316: check_edir(x, y, dir)
                    317: {
                    318:        int     bad = 0;
                    319: 
                    320:        if (x == sp->width - 1)
                    321:                x = 2;
                    322:        else if (x != 0)
                    323:                x = 1;
                    324:        if (y == sp->height - 1)
                    325:                y = 2;
                    326:        else if (y != 0)
                    327:                y = 1;
                    328:        
                    329:        switch (x * 10 + y) {
                    330:        case 00: if (dir != 3) bad++; break;
                    331:        case 01: if (dir < 1 || dir > 3) bad++; break;
                    332:        case 02: if (dir != 1) bad++; break;
                    333:        case 10: if (dir < 3 || dir > 5) bad++; break;
                    334:        case 11: break;
                    335:        case 12: if (dir > 1 && dir < 7) bad++; break;
                    336:        case 20: if (dir != 5) bad++; break;
                    337:        case 21: if (dir < 5) bad++; break;
                    338:        case 22: if (dir != 7) bad++; break;
                    339:        default:
                    340:                yyerror("Unknown value in checkdir!  Get help!");
                    341:                break;
                    342:        }
                    343:        if (bad)
                    344:                yyerror("Bad direction for entrance at exit.");
                    345: }
                    346: 
                    347: check_adir(x, y, dir)
                    348: {
                    349: }
                    350: 
                    351: checkdefs()
                    352: {
                    353:        int     err = 0;
                    354: 
                    355:        if (sp->width == 0) {
                    356:                yyerror("'width' undefined.");
                    357:                err++;
                    358:        }
                    359:        if (sp->height == 0) {
                    360:                yyerror("'height' undefined.");
                    361:                err++;
                    362:        }
                    363:        if (sp->update_secs == 0) {
                    364:                yyerror("'update' undefined.");
                    365:                err++;
                    366:        }
                    367:        if (sp->newplane_time == 0) {
                    368:                yyerror("'newplane' undefined.");
                    369:                err++;
                    370:        }
                    371:        if (err)
                    372:                return (-1);
                    373:        else
                    374:                return (0);
                    375: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.