1 /*- 2 * Copyright (c) 1991 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.proprietary.c% 6 */ 7 8 #ifndef lint 9 static char sccsid[] = "@(#)parse.c 4.3 (Berkeley) 04/17/91"; 10 #endif /* not lint */ 11 12 #include "awk.def" 13 #include "awk.h" 14 #include "stdio.h" 15 node *ALLOC(n) 16 { 17 register node *x; 18 x = (node *) malloc(sizeof(node) + (n-1)*sizeof(node *)); 19 if (x == NULL) 20 error(FATAL, "out of space in ALLOC"); 21 return(x); 22 } 23 node *exptostat(a) node *a; 24 { 25 a->ntype = NSTAT; 26 return(a); 27 } 28 node *nullstat; 29 node *node0(a) 30 { 31 register node *x; 32 x=ALLOC(0); 33 x->nnext = NULL; 34 x->nobj=a; 35 return(x); 36 } 37 node *node1(a,b) node *b; 38 { 39 register node *x; 40 x=ALLOC(1); 41 x->nnext = NULL; 42 x->nobj=a; 43 x->narg[0]=b; 44 return(x); 45 } 46 node *node2(a,b,c) node *b, *c; 47 { 48 register node *x; 49 x = ALLOC(2); 50 x->nnext = NULL; 51 x->nobj = a; 52 x->narg[0] = b; 53 x->narg[1] = c; 54 return(x); 55 } 56 node *node3(a,b,c,d) node *b, *c, *d; 57 { 58 register node *x; 59 x = ALLOC(3); 60 x->nnext = NULL; 61 x->nobj = a; 62 x->narg[0] = b; 63 x->narg[1] = c; 64 x->narg[2] = d; 65 return(x); 66 } 67 node *node4(a,b,c,d,e) node *b, *c, *d, *e; 68 { 69 register node *x; 70 x = ALLOC(4); 71 x->nnext = NULL; 72 x->nobj = a; 73 x->narg[0] = b; 74 x->narg[1] = c; 75 x->narg[2] = d; 76 x->narg[3] = e; 77 return(x); 78 } 79 node *stat3(a,b,c,d) node *b, *c, *d; 80 { 81 register node *x; 82 x = node3(a,b,c,d); 83 x->ntype = NSTAT; 84 return(x); 85 } 86 node *op2(a,b,c) node *b, *c; 87 { 88 register node *x; 89 x = node2(a,b,c); 90 x->ntype = NEXPR; 91 return(x); 92 } 93 node *op1(a,b) node *b; 94 { 95 register node *x; 96 x = node1(a,b); 97 x->ntype = NEXPR; 98 return(x); 99 } 100 node *stat1(a,b) node *b; 101 { 102 register node *x; 103 x = node1(a,b); 104 x->ntype = NSTAT; 105 return(x); 106 } 107 node *op3(a,b,c,d) node *b, *c, *d; 108 { 109 register node *x; 110 x = node3(a,b,c,d); 111 x->ntype = NEXPR; 112 return(x); 113 } 114 node *stat2(a,b,c) node *b, *c; 115 { 116 register node *x; 117 x = node2(a,b,c); 118 x->ntype = NSTAT; 119 return(x); 120 } 121 node *stat4(a,b,c,d,e) node *b, *c, *d, *e; 122 { 123 register node *x; 124 x = node4(a,b,c,d,e); 125 x->ntype = NSTAT; 126 return(x); 127 } 128 node *valtonode(a, b) cell *a; 129 { 130 register node *x; 131 x = node0(a); 132 x->ntype = NVALUE; 133 x->subtype = b; 134 return(x); 135 } 136 node *pa2stat(a,b,c) node *a, *b, *c; 137 { 138 register node *x; 139 x = node3(paircnt++, a, b, c); 140 x->ntype = NPA2; 141 return(x); 142 } 143 node *linkum(a,b) node *a, *b; 144 { 145 register node *c; 146 if(a == NULL) return(b); 147 else if(b == NULL) return(a); 148 for(c=a; c->nnext != NULL; c=c->nnext); 149 c->nnext = b; 150 return(a); 151 } 152 node *genprint() 153 { 154 register node *x; 155 x = stat2(PRINT,valtonode(lookup("$record", symtab, 0), CFLD), nullstat); 156 return(x); 157 } 158