Annotation of src/usr.bin/window/parser3.c, Revision 1.1
1.1 ! deraadt 1: /* $NetBSD: parser3.c,v 1.3 1995/09/28 10:34:33 tls 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. All advertising materials mentioning features or use of this software
! 19: * must display the following acknowledgement:
! 20: * This product includes software developed by the University of
! 21: * California, Berkeley and its contributors.
! 22: * 4. Neither the name of the University nor the names of its contributors
! 23: * may be used to endorse or promote products derived from this software
! 24: * without specific prior written permission.
! 25: *
! 26: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
! 27: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 28: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 29: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
! 30: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 31: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 32: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 33: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 34: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 35: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 36: * SUCH DAMAGE.
! 37: */
! 38:
! 39: #ifndef lint
! 40: #if 0
! 41: static char sccsid[] = "@(#)parser3.c 8.1 (Berkeley) 6/6/93";
! 42: #else
! 43: static char rcsid[] = "$NetBSD: parser3.c,v 1.3 1995/09/28 10:34:33 tls Exp $";
! 44: #endif
! 45: #endif /* not lint */
! 46:
! 47: #include "parser.h"
! 48:
! 49: /*
! 50: * =
! 51: * ? :
! 52: * ||
! 53: * &&
! 54: * |
! 55: * ^
! 56: * &
! 57: * == !=
! 58: * <= >=
! 59: * << >>
! 60: * + -
! 61: * * / %
! 62: * unary - + ~ !
! 63: */
! 64: p_expr(v, flag)
! 65: register struct value *v;
! 66: char flag;
! 67: {
! 68: struct value t;
! 69: int ret;
! 70:
! 71: if (p_expr0(&t, flag) < 0)
! 72: return -1;
! 73:
! 74: if (token != T_ASSIGN) {
! 75: *v = t;
! 76: return 0;
! 77: }
! 78: switch (t.v_type) {
! 79: case V_NUM:
! 80: p_error("%d: Not a variable.", t.v_num);
! 81: case V_ERR:
! 82: t.v_str = 0;
! 83: break;
! 84: }
! 85: ret = p_assign(t.v_str, v, flag);
! 86: if (t.v_str != 0)
! 87: str_free(t.v_str);
! 88: return ret;
! 89: }
! 90:
! 91: /*
! 92: * ? :
! 93: */
! 94: p_expr0(v, flag)
! 95: register struct value *v;
! 96: char flag;
! 97: {
! 98: struct value t;
! 99: char true;
! 100:
! 101: if (p_expr1(v, flag) < 0)
! 102: return -1;
! 103: if (token != T_QUEST)
! 104: return 0;
! 105: switch (v->v_type) {
! 106: case V_NUM:
! 107: true = v->v_num != 0;
! 108: break;
! 109: case V_STR:
! 110: p_error("?: Numeric left operand required.");
! 111: str_free(v->v_str);
! 112: v->v_type = V_ERR;
! 113: case V_ERR:
! 114: flag = 0;
! 115: break;
! 116: }
! 117: (void) s_gettok();
! 118: v->v_type = V_ERR;
! 119: if ((flag && true ? p_expr1(v, 1) : p_expr1(&t, 0)) < 0)
! 120: return -1;
! 121: if (token != T_COLON) {
! 122: val_free(*v);
! 123: p_synerror();
! 124: return -1;
! 125: }
! 126: (void) s_gettok();
! 127: return flag && !true ? p_expr1(v, 1) : p_expr1(&t, 0);
! 128: }
! 129:
! 130: /*
! 131: * ||
! 132: */
! 133: p_expr1(v, flag)
! 134: register struct value *v;
! 135: char flag;
! 136: {
! 137: char true = 0;
! 138:
! 139: if (p_expr2(v, flag) < 0)
! 140: return -1;
! 141: if (token != T_OROR)
! 142: return 0;
! 143: for (;;) {
! 144: switch (v->v_type) {
! 145: case V_NUM:
! 146: v->v_num = true = true || v->v_num != 0;
! 147: break;
! 148: case V_STR:
! 149: p_error("||: Numeric operands required.");
! 150: str_free(v->v_str);
! 151: v->v_type = V_ERR;
! 152: case V_ERR:
! 153: flag = 0;
! 154: break;
! 155: }
! 156: if (token != T_OROR)
! 157: return 0;
! 158: (void) s_gettok();
! 159: if (p_expr2(v, flag && !true) < 0)
! 160: return -1;
! 161: }
! 162: }
! 163:
! 164: /*
! 165: * &&
! 166: */
! 167: p_expr2(v, flag)
! 168: register struct value *v;
! 169: char flag;
! 170: {
! 171: char true = 1;
! 172:
! 173: if (p_expr3_10(3, v, flag) < 0)
! 174: return -1;
! 175: if (token != T_ANDAND)
! 176: return 0;
! 177: for (;;) {
! 178: switch (v->v_type) {
! 179: case V_NUM:
! 180: v->v_num = true = true && v->v_num != 0;
! 181: break;
! 182: case V_STR:
! 183: p_error("&&: Numeric operands required.");
! 184: str_free(v->v_str);
! 185: v->v_type = V_ERR;
! 186: case V_ERR:
! 187: flag = 0;
! 188: break;
! 189: }
! 190: if (token != T_ANDAND)
! 191: return 0;
! 192: (void) s_gettok();
! 193: if (p_expr3_10(3, v, flag && true) < 0)
! 194: return -1;
! 195: }
! 196: /*NOTREACHED*/
! 197: }