d5c7149d977bb30b9c318d86a3eff2bb677f5451
[p5sagit/p5-mst-13.2.git] / x2p / a2p.y
1 %{
2 /* $Header: a2p.y,v 2.0 88/06/05 00:15:38 root Exp $
3  *
4  * $Log:        a2p.y,v $
5  * Revision 2.0  88/06/05  00:15:38  root
6  * Baseline version 2.0.
7  * 
8  */
9
10 #include "INTERN.h"
11 #include "a2p.h"
12
13 int root;
14
15 %}
16 %token BEGIN END
17 %token REGEX
18 %token SEMINEW NEWLINE COMMENT
19 %token FUN1 GRGR
20 %token PRINT PRINTF SPRINTF SPLIT
21 %token IF ELSE WHILE FOR IN
22 %token EXIT NEXT BREAK CONTINUE
23
24 %right ASGNOP
25 %left OROR
26 %left ANDAND
27 %left NOT
28 %left NUMBER VAR SUBSTR INDEX
29 %left GETLINE
30 %nonassoc RELOP MATCHOP
31 %left OR
32 %left STRING
33 %left '+' '-'
34 %left '*' '/' '%'
35 %right UMINUS
36 %left INCR DECR
37 %left FIELD VFIELD
38
39 %%
40
41 program : junk begin hunks end
42                 { root = oper4(OPROG,$1,$2,$3,$4); }
43         ;
44
45 begin   : BEGIN '{' maybe states '}' junk
46                 { $$ = oper3(OJUNK,$3,$4,$6); in_begin = FALSE; }
47         | /* NULL */
48                 { $$ = Nullop; }
49         ;
50
51 end     : END '{' maybe states '}'
52                 { $$ = oper2(OJUNK,$3,$4); }
53         | end NEWLINE
54                 { $$ = $1; }
55         | /* NULL */
56                 { $$ = Nullop; }
57         ;
58
59 hunks   : hunks hunk junk
60                 { $$ = oper3(OHUNKS,$1,$2,$3); }
61         | /* NULL */
62                 { $$ = Nullop; }
63         ;
64
65 hunk    : patpat
66                 { $$ = oper1(OHUNK,$1); need_entire = TRUE; }
67         | patpat '{' maybe states '}'
68                 { $$ = oper2(OHUNK,$1,oper2(OJUNK,$3,$4)); }
69         | '{' maybe states '}'
70                 { $$ = oper2(OHUNK,Nullop,oper2(OJUNK,$2,$3)); }
71         ;
72
73 patpat  : pat
74                 { $$ = oper1(OPAT,$1); }
75         | pat ',' pat
76                 { $$ = oper2(ORANGE,$1,$3); }
77         ;
78
79 pat     : REGEX
80                 { $$ = oper1(OREGEX,$1); }
81         | match
82         | rel
83         | compound_pat
84         ;
85
86 compound_pat
87         : '(' compound_pat ')'
88                 { $$ = oper1(OPPAREN,$2); }
89         | pat ANDAND pat
90                 { $$ = oper2(OPANDAND,$1,$3); }
91         | pat OROR pat
92                 { $$ = oper2(OPOROR,$1,$3); }
93         | NOT pat
94                 { $$ = oper1(OPNOT,$2); }
95         ;
96
97 cond    : expr
98         | match
99         | rel
100         | compound_cond
101         ;
102
103 compound_cond
104         : '(' compound_cond ')'
105                 { $$ = oper1(OCPAREN,$2); }
106         | cond ANDAND cond
107                 { $$ = oper2(OCANDAND,$1,$3); }
108         | cond OROR cond
109                 { $$ = oper2(OCOROR,$1,$3); }
110         | NOT cond
111                 { $$ = oper1(OCNOT,$2); }
112         ;
113
114 rel     : expr RELOP expr
115                 { $$ = oper3(ORELOP,$2,$1,$3); }
116         | '(' rel ')'
117                 { $$ = oper1(ORPAREN,$2); }
118         ;
119
120 match   : expr MATCHOP REGEX
121                 { $$ = oper3(OMATCHOP,$2,$1,oper1(OREGEX,$3)); }
122         | '(' match ')'
123                 { $$ = oper1(OMPAREN,$2); }
124         ;
125
126 expr    : term
127                 { $$ = $1; }
128         | expr term
129                 { $$ = oper2(OCONCAT,$1,$2); }
130         | variable ASGNOP expr
131                 { $$ = oper3(OASSIGN,$2,$1,$3);
132                         if ((ops[$1].ival & 255) == OFLD)
133                             lval_field = TRUE;
134                         if ((ops[$1].ival & 255) == OVFLD)
135                             lval_field = TRUE;
136                 }
137         ;
138
139 term    : variable
140                 { $$ = $1; }
141         | term '+' term
142                 { $$ = oper2(OADD,$1,$3); }
143         | term '-' term
144                 { $$ = oper2(OSUB,$1,$3); }
145         | term '*' term
146                 { $$ = oper2(OMULT,$1,$3); }
147         | term '/' term
148                 { $$ = oper2(ODIV,$1,$3); }
149         | term '%' term
150                 { $$ = oper2(OMOD,$1,$3); }
151         | variable INCR
152                 { $$ = oper1(OPOSTINCR,$1); }
153         | variable DECR
154                 { $$ = oper1(OPOSTDECR,$1); }
155         | INCR variable
156                 { $$ = oper1(OPREINCR,$2); }
157         | DECR variable
158                 { $$ = oper1(OPREDECR,$2); }
159         | '-' term %prec UMINUS
160                 { $$ = oper1(OUMINUS,$2); }
161         | '+' term %prec UMINUS
162                 { $$ = oper1(OUPLUS,$2); }
163         | '(' expr ')'
164                 { $$ = oper1(OPAREN,$2); }
165         | GETLINE
166                 { $$ = oper0(OGETLINE); }
167         | FUN1
168                 { $$ = oper0($1); need_entire = do_chop = TRUE; }
169         | FUN1 '(' ')'
170                 { $$ = oper1($1,Nullop); need_entire = do_chop = TRUE; }
171         | FUN1 '(' expr ')'
172                 { $$ = oper1($1,$3); }
173         | SPRINTF print_list
174                 { $$ = oper1(OSPRINTF,$2); }
175         | SUBSTR '(' expr ',' expr ',' expr ')'
176                 { $$ = oper3(OSUBSTR,$3,$5,$7); }
177         | SUBSTR '(' expr ',' expr ')'
178                 { $$ = oper2(OSUBSTR,$3,$5); }
179         | SPLIT '(' expr ',' VAR ',' expr ')'
180                 { $$ = oper3(OSPLIT,$3,numary($5),$7); }
181         | SPLIT '(' expr ',' VAR ')'
182                 { $$ = oper2(OSPLIT,$3,numary($5)); }
183         | INDEX '(' expr ',' expr ')'
184                 { $$ = oper2(OINDEX,$3,$5); }
185         ;
186
187 variable: NUMBER
188                 { $$ = oper1(ONUM,$1); }
189         | STRING
190                 { $$ = oper1(OSTR,$1); }
191         | VAR
192                 { $$ = oper1(OVAR,$1); }
193         | VAR '[' expr ']'
194                 { $$ = oper2(OVAR,$1,$3); }
195         | FIELD
196                 { $$ = oper1(OFLD,$1); }
197         | VFIELD term
198                 { $$ = oper1(OVFLD,$2); }
199         ;
200
201 print_list
202         : expr
203         | clist
204         | /* NULL */
205                 { $$ = Nullop; }
206         ;
207
208 clist   : expr ',' expr
209                 { $$ = oper2(OCOMMA,$1,$3); }
210         | clist ',' expr
211                 { $$ = oper2(OCOMMA,$1,$3); }
212         | '(' clist ')'         /* these parens are invisible */
213                 { $$ = $2; }
214         ;
215
216 junk    : junk hunksep
217                 { $$ = oper2(OJUNK,$1,$2); }
218         | /* NULL */
219                 { $$ = Nullop; }
220         ;
221
222 hunksep : ';'
223                 { $$ = oper0(OSEMICOLON); }
224         | SEMINEW
225                 { $$ = oper0(OSEMICOLON); }
226         | NEWLINE
227                 { $$ = oper0(ONEWLINE); }
228         | COMMENT
229                 { $$ = oper1(OCOMMENT,$1); }
230         ;
231
232 maybe   : maybe nlstuff
233                 { $$ = oper2(OJUNK,$1,$2); }
234         | /* NULL */
235                 { $$ = Nullop; }
236         ;
237
238 nlstuff : NEWLINE
239                 { $$ = oper0(ONEWLINE); }
240         | COMMENT
241                 { $$ = oper1(OCOMMENT,$1); }
242         ;
243
244 separator
245         : ';' maybe
246                 { $$ = oper2(OJUNK,oper0(OSEMICOLON),$2); }
247         | SEMINEW maybe
248                 { $$ = oper2(OJUNK,oper0(OSNEWLINE),$2); }
249         | NEWLINE maybe
250                 { $$ = oper2(OJUNK,oper0(OSNEWLINE),$2); }
251         | COMMENT maybe
252                 { $$ = oper2(OJUNK,oper1(OSCOMMENT,$1),$2); }
253         ;
254
255 states  : states statement
256                 { $$ = oper2(OSTATES,$1,$2); }
257         | /* NULL */
258                 { $$ = Nullop; }
259         ;
260
261 statement
262         : simple separator maybe
263                 { $$ = oper2(OJUNK,oper2(OSTATE,$1,$2),$3); }
264         | ';' maybe
265                 { $$ = oper2(OSTATE,Nullop,oper2(OJUNK,oper0(OSEMICOLON),$2)); }
266         | SEMINEW maybe
267                 { $$ = oper2(OSTATE,Nullop,oper2(OJUNK,oper0(OSNEWLINE),$2)); }
268         | compound
269         ;
270
271 simpnull: simple
272         | /* NULL */
273                 { $$ = Nullop; }
274         ;
275
276 simple
277         : expr
278         | PRINT print_list redir expr
279                 { $$ = oper3(OPRINT,$2,$3,$4);
280                     do_opens = TRUE;
281                     saw_ORS = saw_OFS = TRUE;
282                     if (!$2) need_entire = TRUE;
283                     if (ops[$4].ival != OSTR + (1<<8)) do_fancy_opens = TRUE; }
284         | PRINT print_list
285                 { $$ = oper1(OPRINT,$2);
286                     if (!$2) need_entire = TRUE;
287                     saw_ORS = saw_OFS = TRUE;
288                 }
289         | PRINTF print_list redir expr
290                 { $$ = oper3(OPRINTF,$2,$3,$4);
291                     do_opens = TRUE;
292                     if (!$2) need_entire = TRUE;
293                     if (ops[$4].ival != OSTR + (1<<8)) do_fancy_opens = TRUE; }
294         | PRINTF print_list
295                 { $$ = oper1(OPRINTF,$2);
296                     if (!$2) need_entire = TRUE;
297                 }
298         | BREAK
299                 { $$ = oper0(OBREAK); }
300         | NEXT
301                 { $$ = oper0(ONEXT); }
302         | EXIT
303                 { $$ = oper0(OEXIT); }
304         | EXIT expr
305                 { $$ = oper1(OEXIT,$2); }
306         | CONTINUE
307                 { $$ = oper0(OCONTINUE); }
308         ;
309
310 redir   : RELOP
311                 { $$ = oper1(OREDIR,string(">",1)); }
312         | GRGR
313                 { $$ = oper1(OREDIR,string(">>",2)); }
314         | '|'
315                 { $$ = oper1(OREDIR,string("|",1)); }
316         ;
317
318 compound
319         : IF '(' cond ')' maybe statement
320                 { $$ = oper2(OIF,$3,bl($6,$5)); }
321         | IF '(' cond ')' maybe statement ELSE maybe statement
322                 { $$ = oper3(OIF,$3,bl($6,$5),bl($9,$8)); }
323         | WHILE '(' cond ')' maybe statement
324                 { $$ = oper2(OWHILE,$3,bl($6,$5)); }
325         | FOR '(' simpnull ';' cond ';' simpnull ')' maybe statement
326                 { $$ = oper4(OFOR,$3,$5,$7,bl($10,$9)); }
327         | FOR '(' simpnull ';'  ';' simpnull ')' maybe statement
328                 { $$ = oper4(OFOR,$3,string("",0),$6,bl($9,$8)); }
329         | FOR '(' VAR IN VAR ')' maybe statement
330                 { $$ = oper3(OFORIN,$3,$5,bl($8,$7)); }
331         | '{' maybe states '}' maybe
332                 { $$ = oper3(OBLOCK,oper2(OJUNK,$2,$3),Nullop,$5); }
333         ;
334
335 %%
336 #include "a2py.c"