xref: /openbsd-src/usr.bin/awk/parse.c (revision a4afd6dad3fba28f80e70208181c06c482259988)
1 /****************************************************************
2 Copyright (C) AT&T and Lucent Technologies 1996
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 names of AT&T or Lucent Technologies
11 or any of their entities not be used in advertising or publicity
12 pertaining to distribution of the software without specific,
13 written prior permission.
14 
15 AT&T AND LUCENT DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS. IN NO EVENT SHALL AT&T OR LUCENT OR ANY OF THEIR
18 ENTITIES BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
19 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
21 OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
22 USE OR PERFORMANCE OF THIS SOFTWARE.
23 ****************************************************************/
24 
25 #define DEBUG
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include "awk.h"
30 #include "awkgram.h"
31 
32 Node *nodealloc(int n)
33 {
34 	Node *x;
35 
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(Node *a)
45 {
46 	a->ntype = NSTAT;
47 	return(a);
48 }
49 
50 Node *node1(int a, Node *b)
51 {
52 	Node *x;
53 
54 	x = nodealloc(1);
55 	x->nobj = a;
56 	x->narg[0]=b;
57 	return(x);
58 }
59 
60 Node *node2(int a, Node *b, Node *c)
61 {
62 	Node *x;
63 
64 	x = nodealloc(2);
65 	x->nobj = a;
66 	x->narg[0] = b;
67 	x->narg[1] = c;
68 	return(x);
69 }
70 
71 Node *node3(int a, Node *b, Node *c, Node *d)
72 {
73 	Node *x;
74 
75 	x = nodealloc(3);
76 	x->nobj = a;
77 	x->narg[0] = b;
78 	x->narg[1] = c;
79 	x->narg[2] = d;
80 	return(x);
81 }
82 
83 Node *node4(int a, Node *b, Node *c, Node *d, Node *e)
84 {
85 	Node *x;
86 
87 	x = nodealloc(4);
88 	x->nobj = a;
89 	x->narg[0] = b;
90 	x->narg[1] = c;
91 	x->narg[2] = d;
92 	x->narg[3] = e;
93 	return(x);
94 }
95 
96 Node *stat1(int a, Node *b)
97 {
98 	Node *x;
99 
100 	x = node1(a,b);
101 	x->ntype = NSTAT;
102 	return(x);
103 }
104 
105 Node *stat2(int a, Node *b, Node *c)
106 {
107 	Node *x;
108 
109 	x = node2(a,b,c);
110 	x->ntype = NSTAT;
111 	return(x);
112 }
113 
114 Node *stat3(int a, Node *b, Node *c, Node *d)
115 {
116 	Node *x;
117 
118 	x = node3(a,b,c,d);
119 	x->ntype = NSTAT;
120 	return(x);
121 }
122 
123 Node *stat4(int a, Node *b, Node *c, Node *d, Node *e)
124 {
125 	Node *x;
126 
127 	x = node4(a,b,c,d,e);
128 	x->ntype = NSTAT;
129 	return(x);
130 }
131 
132 Node *op1(int a, Node *b)
133 {
134 	Node *x;
135 
136 	x = node1(a,b);
137 	x->ntype = NEXPR;
138 	return(x);
139 }
140 
141 Node *op2(int a, Node *b, Node *c)
142 {
143 	Node *x;
144 
145 	x = node2(a,b,c);
146 	x->ntype = NEXPR;
147 	return(x);
148 }
149 
150 Node *op3(int a, Node *b, Node *c, Node *d)
151 {
152 	Node *x;
153 
154 	x = node3(a,b,c,d);
155 	x->ntype = NEXPR;
156 	return(x);
157 }
158 
159 Node *op4(int a, Node *b, Node *c, Node *d, Node *e)
160 {
161 	Node *x;
162 
163 	x = node4(a,b,c,d,e);
164 	x->ntype = NEXPR;
165 	return(x);
166 }
167 
168 Node *valtonode(Cell *a, int b)
169 {
170 	Node *x;
171 
172 	a->ctype = OCELL;
173 	a->csub = b;
174 	x = node1(0, (Node *) a);
175 	x->ntype = NVALUE;
176 	return(x);
177 }
178 
179 Node *rectonode(void)	/* make $0 into a Node */
180 {
181 	return valtonode(recloc, CFLD);
182 }
183 
184 Node *makearr(Node *p)
185 {
186 	Cell *cp;
187 
188 	if (isvalue(p)) {
189 		cp = (Cell *) (p->narg[0]);
190 		if (isfunc(cp))
191 			ERROR "%s is a function, not an array", cp->nval SYNTAX;
192 		else if (!isarr(cp)) {
193 			xfree(cp->sval);
194 			cp->sval = (char *) makesymtab(NSYMTAB);
195 			cp->tval = ARR;
196 		}
197 	}
198 	return p;
199 }
200 
201 Node *pa2stat(Node *a, Node *b, Node *c)	/* pat, pat {...} */
202 {
203 	Node *x;
204 
205 	x = node4(PASTAT2, a, b, c, (Node *) paircnt);
206 	paircnt++;
207 	x->ntype = NSTAT;
208 	return(x);
209 }
210 
211 Node *linkum(Node *a, Node *b)
212 {
213 	Node *c;
214 
215 	if (errorflag)	/* don't link things that are wrong */
216 		return a;
217 	if (a == NULL)
218 		return(b);
219 	else if (b == NULL)
220 		return(a);
221 	for (c = a; c->nnext != NULL; c = c->nnext)
222 		;
223 	c->nnext = b;
224 	return(a);
225 }
226 
227 void defn(Cell *v, Node *vl, Node *st)	/* turn on FCN bit in definition, */
228 {					/*   body of function, arglist */
229 	Node *p;
230 	int n;
231 
232 	if (isarr(v)) {
233 		ERROR "`%s' is an array name and a function name", v->nval SYNTAX;
234 		return;
235 	}
236 	v->tval = FCN;
237 	v->sval = (char *) st;
238 	n = 0;	/* count arguments */
239 	for (p = vl; p; p = p->nnext)
240 		n++;
241 	v->fval = n;
242 	dprintf( ("defining func %s (%d args)\n", v->nval, n) );
243 }
244 
245 int isarg(char *s)		/* is s in argument list for current function? */
246 {			/* return -1 if not, otherwise arg # */
247 	extern Node *arglist;
248 	Node *p = arglist;
249 	int n;
250 
251 	for (n = 0; p != 0; p = p->nnext, n++)
252 		if (strcmp(((Cell *)(p->narg[0]))->nval, s) == 0)
253 			return n;
254 	return -1;
255 }
256