Blame


1 63a68686 2008-11-03 jas /****************************************************************
2 63a68686 2008-11-03 jas Copyright (C) Lucent Technologies 1997
3 63a68686 2008-11-03 jas All Rights Reserved
4 63a68686 2008-11-03 jas
5 63a68686 2008-11-03 jas Permission to use, copy, modify, and distribute this software and
6 63a68686 2008-11-03 jas its documentation for any purpose and without fee is hereby
7 63a68686 2008-11-03 jas granted, provided that the above copyright notice appear in all
8 63a68686 2008-11-03 jas copies and that both that the copyright notice and this
9 63a68686 2008-11-03 jas permission notice and warranty disclaimer appear in supporting
10 63a68686 2008-11-03 jas documentation, and that the name Lucent Technologies or any of
11 63a68686 2008-11-03 jas its entities not be used in advertising or publicity pertaining
12 63a68686 2008-11-03 jas to distribution of the software without specific, written prior
13 63a68686 2008-11-03 jas permission.
14 63a68686 2008-11-03 jas
15 63a68686 2008-11-03 jas LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 63a68686 2008-11-03 jas INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
17 63a68686 2008-11-03 jas IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
18 63a68686 2008-11-03 jas SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 63a68686 2008-11-03 jas WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
20 63a68686 2008-11-03 jas IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
21 63a68686 2008-11-03 jas ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
22 63a68686 2008-11-03 jas THIS SOFTWARE.
23 63a68686 2008-11-03 jas ****************************************************************/
24 63a68686 2008-11-03 jas
25 63a68686 2008-11-03 jas %{
26 63a68686 2008-11-03 jas #include <stdio.h>
27 63a68686 2008-11-03 jas #include <string.h>
28 63a68686 2008-11-03 jas #include "awk.h"
29 63a68686 2008-11-03 jas
30 63a68686 2008-11-03 jas #define makedfa(a,b) compre(a)
31 63a68686 2008-11-03 jas
32 63a68686 2008-11-03 jas void checkdup(Node *list, Cell *item);
33 63a68686 2008-11-03 jas int yywrap(void) { return(1); }
34 63a68686 2008-11-03 jas
35 63a68686 2008-11-03 jas Node *beginloc = 0;
36 63a68686 2008-11-03 jas Node *endloc = 0;
37 63a68686 2008-11-03 jas int infunc = 0; /* = 1 if in arglist or body of func */
38 63a68686 2008-11-03 jas int inloop = 0; /* = 1 if in while, for, do */
39 63a68686 2008-11-03 jas char *curfname = 0; /* current function name */
40 63a68686 2008-11-03 jas Node *arglist = 0; /* list of args for current function */
41 63a68686 2008-11-03 jas %}
42 63a68686 2008-11-03 jas
43 63a68686 2008-11-03 jas %union {
44 63a68686 2008-11-03 jas Node *p;
45 63a68686 2008-11-03 jas Cell *cp;
46 63a68686 2008-11-03 jas int i;
47 63a68686 2008-11-03 jas char *s;
48 63a68686 2008-11-03 jas }
49 63a68686 2008-11-03 jas
50 63a68686 2008-11-03 jas %token <i> FIRSTTOKEN /* must be first */
51 63a68686 2008-11-03 jas %token <p> PROGRAM PASTAT PASTAT2 XBEGIN XEND
52 63a68686 2008-11-03 jas %token <i> NL ',' '{' '(' '|' ';' '/' ')' '}' '[' ']'
53 63a68686 2008-11-03 jas %token <i> ARRAY
54 63a68686 2008-11-03 jas %token <i> MATCH NOTMATCH MATCHOP
55 63a68686 2008-11-03 jas %token <i> FINAL DOT ALL CCL NCCL CHAR OR STAR QUEST PLUS
56 63a68686 2008-11-03 jas %token <i> AND BOR APPEND EQ GE GT LE LT NE IN
57 63a68686 2008-11-03 jas %token <i> ARG BLTIN BREAK CLOSE CONTINUE DELETE DO EXIT FOR FUNC
58 63a68686 2008-11-03 jas %token <i> SUB GSUB IF INDEX LSUBSTR MATCHFCN NEXT NEXTFILE
59 63a68686 2008-11-03 jas %token <i> ADD MINUS MULT DIVIDE MOD
60 63a68686 2008-11-03 jas %token <i> ASSIGN ASGNOP ADDEQ SUBEQ MULTEQ DIVEQ MODEQ POWEQ
61 63a68686 2008-11-03 jas %token <i> PRINT PRINTF SPRINTF
62 63a68686 2008-11-03 jas %token <p> ELSE INTEST CONDEXPR
63 63a68686 2008-11-03 jas %token <i> POSTINCR PREINCR POSTDECR PREDECR
64 63a68686 2008-11-03 jas %token <cp> VAR IVAR VARNF CALL NUMBER STRING
65 63a68686 2008-11-03 jas %token <s> REGEXPR
66 63a68686 2008-11-03 jas
67 63a68686 2008-11-03 jas %type <p> pas pattern ppattern plist pplist patlist prarg term re
68 63a68686 2008-11-03 jas %type <p> pa_pat pa_stat pa_stats
69 63a68686 2008-11-03 jas %type <s> reg_expr
70 63a68686 2008-11-03 jas %type <p> simple_stmt opt_simple_stmt stmt stmtlist
71 63a68686 2008-11-03 jas %type <p> var varname funcname varlist
72 63a68686 2008-11-03 jas %type <p> for if else while
73 63a68686 2008-11-03 jas %type <i> do st
74 63a68686 2008-11-03 jas %type <i> pst opt_pst lbrace rbrace rparen comma nl opt_nl and bor
75 63a68686 2008-11-03 jas %type <i> subop print
76 63a68686 2008-11-03 jas
77 63a68686 2008-11-03 jas %right ASGNOP
78 63a68686 2008-11-03 jas %right '?'
79 63a68686 2008-11-03 jas %right ':'
80 63a68686 2008-11-03 jas %left BOR
81 63a68686 2008-11-03 jas %left AND
82 63a68686 2008-11-03 jas %left GETLINE
83 63a68686 2008-11-03 jas %nonassoc APPEND EQ GE GT LE LT NE MATCHOP IN '|'
84 63a68686 2008-11-03 jas %left ARG BLTIN BREAK CALL CLOSE CONTINUE DELETE DO EXIT FOR FUNC
85 63a68686 2008-11-03 jas %left GSUB IF INDEX LSUBSTR MATCHFCN NEXT NUMBER
86 63a68686 2008-11-03 jas %left PRINT PRINTF RETURN SPLIT SPRINTF STRING SUB SUBSTR
87 63a68686 2008-11-03 jas %left REGEXPR VAR VARNF IVAR WHILE '('
88 63a68686 2008-11-03 jas %left CAT
89 63a68686 2008-11-03 jas %left '+' '-'
90 63a68686 2008-11-03 jas %left '*' '/' '%'
91 63a68686 2008-11-03 jas %left NOT UMINUS
92 63a68686 2008-11-03 jas %right POWER
93 63a68686 2008-11-03 jas %right DECR INCR
94 63a68686 2008-11-03 jas %left INDIRECT
95 63a68686 2008-11-03 jas %token LASTTOKEN /* must be last */
96 63a68686 2008-11-03 jas
97 63a68686 2008-11-03 jas %%
98 63a68686 2008-11-03 jas
99 63a68686 2008-11-03 jas program:
100 63a68686 2008-11-03 jas pas { if (errorflag==0)
101 63a68686 2008-11-03 jas winner = (Node *)stat3(PROGRAM, beginloc, $1, endloc); }
102 63a68686 2008-11-03 jas | error { yyclearin; bracecheck(); SYNTAX("bailing out"); }
103 63a68686 2008-11-03 jas ;
104 63a68686 2008-11-03 jas
105 63a68686 2008-11-03 jas and:
106 63a68686 2008-11-03 jas AND | and NL
107 63a68686 2008-11-03 jas ;
108 63a68686 2008-11-03 jas
109 63a68686 2008-11-03 jas bor:
110 63a68686 2008-11-03 jas BOR | bor NL
111 63a68686 2008-11-03 jas ;
112 63a68686 2008-11-03 jas
113 63a68686 2008-11-03 jas comma:
114 63a68686 2008-11-03 jas ',' | comma NL
115 63a68686 2008-11-03 jas ;
116 63a68686 2008-11-03 jas
117 63a68686 2008-11-03 jas do:
118 63a68686 2008-11-03 jas DO | do NL
119 63a68686 2008-11-03 jas ;
120 63a68686 2008-11-03 jas
121 63a68686 2008-11-03 jas else:
122 63a68686 2008-11-03 jas ELSE | else NL
123 63a68686 2008-11-03 jas ;
124 63a68686 2008-11-03 jas
125 63a68686 2008-11-03 jas for:
126 63a68686 2008-11-03 jas FOR '(' opt_simple_stmt ';' opt_nl pattern ';' opt_nl opt_simple_stmt rparen {inloop++;} stmt
127 63a68686 2008-11-03 jas { --inloop; $$ = stat4(FOR, $3, notnull($6), $9, $12); }
128 63a68686 2008-11-03 jas | FOR '(' opt_simple_stmt ';' ';' opt_nl opt_simple_stmt rparen {inloop++;} stmt
129 63a68686 2008-11-03 jas { --inloop; $$ = stat4(FOR, $3, NIL, $7, $10); }
130 63a68686 2008-11-03 jas | FOR '(' varname IN varname rparen {inloop++;} stmt
131 63a68686 2008-11-03 jas { --inloop; $$ = stat3(IN, $3, makearr($5), $8); }
132 63a68686 2008-11-03 jas ;
133 63a68686 2008-11-03 jas
134 63a68686 2008-11-03 jas funcname:
135 63a68686 2008-11-03 jas VAR { setfname($1); }
136 63a68686 2008-11-03 jas | CALL { setfname($1); }
137 63a68686 2008-11-03 jas ;
138 63a68686 2008-11-03 jas
139 63a68686 2008-11-03 jas if:
140 63a68686 2008-11-03 jas IF '(' pattern rparen { $$ = notnull($3); }
141 63a68686 2008-11-03 jas ;
142 63a68686 2008-11-03 jas
143 63a68686 2008-11-03 jas lbrace:
144 63a68686 2008-11-03 jas '{' | lbrace NL
145 63a68686 2008-11-03 jas ;
146 63a68686 2008-11-03 jas
147 63a68686 2008-11-03 jas nl:
148 63a68686 2008-11-03 jas NL | nl NL
149 63a68686 2008-11-03 jas ;
150 63a68686 2008-11-03 jas
151 63a68686 2008-11-03 jas opt_nl:
152 63a68686 2008-11-03 jas /* empty */ { $$ = 0; }
153 63a68686 2008-11-03 jas | nl
154 63a68686 2008-11-03 jas ;
155 63a68686 2008-11-03 jas
156 63a68686 2008-11-03 jas opt_pst:
157 63a68686 2008-11-03 jas /* empty */ { $$ = 0; }
158 63a68686 2008-11-03 jas | pst
159 63a68686 2008-11-03 jas ;
160 63a68686 2008-11-03 jas
161 63a68686 2008-11-03 jas
162 63a68686 2008-11-03 jas opt_simple_stmt:
163 63a68686 2008-11-03 jas /* empty */ { $$ = 0; }
164 63a68686 2008-11-03 jas | simple_stmt
165 63a68686 2008-11-03 jas ;
166 63a68686 2008-11-03 jas
167 63a68686 2008-11-03 jas pas:
168 63a68686 2008-11-03 jas opt_pst { $$ = 0; }
169 63a68686 2008-11-03 jas | opt_pst pa_stats opt_pst { $$ = $2; }
170 63a68686 2008-11-03 jas ;
171 63a68686 2008-11-03 jas
172 63a68686 2008-11-03 jas pa_pat:
173 63a68686 2008-11-03 jas pattern { $$ = notnull($1); }
174 63a68686 2008-11-03 jas ;
175 63a68686 2008-11-03 jas
176 63a68686 2008-11-03 jas pa_stat:
177 63a68686 2008-11-03 jas pa_pat { $$ = stat2(PASTAT, $1, stat2(PRINT, rectonode(), NIL)); }
178 63a68686 2008-11-03 jas | pa_pat lbrace stmtlist '}' { $$ = stat2(PASTAT, $1, $3); }
179 63a68686 2008-11-03 jas | pa_pat ',' pa_pat { $$ = pa2stat($1, $3, stat2(PRINT, rectonode(), NIL)); }
180 63a68686 2008-11-03 jas | pa_pat ',' pa_pat lbrace stmtlist '}' { $$ = pa2stat($1, $3, $5); }
181 63a68686 2008-11-03 jas | lbrace stmtlist '}' { $$ = stat2(PASTAT, NIL, $2); }
182 63a68686 2008-11-03 jas | XBEGIN lbrace stmtlist '}'
183 63a68686 2008-11-03 jas { beginloc = linkum(beginloc, $3); $$ = 0; }
184 63a68686 2008-11-03 jas | XEND lbrace stmtlist '}'
185 63a68686 2008-11-03 jas { endloc = linkum(endloc, $3); $$ = 0; }
186 63a68686 2008-11-03 jas | FUNC funcname '(' varlist rparen {infunc++;} lbrace stmtlist '}'
187 63a68686 2008-11-03 jas { infunc--; curfname=0; defn((Cell *)$2, $4, $8); $$ = 0; }
188 63a68686 2008-11-03 jas ;
189 63a68686 2008-11-03 jas
190 63a68686 2008-11-03 jas pa_stats:
191 63a68686 2008-11-03 jas pa_stat
192 63a68686 2008-11-03 jas | pa_stats opt_pst pa_stat { $$ = linkum($1, $3); }
193 63a68686 2008-11-03 jas ;
194 63a68686 2008-11-03 jas
195 63a68686 2008-11-03 jas patlist:
196 63a68686 2008-11-03 jas pattern
197 63a68686 2008-11-03 jas | patlist comma pattern { $$ = linkum($1, $3); }
198 63a68686 2008-11-03 jas ;
199 63a68686 2008-11-03 jas
200 63a68686 2008-11-03 jas ppattern:
201 63a68686 2008-11-03 jas var ASGNOP ppattern { $$ = op2($2, $1, $3); }
202 63a68686 2008-11-03 jas | ppattern '?' ppattern ':' ppattern %prec '?'
203 63a68686 2008-11-03 jas { $$ = op3(CONDEXPR, notnull($1), $3, $5); }
204 63a68686 2008-11-03 jas | ppattern bor ppattern %prec BOR
205 63a68686 2008-11-03 jas { $$ = op2(BOR, notnull($1), notnull($3)); }
206 63a68686 2008-11-03 jas | ppattern and ppattern %prec AND
207 63a68686 2008-11-03 jas { $$ = op2(AND, notnull($1), notnull($3)); }
208 63a68686 2008-11-03 jas | ppattern MATCHOP reg_expr { $$ = op3($2, NIL, $1, (Node*)makedfa($3, 0)); }
209 63a68686 2008-11-03 jas | ppattern MATCHOP ppattern
210 63a68686 2008-11-03 jas { if (constnode($3))
211 63a68686 2008-11-03 jas $$ = op3($2, NIL, $1, (Node*)makedfa(strnode($3), 0));
212 63a68686 2008-11-03 jas else
213 63a68686 2008-11-03 jas $$ = op3($2, (Node *)1, $1, $3); }
214 63a68686 2008-11-03 jas | ppattern IN varname { $$ = op2(INTEST, $1, makearr($3)); }
215 63a68686 2008-11-03 jas | '(' plist ')' IN varname { $$ = op2(INTEST, $2, makearr($5)); }
216 63a68686 2008-11-03 jas | ppattern term %prec CAT { $$ = op2(CAT, $1, $2); }
217 63a68686 2008-11-03 jas | re
218 63a68686 2008-11-03 jas | term
219 63a68686 2008-11-03 jas ;
220 63a68686 2008-11-03 jas
221 63a68686 2008-11-03 jas pattern:
222 63a68686 2008-11-03 jas var ASGNOP pattern { $$ = op2($2, $1, $3); }
223 63a68686 2008-11-03 jas | pattern '?' pattern ':' pattern %prec '?'
224 63a68686 2008-11-03 jas { $$ = op3(CONDEXPR, notnull($1), $3, $5); }
225 63a68686 2008-11-03 jas | pattern bor pattern %prec BOR
226 63a68686 2008-11-03 jas { $$ = op2(BOR, notnull($1), notnull($3)); }
227 63a68686 2008-11-03 jas | pattern and pattern %prec AND
228 63a68686 2008-11-03 jas { $$ = op2(AND, notnull($1), notnull($3)); }
229 63a68686 2008-11-03 jas | pattern EQ pattern { $$ = op2($2, $1, $3); }
230 63a68686 2008-11-03 jas | pattern GE pattern { $$ = op2($2, $1, $3); }
231 63a68686 2008-11-03 jas | pattern GT pattern { $$ = op2($2, $1, $3); }
232 63a68686 2008-11-03 jas | pattern LE pattern { $$ = op2($2, $1, $3); }
233 63a68686 2008-11-03 jas | pattern LT pattern { $$ = op2($2, $1, $3); }
234 63a68686 2008-11-03 jas | pattern NE pattern { $$ = op2($2, $1, $3); }
235 63a68686 2008-11-03 jas | pattern MATCHOP reg_expr { $$ = op3($2, NIL, $1, (Node*)makedfa($3, 0)); }
236 63a68686 2008-11-03 jas | pattern MATCHOP pattern
237 63a68686 2008-11-03 jas { if (constnode($3))
238 63a68686 2008-11-03 jas $$ = op3($2, NIL, $1, (Node*)makedfa(strnode($3), 0));
239 63a68686 2008-11-03 jas else
240 63a68686 2008-11-03 jas $$ = op3($2, (Node *)1, $1, $3); }
241 63a68686 2008-11-03 jas | pattern IN varname { $$ = op2(INTEST, $1, makearr($3)); }
242 63a68686 2008-11-03 jas | '(' plist ')' IN varname { $$ = op2(INTEST, $2, makearr($5)); }
243 63a68686 2008-11-03 jas | pattern '|' GETLINE var {
244 63a68686 2008-11-03 jas if (safe) SYNTAX("cmd | getline is unsafe");
245 63a68686 2008-11-03 jas else $$ = op3(GETLINE, $4, itonp($2), $1); }
246 63a68686 2008-11-03 jas | pattern '|' GETLINE {
247 63a68686 2008-11-03 jas if (safe) SYNTAX("cmd | getline is unsafe");
248 63a68686 2008-11-03 jas else $$ = op3(GETLINE, (Node*)0, itonp($2), $1); }
249 63a68686 2008-11-03 jas | pattern term %prec CAT { $$ = op2(CAT, $1, $2); }
250 63a68686 2008-11-03 jas | re
251 63a68686 2008-11-03 jas | term
252 63a68686 2008-11-03 jas ;
253 63a68686 2008-11-03 jas
254 63a68686 2008-11-03 jas plist:
255 63a68686 2008-11-03 jas pattern comma pattern { $$ = linkum($1, $3); }
256 63a68686 2008-11-03 jas | plist comma pattern { $$ = linkum($1, $3); }
257 63a68686 2008-11-03 jas ;
258 63a68686 2008-11-03 jas
259 63a68686 2008-11-03 jas pplist:
260 63a68686 2008-11-03 jas ppattern
261 63a68686 2008-11-03 jas | pplist comma ppattern { $$ = linkum($1, $3); }
262 63a68686 2008-11-03 jas ;
263 63a68686 2008-11-03 jas
264 63a68686 2008-11-03 jas prarg:
265 63a68686 2008-11-03 jas /* empty */ { $$ = rectonode(); }
266 63a68686 2008-11-03 jas | pplist
267 63a68686 2008-11-03 jas | '(' plist ')' { $$ = $2; }
268 63a68686 2008-11-03 jas ;
269 63a68686 2008-11-03 jas
270 63a68686 2008-11-03 jas print:
271 63a68686 2008-11-03 jas PRINT | PRINTF
272 63a68686 2008-11-03 jas ;
273 63a68686 2008-11-03 jas
274 63a68686 2008-11-03 jas pst:
275 63a68686 2008-11-03 jas NL | ';' | pst NL | pst ';'
276 63a68686 2008-11-03 jas ;
277 63a68686 2008-11-03 jas
278 63a68686 2008-11-03 jas rbrace:
279 63a68686 2008-11-03 jas '}' | rbrace NL
280 63a68686 2008-11-03 jas ;
281 63a68686 2008-11-03 jas
282 63a68686 2008-11-03 jas re:
283 63a68686 2008-11-03 jas reg_expr
284 63a68686 2008-11-03 jas { $$ = op3(MATCH, NIL, rectonode(), (Node*)makedfa($1, 0)); }
285 63a68686 2008-11-03 jas | NOT re { $$ = op1(NOT, notnull($2)); }
286 63a68686 2008-11-03 jas ;
287 63a68686 2008-11-03 jas
288 63a68686 2008-11-03 jas reg_expr:
289 63a68686 2008-11-03 jas '/' {startreg();} REGEXPR '/' { $$ = $3; }
290 63a68686 2008-11-03 jas ;
291 63a68686 2008-11-03 jas
292 63a68686 2008-11-03 jas rparen:
293 63a68686 2008-11-03 jas ')' | rparen NL
294 63a68686 2008-11-03 jas ;
295 63a68686 2008-11-03 jas
296 63a68686 2008-11-03 jas simple_stmt:
297 63a68686 2008-11-03 jas print prarg '|' term {
298 63a68686 2008-11-03 jas if (safe) SYNTAX("print | is unsafe");
299 63a68686 2008-11-03 jas else $$ = stat3($1, $2, itonp($3), $4); }
300 63a68686 2008-11-03 jas | print prarg APPEND term {
301 63a68686 2008-11-03 jas if (safe) SYNTAX("print >> is unsafe");
302 63a68686 2008-11-03 jas else $$ = stat3($1, $2, itonp($3), $4); }
303 63a68686 2008-11-03 jas | print prarg GT term {
304 63a68686 2008-11-03 jas if (safe) SYNTAX("print > is unsafe");
305 63a68686 2008-11-03 jas else $$ = stat3($1, $2, itonp($3), $4); }
306 63a68686 2008-11-03 jas | print prarg { $$ = stat3($1, $2, NIL, NIL); }
307 63a68686 2008-11-03 jas | DELETE varname '[' patlist ']' { $$ = stat2(DELETE, makearr($2), $4); }
308 63a68686 2008-11-03 jas | DELETE varname { $$ = stat2(DELETE, makearr($2), 0); }
309 63a68686 2008-11-03 jas | pattern { $$ = exptostat($1); }
310 63a68686 2008-11-03 jas | error { yyclearin; SYNTAX("illegal statement"); }
311 63a68686 2008-11-03 jas ;
312 63a68686 2008-11-03 jas
313 63a68686 2008-11-03 jas st:
314 63a68686 2008-11-03 jas nl
315 63a68686 2008-11-03 jas | ';' opt_nl
316 63a68686 2008-11-03 jas ;
317 63a68686 2008-11-03 jas
318 63a68686 2008-11-03 jas stmt:
319 63a68686 2008-11-03 jas BREAK st { if (!inloop) SYNTAX("break illegal outside of loops");
320 63a68686 2008-11-03 jas $$ = stat1(BREAK, NIL); }
321 63a68686 2008-11-03 jas | CLOSE pattern st { $$ = stat1(CLOSE, $2); }
322 63a68686 2008-11-03 jas | CONTINUE st { if (!inloop) SYNTAX("continue illegal outside of loops");
323 63a68686 2008-11-03 jas $$ = stat1(CONTINUE, NIL); }
324 63a68686 2008-11-03 jas | do {inloop++;} stmt {--inloop;} WHILE '(' pattern ')' st
325 63a68686 2008-11-03 jas { $$ = stat2(DO, $3, notnull($7)); }
326 63a68686 2008-11-03 jas | EXIT pattern st { $$ = stat1(EXIT, $2); }
327 63a68686 2008-11-03 jas | EXIT st { $$ = stat1(EXIT, NIL); }
328 63a68686 2008-11-03 jas | for
329 63a68686 2008-11-03 jas | if stmt else stmt { $$ = stat3(IF, $1, $2, $4); }
330 63a68686 2008-11-03 jas | if stmt { $$ = stat3(IF, $1, $2, NIL); }
331 63a68686 2008-11-03 jas | lbrace stmtlist rbrace { $$ = $2; }
332 63a68686 2008-11-03 jas | NEXT st { if (infunc)
333 63a68686 2008-11-03 jas SYNTAX("next is illegal inside a function");
334 63a68686 2008-11-03 jas $$ = stat1(NEXT, NIL); }
335 63a68686 2008-11-03 jas | NEXTFILE st { if (infunc)
336 63a68686 2008-11-03 jas SYNTAX("nextfile is illegal inside a function");
337 63a68686 2008-11-03 jas $$ = stat1(NEXTFILE, NIL); }
338 63a68686 2008-11-03 jas | RETURN pattern st { $$ = stat1(RETURN, $2); }
339 63a68686 2008-11-03 jas | RETURN st { $$ = stat1(RETURN, NIL); }
340 63a68686 2008-11-03 jas | simple_stmt st
341 63a68686 2008-11-03 jas | while {inloop++;} stmt { --inloop; $$ = stat2(WHILE, $1, $3); }
342 63a68686 2008-11-03 jas | ';' opt_nl { $$ = 0; }
343 63a68686 2008-11-03 jas ;
344 63a68686 2008-11-03 jas
345 63a68686 2008-11-03 jas stmtlist:
346 63a68686 2008-11-03 jas stmt
347 63a68686 2008-11-03 jas | stmtlist stmt { $$ = linkum($1, $2); }
348 63a68686 2008-11-03 jas ;
349 63a68686 2008-11-03 jas
350 63a68686 2008-11-03 jas subop:
351 63a68686 2008-11-03 jas SUB | GSUB
352 63a68686 2008-11-03 jas ;
353 63a68686 2008-11-03 jas
354 63a68686 2008-11-03 jas term:
355 63a68686 2008-11-03 jas term '/' ASGNOP term { $$ = op2(DIVEQ, $1, $4); }
356 63a68686 2008-11-03 jas | term '+' term { $$ = op2(ADD, $1, $3); }
357 63a68686 2008-11-03 jas | term '-' term { $$ = op2(MINUS, $1, $3); }
358 63a68686 2008-11-03 jas | term '*' term { $$ = op2(MULT, $1, $3); }
359 63a68686 2008-11-03 jas | term '/' term { $$ = op2(DIVIDE, $1, $3); }
360 63a68686 2008-11-03 jas | term '%' term { $$ = op2(MOD, $1, $3); }
361 63a68686 2008-11-03 jas | term POWER term { $$ = op2(POWER, $1, $3); }
362 63a68686 2008-11-03 jas | '-' term %prec UMINUS { $$ = op1(UMINUS, $2); }
363 63a68686 2008-11-03 jas | '+' term %prec UMINUS { $$ = $2; }
364 63a68686 2008-11-03 jas | NOT term %prec UMINUS { $$ = op1(NOT, notnull($2)); }
365 63a68686 2008-11-03 jas | BLTIN '(' ')' { $$ = op2(BLTIN, itonp($1), rectonode()); }
366 63a68686 2008-11-03 jas | BLTIN '(' patlist ')' { $$ = op2(BLTIN, itonp($1), $3); }
367 63a68686 2008-11-03 jas | BLTIN { $$ = op2(BLTIN, itonp($1), rectonode()); }
368 63a68686 2008-11-03 jas | CALL '(' ')' { $$ = op2(CALL, celltonode($1,CVAR), NIL); }
369 63a68686 2008-11-03 jas | CALL '(' patlist ')' { $$ = op2(CALL, celltonode($1,CVAR), $3); }
370 63a68686 2008-11-03 jas | DECR var { $$ = op1(PREDECR, $2); }
371 63a68686 2008-11-03 jas | INCR var { $$ = op1(PREINCR, $2); }
372 63a68686 2008-11-03 jas | var DECR { $$ = op1(POSTDECR, $1); }
373 63a68686 2008-11-03 jas | var INCR { $$ = op1(POSTINCR, $1); }
374 63a68686 2008-11-03 jas | GETLINE var LT term { $$ = op3(GETLINE, $2, itonp($3), $4); }
375 63a68686 2008-11-03 jas | GETLINE LT term { $$ = op3(GETLINE, NIL, itonp($2), $3); }
376 63a68686 2008-11-03 jas | GETLINE var { $$ = op3(GETLINE, $2, NIL, NIL); }
377 63a68686 2008-11-03 jas | GETLINE { $$ = op3(GETLINE, NIL, NIL, NIL); }
378 63a68686 2008-11-03 jas | INDEX '(' pattern comma pattern ')'
379 63a68686 2008-11-03 jas { $$ = op2(INDEX, $3, $5); }
380 63a68686 2008-11-03 jas | INDEX '(' pattern comma reg_expr ')'
381 63a68686 2008-11-03 jas { SYNTAX("index() doesn't permit regular expressions");
382 63a68686 2008-11-03 jas $$ = op2(INDEX, $3, (Node*)$5); }
383 63a68686 2008-11-03 jas | '(' pattern ')' { $$ = $2; }
384 63a68686 2008-11-03 jas | MATCHFCN '(' pattern comma reg_expr ')'
385 63a68686 2008-11-03 jas { $$ = op3(MATCHFCN, NIL, $3, (Node*)makedfa($5, 1)); }
386 63a68686 2008-11-03 jas | MATCHFCN '(' pattern comma pattern ')'
387 63a68686 2008-11-03 jas { if (constnode($5))
388 63a68686 2008-11-03 jas $$ = op3(MATCHFCN, NIL, $3, (Node*)makedfa(strnode($5), 1));
389 63a68686 2008-11-03 jas else
390 63a68686 2008-11-03 jas $$ = op3(MATCHFCN, (Node *)1, $3, $5); }
391 63a68686 2008-11-03 jas | NUMBER { $$ = celltonode($1, CCON); }
392 63a68686 2008-11-03 jas | SPLIT '(' pattern comma varname comma pattern ')' /* string */
393 63a68686 2008-11-03 jas { $$ = op4(SPLIT, $3, makearr($5), $7, (Node*)STRING); }
394 63a68686 2008-11-03 jas | SPLIT '(' pattern comma varname comma reg_expr ')' /* const /regexp/ */
395 63a68686 2008-11-03 jas { $$ = op4(SPLIT, $3, makearr($5), (Node*)makedfa($7, 1), (Node *)REGEXPR); }
396 63a68686 2008-11-03 jas | SPLIT '(' pattern comma varname ')'
397 63a68686 2008-11-03 jas { $$ = op4(SPLIT, $3, makearr($5), NIL, (Node*)STRING); } /* default */
398 63a68686 2008-11-03 jas | SPRINTF '(' patlist ')' { $$ = op1($1, $3); }
399 63a68686 2008-11-03 jas | STRING { $$ = celltonode($1, CCON); }
400 63a68686 2008-11-03 jas | subop '(' reg_expr comma pattern ')'
401 63a68686 2008-11-03 jas { $$ = op4($1, NIL, (Node*)makedfa($3, 1), $5, rectonode()); }
402 63a68686 2008-11-03 jas | subop '(' pattern comma pattern ')'
403 63a68686 2008-11-03 jas { if (constnode($3))
404 63a68686 2008-11-03 jas $$ = op4($1, NIL, (Node*)makedfa(strnode($3), 1), $5, rectonode());
405 63a68686 2008-11-03 jas else
406 63a68686 2008-11-03 jas $$ = op4($1, (Node *)1, $3, $5, rectonode()); }
407 63a68686 2008-11-03 jas | subop '(' reg_expr comma pattern comma var ')'
408 63a68686 2008-11-03 jas { $$ = op4($1, NIL, (Node*)makedfa($3, 1), $5, $7); }
409 63a68686 2008-11-03 jas | subop '(' pattern comma pattern comma var ')'
410 63a68686 2008-11-03 jas { if (constnode($3))
411 63a68686 2008-11-03 jas $$ = op4($1, NIL, (Node*)makedfa(strnode($3), 1), $5, $7);
412 63a68686 2008-11-03 jas else
413 63a68686 2008-11-03 jas $$ = op4($1, (Node *)1, $3, $5, $7); }
414 63a68686 2008-11-03 jas | SUBSTR '(' pattern comma pattern comma pattern ')'
415 63a68686 2008-11-03 jas { $$ = op3(SUBSTR, $3, $5, $7); }
416 63a68686 2008-11-03 jas | SUBSTR '(' pattern comma pattern ')'
417 63a68686 2008-11-03 jas { $$ = op3(SUBSTR, $3, $5, NIL); }
418 63a68686 2008-11-03 jas | var
419 63a68686 2008-11-03 jas ;
420 63a68686 2008-11-03 jas
421 63a68686 2008-11-03 jas var:
422 63a68686 2008-11-03 jas varname
423 63a68686 2008-11-03 jas | varname '[' patlist ']' { $$ = op2(ARRAY, makearr($1), $3); }
424 63a68686 2008-11-03 jas | IVAR { $$ = op1(INDIRECT, celltonode($1, CVAR)); }
425 63a68686 2008-11-03 jas | INDIRECT term { $$ = op1(INDIRECT, $2); }
426 63a68686 2008-11-03 jas ;
427 63a68686 2008-11-03 jas
428 63a68686 2008-11-03 jas varlist:
429 63a68686 2008-11-03 jas /* nothing */ { arglist = $$ = 0; }
430 63a68686 2008-11-03 jas | VAR { arglist = $$ = celltonode($1,CVAR); }
431 63a68686 2008-11-03 jas | varlist comma VAR {
432 63a68686 2008-11-03 jas checkdup($1, $3);
433 63a68686 2008-11-03 jas arglist = $$ = linkum($1,celltonode($3,CVAR)); }
434 63a68686 2008-11-03 jas ;
435 63a68686 2008-11-03 jas
436 63a68686 2008-11-03 jas varname:
437 63a68686 2008-11-03 jas VAR { $$ = celltonode($1, CVAR); }
438 63a68686 2008-11-03 jas | ARG { $$ = op1(ARG, itonp($1)); }
439 63a68686 2008-11-03 jas | VARNF { $$ = op1(VARNF, (Node *) $1); }
440 63a68686 2008-11-03 jas ;
441 63a68686 2008-11-03 jas
442 63a68686 2008-11-03 jas
443 63a68686 2008-11-03 jas while:
444 63a68686 2008-11-03 jas WHILE '(' pattern rparen { $$ = notnull($3); }
445 63a68686 2008-11-03 jas ;
446 63a68686 2008-11-03 jas
447 63a68686 2008-11-03 jas %%
448 63a68686 2008-11-03 jas
449 63a68686 2008-11-03 jas void setfname(Cell *p)
450 63a68686 2008-11-03 jas {
451 63a68686 2008-11-03 jas if (isarr(p))
452 63a68686 2008-11-03 jas SYNTAX("%s is an array, not a function", p->nval);
453 63a68686 2008-11-03 jas else if (isfcn(p))
454 63a68686 2008-11-03 jas SYNTAX("you can't define function %s more than once", p->nval);
455 63a68686 2008-11-03 jas curfname = p->nval;
456 63a68686 2008-11-03 jas }
457 63a68686 2008-11-03 jas
458 63a68686 2008-11-03 jas int constnode(Node *p)
459 63a68686 2008-11-03 jas {
460 63a68686 2008-11-03 jas return isvalue(p) && ((Cell *) (p->narg[0]))->csub == CCON;
461 63a68686 2008-11-03 jas }
462 63a68686 2008-11-03 jas
463 63a68686 2008-11-03 jas char *strnode(Node *p)
464 63a68686 2008-11-03 jas {
465 63a68686 2008-11-03 jas return ((Cell *)(p->narg[0]))->sval;
466 63a68686 2008-11-03 jas }
467 63a68686 2008-11-03 jas
468 63a68686 2008-11-03 jas Node *notnull(Node *n)
469 63a68686 2008-11-03 jas {
470 63a68686 2008-11-03 jas switch (n->nobj) {
471 63a68686 2008-11-03 jas case LE: case LT: case EQ: case NE: case GT: case GE:
472 63a68686 2008-11-03 jas case BOR: case AND: case NOT:
473 63a68686 2008-11-03 jas return n;
474 63a68686 2008-11-03 jas default:
475 63a68686 2008-11-03 jas return op2(NE, n, nullnode);
476 63a68686 2008-11-03 jas }
477 63a68686 2008-11-03 jas }
478 63a68686 2008-11-03 jas
479 63a68686 2008-11-03 jas void checkdup(Node *vl, Cell *cp) /* check if name already in list */
480 63a68686 2008-11-03 jas {
481 63a68686 2008-11-03 jas char *s = cp->nval;
482 63a68686 2008-11-03 jas for ( ; vl; vl = vl->nnext) {
483 63a68686 2008-11-03 jas if (strcmp(s, ((Cell *)(vl->narg[0]))->nval) == 0) {
484 63a68686 2008-11-03 jas SYNTAX("duplicate argument %s", s);
485 63a68686 2008-11-03 jas break;
486 63a68686 2008-11-03 jas }
487 63a68686 2008-11-03 jas }
488 63a68686 2008-11-03 jas }
489 63a68686 2008-11-03 jas