xref: /dragonfly/usr.bin/window/parser3.c (revision 235099c3)
1 /*	$NetBSD: parser3.c,v 1.6 2003/08/07 11:17:28 agc 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. 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 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)parser3.c	8.1 (Berkeley) 6/6/93";
39 #else
40 __RCSID("$NetBSD: parser3.c,v 1.6 2003/08/07 11:17:28 agc Exp $");
41 #endif
42 #endif /* not lint */
43 
44 #include "defs.h"
45 #include "parser.h"
46 
47 /*
48  * =
49  * ? :
50  * ||
51  * &&
52  * |
53  * ^
54  * &
55  * == !=
56  * <= >=
57  * << >>
58  * + -
59  * * / %
60  * unary - + ~ !
61  */
62 int
63 p_expr(struct value *v, 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 int
92 p_expr0(struct value *v, char flag)
93 {
94 	struct value t;
95 	char true = 0;
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 int
130 p_expr1(struct value *v, char flag)
131 {
132 	char true = 0;
133 
134 	if (p_expr2(v, flag) < 0)
135 		return -1;
136 	if (token != T_OROR)
137 		return 0;
138 	for (;;) {
139 		switch (v->v_type) {
140 		case V_NUM:
141 			v->v_num = true = true || v->v_num != 0;
142 			break;
143 		case V_STR:
144 			p_error("||: Numeric operands required.");
145 			str_free(v->v_str);
146 			v->v_type = V_ERR;
147 		case V_ERR:
148 			flag = 0;
149 			break;
150 		}
151 		if (token != T_OROR)
152 			return 0;
153 		(void) s_gettok();
154 		if (p_expr2(v, flag && !true) < 0)
155 			return -1;
156 	}
157 }
158 
159 /*
160  * &&
161  */
162 int
163 p_expr2(struct value *v, char flag)
164 {
165 	char true = 1;
166 
167 	if (p_expr3_10(3, v, flag) < 0)
168 		return -1;
169 	if (token != T_ANDAND)
170 		return 0;
171 	for (;;) {
172 		switch (v->v_type) {
173 		case V_NUM:
174 			v->v_num = true = true && v->v_num != 0;
175 			break;
176 		case V_STR:
177 			p_error("&&: Numeric operands required.");
178 			str_free(v->v_str);
179 			v->v_type = V_ERR;
180 		case V_ERR:
181 			flag = 0;
182 			break;
183 		}
184 		if (token != T_ANDAND)
185 			return 0;
186 		(void) s_gettok();
187 		if (p_expr3_10(3, v, flag && true) < 0)
188 			return -1;
189 	}
190 	/*NOTREACHED*/
191 }
192