|
|
1.1 root 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 [email protected]
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.
25: */
26: /*
27: * HISTORY
28: * $Log: db_expr.c,v $
29: * Revision 1.1 1992/03/25 21:45:09 pace
30: * Initial revision
31: *
32: * Revision 2.3 91/02/05 17:06:25 mrt
33: * Changed to new Mach copyright
34: * [91/01/31 16:17:46 mrt]
35: *
36: * Revision 2.2 90/08/27 21:50:57 dbg
37: * Use '..' instead of '$$' for db_prev.
38: * Use '+' for db_next.
39: * [90/08/22 dbg]
40: *
41: * Allow repeated unary operators.
42: * [90/08/20 dbg]
43: *
44: * Reflected back rename of db_symbol_value->db_value_of_name
45: * [90/08/20 af]
46: * Reduce lint.
47: * [90/08/07 dbg]
48: * Created.
49: * [90/07/25 dbg]
50: *
51: */
52: /*
53: * Author: David B. Golub, Carnegie Mellon University
54: * Date: 7/90
55: */
56: #include "param.h"
57: #include "proc.h"
58: #include <machine/db_machdep.h>
59: #include <ddb/db_lex.h>
60: #include <ddb/db_access.h>
61: #include <ddb/db_command.h>
62:
63: boolean_t
64: db_term(valuep)
65: db_expr_t *valuep;
66: {
67: int t;
68:
69: t = db_read_token();
70: if (t == tIDENT) {
71: if (!db_value_of_name(db_tok_string, valuep)) {
72: db_error("Symbol not found\n");
73: /*NOTREACHED*/
74: }
75: return (TRUE);
76: }
77: if (t == tNUMBER) {
78: *valuep = (db_expr_t)db_tok_number;
79: return (TRUE);
80: }
81: if (t == tDOT) {
82: *valuep = (db_expr_t)db_dot;
83: return (TRUE);
84: }
85: if (t == tDOTDOT) {
86: *valuep = (db_expr_t)db_prev;
87: return (TRUE);
88: }
89: if (t == tPLUS) {
90: *valuep = (db_expr_t) db_next;
91: return (TRUE);
92: }
93: if (t == tDITTO) {
94: *valuep = (db_expr_t)db_last_addr;
95: return (TRUE);
96: }
97: if (t == tDOLLAR) {
98: if (!db_get_variable(valuep))
99: return (FALSE);
100: return (TRUE);
101: }
102: if (t == tLPAREN) {
103: if (!db_expression(valuep)) {
104: db_error("Syntax error\n");
105: /*NOTREACHED*/
106: }
107: t = db_read_token();
108: if (t != tRPAREN) {
109: db_error("Syntax error\n");
110: /*NOTREACHED*/
111: }
112: return (TRUE);
113: }
114: db_unread_token(t);
115: return (FALSE);
116: }
117:
118: boolean_t
119: db_unary(valuep)
120: db_expr_t *valuep;
121: {
122: int t;
123:
124: t = db_read_token();
125: if (t == tMINUS) {
126: if (!db_unary(valuep)) {
127: db_error("Syntax error\n");
128: /*NOTREACHED*/
129: }
130: *valuep = -*valuep;
131: return (TRUE);
132: }
133: if (t == tSTAR) {
134: /* indirection */
135: if (!db_unary(valuep)) {
136: db_error("Syntax error\n");
137: /*NOTREACHED*/
138: }
139: *valuep = db_get_value((db_addr_t)*valuep, sizeof(int), FALSE);
140: return (TRUE);
141: }
142: db_unread_token(t);
143: return (db_term(valuep));
144: }
145:
146: boolean_t
147: db_mult_expr(valuep)
148: db_expr_t *valuep;
149: {
150: db_expr_t lhs, rhs;
151: int t;
152:
153: if (!db_unary(&lhs))
154: return (FALSE);
155:
156: t = db_read_token();
157: while (t == tSTAR || t == tSLASH || t == tPCT || t == tHASH) {
158: if (!db_term(&rhs)) {
159: db_error("Syntax error\n");
160: /*NOTREACHED*/
161: }
162: if (t == tSTAR)
163: lhs *= rhs;
164: else {
165: if (rhs == 0) {
166: db_error("Divide by 0\n");
167: /*NOTREACHED*/
168: }
169: if (t == tSLASH)
170: lhs /= rhs;
171: else if (t == tPCT)
172: lhs %= rhs;
173: else
174: lhs = ((lhs+rhs-1)/rhs)*rhs;
175: }
176: t = db_read_token();
177: }
178: db_unread_token(t);
179: *valuep = lhs;
180: return (TRUE);
181: }
182:
183: boolean_t
184: db_add_expr(valuep)
185: db_expr_t *valuep;
186: {
187: db_expr_t lhs, rhs;
188: int t;
189:
190: if (!db_mult_expr(&lhs))
191: return (FALSE);
192:
193: t = db_read_token();
194: while (t == tPLUS || t == tMINUS) {
195: if (!db_mult_expr(&rhs)) {
196: db_error("Syntax error\n");
197: /*NOTREACHED*/
198: }
199: if (t == tPLUS)
200: lhs += rhs;
201: else
202: lhs -= rhs;
203: t = db_read_token();
204: }
205: db_unread_token(t);
206: *valuep = lhs;
207: return (TRUE);
208: }
209:
210: boolean_t
211: db_shift_expr(valuep)
212: db_expr_t *valuep;
213: {
214: db_expr_t lhs, rhs;
215: int t;
216:
217: if (!db_add_expr(&lhs))
218: return (FALSE);
219:
220: t = db_read_token();
221: while (t == tSHIFT_L || t == tSHIFT_R) {
222: if (!db_add_expr(&rhs)) {
223: db_error("Syntax error\n");
224: /*NOTREACHED*/
225: }
226: if (rhs < 0) {
227: db_error("Negative shift amount\n");
228: /*NOTREACHED*/
229: }
230: if (t == tSHIFT_L)
231: lhs <<= rhs;
232: else {
233: /* Shift right is unsigned */
234: lhs = (unsigned) lhs >> rhs;
235: }
236: t = db_read_token();
237: }
238: db_unread_token(t);
239: *valuep = lhs;
240: return (TRUE);
241: }
242:
243: int
244: db_expression(valuep)
245: db_expr_t *valuep;
246: {
247: return (db_shift_expr(valuep));
248: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.