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