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