ANSI
[unix-history] / usr / src / old / awk / parse.c
CommitLineData
b803c326
SL
1#ifndef lint
2static char sccsid[] = "@(#)parse.c 4.2 %G%";
3#endif
3da1e57a
KM
4
5#include "awk.def"
6#include "awk.h"
7#include "stdio.h"
8node *ALLOC(n)
9{
10 register node *x;
11 x = (node *) malloc(sizeof(node) + (n-1)*sizeof(node *));
12 if (x == NULL)
13 error(FATAL, "out of space in ALLOC");
14 return(x);
15}
16node *exptostat(a) node *a;
17{
18 a->ntype = NSTAT;
19 return(a);
20}
21node *nullstat;
22node *node0(a)
23{
24 register node *x;
25 x=ALLOC(0);
26 x->nnext = NULL;
27 x->nobj=a;
28 return(x);
29}
30node *node1(a,b) node *b;
31{
32 register node *x;
33 x=ALLOC(1);
34 x->nnext = NULL;
35 x->nobj=a;
36 x->narg[0]=b;
37 return(x);
38}
39node *node2(a,b,c) node *b, *c;
40{
41 register node *x;
42 x = ALLOC(2);
43 x->nnext = NULL;
44 x->nobj = a;
45 x->narg[0] = b;
46 x->narg[1] = c;
47 return(x);
48}
49node *node3(a,b,c,d) node *b, *c, *d;
50{
51 register node *x;
52 x = ALLOC(3);
53 x->nnext = NULL;
54 x->nobj = a;
55 x->narg[0] = b;
56 x->narg[1] = c;
57 x->narg[2] = d;
58 return(x);
59}
60node *node4(a,b,c,d,e) node *b, *c, *d, *e;
61{
62 register node *x;
63 x = ALLOC(4);
64 x->nnext = NULL;
65 x->nobj = a;
66 x->narg[0] = b;
67 x->narg[1] = c;
68 x->narg[2] = d;
69 x->narg[3] = e;
70 return(x);
71}
72node *stat3(a,b,c,d) node *b, *c, *d;
73{
74 register node *x;
75 x = node3(a,b,c,d);
76 x->ntype = NSTAT;
77 return(x);
78}
79node *op2(a,b,c) node *b, *c;
80{
81 register node *x;
82 x = node2(a,b,c);
83 x->ntype = NEXPR;
84 return(x);
85}
86node *op1(a,b) node *b;
87{
88 register node *x;
89 x = node1(a,b);
90 x->ntype = NEXPR;
91 return(x);
92}
93node *stat1(a,b) node *b;
94{
95 register node *x;
96 x = node1(a,b);
97 x->ntype = NSTAT;
98 return(x);
99}
100node *op3(a,b,c,d) node *b, *c, *d;
101{
102 register node *x;
103 x = node3(a,b,c,d);
104 x->ntype = NEXPR;
105 return(x);
106}
107node *stat2(a,b,c) node *b, *c;
108{
109 register node *x;
110 x = node2(a,b,c);
111 x->ntype = NSTAT;
112 return(x);
113}
114node *stat4(a,b,c,d,e) node *b, *c, *d, *e;
115{
116 register node *x;
117 x = node4(a,b,c,d,e);
118 x->ntype = NSTAT;
119 return(x);
120}
121node *valtonode(a, b) cell *a;
122{
123 register node *x;
124 x = node0(a);
125 x->ntype = NVALUE;
126 x->subtype = b;
127 return(x);
128}
129node *pa2stat(a,b,c) node *a, *b, *c;
130{
131 register node *x;
132 x = node3(paircnt++, a, b, c);
133 x->ntype = NPA2;
134 return(x);
135}
136node *linkum(a,b) node *a, *b;
137{
138 register node *c;
139 if(a == NULL) return(b);
140 else if(b == NULL) return(a);
141 for(c=a; c->nnext != NULL; c=c->nnext);
142 c->nnext = b;
143 return(a);
144}
145node *genprint()
146{
147 register node *x;
148 x = stat2(PRINT,valtonode(lookup("$record", symtab, 0), CFLD), nullstat);
149 return(x);
150}