]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/awk/parse.c
merge
[plan9front.git] / sys / src / cmd / awk / parse.c
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 #include <u.h>
26 #include <libc.h>
27 #include <bio.h>
28 #include "awk.h"
29 #include "y.tab.h"
30
31 Node *nodealloc(int n)
32 {
33         Node *x;
34
35         x = (Node *) malloc(sizeof(Node) + (n-1)*sizeof(Node *));
36         if (x == nil)
37                 FATAL("out of space in nodealloc");
38         x->nnext = nil;
39         x->lineno = lineno;
40         return(x);
41 }
42
43 Node *exptostat(Node *a)
44 {
45         a->ntype = NSTAT;
46         return(a);
47 }
48
49 Node *node1(int a, Node *b)
50 {
51         Node *x;
52
53         x = nodealloc(1);
54         x->nobj = a;
55         x->narg[0]=b;
56         return(x);
57 }
58
59 Node *node2(int a, Node *b, Node *c)
60 {
61         Node *x;
62
63         x = nodealloc(2);
64         x->nobj = a;
65         x->narg[0] = b;
66         x->narg[1] = c;
67         return(x);
68 }
69
70 Node *node3(int a, Node *b, Node *c, Node *d)
71 {
72         Node *x;
73
74         x = nodealloc(3);
75         x->nobj = a;
76         x->narg[0] = b;
77         x->narg[1] = c;
78         x->narg[2] = d;
79         return(x);
80 }
81
82 Node *node4(int a, Node *b, Node *c, Node *d, Node *e)
83 {
84         Node *x;
85
86         x = nodealloc(4);
87         x->nobj = a;
88         x->narg[0] = b;
89         x->narg[1] = c;
90         x->narg[2] = d;
91         x->narg[3] = e;
92         return(x);
93 }
94
95 Node *stat1(int a, Node *b)
96 {
97         Node *x;
98
99         x = node1(a,b);
100         x->ntype = NSTAT;
101         return(x);
102 }
103
104 Node *stat2(int a, Node *b, Node *c)
105 {
106         Node *x;
107
108         x = node2(a,b,c);
109         x->ntype = NSTAT;
110         return(x);
111 }
112
113 Node *stat3(int a, Node *b, Node *c, Node *d)
114 {
115         Node *x;
116
117         x = node3(a,b,c,d);
118         x->ntype = NSTAT;
119         return(x);
120 }
121
122 Node *stat4(int a, Node *b, Node *c, Node *d, Node *e)
123 {
124         Node *x;
125
126         x = node4(a,b,c,d,e);
127         x->ntype = NSTAT;
128         return(x);
129 }
130
131 Node *op1(int a, Node *b)
132 {
133         Node *x;
134
135         x = node1(a,b);
136         x->ntype = NEXPR;
137         return(x);
138 }
139
140 Node *op2(int a, Node *b, Node *c)
141 {
142         Node *x;
143
144         x = node2(a,b,c);
145         x->ntype = NEXPR;
146         return(x);
147 }
148
149 Node *op3(int a, Node *b, Node *c, Node *d)
150 {
151         Node *x;
152
153         x = node3(a,b,c,d);
154         x->ntype = NEXPR;
155         return(x);
156 }
157
158 Node *op4(int a, Node *b, Node *c, Node *d, Node *e)
159 {
160         Node *x;
161
162         x = node4(a,b,c,d,e);
163         x->ntype = NEXPR;
164         return(x);
165 }
166
167 Node *celltonode(Cell *a, int b)
168 {
169         Node *x;
170
171         a->ctype = OCELL;
172         a->csub = b;
173         x = node1(0, (Node *) a);
174         x->ntype = NVALUE;
175         return(x);
176 }
177
178 Node *rectonode(void)   /* make $0 into a Node */
179 {
180         extern Cell *literal0;
181         return op1(INDIRECT, celltonode(literal0, CUNK));
182 }
183
184 Node *makearr(Node *p)
185 {
186         Cell *cp;
187
188         if (isvalue(p)) {
189                 cp = (Cell *) (p->narg[0]);
190                 if (isfcn(cp))
191                         SYNTAX( "%s is a function, not an array", cp->nval );
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 #define PA2NUM  50      /* max number of pat,pat patterns allowed */
202 int     paircnt;                /* number of them in use */
203 int     pairstack[PA2NUM];      /* state of each pat,pat */
204
205 Node *pa2stat(Node *a, Node *b, Node *c)        /* pat, pat {...} */
206 {
207         Node *x;
208
209         x = node4(PASTAT2, a, b, c, itonp(paircnt));
210         if (paircnt++ >= PA2NUM)
211                 SYNTAX( "limited to %d pat,pat statements", PA2NUM );
212         x->ntype = NSTAT;
213         return(x);
214 }
215
216 Node *linkum(Node *a, Node *b)
217 {
218         Node *c;
219
220         if (errorflag)  /* don't link things that are wrong */
221                 return a;
222         if (a == nil)
223                 return(b);
224         else if (b == nil)
225                 return(a);
226         for (c = a; c->nnext != nil; c = c->nnext)
227                 ;
228         c->nnext = b;
229         return(a);
230 }
231
232 void defn(Cell *v, Node *vl, Node *st)  /* turn on FCN bit in definition, */
233 {                                       /*   body of function, arglist */
234         Node *p;
235         int n;
236
237         if (isarr(v)) {
238                 SYNTAX( "`%s' is an array name and a function name", v->nval );
239                 return;
240         }
241         v->tval = FCN;
242         v->sval = (char *) st;
243         n = 0;  /* count arguments */
244         for (p = vl; p; p = p->nnext)
245                 n++;
246         v->fval = n;
247         dprint( ("defining func %s (%d args)\n", v->nval, n) );
248 }
249
250 int isarg(char *s)              /* is s in argument list for current function? */
251 {                       /* return -1 if not, otherwise arg # */
252         extern Node *arglist;
253         Node *p = arglist;
254         int n;
255
256         for (n = 0; p != 0; p = p->nnext, n++)
257                 if (strcmp(((Cell *)(p->narg[0]))->nval, s) == 0)
258                         return n;
259         return -1;
260 }
261
262 int ptoi(void *p)       /* convert pointer to integer */
263 {
264         return (int) (vlong) p; /* swearing that p fits, of course */
265 }
266
267 Node *itonp(int i)      /* and vice versa */
268 {
269         return (Node *) (long) i;
270 }