expr.c (95998) | expr.c (99939) |
---|---|
1/* $OpenBSD: expr.c,v 1.14 2002/04/26 16:15:16 espie Exp $ */ 2/* $NetBSD: expr.c,v 1.7 1995/09/28 05:37:31 tls Exp $ */ 3 4/* 5 * Copyright (c) 1989, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by --- 34 unchanged lines hidden (view full) --- 43#else 44#if 0 45static char rcsid[] = "$OpenBSD: expr.c,v 1.14 2002/04/26 16:15:16 espie Exp $"; 46#endif 47#endif 48#endif /* not lint */ 49 50#include <sys/cdefs.h> | 1/* $OpenBSD: expr.c,v 1.14 2002/04/26 16:15:16 espie Exp $ */ 2/* $NetBSD: expr.c,v 1.7 1995/09/28 05:37:31 tls Exp $ */ 3 4/* 5 * Copyright (c) 1989, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by --- 34 unchanged lines hidden (view full) --- 43#else 44#if 0 45static char rcsid[] = "$OpenBSD: expr.c,v 1.14 2002/04/26 16:15:16 espie Exp $"; 46#endif 47#endif 48#endif /* not lint */ 49 50#include <sys/cdefs.h> |
51__FBSDID("$FreeBSD: head/usr.bin/m4/expr.c 95998 2002-05-03 20:46:10Z jmallett $"); | 51__FBSDID("$FreeBSD: head/usr.bin/m4/expr.c 99939 2002-07-14 02:03:23Z jmallett $"); |
52 53#include <sys/types.h> 54#include <ctype.h> 55#include <err.h> 56#include <stddef.h> 57#include <stdio.h> 58#include "mdef.h" 59#include "extern.h" --- 103 unchanged lines hidden (view full) --- 163 printf("m4: ill-formed expression.\n"); 164 return FALSE; 165} 166 167/* 168 * query : lor | lor '?' query ':' query 169 */ 170static int | 52 53#include <sys/types.h> 54#include <ctype.h> 55#include <err.h> 56#include <stddef.h> 57#include <stdio.h> 58#include "mdef.h" 59#include "extern.h" --- 103 unchanged lines hidden (view full) --- 163 printf("m4: ill-formed expression.\n"); 164 return FALSE; 165} 166 167/* 168 * query : lor | lor '?' query ':' query 169 */ 170static int |
171query() | 171query(void) |
172{ 173 int result, true_val, false_val; 174 175 result = lor(); 176 if (skipws() != '?') { 177 ungetch(); 178 return result; 179 } --- 5 unchanged lines hidden (view full) --- 185 false_val = query(); 186 return result ? true_val : false_val; 187} 188 189/* 190 * lor : land { '||' land } 191 */ 192static int | 172{ 173 int result, true_val, false_val; 174 175 result = lor(); 176 if (skipws() != '?') { 177 ungetch(); 178 return result; 179 } --- 5 unchanged lines hidden (view full) --- 185 false_val = query(); 186 return result ? true_val : false_val; 187} 188 189/* 190 * lor : land { '||' land } 191 */ 192static int |
193lor() | 193lor(void) |
194{ 195 int c, vl, vr; 196 197 vl = land(); 198 while ((c = skipws()) == '|') { 199 if (getch() != '|') 200 ungetch(); 201 vr = land(); 202 vl = vl || vr; 203 } 204 205 ungetch(); 206 return vl; 207} 208 209/* 210 * land : not { '&&' not } 211 */ 212static int | 194{ 195 int c, vl, vr; 196 197 vl = land(); 198 while ((c = skipws()) == '|') { 199 if (getch() != '|') 200 ungetch(); 201 vr = land(); 202 vl = vl || vr; 203 } 204 205 ungetch(); 206 return vl; 207} 208 209/* 210 * land : not { '&&' not } 211 */ 212static int |
213land() | 213land(void) |
214{ 215 int c, vl, vr; 216 217 vl = not(); 218 while ((c = skipws()) == '&') { 219 if (getch() != '&') 220 ungetch(); 221 vr = not(); 222 vl = vl && vr; 223 } 224 225 ungetch(); 226 return vl; 227} 228 229/* 230 * not : eqrel | '!' not 231 */ 232static int | 214{ 215 int c, vl, vr; 216 217 vl = not(); 218 while ((c = skipws()) == '&') { 219 if (getch() != '&') 220 ungetch(); 221 vr = not(); 222 vl = vl && vr; 223 } 224 225 ungetch(); 226 return vl; 227} 228 229/* 230 * not : eqrel | '!' not 231 */ 232static int |
233not() | 233not(void) |
234{ 235 int val, c; 236 237 if ((c = skipws()) == '!' && getch() != '=') { 238 ungetch(); 239 val = not(); 240 return !val; 241 } 242 243 if (c == '!') 244 ungetch(); 245 ungetch(); 246 return eqrel(); 247} 248 249/* 250 * eqrel : shift { eqrelop shift } 251 */ 252static int | 234{ 235 int val, c; 236 237 if ((c = skipws()) == '!' && getch() != '=') { 238 ungetch(); 239 val = not(); 240 return !val; 241 } 242 243 if (c == '!') 244 ungetch(); 245 ungetch(); 246 return eqrel(); 247} 248 249/* 250 * eqrel : shift { eqrelop shift } 251 */ 252static int |
253eqrel() | 253eqrel(void) |
254{ 255 int vl, vr, op; 256 257 vl = shift(); 258 while ((op = geteqrel()) != -1) { 259 vr = shift(); 260 261 switch (op) { --- 21 unchanged lines hidden (view full) --- 283 } 284 return vl; 285} 286 287/* 288 * shift : primary { shop primary } 289 */ 290static int | 254{ 255 int vl, vr, op; 256 257 vl = shift(); 258 while ((op = geteqrel()) != -1) { 259 vr = shift(); 260 261 switch (op) { --- 21 unchanged lines hidden (view full) --- 283 } 284 return vl; 285} 286 287/* 288 * shift : primary { shop primary } 289 */ 290static int |
291shift() | 291shift(void) |
292{ 293 int vl, vr, c; 294 295 vl = primary(); 296 while (((c = skipws()) == '<' || c == '>') && getch() == c) { 297 vr = primary(); 298 299 if (c == '<') --- 7 unchanged lines hidden (view full) --- 307 ungetch(); 308 return vl; 309} 310 311/* 312 * primary : term { addop term } 313 */ 314static int | 292{ 293 int vl, vr, c; 294 295 vl = primary(); 296 while (((c = skipws()) == '<' || c == '>') && getch() == c) { 297 vr = primary(); 298 299 if (c == '<') --- 7 unchanged lines hidden (view full) --- 307 ungetch(); 308 return vl; 309} 310 311/* 312 * primary : term { addop term } 313 */ 314static int |
315primary() | 315primary(void) |
316{ 317 int c, vl, vr; 318 319 vl = term(); 320 while ((c = skipws()) == '+' || c == '-') { 321 vr = term(); 322 323 if (c == '+') --- 5 unchanged lines hidden (view full) --- 329 ungetch(); 330 return vl; 331} 332 333/* 334 * <term> := <exp> { <mulop> <exp> } 335 */ 336static int | 316{ 317 int c, vl, vr; 318 319 vl = term(); 320 while ((c = skipws()) == '+' || c == '-') { 321 vr = term(); 322 323 if (c == '+') --- 5 unchanged lines hidden (view full) --- 329 ungetch(); 330 return vl; 331} 332 333/* 334 * <term> := <exp> { <mulop> <exp> } 335 */ 336static int |
337term() | 337term(void) |
338{ 339 int c, vl, vr; 340 341 vl = exp(); 342 while ((c = skipws()) == '*' || c == '/' || c == '%') { 343 vr = exp(); 344 345 switch (c) { --- 17 unchanged lines hidden (view full) --- 363 ungetch(); 364 return vl; 365} 366 367/* 368 * <term> := <unary> { <expop> <unary> } 369 */ 370static int | 338{ 339 int c, vl, vr; 340 341 vl = exp(); 342 while ((c = skipws()) == '*' || c == '/' || c == '%') { 343 vr = exp(); 344 345 switch (c) { --- 17 unchanged lines hidden (view full) --- 363 ungetch(); 364 return vl; 365} 366 367/* 368 * <term> := <unary> { <expop> <unary> } 369 */ 370static int |
371exp() | 371exp(void) |
372{ 373 int c, vl, vr, n; 374 375 vl = unary(); 376 switch (c = skipws()) { 377 378 case '*': 379 if (getch() != '*') { --- 12 unchanged lines hidden (view full) --- 392 ungetch(); 393 return vl; 394} 395 396/* 397 * unary : factor | unop unary 398 */ 399static int | 372{ 373 int c, vl, vr, n; 374 375 vl = unary(); 376 switch (c = skipws()) { 377 378 case '*': 379 if (getch() != '*') { --- 12 unchanged lines hidden (view full) --- 392 ungetch(); 393 return vl; 394} 395 396/* 397 * unary : factor | unop unary 398 */ 399static int |
400unary() | 400unary(void) |
401{ 402 int val, c; 403 404 if ((c = skipws()) == '+' || c == '-' || c == '~') { 405 val = unary(); 406 407 switch (c) { 408 case '+': --- 8 unchanged lines hidden (view full) --- 417 ungetch(); 418 return factor(); 419} 420 421/* 422 * factor : constant | '(' query ')' 423 */ 424static int | 401{ 402 int val, c; 403 404 if ((c = skipws()) == '+' || c == '-' || c == '~') { 405 val = unary(); 406 407 switch (c) { 408 case '+': --- 8 unchanged lines hidden (view full) --- 417 ungetch(); 418 return factor(); 419} 420 421/* 422 * factor : constant | '(' query ')' 423 */ 424static int |
425factor() | 425factor(void) |
426{ 427 int val; 428 429 if (skipws() == '(') { 430 val = query(); 431 if (skipws() != ')') 432 experr("bad factor"); 433 return val; 434 } 435 436 ungetch(); 437 return constant(); 438} 439 440/* 441 * constant: num | 'char' 442 * Note: constant() handles multi-byte constants 443 */ 444static int | 426{ 427 int val; 428 429 if (skipws() == '(') { 430 val = query(); 431 if (skipws() != ')') 432 experr("bad factor"); 433 return val; 434 } 435 436 ungetch(); 437 return constant(); 438} 439 440/* 441 * constant: num | 'char' 442 * Note: constant() handles multi-byte constants 443 */ 444static int |
445constant() | 445constant(void) |
446{ 447 int i; 448 int value; 449 int c; 450 int v[sizeof(int)]; 451 452 if (skipws() != '\'') { 453 ungetch(); --- 44 unchanged lines hidden (view full) --- 498 } 499 return value; 500} 501 502/* 503 * num : digit | num digit 504 */ 505static int | 446{ 447 int i; 448 int value; 449 int c; 450 int v[sizeof(int)]; 451 452 if (skipws() != '\'') { 453 ungetch(); --- 44 unchanged lines hidden (view full) --- 498 } 499 return value; 500} 501 502/* 503 * num : digit | num digit 504 */ 505static int |
506num() | 506num(void) |
507{ 508 int rval, c, base; 509 int ndig; 510 511 rval = 0; 512 ndig = 0; 513 c = skipws(); 514 if (c == '0') { --- 41 unchanged lines hidden (view full) --- 556 557 return rval; 558} 559 560/* 561 * eqrel : '=' | '==' | '!=' | '<' | '>' | '<=' | '>=' 562 */ 563static int | 507{ 508 int rval, c, base; 509 int ndig; 510 511 rval = 0; 512 ndig = 0; 513 c = skipws(); 514 if (c == '0') { --- 41 unchanged lines hidden (view full) --- 556 557 return rval; 558} 559 560/* 561 * eqrel : '=' | '==' | '!=' | '<' | '>' | '<=' | '>=' 562 */ 563static int |
564geteqrel() | 564geteqrel(void) |
565{ 566 int c1, c2; 567 568 c1 = skipws(); 569 c2 = getch(); 570 571 switch (c1) { 572 --- 27 unchanged lines hidden (view full) --- 600 return -1; 601 } 602} 603 604/* 605 * Skip over any white space and return terminating char. 606 */ 607static int | 565{ 566 int c1, c2; 567 568 c1 = skipws(); 569 c2 = getch(); 570 571 switch (c1) { 572 --- 27 unchanged lines hidden (view full) --- 600 return -1; 601 } 602} 603 604/* 605 * Skip over any white space and return terminating char. 606 */ 607static int |
608skipws() | 608skipws(void) |
609{ 610 int c; 611 612 while ((c = getch()) <= ' ' && c > EOS) 613 ; 614 return c; 615} 616 617/* 618 * resets environment to eval(), prints an error 619 * and forces eval to return FALSE. 620 */ 621static void 622experr(const char *msg) 623{ 624 printf("m4: %s in expr %s.\n", msg, where); 625 longjmp(expjump, -1); 626} | 609{ 610 int c; 611 612 while ((c = getch()) <= ' ' && c > EOS) 613 ; 614 return c; 615} 616 617/* 618 * resets environment to eval(), prints an error 619 * and forces eval to return FALSE. 620 */ 621static void 622experr(const char *msg) 623{ 624 printf("m4: %s in expr %s.\n", msg, where); 625 longjmp(expjump, -1); 626} |