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