added my responsibility for the `cpm' port
[unix-history] / sys / ddb / db_expr.c
CommitLineData
15637ed4
RG
1/*
2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
4 * All Rights Reserved.
5 *
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
11 *
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15 *
16 * Carnegie Mellon requests users of this software to return to
17 *
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
22 *
23 * any improvements or extensions that they make and grant Carnegie the
24 * rights to redistribute these changes.
15637ed4 25 *
4c45483e 26 * $Id: db_expr.c,v 1.2 1993/10/16 16:47:14 rgrimes Exp $
15637ed4 27 */
cbeffc91 28
15637ed4
RG
29/*
30 * Author: David B. Golub, Carnegie Mellon University
31 * Date: 7/90
32 */
33#include "param.h"
4c45483e 34#include "systm.h"
15637ed4 35#include "proc.h"
4c45483e 36#include "ddb/ddb.h"
15637ed4
RG
37#include <ddb/db_lex.h>
38#include <ddb/db_access.h>
39#include <ddb/db_command.h>
40
41boolean_t
42db_term(valuep)
43 db_expr_t *valuep;
44{
45 int t;
46
47 t = db_read_token();
48 if (t == tIDENT) {
49 if (!db_value_of_name(db_tok_string, valuep)) {
50 db_error("Symbol not found\n");
51 /*NOTREACHED*/
52 }
53 return (TRUE);
54 }
55 if (t == tNUMBER) {
56 *valuep = (db_expr_t)db_tok_number;
57 return (TRUE);
58 }
59 if (t == tDOT) {
60 *valuep = (db_expr_t)db_dot;
61 return (TRUE);
62 }
63 if (t == tDOTDOT) {
64 *valuep = (db_expr_t)db_prev;
65 return (TRUE);
66 }
67 if (t == tPLUS) {
68 *valuep = (db_expr_t) db_next;
69 return (TRUE);
70 }
71 if (t == tDITTO) {
72 *valuep = (db_expr_t)db_last_addr;
73 return (TRUE);
74 }
75 if (t == tDOLLAR) {
76 if (!db_get_variable(valuep))
77 return (FALSE);
78 return (TRUE);
79 }
80 if (t == tLPAREN) {
81 if (!db_expression(valuep)) {
82 db_error("Syntax error\n");
83 /*NOTREACHED*/
84 }
85 t = db_read_token();
86 if (t != tRPAREN) {
87 db_error("Syntax error\n");
88 /*NOTREACHED*/
89 }
90 return (TRUE);
91 }
92 db_unread_token(t);
93 return (FALSE);
94}
95
96boolean_t
97db_unary(valuep)
98 db_expr_t *valuep;
99{
100 int t;
101
102 t = db_read_token();
103 if (t == tMINUS) {
104 if (!db_unary(valuep)) {
105 db_error("Syntax error\n");
106 /*NOTREACHED*/
107 }
108 *valuep = -*valuep;
109 return (TRUE);
110 }
111 if (t == tSTAR) {
112 /* indirection */
113 if (!db_unary(valuep)) {
114 db_error("Syntax error\n");
115 /*NOTREACHED*/
116 }
117 *valuep = db_get_value((db_addr_t)*valuep, sizeof(int), FALSE);
118 return (TRUE);
119 }
120 db_unread_token(t);
121 return (db_term(valuep));
122}
123
124boolean_t
125db_mult_expr(valuep)
126 db_expr_t *valuep;
127{
128 db_expr_t lhs, rhs;
129 int t;
130
131 if (!db_unary(&lhs))
132 return (FALSE);
133
134 t = db_read_token();
135 while (t == tSTAR || t == tSLASH || t == tPCT || t == tHASH) {
136 if (!db_term(&rhs)) {
137 db_error("Syntax error\n");
138 /*NOTREACHED*/
139 }
140 if (t == tSTAR)
141 lhs *= rhs;
142 else {
143 if (rhs == 0) {
144 db_error("Divide by 0\n");
145 /*NOTREACHED*/
146 }
147 if (t == tSLASH)
148 lhs /= rhs;
149 else if (t == tPCT)
150 lhs %= rhs;
151 else
152 lhs = ((lhs+rhs-1)/rhs)*rhs;
153 }
154 t = db_read_token();
155 }
156 db_unread_token(t);
157 *valuep = lhs;
158 return (TRUE);
159}
160
161boolean_t
162db_add_expr(valuep)
163 db_expr_t *valuep;
164{
165 db_expr_t lhs, rhs;
166 int t;
167
168 if (!db_mult_expr(&lhs))
169 return (FALSE);
170
171 t = db_read_token();
172 while (t == tPLUS || t == tMINUS) {
173 if (!db_mult_expr(&rhs)) {
174 db_error("Syntax error\n");
175 /*NOTREACHED*/
176 }
177 if (t == tPLUS)
178 lhs += rhs;
179 else
180 lhs -= rhs;
181 t = db_read_token();
182 }
183 db_unread_token(t);
184 *valuep = lhs;
185 return (TRUE);
186}
187
188boolean_t
189db_shift_expr(valuep)
190 db_expr_t *valuep;
191{
192 db_expr_t lhs, rhs;
193 int t;
194
195 if (!db_add_expr(&lhs))
196 return (FALSE);
197
198 t = db_read_token();
199 while (t == tSHIFT_L || t == tSHIFT_R) {
200 if (!db_add_expr(&rhs)) {
201 db_error("Syntax error\n");
202 /*NOTREACHED*/
203 }
204 if (rhs < 0) {
205 db_error("Negative shift amount\n");
206 /*NOTREACHED*/
207 }
208 if (t == tSHIFT_L)
209 lhs <<= rhs;
210 else {
211 /* Shift right is unsigned */
212 lhs = (unsigned) lhs >> rhs;
213 }
214 t = db_read_token();
215 }
216 db_unread_token(t);
217 *valuep = lhs;
218 return (TRUE);
219}
220
221int
222db_expression(valuep)
223 db_expr_t *valuep;
224{
225 return (db_shift_expr(valuep));
226}