Initial import from FreeBSD RELENG_4:
[games.git] / usr.bin / window / parser3.c
1 /*
2  * Copyright (c) 1983, 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  * Edward Wang at The University of California, Berkeley.
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. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 #ifndef lint
38 static char sccsid[] = "@(#)parser3.c   8.1 (Berkeley) 6/6/93";
39 static char rcsid[] =
40   "$FreeBSD: src/usr.bin/window/parser3.c,v 1.1.1.1.14.1 2001/05/17 09:45:00 obrien Exp $";
41 #endif /* not lint */
42
43 #include "parser.h"
44
45 /*
46  * =
47  * ? :
48  * ||
49  * &&
50  * |
51  * ^
52  * &
53  * == !=
54  * <= >=
55  * << >>
56  * + -
57  * * / %
58  * unary - + ~ !
59  */
60 p_expr(v, flag)
61 register struct value *v;
62 char flag;
63 {
64         struct value t;
65         int ret;
66
67         if (p_expr0(&t, flag) < 0)
68                 return -1;
69
70         if (token != T_ASSIGN) {
71                 *v = t;
72                 return 0;
73         }
74         switch (t.v_type) {
75         case V_NUM:
76                 p_error("%d: Not a variable.", t.v_num);
77         case V_ERR:
78                 t.v_str = 0;
79                 break;
80         }
81         ret = p_assign(t.v_str, v, flag);
82         if (t.v_str != 0)
83                 str_free(t.v_str);
84         return ret;
85 }
86
87 /*
88  * ? :
89  */
90 p_expr0(v, flag)
91 register struct value *v;
92 char flag;
93 {
94         struct value t;
95         char true;
96
97         if (p_expr1(v, flag) < 0)
98                 return -1;
99         if (token != T_QUEST)
100                 return 0;
101         switch (v->v_type) {
102         case V_NUM:
103                 true = v->v_num != 0;
104                 break;
105         case V_STR:
106                 p_error("?: Numeric left operand required.");
107                 str_free(v->v_str);
108                 v->v_type = V_ERR;
109         case V_ERR:
110                 flag = 0;
111                 break;
112         }
113         (void) s_gettok();
114         v->v_type = V_ERR;
115         if ((flag && true ? p_expr1(v, 1) : p_expr1(&t, 0)) < 0)
116                 return -1;
117         if (token != T_COLON) {
118                 val_free(*v);
119                 p_synerror();
120                 return -1;
121         }
122         (void) s_gettok();
123         return flag && !true ? p_expr1(v, 1) : p_expr1(&t, 0);
124 }
125
126 /*
127  * ||
128  */
129 p_expr1(v, flag)
130 register struct value *v;
131 char flag;
132 {
133         char true = 0;
134
135         if (p_expr2(v, flag) < 0)
136                 return -1;
137         if (token != T_OROR)
138                 return 0;
139         for (;;) {
140                 switch (v->v_type) {
141                 case V_NUM:
142                         v->v_num = true = true || v->v_num != 0;
143                         break;
144                 case V_STR:
145                         p_error("||: Numeric operands required.");
146                         str_free(v->v_str);
147                         v->v_type = V_ERR;
148                 case V_ERR:
149                         flag = 0;
150                         break;
151                 }
152                 if (token != T_OROR)
153                         return 0;
154                 (void) s_gettok();
155                 if (p_expr2(v, flag && !true) < 0)
156                         return -1;
157         }
158 }
159
160 /*
161  * &&
162  */
163 p_expr2(v, flag)
164 register struct value *v;
165 char flag;
166 {
167         char true = 1;
168
169         if (p_expr3_10(3, v, flag) < 0)
170                 return -1;
171         if (token != T_ANDAND)
172                 return 0;
173         for (;;) {
174                 switch (v->v_type) {
175                 case V_NUM:
176                         v->v_num = true = true && v->v_num != 0;
177                         break;
178                 case V_STR:
179                         p_error("&&: Numeric operands required.");
180                         str_free(v->v_str);
181                         v->v_type = V_ERR;
182                 case V_ERR:
183                         flag = 0;
184                         break;
185                 }
186                 if (token != T_ANDAND)
187                         return 0;
188                 (void) s_gettok();
189                 if (p_expr3_10(3, v, flag && true) < 0)
190                         return -1;
191         }
192         /*NOTREACHED*/
193 }