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