1 /**************************************************************** 2 Copyright (C) Lucent Technologies 1997 3 All Rights Reserved 4 5 Permission to use, copy, modify, and distribute this software and 6 its documentation for any purpose and without fee is hereby 7 granted, provided that the above copyright notice appear in all 8 copies and that both that the copyright notice and this 9 permission notice and warranty disclaimer appear in supporting 10 documentation, and that the name Lucent Technologies or any of 11 its entities not be used in advertising or publicity pertaining 12 to distribution of the software without specific, written prior 13 permission. 14 15 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 17 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 18 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 20 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 21 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 22 THIS SOFTWARE. 23 ****************************************************************/ 24 25 #if HAVE_NBTOOL_CONFIG_H 26 #include "nbtool_config.h" 27 #endif 28 29 #define DEBUG 30 #include <stdio.h> 31 #include <string.h> 32 #include <stdlib.h> 33 #include "awk.h" 34 #include "awkgram.h" 35 36 Node *nodealloc(int n) 37 { 38 Node *x; 39 40 x = malloc(sizeof(Node) + (n-1)*sizeof(Node *)); 41 if (x == NULL) 42 FATAL("out of space in nodealloc"); 43 x->nnext = NULL; 44 x->lineno = lineno; 45 return(x); 46 } 47 48 Node *exptostat(Node *a) 49 { 50 a->ntype = NSTAT; 51 return(a); 52 } 53 54 Node *node1(int a, Node *b) 55 { 56 Node *x; 57 58 x = nodealloc(1); 59 x->nobj = a; 60 x->narg[0]=b; 61 return(x); 62 } 63 64 Node *node2(int a, Node *b, Node *c) 65 { 66 Node *x; 67 68 x = nodealloc(2); 69 x->nobj = a; 70 x->narg[0] = b; 71 x->narg[1] = c; 72 return(x); 73 } 74 75 Node *node3(int a, Node *b, Node *c, Node *d) 76 { 77 Node *x; 78 79 x = nodealloc(3); 80 x->nobj = a; 81 x->narg[0] = b; 82 x->narg[1] = c; 83 x->narg[2] = d; 84 return(x); 85 } 86 87 Node *node4(int a, Node *b, Node *c, Node *d, Node *e) 88 { 89 Node *x; 90 91 x = nodealloc(4); 92 x->nobj = a; 93 x->narg[0] = b; 94 x->narg[1] = c; 95 x->narg[2] = d; 96 x->narg[3] = e; 97 return(x); 98 } 99 100 Node *node5(int a, Node *b, Node *c, Node *d, Node *e, Node *f) 101 { 102 Node *x; 103 104 x = nodealloc(5); 105 x->nobj = a; 106 x->narg[0] = b; 107 x->narg[1] = c; 108 x->narg[2] = d; 109 x->narg[3] = e; 110 x->narg[4] = f; 111 return(x); 112 } 113 114 Node *stat1(int a, Node *b) 115 { 116 Node *x; 117 118 x = node1(a,b); 119 x->ntype = NSTAT; 120 return(x); 121 } 122 123 Node *stat2(int a, Node *b, Node *c) 124 { 125 Node *x; 126 127 x = node2(a,b,c); 128 x->ntype = NSTAT; 129 return(x); 130 } 131 132 Node *stat3(int a, Node *b, Node *c, Node *d) 133 { 134 Node *x; 135 136 x = node3(a,b,c,d); 137 x->ntype = NSTAT; 138 return(x); 139 } 140 141 Node *stat4(int a, Node *b, Node *c, Node *d, Node *e) 142 { 143 Node *x; 144 145 x = node4(a,b,c,d,e); 146 x->ntype = NSTAT; 147 return(x); 148 } 149 150 Node *op1(int a, Node *b) 151 { 152 Node *x; 153 154 x = node1(a,b); 155 x->ntype = NEXPR; 156 return(x); 157 } 158 159 Node *op2(int a, Node *b, Node *c) 160 { 161 Node *x; 162 163 x = node2(a,b,c); 164 x->ntype = NEXPR; 165 return(x); 166 } 167 168 Node *op3(int a, Node *b, Node *c, Node *d) 169 { 170 Node *x; 171 172 x = node3(a,b,c,d); 173 x->ntype = NEXPR; 174 return(x); 175 } 176 177 Node *op4(int a, Node *b, Node *c, Node *d, Node *e) 178 { 179 Node *x; 180 181 x = node4(a,b,c,d,e); 182 x->ntype = NEXPR; 183 return(x); 184 } 185 186 Node *op5(int a, Node *b, Node *c, Node *d, Node *e, Node *f) 187 { 188 Node *x; 189 190 x = node5(a,b,c,d,e, f); 191 x->ntype = NEXPR; 192 return(x); 193 } 194 195 Node *celltonode(Cell *a, int b) 196 { 197 Node *x; 198 199 a->ctype = OCELL; 200 a->csub = b; 201 x = node1(0, (Node *) a); 202 x->ntype = NVALUE; 203 return(x); 204 } 205 206 Node *rectonode(void) /* make $0 into a Node */ 207 { 208 extern Cell *literal0; 209 return op1(INDIRECT, celltonode(literal0, CUNK)); 210 } 211 212 Node *makearr(Node *p) 213 { 214 Cell *cp; 215 216 if (isvalue(p)) { 217 cp = (Cell *) (p->narg[0]); 218 if (isfcn(cp)) 219 SYNTAX( "%s is a function, not an array", cp->nval ); 220 else if (!isarr(cp)) { 221 xfree(cp->sval); 222 cp->sval = (char *) makesymtab(NSYMTAB); 223 cp->tval = ARR; 224 } 225 } 226 return p; 227 } 228 229 #define PA2NUM 50 /* max number of pat,pat patterns allowed */ 230 int paircnt; /* number of them in use */ 231 int pairstack[PA2NUM]; /* state of each pat,pat */ 232 233 Node *pa2stat(Node *a, Node *b, Node *c) /* pat, pat {...} */ 234 { 235 Node *x; 236 237 x = node4(PASTAT2, a, b, c, itonp(paircnt)); 238 if (paircnt++ >= PA2NUM) 239 SYNTAX( "limited to %d pat,pat statements", PA2NUM ); 240 x->ntype = NSTAT; 241 return(x); 242 } 243 244 Node *linkum(Node *a, Node *b) 245 { 246 Node *c; 247 248 if (errorflag) /* don't link things that are wrong */ 249 return a; 250 if (a == NULL) 251 return(b); 252 else if (b == NULL) 253 return(a); 254 for (c = a; c->nnext != NULL; c = c->nnext) 255 ; 256 c->nnext = b; 257 return(a); 258 } 259 260 void defn(Cell *v, Node *vl, Node *st) /* turn on FCN bit in definition, */ 261 { /* body of function, arglist */ 262 Node *p; 263 int n; 264 265 if (isarr(v)) { 266 SYNTAX( "`%s' is an array name and a function name", v->nval ); 267 return; 268 } 269 if (isarg(v->nval) != -1) { 270 SYNTAX( "`%s' is both function name and argument name", v->nval ); 271 return; 272 } 273 274 v->tval = FCN; 275 v->sval = (char *) st; 276 n = 0; /* count arguments */ 277 for (p = vl; p; p = p->nnext) 278 n++; 279 v->fval = n; 280 dprintf( ("defining func %s (%d args)\n", v->nval, n) ); 281 } 282 283 int isarg(const char *s) /* is s in argument list for current function? */ 284 { /* return -1 if not, otherwise arg # */ 285 extern Node *arglist; 286 Node *p = arglist; 287 int n; 288 289 for (n = 0; p != 0; p = p->nnext, n++) 290 if (strcmp(((Cell *)(p->narg[0]))->nval, s) == 0) 291 return n; 292 return -1; 293 } 294 295 int ptoi(void *p) /* convert pointer to integer */ 296 { 297 return (int) (long) p; /* swearing that p fits, of course */ 298 } 299 300 Node *itonp(int i) /* and vice versa */ 301 { 302 return (Node *) (long) i; 303 } 304