Annotation of Net2/ddb/db_expr.c, revision 1.1.1.2

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.