1 /* 2 Copyright (c) 1989 AT&T 3 All Rights Reserved 4 5 THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T. 6 7 The copyright notice above does not evidence any 8 actual or intended publication of such source code. 9 */ 10 11 #define DEBUG 12 #include <stdio.h> 13 #include <string.h> 14 #include <stdlib.h> 15 #include "awk.h" 16 #include "y.tab.h" 17 18 Node *nodealloc(int n) 19 { 20 Node *x; 21 22 x = (Node *) malloc(sizeof(Node) + (n-1)*sizeof(Node *)); 23 if (x == NULL) 24 ERROR "out of space in nodealloc" FATAL; 25 x->nnext = NULL; 26 x->lineno = lineno; 27 return(x); 28 } 29 30 Node *exptostat(Node *a) 31 { 32 a->ntype = NSTAT; 33 return(a); 34 } 35 36 Node *node1(int a, Node *b) 37 { 38 Node *x; 39 40 x = nodealloc(1); 41 x->nobj = a; 42 x->narg[0]=b; 43 return(x); 44 } 45 46 Node *node2(int a, Node *b, Node *c) 47 { 48 Node *x; 49 50 x = nodealloc(2); 51 x->nobj = a; 52 x->narg[0] = b; 53 x->narg[1] = c; 54 return(x); 55 } 56 57 Node *node3(int a, Node *b, Node *c, Node *d) 58 { 59 Node *x; 60 61 x = nodealloc(3); 62 x->nobj = a; 63 x->narg[0] = b; 64 x->narg[1] = c; 65 x->narg[2] = d; 66 return(x); 67 } 68 69 Node *node4(int a, Node *b, Node *c, Node *d, Node *e) 70 { 71 Node *x; 72 73 x = nodealloc(4); 74 x->nobj = a; 75 x->narg[0] = b; 76 x->narg[1] = c; 77 x->narg[2] = d; 78 x->narg[3] = e; 79 return(x); 80 } 81 82 Node *stat1(int a, Node *b) 83 { 84 Node *x; 85 86 x = node1(a,b); 87 x->ntype = NSTAT; 88 return(x); 89 } 90 91 Node *stat2(int a, Node *b, Node *c) 92 { 93 Node *x; 94 95 x = node2(a,b,c); 96 x->ntype = NSTAT; 97 return(x); 98 } 99 100 Node *stat3(int a, Node *b, Node *c, Node *d) 101 { 102 Node *x; 103 104 x = node3(a,b,c,d); 105 x->ntype = NSTAT; 106 return(x); 107 } 108 109 Node *stat4(int a, Node *b, Node *c, Node *d, Node *e) 110 { 111 Node *x; 112 113 x = node4(a,b,c,d,e); 114 x->ntype = NSTAT; 115 return(x); 116 } 117 118 Node *op1(int a, Node *b) 119 { 120 Node *x; 121 122 x = node1(a,b); 123 x->ntype = NEXPR; 124 return(x); 125 } 126 127 Node *op2(int a, Node *b, Node *c) 128 { 129 Node *x; 130 131 x = node2(a,b,c); 132 x->ntype = NEXPR; 133 return(x); 134 } 135 136 Node *op3(int a, Node *b, Node *c, Node *d) 137 { 138 Node *x; 139 140 x = node3(a,b,c,d); 141 x->ntype = NEXPR; 142 return(x); 143 } 144 145 Node *op4(int a, Node *b, Node *c, Node *d, Node *e) 146 { 147 Node *x; 148 149 x = node4(a,b,c,d,e); 150 x->ntype = NEXPR; 151 return(x); 152 } 153 154 Node *valtonode(Cell *a, int b) 155 { 156 Node *x; 157 158 a->ctype = OCELL; 159 a->csub = b; 160 x = node1(0, (Node *) a); 161 x->ntype = NVALUE; 162 return(x); 163 } 164 165 Node *rectonode(void) /* make $0 into a Node */ 166 { 167 return valtonode(recloc, CFLD); 168 } 169 170 Node *makearr(Node *p) 171 { 172 Cell *cp; 173 174 if (isvalue(p)) { 175 cp = (Cell *) (p->narg[0]); 176 if (isfunc(cp)) 177 ERROR "%s is a function, not an array", cp->nval SYNTAX; 178 else if (!isarr(cp)) { 179 xfree(cp->sval); 180 cp->sval = (uchar *) makesymtab(NSYMTAB); 181 cp->tval = ARR; 182 } 183 } 184 return p; 185 } 186 187 Node *pa2stat(Node *a, Node *b, Node *c) /* pat, pat {...} */ 188 { 189 Node *x; 190 191 x = node4(PASTAT2, a, b, c, (Node *) paircnt); 192 paircnt++; 193 x->ntype = NSTAT; 194 return(x); 195 } 196 197 Node *linkum(Node *a, Node *b) 198 { 199 Node *c; 200 201 if (errorflag) /* don't link things that are wrong */ 202 return a; 203 if (a == NULL) 204 return(b); 205 else if (b == NULL) 206 return(a); 207 for (c = a; c->nnext != NULL; c = c->nnext) 208 ; 209 c->nnext = b; 210 return(a); 211 } 212 213 void defn(Cell *v, Node *vl, Node *st) /* turn on FCN bit in definition, */ 214 { /* body of function, arglist */ 215 Node *p; 216 int n; 217 218 if (isarr(v)) { 219 ERROR "`%s' is an array name and a function name", v->nval SYNTAX; 220 return; 221 } 222 v->tval = FCN; 223 v->sval = (uchar *) st; 224 n = 0; /* count arguments */ 225 for (p = vl; p; p = p->nnext) 226 n++; 227 v->fval = n; 228 dprintf( ("defining func %s (%d args)\n", v->nval, n) ); 229 } 230 231 isarg(uchar *s) /* is s in argument list for current function? */ 232 { /* return -1 if not, otherwise arg # */ 233 extern Node *arglist; 234 Node *p = arglist; 235 int n; 236 237 for (n = 0; p != 0; p = p->nnext, n++) 238 if (strcmp(((Cell *)(p->narg[0]))->nval, s) == 0) 239 return n; 240 return -1; 241 } 242