143
|
1 /* parse.y -*- text -*- |
1
|
2 |
|
3 Copyright (C) 1992, 1993 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 // Parser for Octave. |
|
24 |
|
25 /* |
|
26 * C decarations. |
|
27 */ |
|
28 %{ |
|
29 #define YYDEBUG 1 |
|
30 |
240
|
31 #ifdef HAVE_CONFIG_H |
|
32 #include "config.h" |
|
33 #endif |
|
34 |
1
|
35 #include "SLStack.h" |
|
36 |
|
37 #include "Matrix.h" |
|
38 |
|
39 #include "error.h" |
|
40 #include "variables.h" |
168
|
41 #include "octave-hist.h" |
1
|
42 #include "user-prefs.h" |
|
43 #include "input.h" |
|
44 #include "utils.h" |
|
45 #include "tree.h" |
|
46 #include "symtab.h" |
|
47 #include "builtins.h" |
|
48 #include "octave.h" |
|
49 #include "parse.h" |
|
50 #include "lex.h" |
143
|
51 #include "token.h" |
1
|
52 |
|
53 // Nonzero means we're in the middle of defining a function. |
|
54 int defining_func = 0; |
|
55 |
|
56 // Nonzero means we're in the middle of defining a loop. |
|
57 int looping = 0; |
|
58 |
|
59 // Nonzero means we're in the middle of defining a conditional expression. |
|
60 int iffing = 0; |
|
61 |
|
62 // Nonzero means we need to do some extra lookahead to avoid being |
|
63 // screwed by bogus function syntax. |
|
64 int maybe_screwed = 0; |
|
65 |
|
66 // Nonzero means we need to do some extra lookahead to avoid being |
|
67 // screwed by bogus function syntax. |
|
68 int maybe_screwed_again = 0; |
|
69 |
|
70 // Temporary symbol table pointer used to cope with bogus function syntax. |
|
71 symbol_table *tmp_local_sym_tab = (symbol_table *) NULL; |
|
72 |
|
73 // Stack to hold list of literal matrices. |
|
74 SLStack <tree_matrix *> ml; |
|
75 |
|
76 // A nonzero element corresponding to an element of ml means we just |
|
77 // started reading a new matrix. This should probably be part of a |
|
78 // new struct for matrix lists... |
|
79 SLStack <int> mlnm; |
|
80 |
|
81 // The current input line number. |
|
82 int input_line_number = 0; |
|
83 |
|
84 // The column of the current token. |
143
|
85 int current_input_column = 1; |
1
|
86 |
|
87 // Buffer for help text snagged from M-files. |
|
88 // Probably shouldn't be a fixed size... |
|
89 char help_buf [HELP_BUF_LENGTH]; |
|
90 |
|
91 // Nonzero means we're working on a plot command. |
|
92 int plotting = 0; |
|
93 |
113
|
94 // Nonzero means we've seen something that means we must be past the |
|
95 // range part of a plot command. |
|
96 int past_plot_range = 0; |
|
97 |
1
|
98 // Nonzero means we're looking at the range part of a plot command. |
|
99 int in_plot_range = 0; |
|
100 |
|
101 // Nonzero means we're looking at the using part of a plot command. |
|
102 int in_plot_using = 0; |
|
103 |
|
104 // Nonzero means we're looking at the style part of a plot command. |
|
105 int in_plot_style = 0; |
|
106 |
143
|
107 // Check to see that end statements are properly matched. |
|
108 static int check_end (token *tok, token::end_tok_type expected); |
1
|
109 |
|
110 // Error mesages for mismatched end statements. |
143
|
111 static void end_error (char *type, token::end_tok_type ettype, int l, int c); |
1
|
112 |
|
113 // Generic error messages. |
|
114 static void yyerror (char *s); |
|
115 |
|
116 static tree *maybe_convert_to_ans_assign (tree *expr); |
|
117 static void maybe_warn_assign_as_truth_value (tree *expr); |
|
118 |
|
119 #define ABORT_PARSE \ |
|
120 do \ |
|
121 { \ |
|
122 global_command = NULL_TREE; \ |
|
123 reset_parser (); \ |
|
124 yyerrok; \ |
|
125 if (interactive) \ |
|
126 YYACCEPT; \ |
|
127 else \ |
|
128 YYABORT; \ |
|
129 } \ |
|
130 while (0) |
|
131 |
|
132 %} |
|
133 |
|
134 /* |
|
135 * Bison declarations. |
|
136 */ |
|
137 %union |
|
138 { |
143
|
139 // The type of the basic tokens returned by the lexer. |
|
140 token *tok_val; |
|
141 |
|
142 // Types for the nonterminals we generate. |
1
|
143 tree *tree_type; |
|
144 tree_constant *tree_constant_type; |
|
145 tree_matrix *tree_matrix_type; |
|
146 tree_identifier *tree_identifier_type; |
|
147 tree_function *tree_function_type; |
|
148 tree_index_expression *tree_index_expression_type; |
|
149 tree_colon_expression *tree_colon_expression_type; |
|
150 tree_argument_list *tree_argument_list_type; |
|
151 tree_parameter_list *tree_parameter_list_type; |
|
152 tree_word_list *tree_word_list_type; |
|
153 tree_command *tree_command_type; |
|
154 tree_if_command *tree_if_command_type; |
195
|
155 tree_global_command *tree_global_command_type; |
1
|
156 tree_command_list *tree_command_list_type; |
|
157 tree_word_list_command *tree_word_list_command_type; |
|
158 tree_plot_command *tree_plot_command_type; |
|
159 tree_subplot_list *tree_subplot_list_type; |
|
160 tree_plot_limits *tree_plot_limits_type; |
|
161 tree_plot_range *tree_plot_range_type; |
|
162 tree_subplot_using *tree_subplot_using_type; |
|
163 tree_subplot_style *tree_subplot_style_type; |
|
164 } |
|
165 |
143
|
166 // Tokens with line and column information. |
|
167 %token <tok_val> '=' ':' '-' '+' '*' '/' |
|
168 %token <tok_val> EXPR_AND EXPR_OR EXPR_NOT |
|
169 %token <tok_val> EXPR_LT EXPR_LE EXPR_EQ EXPR_NE EXPR_GE EXPR_GT |
|
170 %token <tok_val> LEFTDIV EMUL EDIV ELEFTDIV QUOTE TRANSPOSE |
|
171 %token <tok_val> PLUS_PLUS MINUS_MINUS POW EPOW |
|
172 %token <tok_val> NUM IMAG_NUM |
169
|
173 %token <tok_val> NAME SCREW |
143
|
174 %token <tok_val> END |
|
175 %token <tok_val> PLOT |
|
176 %token <tok_val> TEXT STYLE |
191
|
177 %token <tok_val> FOR WHILE IF ELSEIF ELSE BREAK CONTINUE FUNC_RET |
1
|
178 |
143
|
179 // Other tokens. |
191
|
180 %token FCN SCREW_TWO |
206
|
181 %token GLOBAL |
|
182 %token ELLIPSIS |
|
183 %token END_OF_INPUT |
1
|
184 %token USING TITLE WITH COLON OPEN_BRACE CLOSE_BRACE |
|
185 |
143
|
186 // Nonterminals we construct. |
1
|
187 %type <tree_type> input command |
|
188 %type <tree_type> ans_expression expression simple_expr simple_expr1 |
|
189 %type <tree_type> title |
|
190 %type <tree_matrix_type> matrix |
|
191 %type <tree_identifier_type> identifier |
|
192 %type <tree_function_type> func_def func_def1 func_def2 func_def3 |
|
193 %type <tree_index_expression_type> variable |
|
194 %type <tree_colon_expression_type> colon_expr |
|
195 %type <tree_argument_list_type> arg_list arg_list1 |
|
196 %type <tree_parameter_list_type> param_list param_list1 func_def1a |
|
197 %type <tree_word_list_type> word_list word_list1 |
|
198 %type <tree_command_type> statement |
|
199 %type <tree_if_command_type> elseif |
195
|
200 %type <tree_global_command_type> global_decl global_decl1 |
1
|
201 %type <tree_command_list_type> simple_list simple_list1 list list1 opt_list |
|
202 %type <tree_word_list_command_type> word_list_cmd |
|
203 %type <tree_plot_command_type> plot_command |
|
204 %type <tree_subplot_list_type> plot_command1 plot_command2 plot_options |
|
205 %type <tree_plot_limits_type> ranges |
|
206 %type <tree_plot_range_type> ranges1 |
|
207 %type <tree_subplot_using_type> using using1 |
|
208 %type <tree_subplot_style_type> style |
|
209 |
143
|
210 // Precedence and associativity. |
1
|
211 %left ';' ',' '\n' |
|
212 %right '=' |
|
213 %left EXPR_AND EXPR_OR |
|
214 %left EXPR_LT EXPR_LE EXPR_EQ EXPR_NE EXPR_GE EXPR_GT |
|
215 %left ':' |
|
216 %left '-' '+' |
|
217 %left '*' '/' LEFTDIV EMUL EDIV ELEFTDIV |
|
218 %left QUOTE TRANSPOSE |
|
219 %left UNARY PLUS_PLUS MINUS_MINUS EXPR_NOT |
|
220 %right POW EPOW |
|
221 |
169
|
222 // There are 19 shift/reduce conflicts, ok? |
|
223 %expect 19 |
143
|
224 |
|
225 // Where to start. |
1
|
226 %start input |
|
227 |
|
228 /* |
|
229 * Grammar rules. |
|
230 */ |
|
231 %% |
|
232 |
|
233 input : '\n' |
|
234 { |
|
235 global_command = NULL_TREE; |
|
236 promptflag = 1; |
|
237 YYACCEPT; |
|
238 } |
|
239 | END_OF_INPUT |
|
240 { |
|
241 global_command = NULL_TREE; |
|
242 promptflag = 1; |
|
243 YYABORT; |
|
244 } |
|
245 | simple_list '\n' |
|
246 { |
|
247 global_command = $1; |
|
248 promptflag = 1; |
|
249 YYACCEPT; |
|
250 } |
|
251 | simple_list END_OF_INPUT |
|
252 { |
|
253 global_command = $1; |
|
254 promptflag = 1; |
|
255 YYACCEPT; |
|
256 } |
|
257 | error |
|
258 { ABORT_PARSE; } |
|
259 | error '\n' |
|
260 { ABORT_PARSE; } |
|
261 | error END_OF_INPUT |
|
262 { ABORT_PARSE; } |
|
263 | simple_list error |
|
264 { ABORT_PARSE; } |
|
265 | simple_list error END_OF_INPUT |
|
266 { ABORT_PARSE; } |
|
267 ; |
|
268 |
|
269 simple_list : semi_comma |
|
270 { $$ = (tree_command_list *) NULL; } |
|
271 | comma_semi |
|
272 { $$ = (tree_command_list *) NULL; } |
|
273 | simple_list1 |
|
274 { $$ = $1->reverse (); } |
|
275 | simple_list1 semi_comma |
|
276 { |
|
277 $1->set_print_flag (0); |
|
278 $$ = $1->reverse (); |
|
279 } |
|
280 | simple_list1 comma_semi |
|
281 { $$ = $1->reverse (); } |
|
282 ; |
|
283 |
|
284 simple_list1 : command |
|
285 { $$ = new tree_command_list ($1); } |
|
286 | semi_comma command |
|
287 { $$ = new tree_command_list ($2); } |
|
288 | comma_semi command |
|
289 { $$ = new tree_command_list ($2); } |
|
290 | simple_list1 semi_comma command |
|
291 { |
|
292 $1->set_print_flag (0); |
|
293 $$ = $1->chain ($3); |
|
294 } |
|
295 | simple_list1 comma_semi command |
|
296 { $$ = $1->chain ($3); } |
|
297 ; |
|
298 |
|
299 semi_comma : ';' |
|
300 | semi_comma ',' |
|
301 | semi_comma ';' |
|
302 ; |
|
303 |
|
304 comma_semi : ',' |
|
305 | comma_semi ';' |
|
306 | comma_semi ',' |
|
307 ; |
|
308 |
|
309 comma_nl_sep : ',' |
|
310 | '\n' |
|
311 | comma_nl_sep sep |
|
312 ; |
|
313 |
|
314 semi_sep : ';' |
|
315 | semi_sep sep |
|
316 ; |
|
317 |
|
318 opt_list : // empty |
|
319 { $$ = new tree_command_list (); } |
|
320 | list |
|
321 { $$ = $1; } |
|
322 |
|
323 list : list1 |
|
324 { $$ = $1->reverse (); } |
|
325 | list1 comma_nl_sep |
|
326 { $$ = $1->reverse (); } |
|
327 | list1 semi_sep |
|
328 { |
|
329 $1->set_print_flag (0); |
|
330 $$ = $1->reverse (); |
|
331 } |
|
332 ; |
|
333 |
|
334 list1 : command |
|
335 { $$ = new tree_command_list ($1); } |
|
336 | list1 comma_nl_sep command |
|
337 { $$ = $1->chain ($3); } |
|
338 | list1 semi_sep command |
|
339 { |
|
340 $1->set_print_flag (0); |
|
341 $$ = $1->chain ($3); |
|
342 } |
|
343 ; |
|
344 |
|
345 command : plot_command |
|
346 { $$ = $1; } |
|
347 | statement |
|
348 { $$ = $1; } |
|
349 | ans_expression |
|
350 { $$ = $1; } |
|
351 | func_def |
|
352 { $$ = $1; } |
|
353 | global_decl |
191
|
354 { $$ = $1; } |
1
|
355 ; |
|
356 |
|
357 plot_command : PLOT plot_command1 |
|
358 { |
|
359 tree_subplot_list *tmp = $2->reverse (); |
143
|
360 $$ = new tree_plot_command (tmp, $1->pttype ()); |
1
|
361 plotting = 0; |
113
|
362 past_plot_range = 0; |
1
|
363 in_plot_range = 0; |
|
364 in_plot_using = 0; |
|
365 in_plot_style = 0; |
|
366 } |
|
367 | PLOT ranges plot_command1 |
|
368 { |
|
369 tree_subplot_list *tmp = $3->reverse (); |
143
|
370 $$ = new tree_plot_command (tmp, $2, $1->pttype ()); |
1
|
371 plotting = 0; |
113
|
372 past_plot_range = 0; |
1
|
373 in_plot_range = 0; |
|
374 in_plot_using = 0; |
|
375 in_plot_style = 0; |
|
376 } |
|
377 ; |
|
378 |
|
379 ranges : ranges1 |
|
380 { $$ = new tree_plot_limits ($1); } |
|
381 | ranges1 ranges1 |
|
382 { $$ = new tree_plot_limits ($1, $2); } |
|
383 | ranges1 ranges1 ranges1 |
|
384 { $$ = new tree_plot_limits ($1, $2, $3); } |
|
385 ; |
|
386 |
|
387 ranges1 : OPEN_BRACE expression COLON expression CLOSE_BRACE |
|
388 { $$ = new tree_plot_range ($2, $4); } |
|
389 | OPEN_BRACE COLON expression CLOSE_BRACE |
|
390 { $$ = new tree_plot_range (NULL, $3); } |
|
391 | OPEN_BRACE expression COLON CLOSE_BRACE |
|
392 { $$ = new tree_plot_range ($2, NULL); } |
|
393 | OPEN_BRACE COLON CLOSE_BRACE |
|
394 { $$ = new tree_plot_range (); } |
|
395 | OPEN_BRACE CLOSE_BRACE |
|
396 { $$ = new tree_plot_range (); } |
|
397 ; |
|
398 |
|
399 plot_command1 : plot_command2 |
|
400 { $$ = $1; } |
|
401 | plot_command1 ',' plot_command2 |
|
402 { $$ = $1->chain ($3); } |
|
403 ; |
|
404 |
|
405 plot_command2 : expression |
|
406 { $$ = new tree_subplot_list ($1); } |
|
407 | expression plot_options |
|
408 { $$ = $2->set_data ($1); } |
|
409 ; |
|
410 |
|
411 plot_options : using |
|
412 { $$ = new tree_subplot_list ($1, NULL, NULL); } |
|
413 | title |
|
414 { $$ = new tree_subplot_list (NULL, $1, NULL); } |
|
415 | style |
|
416 { $$ = new tree_subplot_list (NULL, NULL, $1); } |
|
417 | using title |
|
418 { $$ = new tree_subplot_list ($1, $2, NULL); } |
|
419 | title using |
|
420 { $$ = new tree_subplot_list ($2, $1, NULL); } |
|
421 | using style |
|
422 { $$ = new tree_subplot_list ($1, NULL, $2); } |
|
423 | style using |
|
424 { $$ = new tree_subplot_list ($2, NULL, $1); } |
|
425 | title style |
|
426 { $$ = new tree_subplot_list (NULL, $1, $2); } |
|
427 | style title |
|
428 { $$ = new tree_subplot_list (NULL, $2, $1); } |
|
429 | using title style |
|
430 { $$ = new tree_subplot_list ($1, $2, $3); } |
|
431 | using style title |
|
432 { $$ = new tree_subplot_list ($1, $3, $2); } |
|
433 | title using style |
|
434 { $$ = new tree_subplot_list ($2, $1, $3); } |
|
435 | title style using |
|
436 { $$ = new tree_subplot_list ($3, $1, $2); } |
|
437 | style using title |
|
438 { $$ = new tree_subplot_list ($2, $3, $1); } |
|
439 | style title using |
|
440 { $$ = new tree_subplot_list ($3, $2, $1); } |
|
441 ; |
|
442 |
|
443 using : using1 |
|
444 { |
|
445 $$ = $1; |
|
446 in_plot_using = 0; |
|
447 } |
|
448 | using1 expression |
|
449 { |
|
450 $$ = $1->set_format ($2); |
|
451 in_plot_using = 0; |
|
452 } |
|
453 ; |
|
454 |
|
455 using1 : USING expression |
|
456 { |
|
457 tree_subplot_using *tmp = new tree_subplot_using (); |
|
458 $$ = tmp->add_qualifier ($2); |
|
459 } |
|
460 | using1 COLON expression |
|
461 { $$ = $1->add_qualifier ($3); } |
|
462 ; |
|
463 |
|
464 title : TITLE expression |
|
465 { $$ = $2; } |
|
466 ; |
|
467 |
|
468 style : WITH STYLE |
143
|
469 { $$ = new tree_subplot_style ($2->string ()); } |
1
|
470 | WITH STYLE expression |
143
|
471 { $$ = new tree_subplot_style ($2->string (), $3); } |
1
|
472 | WITH STYLE expression bogus_syntax expression |
143
|
473 { $$ = new tree_subplot_style ($2->string (), $3, $5); } |
1
|
474 ; |
|
475 |
|
476 bogus_syntax : // empty |
|
477 ; |
|
478 |
|
479 ans_expression : expression |
|
480 { $$ = maybe_convert_to_ans_assign ($1); } |
|
481 ; |
|
482 |
|
483 global_decl : GLOBAL global_decl1 |
191
|
484 { $$ = $2->reverse (); } |
195
|
485 | GLOBAL global_decl1 ',' |
|
486 { $$ = $2->reverse (); } |
1
|
487 ; |
|
488 |
|
489 global_decl1 : NAME |
191
|
490 { |
195
|
491 $$ = new tree_global_command |
|
492 ($1->sym_rec (), $1->line (), $1->column ()); |
191
|
493 } |
1
|
494 | NAME '=' expression |
|
495 { |
195
|
496 $$ = new tree_global_command |
|
497 ($1->sym_rec (), $3, $1->line (), $1->column ()); |
1
|
498 } |
|
499 | global_decl1 optcomma NAME |
191
|
500 { |
195
|
501 $$ = $1->chain ($3->sym_rec (), $3->line (), |
|
502 $3->column ()); |
191
|
503 } |
1
|
504 | global_decl1 optcomma NAME '=' expression |
|
505 { |
195
|
506 $$ = $1->chain ($3->sym_rec (), $5, $3->line (), |
|
507 $3->column ()); |
1
|
508 } |
|
509 ; |
|
510 |
|
511 optcomma : // empty |
|
512 | ',' |
|
513 { |
|
514 if (user_pref.warn_comma_in_global_decl) |
|
515 warning ("comma in global declaration not\ |
|
516 interpreted as a command separator"); |
|
517 } |
|
518 ; |
|
519 |
|
520 statement : WHILE expression optsep opt_list END |
|
521 { |
|
522 maybe_warn_assign_as_truth_value ($2); |
143
|
523 if (check_end ($5, token::while_end)) |
|
524 ABORT_PARSE; |
1
|
525 looping--; |
191
|
526 $$ = new tree_while_command ($2, $4, $1->line (), |
|
527 $1->column ()); |
1
|
528 } |
118
|
529 | FOR variable '=' expression optsep opt_list END |
1
|
530 { |
143
|
531 if (check_end ($7, token::for_end)) |
|
532 ABORT_PARSE; |
1
|
533 looping--; |
191
|
534 $$ = new tree_for_command ($2, $4, $6, |
|
535 $1->line (), $1->column ()); |
1
|
536 } |
|
537 | IF expression optsep opt_list END |
|
538 { |
|
539 maybe_warn_assign_as_truth_value ($2); |
143
|
540 if (check_end ($5, token::if_end)) |
|
541 ABORT_PARSE; |
1
|
542 iffing--; |
191
|
543 $$ = new tree_if_command ($2, $4, |
|
544 $1->line (), $1->column ()); |
1
|
545 } |
|
546 | IF expression optsep opt_list ELSE optsep opt_list END |
|
547 { |
|
548 maybe_warn_assign_as_truth_value ($2); |
143
|
549 if (check_end ($8, token::if_end)) |
|
550 ABORT_PARSE; |
1
|
551 iffing--; |
191
|
552 tree_if_command *t1 = new tree_if_command |
|
553 ($7, $5->line (), $5->column ()); |
|
554 $$ = t1->chain ($2, $4, $1->line (), $1->column ()); |
1
|
555 } |
|
556 | IF expression optsep opt_list elseif END |
|
557 { |
|
558 maybe_warn_assign_as_truth_value ($2); |
143
|
559 if (check_end ($6, token::if_end)) |
|
560 ABORT_PARSE; |
1
|
561 iffing--; |
|
562 tree_if_command *t1 = $5->reverse (); |
|
563 // Add the if list to the new head of the elseif |
|
564 // list, and return the list. |
191
|
565 $$ = t1->chain ($2, $4, $1->line (), $1->column ()); |
1
|
566 } |
|
567 | IF expression optsep opt_list elseif ELSE optsep opt_list END |
|
568 { |
|
569 maybe_warn_assign_as_truth_value ($2); |
143
|
570 if (check_end ($9, token::if_end)) |
|
571 ABORT_PARSE; |
1
|
572 iffing--; |
|
573 // Add the else list to the head of the elseif list, |
|
574 // then reverse the list. |
191
|
575 tree_if_command *t1 = $5->chain ($8, $6->line (), |
|
576 $6->column ()); |
1
|
577 t1 = t1->reverse (); |
|
578 // Add the if list to the new head of the elseif |
|
579 // list, and return the list. |
191
|
580 $$ = t1->chain ($2, $4, $1->line (), $1->column ()); |
1
|
581 } |
|
582 | BREAK |
|
583 { |
|
584 if (!looping) |
|
585 { |
|
586 yyerror ("parse error"); |
|
587 error ("break: only meaningful within a `for'\ |
|
588 or `while' loop"); |
|
589 ABORT_PARSE; |
|
590 } |
191
|
591 $$ = new tree_break_command ($1->line (), $1->column ()); |
1
|
592 } |
|
593 | CONTINUE |
|
594 { |
|
595 if (!looping) |
|
596 { |
|
597 yyerror ("parse error"); |
|
598 error ("continue: only meaningful within a\ |
|
599 `for' or `while' loop"); |
|
600 ABORT_PARSE; |
|
601 } |
191
|
602 $$ = new tree_continue_command ($1->line (), |
|
603 $1->column ()); |
1
|
604 } |
|
605 | FUNC_RET |
|
606 { |
|
607 if (!defining_func) |
|
608 { |
|
609 yyerror ("parse error"); |
|
610 error ("return: only meaningful within a function"); |
|
611 ABORT_PARSE; |
|
612 } |
191
|
613 $$ = new tree_return_command ($1->line (), $1->column ()); |
1
|
614 } |
|
615 ; |
|
616 |
|
617 elseif : ELSEIF optsep expression optsep opt_list |
|
618 { |
|
619 maybe_warn_assign_as_truth_value ($3); |
191
|
620 $$ = new tree_if_command ($3, $5, $1->line (), |
|
621 $1->column ()); |
1
|
622 } |
|
623 | elseif ELSEIF optsep expression optsep opt_list |
|
624 { |
|
625 maybe_warn_assign_as_truth_value ($4); |
191
|
626 $$ = $1->chain ($4, $6, $2->line (), $2->column ()); |
1
|
627 } |
|
628 ; |
|
629 |
|
630 optsep : // empty |
|
631 | sep |
|
632 ; |
|
633 |
|
634 sep : ',' |
|
635 | ';' |
|
636 | '\n' |
|
637 | sep ',' |
|
638 | sep ';' |
|
639 | sep '\n' |
|
640 ; |
|
641 |
|
642 screwed_again : // empty |
|
643 { maybe_screwed_again++; } |
|
644 ; |
|
645 |
|
646 expression : variable '=' expression |
143
|
647 { $$ = new tree_simple_assignment_expression |
|
648 ($1, $3, $2->line (), $2->column ()); } |
1
|
649 | '[' screwed_again matrix_row SCREW_TWO '=' expression |
|
650 { |
|
651 |
|
652 // Will need a way to convert the matrix list to a list of |
143
|
653 // identifiers. If that fails, we can abort here, without losing |
1
|
654 // anything -- no other possible syntax is valid if we've seen the |
|
655 // equals sign as the next token after the `]'. |
|
656 |
|
657 $$ = (tree_multi_assignment_expression *) NULL; |
|
658 maybe_screwed_again--; |
|
659 tree_matrix *tmp = ml.pop (); |
|
660 tmp = tmp->reverse (); |
|
661 tree_return_list *id_list = tmp->to_return_list (); |
|
662 if (id_list == NULL_TREE) |
|
663 { |
|
664 yyerror ("parse error"); |
143
|
665 error ("invalid identifier list for assignment"); |
1
|
666 $$ = (tree_multi_assignment_expression *) NULL; |
|
667 ABORT_PARSE; |
|
668 } |
|
669 else |
143
|
670 $$ = new tree_multi_assignment_expression |
|
671 (id_list, $6, $5->line (), $5->column ()); |
1
|
672 } |
|
673 | NUM '=' expression |
|
674 { |
|
675 yyerror ("parse error"); |
|
676 error ("invalid assignment to a number"); |
|
677 $$ = (tree_simple_assignment_expression *) NULL; |
|
678 ABORT_PARSE; |
|
679 } |
|
680 | simple_expr |
|
681 { $$ = $1; } |
|
682 ; |
|
683 |
|
684 simple_expr : simple_expr1 |
|
685 { $$ = $1; } |
|
686 | identifier PLUS_PLUS |
143
|
687 { $$ = new tree_postfix_expression |
|
688 ($1, tree::increment, $2->line (), $2->column ()); } |
1
|
689 | identifier MINUS_MINUS |
143
|
690 { $$ = new tree_postfix_expression |
|
691 ($1, tree::decrement, $2->line (), $2->column ()); } |
1
|
692 | simple_expr QUOTE |
143
|
693 { $$ = new tree_unary_expression |
|
694 ($1, tree::hermitian, $2->line (), $2->column ()); } |
1
|
695 | simple_expr TRANSPOSE |
143
|
696 { $$ = new tree_unary_expression |
|
697 ($1, tree::transpose, $2->line (), $2->column ()); } |
1
|
698 | simple_expr POW simple_expr |
143
|
699 { $$ = new tree_binary_expression |
|
700 ($1, $3, tree::power, $2->line (), $2->column ()); } |
1
|
701 | simple_expr EPOW simple_expr |
143
|
702 { $$ = new tree_binary_expression |
|
703 ($1, $3, tree::elem_pow, $2->line (), $2->column ()); } |
1
|
704 | simple_expr '+' simple_expr |
143
|
705 { $$ = new tree_binary_expression |
|
706 ($1, $3, tree::add, $2->line (), $2->column ()); } |
1
|
707 | simple_expr '-' simple_expr |
143
|
708 { $$ = new tree_binary_expression |
|
709 ($1, $3, tree::subtract, $2->line (), $2->column ()); } |
1
|
710 | simple_expr '*' simple_expr |
143
|
711 { $$ = new tree_binary_expression |
|
712 ($1, $3, tree::multiply, $2->line (), $2->column ()); } |
1
|
713 | simple_expr '/' simple_expr |
143
|
714 { $$ = new tree_binary_expression |
|
715 ($1, $3, tree::divide, $2->line (), $2->column ()); } |
1
|
716 | simple_expr EMUL simple_expr |
143
|
717 { $$ = new tree_binary_expression |
|
718 ($1, $3, tree::el_mul, $2->line (), $2->column ()); } |
1
|
719 | simple_expr EDIV simple_expr |
143
|
720 { $$ = new tree_binary_expression |
|
721 ($1, $3, tree::el_div, $2->line (), $2->column ()); } |
1
|
722 | simple_expr LEFTDIV simple_expr |
143
|
723 { $$ = new tree_binary_expression |
|
724 ($1, $3, tree::leftdiv, $2->line (), $2->column ()); } |
1
|
725 | simple_expr ELEFTDIV simple_expr |
143
|
726 { $$ = new tree_binary_expression |
|
727 ($1, $3, tree::el_leftdiv, $2->line (), $2->column ()); } |
1
|
728 | simple_expr EXPR_LT simple_expr |
143
|
729 { $$ = new tree_binary_expression |
|
730 ($1, $3, tree::cmp_lt, $2->line (), $2->column ()); } |
1
|
731 | simple_expr EXPR_LE simple_expr |
143
|
732 { $$ = new tree_binary_expression |
|
733 ($1, $3, tree::cmp_le, $2->line (), $2->column ()); } |
1
|
734 | simple_expr EXPR_EQ simple_expr |
143
|
735 { $$ = new tree_binary_expression |
|
736 ($1, $3, tree::cmp_eq, $2->line (), $2->column ()); } |
1
|
737 | simple_expr EXPR_GE simple_expr |
143
|
738 { $$ = new tree_binary_expression |
|
739 ($1, $3, tree::cmp_ge, $2->line (), $2->column ()); } |
1
|
740 | simple_expr EXPR_GT simple_expr |
143
|
741 { $$ = new tree_binary_expression |
|
742 ($1, $3, tree::cmp_gt, $2->line (), $2->column ()); } |
1
|
743 | simple_expr EXPR_NE simple_expr |
143
|
744 { $$ = new tree_binary_expression |
|
745 ($1, $3, tree::cmp_ne, $2->line (), $2->column ()); } |
1
|
746 | simple_expr EXPR_AND simple_expr |
143
|
747 { $$ = new tree_binary_expression |
|
748 ($1, $3, tree::and, $2->line (), $2->column ()); } |
1
|
749 | simple_expr EXPR_OR simple_expr |
143
|
750 { $$ = new tree_binary_expression |
|
751 ($1, $3, tree::or, $2->line (), $2->column ()); } |
1
|
752 ; |
|
753 |
|
754 simple_expr1 : NUM |
143
|
755 { $$ = new tree_constant ($1->number ()); } |
1
|
756 | IMAG_NUM |
143
|
757 { $$ = new tree_constant (Complex (0.0, $1->number ())); } |
1
|
758 | TEXT |
143
|
759 { $$ = new tree_constant ($1->string ()); } |
1
|
760 | word_list_cmd |
|
761 { $$ = $1; } |
|
762 | '(' expression ')' |
|
763 { |
|
764 if ($2->is_assignment_expression ()) |
|
765 ((tree_assignment_expression *) $2) -> in_parens++; |
|
766 $$ = $2; |
|
767 } |
|
768 | variable |
|
769 { $$ = $1; } |
|
770 | matrix |
|
771 { $$ = $1; } |
240
|
772 | '[' ']' |
|
773 { |
|
774 mlnm.pop (); |
|
775 $$ = new tree_constant (Matrix ()); |
|
776 } |
|
777 | '[' ';' ']' |
|
778 { |
|
779 mlnm.pop (); |
|
780 $$ = new tree_constant (Matrix ()); |
|
781 } |
1
|
782 | colon_expr |
|
783 { $$ = $1; } |
|
784 | PLUS_PLUS identifier %prec UNARY |
143
|
785 { $$ = new tree_prefix_expression |
|
786 ($2, tree::increment, $1->line (), $1->column ()); } |
1
|
787 | MINUS_MINUS identifier %prec UNARY |
143
|
788 { $$ = new tree_prefix_expression |
|
789 ($2, tree::decrement, $1->line (), $1->column ()); } |
1
|
790 | EXPR_NOT simple_expr |
143
|
791 { $$ = new tree_unary_expression |
|
792 ($2, tree::not, $1->line (), $1->column ()); } |
1
|
793 | '+' simple_expr %prec UNARY |
|
794 { $$ = $2; } |
|
795 | '-' simple_expr %prec UNARY |
143
|
796 { $$ = new tree_unary_expression |
|
797 ($2, tree::uminus, $1->line (), $1->column ()); } |
1
|
798 ; |
|
799 |
|
800 colon_expr : simple_expr ':' simple_expr |
143
|
801 { $$ = new tree_colon_expression |
|
802 ($1, $3, $2->line (), $2->column ()); } |
1
|
803 | colon_expr ':' simple_expr |
|
804 { |
|
805 $$ = $1->chain ($3); |
|
806 if ($$ == (tree_colon_expression *) NULL) |
|
807 { |
|
808 yyerror ("parse error"); |
|
809 ABORT_PARSE; |
|
810 } |
|
811 } |
|
812 ; |
|
813 |
|
814 word_list_cmd : identifier word_list |
|
815 { $$ = new tree_word_list_command ($1, $2); } |
|
816 ; |
|
817 |
|
818 word_list : word_list1 |
|
819 { $$ = $1->reverse (); } |
|
820 ; |
|
821 |
|
822 word_list1 : TEXT |
143
|
823 { $$ = new tree_word_list ($1->string ()); } |
1
|
824 | word_list1 TEXT |
143
|
825 { $$ = $1->chain ($2->string ()); } |
1
|
826 ; |
|
827 |
|
828 // This is truly disgusting. |
|
829 |
|
830 g_symtab : // empty |
|
831 { curr_sym_tab = global_sym_tab; } |
|
832 ; |
|
833 |
|
834 local_symtab : // empty |
|
835 { curr_sym_tab = tmp_local_sym_tab; } |
|
836 ; |
|
837 |
|
838 safe : // empty |
|
839 { maybe_screwed = 0; } |
|
840 ; |
|
841 |
|
842 are_we_screwed : // empty |
|
843 { maybe_screwed = 1; } |
|
844 ; |
|
845 |
|
846 func_def : FCN g_symtab are_we_screwed func_def1 |
|
847 { |
|
848 curr_sym_tab = top_level_sym_tab; |
|
849 defining_func = 0; |
|
850 $$ = (tree_function *) NULL; |
|
851 } |
|
852 | FCN g_symtab are_we_screwed func_def2 |
|
853 { |
|
854 curr_sym_tab = top_level_sym_tab; |
|
855 defining_func = 0; |
|
856 $$ = (tree_function *) NULL; |
|
857 } |
|
858 ; |
|
859 |
|
860 func_def1 : SCREW safe g_symtab '=' func_def2 |
|
861 { |
143
|
862 tree_identifier *tmp = new tree_identifier |
|
863 ($1->sym_rec (), $1->line (), $1->column ()); |
1
|
864 tree_parameter_list *tpl = new tree_parameter_list (tmp); |
|
865 tpl = tpl->reverse (); |
|
866 tpl->mark_as_formal_parameters (); |
|
867 $$ = $5->define_ret_list (tpl); |
|
868 } |
|
869 | func_def1a ']' g_symtab '=' func_def2 |
|
870 { |
|
871 tree_parameter_list *tpl = $1->reverse (); |
|
872 tpl->mark_as_formal_parameters (); |
|
873 $$ = $5->define_ret_list (tpl); |
|
874 } |
|
875 ; |
|
876 |
|
877 func_def1a : '[' safe local_symtab identifier |
|
878 { $$ = new tree_parameter_list ($4); } |
|
879 | func_def1a ',' identifier |
|
880 { $$ = $1->chain ($3); } |
|
881 ; |
|
882 |
|
883 func_def2 : identifier safe local_symtab func_def3 |
|
884 { |
|
885 char *id_name = $1->name (); |
|
886 // if (is_text_function_name (id_name)) |
|
887 // { |
|
888 // yyerror ("parse error"); |
|
889 // error ("invalid use of reserved word %s", id_name); |
|
890 // ABORT_PARSE; |
|
891 // } |
|
892 |
|
893 // If input is coming from a file, issue a warning if the name of the |
|
894 // file does not match the name of the function stated in the file. |
|
895 // Matlab doesn't provide a diagnostic (it ignores the stated name). |
|
896 |
|
897 if (reading_m_file) |
|
898 { |
|
899 if (strcmp (curr_m_file_name, id_name) != 0) |
195
|
900 { |
|
901 warning ("function name `%s' does not agree\ |
1
|
902 with M-file name `%s.m'", id_name, curr_m_file_name); |
|
903 |
195
|
904 $1->rename (curr_m_file_name); |
197
|
905 id_name = $1->name (); |
195
|
906 } |
|
907 |
|
908 $4->stash_m_file_name (curr_m_file_name); |
|
909 $4->stash_m_file_time (time ((time_t *) NULL)); |
|
910 $4->mark_as_system_m_file (); |
1
|
911 } |
195
|
912 else if (! input_from_tmp_history_file |
|
913 && reading_script_file |
|
914 && strcmp (curr_m_file_name, id_name) == 0) |
1
|
915 { |
195
|
916 warning ("function `%s' defined within\ |
1
|
917 script file `%s.m'", id_name, curr_m_file_name); |
|
918 } |
|
919 |
197
|
920 top_level_sym_tab->clear (id_name); |
|
921 |
143
|
922 $4->stash_function_name (id_name); |
195
|
923 |
|
924 $1->define ($4); |
|
925 $1->document (help_buf); |
|
926 |
1
|
927 $$ = $4; |
|
928 } |
|
929 ; |
|
930 |
|
931 func_def3 : param_list optsep opt_list fcn_end_or_eof |
|
932 { |
|
933 tree_function *fcn = new tree_function ($3, curr_sym_tab); |
|
934 $$ = fcn->define_param_list ($1); |
|
935 } |
|
936 | optsep opt_list fcn_end_or_eof |
|
937 { $$ = new tree_function ($2, curr_sym_tab); } |
|
938 ; |
|
939 |
|
940 fcn_end_or_eof : END |
|
941 { |
143
|
942 if (check_end ($1, token::function_end)) |
1
|
943 ABORT_PARSE; |
|
944 |
|
945 if (reading_m_file) |
|
946 check_for_garbage_after_fcn_def (); |
|
947 } |
|
948 | END_OF_INPUT |
|
949 { |
|
950 if (! (reading_m_file || reading_script_file)) |
|
951 YYABORT; |
|
952 } |
|
953 ; |
|
954 |
|
955 variable : identifier |
191
|
956 { |
|
957 $$ = new tree_index_expression |
|
958 ($1, $1->line (), $1->column ()); |
|
959 } |
1
|
960 | identifier '(' arg_list ')' |
191
|
961 { |
|
962 $$ = new tree_index_expression |
|
963 ($1, $3, $1->line (), $1->column ()); |
|
964 } |
1
|
965 | identifier '(' ')' |
|
966 { |
|
967 $$ = new tree_index_expression |
191
|
968 ($1, (tree_argument_list *) NULL, |
|
969 $1->line (), $1->column ()); |
1
|
970 } |
|
971 | identifier '[' |
|
972 { |
|
973 yyerror ("parse error"); |
|
974 error ("use `(\' and `)\' as index operators, not\ |
|
975 `[\' and `]\'"); |
|
976 $$ = (tree_index_expression *) NULL; |
|
977 ABORT_PARSE; |
|
978 } |
|
979 ; |
|
980 |
240
|
981 param_list : '(' ')' |
1
|
982 { |
240
|
983 quote_is_transpose = 0; |
|
984 $$ = (tree_parameter_list *) NULL; |
|
985 } |
|
986 | param_list1 ')' |
|
987 { |
|
988 quote_is_transpose = 0; |
1
|
989 tree_parameter_list *tmp = $1->reverse (); |
|
990 tmp->mark_as_formal_parameters (); |
|
991 $$ = tmp; |
|
992 } |
206
|
993 | param_list1 ',' ELLIPSIS ')' |
|
994 { |
240
|
995 quote_is_transpose = 0; |
206
|
996 tree_parameter_list *tmp = $1->reverse (); |
|
997 tmp->mark_as_formal_parameters (); |
|
998 tmp->mark_varargs (); |
|
999 $$ = tmp; |
|
1000 } |
1
|
1001 |
|
1002 param_list1 : '(' identifier |
|
1003 { $$ = new tree_parameter_list ($2); } |
|
1004 | param_list1 ',' identifier |
|
1005 { $$ = $1->chain ($3); } |
|
1006 | '(' error |
|
1007 { |
206
|
1008 yyerror ("parse error"); |
|
1009 error ("invalid parameter list"); |
|
1010 ABORT_PARSE; |
1
|
1011 } |
|
1012 | param_list1 ',' error |
|
1013 { |
206
|
1014 yyerror ("parse error"); |
|
1015 error ("invalid parameter list"); |
|
1016 ABORT_PARSE; |
1
|
1017 } |
|
1018 ; |
|
1019 |
|
1020 identifier : NAME |
143
|
1021 { $$ = new tree_identifier |
|
1022 ($1->sym_rec (), $1->line (), $1->column ()); } |
1
|
1023 |
|
1024 arg_list : arg_list1 |
|
1025 { $$ = $1->reverse (); } |
|
1026 ; |
|
1027 |
|
1028 arg_list1 : ':' |
|
1029 { |
|
1030 tree_constant *colon; |
|
1031 colon = new tree_constant (tree_constant_rep::magic_colon); |
|
1032 $$ = new tree_argument_list (colon); |
|
1033 } |
|
1034 | arg_list1 ',' ':' |
|
1035 { |
|
1036 tree_constant *colon; |
|
1037 colon = new tree_constant (tree_constant_rep::magic_colon); |
|
1038 $$ = $1->chain (colon); |
|
1039 if ($$ == NULL_TREE) |
|
1040 { |
|
1041 yyerror ("parse error"); |
|
1042 ABORT_PARSE; |
|
1043 } |
|
1044 } |
|
1045 | expression |
|
1046 { $$ = new tree_argument_list ($1); } |
|
1047 | arg_list1 ',' expression |
|
1048 { |
|
1049 $$ = $1->chain ($3); |
|
1050 if ($$ == NULL_TREE) |
|
1051 { |
|
1052 yyerror ("parse error"); |
|
1053 ABORT_PARSE; |
|
1054 } |
|
1055 } |
|
1056 ; |
|
1057 |
240
|
1058 matrix : '[' screwed_again rows ']' |
1
|
1059 { |
|
1060 mlnm.pop (); |
|
1061 maybe_screwed_again--; |
|
1062 tree_matrix *tmp = ml.pop (); |
|
1063 $$ = tmp->reverse (); |
|
1064 } |
|
1065 ; |
|
1066 |
|
1067 rows : matrix_row |
|
1068 | rows ';' // Ignore trailing semicolon. |
|
1069 | rows ';' matrix_row |
|
1070 ; |
|
1071 |
|
1072 matrix_row : expression // First element on row. |
|
1073 { |
|
1074 if (mlnm.top ()) |
|
1075 { |
|
1076 mlnm.pop (); |
|
1077 mlnm.push (0); |
143
|
1078 tree_matrix *tmp = new tree_matrix ($1, tree::md_none); |
1
|
1079 ml.push (tmp); |
|
1080 } |
|
1081 else |
|
1082 { |
|
1083 tree_matrix *tmp = ml.pop (); |
|
1084 tmp = tmp->chain ($1, tree::md_down); |
|
1085 ml.push (tmp); |
|
1086 } |
|
1087 } |
|
1088 | matrix_row ',' // Ignore trailing comma. |
|
1089 | matrix_row ',' expression |
|
1090 { |
|
1091 tree_matrix *tmp = ml.pop (); |
|
1092 tmp = tmp->chain ($3, tree::md_right); |
|
1093 ml.push (tmp); |
|
1094 } |
|
1095 ; |
|
1096 |
|
1097 %% |
|
1098 |
|
1099 static void |
|
1100 yyerror (char *s) |
|
1101 { |
|
1102 char *line = current_input_line; |
143
|
1103 int err_col = current_input_column - 1; |
1
|
1104 if (err_col == 0) |
|
1105 err_col = strlen (current_input_line) + 1; |
|
1106 |
|
1107 // Print a message like `parse error'. |
|
1108 fprintf (stderr, "\n%s", s); |
|
1109 |
|
1110 // Maybe print the line number and file name. |
|
1111 if (reading_m_file || reading_script_file) |
|
1112 fprintf (stderr, " near line %d of file %s.m", input_line_number, |
|
1113 curr_m_file_name); |
|
1114 |
|
1115 int len = strlen (line); |
|
1116 if (line[len-1] == '\n') |
|
1117 { |
|
1118 len--; |
|
1119 line[len] = '\0'; |
|
1120 } |
|
1121 |
|
1122 // Print the line, maybe with a pointer near the error token. |
|
1123 if (err_col > len) |
|
1124 fprintf (stderr, ":\n\n %s\n\n", line); |
|
1125 else |
|
1126 fprintf (stderr, ":\n\n %s\n %*s\n\n", line, err_col, "^"); |
|
1127 } |
|
1128 |
143
|
1129 static int |
|
1130 check_end (token *tok, token::end_tok_type expected) |
|
1131 { |
|
1132 token::end_tok_type ettype = tok->ettype (); |
|
1133 if (ettype != expected && ettype != token::simple_end) |
|
1134 { |
|
1135 yyerror ("parse error"); |
|
1136 |
|
1137 int l = tok->line (); |
|
1138 int c = tok->column (); |
|
1139 |
|
1140 switch (expected) |
|
1141 { |
|
1142 case token::for_end: |
|
1143 end_error ("for", ettype, l, c); |
|
1144 break; |
|
1145 case token::function_end: |
|
1146 end_error ("function", ettype, l, c); |
|
1147 break; |
|
1148 case token::if_end: |
|
1149 end_error ("if", ettype, l, c); |
|
1150 break; |
|
1151 case token::while_end: |
|
1152 end_error ("while", ettype, l, c); |
|
1153 break; |
|
1154 default: |
|
1155 panic_impossible (); |
|
1156 break; |
|
1157 } |
|
1158 return 1; |
|
1159 } |
|
1160 else |
|
1161 return 0; |
|
1162 } |
|
1163 |
1
|
1164 static void |
143
|
1165 end_error (char *type, token::end_tok_type ettype, int l, int c) |
1
|
1166 { |
143
|
1167 static char *fmt = "%s command matched by `%s' near line %d column %d"; |
|
1168 |
1
|
1169 switch (ettype) |
|
1170 { |
143
|
1171 case token::simple_end: |
|
1172 error (fmt, type, "end", l, c); |
1
|
1173 break; |
143
|
1174 case token::for_end: |
|
1175 error (fmt, type, "endfor", l, c); |
1
|
1176 break; |
143
|
1177 case token::function_end: |
|
1178 error (fmt, type, "endfunction", l, c); |
1
|
1179 break; |
143
|
1180 case token::if_end: |
|
1181 error (fmt, type, "endif", l, c); |
1
|
1182 break; |
143
|
1183 case token::while_end: |
|
1184 error (fmt, type, "endwhile", l, c); |
1
|
1185 break; |
|
1186 default: |
|
1187 panic_impossible (); |
|
1188 break; |
|
1189 } |
|
1190 } |
|
1191 |
|
1192 /* |
|
1193 * Need to make sure that the expression isn't already an identifier |
|
1194 * that has a name, or an assignment expression. |
|
1195 * |
|
1196 * Note that an expression can't be just an identifier anymore -- it |
|
1197 * must at least be an index expression (see the definition of the |
|
1198 * non-terminal `variable' above). |
183
|
1199 * |
|
1200 * XXX FIXME XXX. This isn't quite sufficient. For example, try the |
|
1201 * command `x = 4, x' for `x' previously undefined. |
195
|
1202 * |
|
1203 * XXX FIXME XXX -- we should probably delay doing this until eval-time. |
1
|
1204 */ |
|
1205 tree * |
|
1206 maybe_convert_to_ans_assign (tree *expr) |
|
1207 { |
|
1208 if (expr->is_index_expression ()) |
|
1209 { |
195
|
1210 expr->mark_for_possible_ans_assign (); |
|
1211 return expr; |
|
1212 } |
|
1213 else if (expr->is_assignment_expression () |
|
1214 || expr->is_prefix_expression ()) |
|
1215 { |
|
1216 return expr; |
|
1217 } |
|
1218 else |
|
1219 { |
|
1220 symbol_record *sr = global_sym_tab->lookup ("ans", 1, 0); |
1
|
1221 |
195
|
1222 assert (sr != (symbol_record *) NULL); |
|
1223 |
|
1224 tree_identifier *ans = new tree_identifier (sr); |
|
1225 |
|
1226 return new tree_simple_assignment_expression (ans, expr); |
1
|
1227 } |
|
1228 } |
|
1229 |
|
1230 void |
|
1231 maybe_warn_assign_as_truth_value (tree *expr) |
|
1232 { |
|
1233 if (user_pref.warn_assign_as_truth_value |
|
1234 && expr->is_assignment_expression () |
|
1235 && ((tree_assignment_expression *) expr) -> in_parens < 2) |
|
1236 { |
|
1237 warning ("suggest parenthesis around assignment used as truth value"); |
|
1238 } |
|
1239 } |