xref: /onnv-gate/usr/src/cmd/awk/parse.c (revision 0)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 2.8	*/
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #define DEBUG
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include "awk.h"
31*0Sstevel@tonic-gate #include "y.tab.h"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate Node *nodealloc(n)
34*0Sstevel@tonic-gate {
35*0Sstevel@tonic-gate 	register Node *x;
36*0Sstevel@tonic-gate 	x = (Node *) malloc(sizeof(Node) + (n-1)*sizeof(Node *));
37*0Sstevel@tonic-gate 	if (x == NULL)
38*0Sstevel@tonic-gate 		ERROR "out of space in nodealloc" FATAL;
39*0Sstevel@tonic-gate 	x->nnext = NULL;
40*0Sstevel@tonic-gate 	x->lineno = lineno;
41*0Sstevel@tonic-gate 	return(x);
42*0Sstevel@tonic-gate }
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate Node *exptostat(a) Node *a;
45*0Sstevel@tonic-gate {
46*0Sstevel@tonic-gate 	a->ntype = NSTAT;
47*0Sstevel@tonic-gate 	return(a);
48*0Sstevel@tonic-gate }
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate Node *node1(a,b) Node *b;
51*0Sstevel@tonic-gate {
52*0Sstevel@tonic-gate 	register Node *x;
53*0Sstevel@tonic-gate 	x = nodealloc(1);
54*0Sstevel@tonic-gate 	x->nobj = a;
55*0Sstevel@tonic-gate 	x->narg[0]=b;
56*0Sstevel@tonic-gate 	return(x);
57*0Sstevel@tonic-gate }
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate Node *node2(a,b,c) Node *b, *c;
60*0Sstevel@tonic-gate {
61*0Sstevel@tonic-gate 	register Node *x;
62*0Sstevel@tonic-gate 	x = nodealloc(2);
63*0Sstevel@tonic-gate 	x->nobj = a;
64*0Sstevel@tonic-gate 	x->narg[0] = b;
65*0Sstevel@tonic-gate 	x->narg[1] = c;
66*0Sstevel@tonic-gate 	return(x);
67*0Sstevel@tonic-gate }
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate Node *node3(a,b,c,d) Node *b, *c, *d;
70*0Sstevel@tonic-gate {
71*0Sstevel@tonic-gate 	register Node *x;
72*0Sstevel@tonic-gate 	x = nodealloc(3);
73*0Sstevel@tonic-gate 	x->nobj = a;
74*0Sstevel@tonic-gate 	x->narg[0] = b;
75*0Sstevel@tonic-gate 	x->narg[1] = c;
76*0Sstevel@tonic-gate 	x->narg[2] = d;
77*0Sstevel@tonic-gate 	return(x);
78*0Sstevel@tonic-gate }
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate Node *node4(a,b,c,d,e) Node *b, *c, *d, *e;
81*0Sstevel@tonic-gate {
82*0Sstevel@tonic-gate 	register Node *x;
83*0Sstevel@tonic-gate 	x = nodealloc(4);
84*0Sstevel@tonic-gate 	x->nobj = a;
85*0Sstevel@tonic-gate 	x->narg[0] = b;
86*0Sstevel@tonic-gate 	x->narg[1] = c;
87*0Sstevel@tonic-gate 	x->narg[2] = d;
88*0Sstevel@tonic-gate 	x->narg[3] = e;
89*0Sstevel@tonic-gate 	return(x);
90*0Sstevel@tonic-gate }
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate Node *stat3(a,b,c,d) Node *b, *c, *d;
93*0Sstevel@tonic-gate {
94*0Sstevel@tonic-gate 	register Node *x;
95*0Sstevel@tonic-gate 	x = node3(a,b,c,d);
96*0Sstevel@tonic-gate 	x->ntype = NSTAT;
97*0Sstevel@tonic-gate 	return(x);
98*0Sstevel@tonic-gate }
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate Node *op2(a,b,c) Node *b, *c;
101*0Sstevel@tonic-gate {
102*0Sstevel@tonic-gate 	register Node *x;
103*0Sstevel@tonic-gate 	x = node2(a,b,c);
104*0Sstevel@tonic-gate 	x->ntype = NEXPR;
105*0Sstevel@tonic-gate 	return(x);
106*0Sstevel@tonic-gate }
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate Node *op1(a,b) Node *b;
109*0Sstevel@tonic-gate {
110*0Sstevel@tonic-gate 	register Node *x;
111*0Sstevel@tonic-gate 	x = node1(a,b);
112*0Sstevel@tonic-gate 	x->ntype = NEXPR;
113*0Sstevel@tonic-gate 	return(x);
114*0Sstevel@tonic-gate }
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate Node *stat1(a,b) Node *b;
117*0Sstevel@tonic-gate {
118*0Sstevel@tonic-gate 	register Node *x;
119*0Sstevel@tonic-gate 	x = node1(a,b);
120*0Sstevel@tonic-gate 	x->ntype = NSTAT;
121*0Sstevel@tonic-gate 	return(x);
122*0Sstevel@tonic-gate }
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate Node *op3(a,b,c,d) Node *b, *c, *d;
125*0Sstevel@tonic-gate {
126*0Sstevel@tonic-gate 	register Node *x;
127*0Sstevel@tonic-gate 	x = node3(a,b,c,d);
128*0Sstevel@tonic-gate 	x->ntype = NEXPR;
129*0Sstevel@tonic-gate 	return(x);
130*0Sstevel@tonic-gate }
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate Node *op4(a,b,c,d,e) Node *b, *c, *d, *e;
133*0Sstevel@tonic-gate {
134*0Sstevel@tonic-gate 	register Node *x;
135*0Sstevel@tonic-gate 	x = node4(a,b,c,d,e);
136*0Sstevel@tonic-gate 	x->ntype = NEXPR;
137*0Sstevel@tonic-gate 	return(x);
138*0Sstevel@tonic-gate }
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate Node *stat2(a,b,c) Node *b, *c;
141*0Sstevel@tonic-gate {
142*0Sstevel@tonic-gate 	register Node *x;
143*0Sstevel@tonic-gate 	x = node2(a,b,c);
144*0Sstevel@tonic-gate 	x->ntype = NSTAT;
145*0Sstevel@tonic-gate 	return(x);
146*0Sstevel@tonic-gate }
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate Node *stat4(a,b,c,d,e) Node *b, *c, *d, *e;
149*0Sstevel@tonic-gate {
150*0Sstevel@tonic-gate 	register Node *x;
151*0Sstevel@tonic-gate 	x = node4(a,b,c,d,e);
152*0Sstevel@tonic-gate 	x->ntype = NSTAT;
153*0Sstevel@tonic-gate 	return(x);
154*0Sstevel@tonic-gate }
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate Node *valtonode(a, b) Cell *a;
157*0Sstevel@tonic-gate {
158*0Sstevel@tonic-gate 	register Node *x;
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	a->ctype = OCELL;
161*0Sstevel@tonic-gate 	a->csub = b;
162*0Sstevel@tonic-gate 	x = node1(0, (Node *) a);
163*0Sstevel@tonic-gate 	x->ntype = NVALUE;
164*0Sstevel@tonic-gate 	return(x);
165*0Sstevel@tonic-gate }
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate Node *rectonode()
168*0Sstevel@tonic-gate {
169*0Sstevel@tonic-gate 	/* return valtonode(lookup("$0", symtab), CFLD); */
170*0Sstevel@tonic-gate 	return valtonode(recloc, CFLD);
171*0Sstevel@tonic-gate }
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate Node *makearr(p) Node *p;
174*0Sstevel@tonic-gate {
175*0Sstevel@tonic-gate 	Cell *cp;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	if (isvalue(p)) {
178*0Sstevel@tonic-gate 		cp = (Cell *) (p->narg[0]);
179*0Sstevel@tonic-gate 		if (isfunc(cp))
180*0Sstevel@tonic-gate 			ERROR "%s is a function, not an array", cp->nval SYNTAX;
181*0Sstevel@tonic-gate 		else if (!isarr(cp)) {
182*0Sstevel@tonic-gate 			xfree(cp->sval);
183*0Sstevel@tonic-gate 			cp->sval = (uchar *) makesymtab(NSYMTAB);
184*0Sstevel@tonic-gate 			cp->tval = ARR;
185*0Sstevel@tonic-gate 		}
186*0Sstevel@tonic-gate 	}
187*0Sstevel@tonic-gate 	return p;
188*0Sstevel@tonic-gate }
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate Node *pa2stat(a,b,c) Node *a, *b, *c;
191*0Sstevel@tonic-gate {
192*0Sstevel@tonic-gate 	register Node *x;
193*0Sstevel@tonic-gate 	x = node4(PASTAT2, a, b, c, (Node *) paircnt);
194*0Sstevel@tonic-gate 	paircnt++;
195*0Sstevel@tonic-gate 	x->ntype = NSTAT;
196*0Sstevel@tonic-gate 	return(x);
197*0Sstevel@tonic-gate }
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate Node *linkum(a,b) Node *a, *b;
200*0Sstevel@tonic-gate {
201*0Sstevel@tonic-gate 	register Node *c;
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 	if (errorflag)	/* don't link things that are wrong */
204*0Sstevel@tonic-gate 		return a;
205*0Sstevel@tonic-gate 	if (a == NULL) return(b);
206*0Sstevel@tonic-gate 	else if (b == NULL) return(a);
207*0Sstevel@tonic-gate 	for (c = a; c->nnext != NULL; c = c->nnext)
208*0Sstevel@tonic-gate 		;
209*0Sstevel@tonic-gate 	c->nnext = b;
210*0Sstevel@tonic-gate 	return(a);
211*0Sstevel@tonic-gate }
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate defn(v, vl, st)	/* turn on FCN bit in definition */
214*0Sstevel@tonic-gate 	Cell *v;
215*0Sstevel@tonic-gate 	Node *st, *vl;	/* body of function, arglist */
216*0Sstevel@tonic-gate {
217*0Sstevel@tonic-gate 	Node *p;
218*0Sstevel@tonic-gate 	int n;
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 	if (isarr(v)) {
221*0Sstevel@tonic-gate 		ERROR "`%s' is an array name and a function name", v->nval SYNTAX;
222*0Sstevel@tonic-gate 		return;
223*0Sstevel@tonic-gate 	}
224*0Sstevel@tonic-gate 	v->tval = FCN;
225*0Sstevel@tonic-gate 	v->sval = (uchar *) st;
226*0Sstevel@tonic-gate 	n = 0;	/* count arguments */
227*0Sstevel@tonic-gate 	for (p = vl; p; p = p->nnext)
228*0Sstevel@tonic-gate 		n++;
229*0Sstevel@tonic-gate 	v->fval = n;
230*0Sstevel@tonic-gate 	dprintf( ("defining func %s (%d args)\n", v->nval, n) );
231*0Sstevel@tonic-gate }
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate isarg(s)	/* is s in argument list for current function? */
234*0Sstevel@tonic-gate 	uchar *s;
235*0Sstevel@tonic-gate {
236*0Sstevel@tonic-gate 	extern Node *arglist;
237*0Sstevel@tonic-gate 	Node *p = arglist;
238*0Sstevel@tonic-gate 	int n;
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 	for (n = 0; p != 0; p = p->nnext, n++)
241*0Sstevel@tonic-gate 		if (strcmp(((Cell *)(p->narg[0]))->nval, s) == 0)
242*0Sstevel@tonic-gate 			return n;
243*0Sstevel@tonic-gate 	return -1;
244*0Sstevel@tonic-gate }
245