1 /****************************************************************
2 Copyright (C) Lucent Technologies 1997
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
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
23 ****************************************************************/
30 #include "awkgram.tab.h"
32 Node *nodealloc(int n)
36 x = (Node *) malloc(sizeof(*x) + (n-1) * sizeof(x));
38 FATAL("out of space in nodealloc");
44 Node *exptostat(Node *a)
50 Node *node1(int a, Node *b)
60 Node *node2(int a, Node *b, Node *c)
71 Node *node3(int a, Node *b, Node *c, Node *d)
83 Node *node4(int a, Node *b, Node *c, Node *d, Node *e)
96 Node *node5(int a, Node *b, Node *c, Node *d, Node *e, Node *f)
110 Node *stat1(int a, Node *b)
119 Node *stat2(int a, Node *b, Node *c)
128 Node *stat3(int a, Node *b, Node *c, Node *d)
137 Node *stat4(int a, Node *b, Node *c, Node *d, Node *e)
141 x = node4(a,b,c,d,e);
146 Node *op1(int a, Node *b)
155 Node *op2(int a, Node *b, Node *c)
164 Node *op3(int a, Node *b, Node *c, Node *d)
173 Node *op4(int a, Node *b, Node *c, Node *d, Node *e)
177 x = node4(a,b,c,d,e);
182 Node *op5(int a, Node *b, Node *c, Node *d, Node *e, Node *f)
186 x = node5(a,b,c,d,e,f);
191 Node *celltonode(Cell *a, int b)
197 x = node1(0, (Node *) a);
202 Node *rectonode(void) /* make $0 into a Node */
204 extern Cell *literal0;
205 return op1(INDIRECT, celltonode(literal0, CUNK));
208 Node *makearr(Node *p)
213 cp = (Cell *) (p->narg[0]);
215 SYNTAX( "%s is a function, not an array", cp->nval );
216 else if (!isarr(cp)) {
218 cp->sval = (char *) makesymtab(NSYMTAB);
225 #define PA2NUM 50 /* max number of pat,pat patterns allowed */
226 int paircnt; /* number of them in use */
227 int pairstack[PA2NUM]; /* state of each pat,pat */
229 Node *pa2stat(Node *a, Node *b, Node *c) /* pat, pat {...} */
233 x = node4(PASTAT2, a, b, c, itonp(paircnt));
234 if (paircnt++ >= PA2NUM)
235 SYNTAX( "limited to %d pat,pat statements", PA2NUM );
240 Node *linkum(Node *a, Node *b)
244 if (errorflag) /* don't link things that are wrong */
250 for (c = a; c->nnext != NULL; c = c->nnext)
256 void defn(Cell *v, Node *vl, Node *st) /* turn on FCN bit in definition, */
257 { /* body of function, arglist */
262 SYNTAX( "`%s' is an array name and a function name", v->nval );
265 if (isarg(v->nval) != -1) {
266 SYNTAX( "`%s' is both function name and argument name", v->nval );
271 v->sval = (char *) st;
272 n = 0; /* count arguments */
273 for (p = vl; p; p = p->nnext)
276 DPRINTF("defining func %s (%d args)\n", v->nval, n);
279 int isarg(const char *s) /* is s in argument list for current function? */
280 { /* return -1 if not, otherwise arg # */
281 extern Node *arglist;
285 for (n = 0; p != NULL; p = p->nnext, n++)
286 if (strcmp(((Cell *)(p->narg[0]))->nval, s) == 0)
291 int ptoi(void *p) /* convert pointer to integer */
293 return (int) (long) p; /* swearing that p fits, of course */
296 Node *itonp(int i) /* and vice versa */
298 return (Node *) (long) i;