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