143
|
1 /* parse.y -*- text -*- |
1
|
2 |
315
|
3 Copyright (C) 1992, 1993, 1994 John W. Eaton |
1
|
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 } |
327
|
245 | simple_list |
|
246 { |
|
247 global_command = $1; |
|
248 promptflag = 1; |
|
249 YYACCEPT; |
|
250 } |
1
|
251 | simple_list '\n' |
|
252 { |
|
253 global_command = $1; |
|
254 promptflag = 1; |
|
255 YYACCEPT; |
|
256 } |
|
257 | simple_list END_OF_INPUT |
|
258 { |
|
259 global_command = $1; |
|
260 promptflag = 1; |
|
261 YYACCEPT; |
|
262 } |
|
263 | error |
|
264 { ABORT_PARSE; } |
|
265 | error '\n' |
|
266 { ABORT_PARSE; } |
|
267 | error END_OF_INPUT |
|
268 { ABORT_PARSE; } |
|
269 | simple_list error |
|
270 { ABORT_PARSE; } |
327
|
271 | simple_list error '\n' |
|
272 { ABORT_PARSE; } |
1
|
273 | simple_list error END_OF_INPUT |
|
274 { ABORT_PARSE; } |
|
275 ; |
|
276 |
|
277 simple_list : semi_comma |
|
278 { $$ = (tree_command_list *) NULL; } |
|
279 | comma_semi |
|
280 { $$ = (tree_command_list *) NULL; } |
|
281 | simple_list1 |
|
282 { $$ = $1->reverse (); } |
|
283 | simple_list1 semi_comma |
|
284 { |
|
285 $1->set_print_flag (0); |
|
286 $$ = $1->reverse (); |
|
287 } |
|
288 | simple_list1 comma_semi |
|
289 { $$ = $1->reverse (); } |
|
290 ; |
|
291 |
|
292 simple_list1 : command |
|
293 { $$ = new tree_command_list ($1); } |
|
294 | semi_comma command |
|
295 { $$ = new tree_command_list ($2); } |
|
296 | comma_semi command |
|
297 { $$ = new tree_command_list ($2); } |
|
298 | simple_list1 semi_comma command |
|
299 { |
|
300 $1->set_print_flag (0); |
|
301 $$ = $1->chain ($3); |
|
302 } |
|
303 | simple_list1 comma_semi command |
|
304 { $$ = $1->chain ($3); } |
|
305 ; |
|
306 |
|
307 semi_comma : ';' |
|
308 | semi_comma ',' |
|
309 | semi_comma ';' |
|
310 ; |
|
311 |
|
312 comma_semi : ',' |
|
313 | comma_semi ';' |
|
314 | comma_semi ',' |
|
315 ; |
|
316 |
|
317 comma_nl_sep : ',' |
|
318 | '\n' |
|
319 | comma_nl_sep sep |
|
320 ; |
|
321 |
|
322 semi_sep : ';' |
|
323 | semi_sep sep |
|
324 ; |
|
325 |
|
326 opt_list : // empty |
|
327 { $$ = new tree_command_list (); } |
|
328 | list |
|
329 { $$ = $1; } |
|
330 |
|
331 list : list1 |
|
332 { $$ = $1->reverse (); } |
|
333 | list1 comma_nl_sep |
|
334 { $$ = $1->reverse (); } |
|
335 | list1 semi_sep |
|
336 { |
|
337 $1->set_print_flag (0); |
|
338 $$ = $1->reverse (); |
|
339 } |
|
340 ; |
|
341 |
|
342 list1 : command |
|
343 { $$ = new tree_command_list ($1); } |
|
344 | list1 comma_nl_sep command |
|
345 { $$ = $1->chain ($3); } |
|
346 | list1 semi_sep command |
|
347 { |
|
348 $1->set_print_flag (0); |
|
349 $$ = $1->chain ($3); |
|
350 } |
|
351 ; |
|
352 |
|
353 command : plot_command |
|
354 { $$ = $1; } |
|
355 | statement |
|
356 { $$ = $1; } |
|
357 | ans_expression |
|
358 { $$ = $1; } |
|
359 | func_def |
|
360 { $$ = $1; } |
|
361 | global_decl |
191
|
362 { $$ = $1; } |
1
|
363 ; |
|
364 |
|
365 plot_command : PLOT plot_command1 |
|
366 { |
|
367 tree_subplot_list *tmp = $2->reverse (); |
143
|
368 $$ = new tree_plot_command (tmp, $1->pttype ()); |
1
|
369 plotting = 0; |
113
|
370 past_plot_range = 0; |
1
|
371 in_plot_range = 0; |
|
372 in_plot_using = 0; |
|
373 in_plot_style = 0; |
|
374 } |
|
375 | PLOT ranges plot_command1 |
|
376 { |
|
377 tree_subplot_list *tmp = $3->reverse (); |
143
|
378 $$ = new tree_plot_command (tmp, $2, $1->pttype ()); |
1
|
379 plotting = 0; |
113
|
380 past_plot_range = 0; |
1
|
381 in_plot_range = 0; |
|
382 in_plot_using = 0; |
|
383 in_plot_style = 0; |
|
384 } |
|
385 ; |
|
386 |
|
387 ranges : ranges1 |
|
388 { $$ = new tree_plot_limits ($1); } |
|
389 | ranges1 ranges1 |
|
390 { $$ = new tree_plot_limits ($1, $2); } |
|
391 | ranges1 ranges1 ranges1 |
|
392 { $$ = new tree_plot_limits ($1, $2, $3); } |
|
393 ; |
|
394 |
|
395 ranges1 : OPEN_BRACE expression COLON expression CLOSE_BRACE |
|
396 { $$ = new tree_plot_range ($2, $4); } |
|
397 | OPEN_BRACE COLON expression CLOSE_BRACE |
|
398 { $$ = new tree_plot_range (NULL, $3); } |
|
399 | OPEN_BRACE expression COLON CLOSE_BRACE |
|
400 { $$ = new tree_plot_range ($2, NULL); } |
|
401 | OPEN_BRACE COLON CLOSE_BRACE |
|
402 { $$ = new tree_plot_range (); } |
|
403 | OPEN_BRACE CLOSE_BRACE |
|
404 { $$ = new tree_plot_range (); } |
|
405 ; |
|
406 |
|
407 plot_command1 : plot_command2 |
|
408 { $$ = $1; } |
|
409 | plot_command1 ',' plot_command2 |
|
410 { $$ = $1->chain ($3); } |
|
411 ; |
|
412 |
|
413 plot_command2 : expression |
|
414 { $$ = new tree_subplot_list ($1); } |
|
415 | expression plot_options |
|
416 { $$ = $2->set_data ($1); } |
|
417 ; |
|
418 |
|
419 plot_options : using |
|
420 { $$ = new tree_subplot_list ($1, NULL, NULL); } |
|
421 | title |
|
422 { $$ = new tree_subplot_list (NULL, $1, NULL); } |
|
423 | style |
|
424 { $$ = new tree_subplot_list (NULL, NULL, $1); } |
|
425 | using title |
|
426 { $$ = new tree_subplot_list ($1, $2, NULL); } |
|
427 | title using |
|
428 { $$ = new tree_subplot_list ($2, $1, NULL); } |
|
429 | using style |
|
430 { $$ = new tree_subplot_list ($1, NULL, $2); } |
|
431 | style using |
|
432 { $$ = new tree_subplot_list ($2, NULL, $1); } |
|
433 | title style |
|
434 { $$ = new tree_subplot_list (NULL, $1, $2); } |
|
435 | style title |
|
436 { $$ = new tree_subplot_list (NULL, $2, $1); } |
|
437 | using title style |
|
438 { $$ = new tree_subplot_list ($1, $2, $3); } |
|
439 | using style title |
|
440 { $$ = new tree_subplot_list ($1, $3, $2); } |
|
441 | title using style |
|
442 { $$ = new tree_subplot_list ($2, $1, $3); } |
|
443 | title style using |
|
444 { $$ = new tree_subplot_list ($3, $1, $2); } |
|
445 | style using title |
|
446 { $$ = new tree_subplot_list ($2, $3, $1); } |
|
447 | style title using |
|
448 { $$ = new tree_subplot_list ($3, $2, $1); } |
|
449 ; |
|
450 |
|
451 using : using1 |
|
452 { |
|
453 $$ = $1; |
|
454 in_plot_using = 0; |
|
455 } |
|
456 | using1 expression |
|
457 { |
|
458 $$ = $1->set_format ($2); |
|
459 in_plot_using = 0; |
|
460 } |
|
461 ; |
|
462 |
|
463 using1 : USING expression |
|
464 { |
|
465 tree_subplot_using *tmp = new tree_subplot_using (); |
|
466 $$ = tmp->add_qualifier ($2); |
|
467 } |
|
468 | using1 COLON expression |
|
469 { $$ = $1->add_qualifier ($3); } |
|
470 ; |
|
471 |
|
472 title : TITLE expression |
|
473 { $$ = $2; } |
|
474 ; |
|
475 |
|
476 style : WITH STYLE |
143
|
477 { $$ = new tree_subplot_style ($2->string ()); } |
1
|
478 | WITH STYLE expression |
143
|
479 { $$ = new tree_subplot_style ($2->string (), $3); } |
1
|
480 | WITH STYLE expression bogus_syntax expression |
143
|
481 { $$ = new tree_subplot_style ($2->string (), $3, $5); } |
1
|
482 ; |
|
483 |
|
484 bogus_syntax : // empty |
|
485 ; |
|
486 |
|
487 ans_expression : expression |
|
488 { $$ = maybe_convert_to_ans_assign ($1); } |
|
489 ; |
|
490 |
|
491 global_decl : GLOBAL global_decl1 |
191
|
492 { $$ = $2->reverse (); } |
195
|
493 | GLOBAL global_decl1 ',' |
|
494 { $$ = $2->reverse (); } |
1
|
495 ; |
|
496 |
|
497 global_decl1 : NAME |
191
|
498 { |
195
|
499 $$ = new tree_global_command |
|
500 ($1->sym_rec (), $1->line (), $1->column ()); |
191
|
501 } |
1
|
502 | NAME '=' expression |
|
503 { |
195
|
504 $$ = new tree_global_command |
|
505 ($1->sym_rec (), $3, $1->line (), $1->column ()); |
1
|
506 } |
|
507 | global_decl1 optcomma NAME |
191
|
508 { |
195
|
509 $$ = $1->chain ($3->sym_rec (), $3->line (), |
|
510 $3->column ()); |
191
|
511 } |
1
|
512 | global_decl1 optcomma NAME '=' expression |
|
513 { |
195
|
514 $$ = $1->chain ($3->sym_rec (), $5, $3->line (), |
|
515 $3->column ()); |
1
|
516 } |
|
517 ; |
|
518 |
|
519 optcomma : // empty |
|
520 | ',' |
|
521 { |
|
522 if (user_pref.warn_comma_in_global_decl) |
|
523 warning ("comma in global declaration not\ |
|
524 interpreted as a command separator"); |
|
525 } |
|
526 ; |
|
527 |
|
528 statement : WHILE expression optsep opt_list END |
|
529 { |
|
530 maybe_warn_assign_as_truth_value ($2); |
143
|
531 if (check_end ($5, token::while_end)) |
|
532 ABORT_PARSE; |
1
|
533 looping--; |
191
|
534 $$ = new tree_while_command ($2, $4, $1->line (), |
|
535 $1->column ()); |
1
|
536 } |
118
|
537 | FOR variable '=' expression optsep opt_list END |
1
|
538 { |
143
|
539 if (check_end ($7, token::for_end)) |
|
540 ABORT_PARSE; |
1
|
541 looping--; |
191
|
542 $$ = new tree_for_command ($2, $4, $6, |
|
543 $1->line (), $1->column ()); |
1
|
544 } |
|
545 | IF expression optsep opt_list END |
|
546 { |
|
547 maybe_warn_assign_as_truth_value ($2); |
143
|
548 if (check_end ($5, token::if_end)) |
|
549 ABORT_PARSE; |
1
|
550 iffing--; |
191
|
551 $$ = new tree_if_command ($2, $4, |
|
552 $1->line (), $1->column ()); |
1
|
553 } |
|
554 | IF expression optsep opt_list ELSE optsep opt_list END |
|
555 { |
|
556 maybe_warn_assign_as_truth_value ($2); |
143
|
557 if (check_end ($8, token::if_end)) |
|
558 ABORT_PARSE; |
1
|
559 iffing--; |
191
|
560 tree_if_command *t1 = new tree_if_command |
|
561 ($7, $5->line (), $5->column ()); |
|
562 $$ = t1->chain ($2, $4, $1->line (), $1->column ()); |
1
|
563 } |
|
564 | IF expression optsep opt_list elseif END |
|
565 { |
|
566 maybe_warn_assign_as_truth_value ($2); |
143
|
567 if (check_end ($6, token::if_end)) |
|
568 ABORT_PARSE; |
1
|
569 iffing--; |
|
570 tree_if_command *t1 = $5->reverse (); |
|
571 // Add the if list to the new head of the elseif |
|
572 // list, and return the list. |
191
|
573 $$ = t1->chain ($2, $4, $1->line (), $1->column ()); |
1
|
574 } |
|
575 | IF expression optsep opt_list elseif ELSE optsep opt_list END |
|
576 { |
|
577 maybe_warn_assign_as_truth_value ($2); |
143
|
578 if (check_end ($9, token::if_end)) |
|
579 ABORT_PARSE; |
1
|
580 iffing--; |
|
581 // Add the else list to the head of the elseif list, |
|
582 // then reverse the list. |
191
|
583 tree_if_command *t1 = $5->chain ($8, $6->line (), |
|
584 $6->column ()); |
1
|
585 t1 = t1->reverse (); |
|
586 // Add the if list to the new head of the elseif |
|
587 // list, and return the list. |
191
|
588 $$ = t1->chain ($2, $4, $1->line (), $1->column ()); |
1
|
589 } |
|
590 | BREAK |
|
591 { |
|
592 if (!looping) |
|
593 { |
|
594 yyerror ("parse error"); |
|
595 error ("break: only meaningful within a `for'\ |
|
596 or `while' loop"); |
|
597 ABORT_PARSE; |
|
598 } |
191
|
599 $$ = new tree_break_command ($1->line (), $1->column ()); |
1
|
600 } |
|
601 | CONTINUE |
|
602 { |
|
603 if (!looping) |
|
604 { |
|
605 yyerror ("parse error"); |
|
606 error ("continue: only meaningful within a\ |
|
607 `for' or `while' loop"); |
|
608 ABORT_PARSE; |
|
609 } |
191
|
610 $$ = new tree_continue_command ($1->line (), |
|
611 $1->column ()); |
1
|
612 } |
|
613 | FUNC_RET |
|
614 { |
|
615 if (!defining_func) |
|
616 { |
|
617 yyerror ("parse error"); |
|
618 error ("return: only meaningful within a function"); |
|
619 ABORT_PARSE; |
|
620 } |
191
|
621 $$ = new tree_return_command ($1->line (), $1->column ()); |
1
|
622 } |
|
623 ; |
|
624 |
|
625 elseif : ELSEIF optsep expression optsep opt_list |
|
626 { |
|
627 maybe_warn_assign_as_truth_value ($3); |
191
|
628 $$ = new tree_if_command ($3, $5, $1->line (), |
|
629 $1->column ()); |
1
|
630 } |
|
631 | elseif ELSEIF optsep expression optsep opt_list |
|
632 { |
|
633 maybe_warn_assign_as_truth_value ($4); |
191
|
634 $$ = $1->chain ($4, $6, $2->line (), $2->column ()); |
1
|
635 } |
|
636 ; |
|
637 |
|
638 optsep : // empty |
|
639 | sep |
|
640 ; |
|
641 |
|
642 sep : ',' |
|
643 | ';' |
|
644 | '\n' |
|
645 | sep ',' |
|
646 | sep ';' |
|
647 | sep '\n' |
|
648 ; |
|
649 |
|
650 screwed_again : // empty |
|
651 { maybe_screwed_again++; } |
|
652 ; |
|
653 |
|
654 expression : variable '=' expression |
143
|
655 { $$ = new tree_simple_assignment_expression |
|
656 ($1, $3, $2->line (), $2->column ()); } |
1
|
657 | '[' screwed_again matrix_row SCREW_TWO '=' expression |
|
658 { |
|
659 |
|
660 // Will need a way to convert the matrix list to a list of |
143
|
661 // identifiers. If that fails, we can abort here, without losing |
1
|
662 // anything -- no other possible syntax is valid if we've seen the |
|
663 // equals sign as the next token after the `]'. |
|
664 |
|
665 $$ = (tree_multi_assignment_expression *) NULL; |
|
666 maybe_screwed_again--; |
|
667 tree_matrix *tmp = ml.pop (); |
|
668 tmp = tmp->reverse (); |
|
669 tree_return_list *id_list = tmp->to_return_list (); |
|
670 if (id_list == NULL_TREE) |
|
671 { |
|
672 yyerror ("parse error"); |
143
|
673 error ("invalid identifier list for assignment"); |
1
|
674 $$ = (tree_multi_assignment_expression *) NULL; |
|
675 ABORT_PARSE; |
|
676 } |
|
677 else |
143
|
678 $$ = new tree_multi_assignment_expression |
|
679 (id_list, $6, $5->line (), $5->column ()); |
1
|
680 } |
|
681 | NUM '=' expression |
|
682 { |
|
683 yyerror ("parse error"); |
|
684 error ("invalid assignment to a number"); |
|
685 $$ = (tree_simple_assignment_expression *) NULL; |
|
686 ABORT_PARSE; |
|
687 } |
|
688 | simple_expr |
|
689 { $$ = $1; } |
|
690 ; |
|
691 |
|
692 simple_expr : simple_expr1 |
|
693 { $$ = $1; } |
|
694 | identifier PLUS_PLUS |
143
|
695 { $$ = new tree_postfix_expression |
|
696 ($1, tree::increment, $2->line (), $2->column ()); } |
1
|
697 | identifier MINUS_MINUS |
143
|
698 { $$ = new tree_postfix_expression |
|
699 ($1, tree::decrement, $2->line (), $2->column ()); } |
1
|
700 | simple_expr QUOTE |
143
|
701 { $$ = new tree_unary_expression |
|
702 ($1, tree::hermitian, $2->line (), $2->column ()); } |
1
|
703 | simple_expr TRANSPOSE |
143
|
704 { $$ = new tree_unary_expression |
|
705 ($1, tree::transpose, $2->line (), $2->column ()); } |
1
|
706 | simple_expr POW simple_expr |
143
|
707 { $$ = new tree_binary_expression |
|
708 ($1, $3, tree::power, $2->line (), $2->column ()); } |
1
|
709 | simple_expr EPOW simple_expr |
143
|
710 { $$ = new tree_binary_expression |
|
711 ($1, $3, tree::elem_pow, $2->line (), $2->column ()); } |
1
|
712 | simple_expr '+' simple_expr |
143
|
713 { $$ = new tree_binary_expression |
|
714 ($1, $3, tree::add, $2->line (), $2->column ()); } |
1
|
715 | simple_expr '-' simple_expr |
143
|
716 { $$ = new tree_binary_expression |
|
717 ($1, $3, tree::subtract, $2->line (), $2->column ()); } |
1
|
718 | simple_expr '*' simple_expr |
143
|
719 { $$ = new tree_binary_expression |
|
720 ($1, $3, tree::multiply, $2->line (), $2->column ()); } |
1
|
721 | simple_expr '/' simple_expr |
143
|
722 { $$ = new tree_binary_expression |
|
723 ($1, $3, tree::divide, $2->line (), $2->column ()); } |
1
|
724 | simple_expr EMUL simple_expr |
143
|
725 { $$ = new tree_binary_expression |
|
726 ($1, $3, tree::el_mul, $2->line (), $2->column ()); } |
1
|
727 | simple_expr EDIV simple_expr |
143
|
728 { $$ = new tree_binary_expression |
|
729 ($1, $3, tree::el_div, $2->line (), $2->column ()); } |
1
|
730 | simple_expr LEFTDIV simple_expr |
143
|
731 { $$ = new tree_binary_expression |
|
732 ($1, $3, tree::leftdiv, $2->line (), $2->column ()); } |
1
|
733 | simple_expr ELEFTDIV simple_expr |
143
|
734 { $$ = new tree_binary_expression |
|
735 ($1, $3, tree::el_leftdiv, $2->line (), $2->column ()); } |
1
|
736 | simple_expr EXPR_LT simple_expr |
143
|
737 { $$ = new tree_binary_expression |
|
738 ($1, $3, tree::cmp_lt, $2->line (), $2->column ()); } |
1
|
739 | simple_expr EXPR_LE simple_expr |
143
|
740 { $$ = new tree_binary_expression |
|
741 ($1, $3, tree::cmp_le, $2->line (), $2->column ()); } |
1
|
742 | simple_expr EXPR_EQ simple_expr |
143
|
743 { $$ = new tree_binary_expression |
|
744 ($1, $3, tree::cmp_eq, $2->line (), $2->column ()); } |
1
|
745 | simple_expr EXPR_GE simple_expr |
143
|
746 { $$ = new tree_binary_expression |
|
747 ($1, $3, tree::cmp_ge, $2->line (), $2->column ()); } |
1
|
748 | simple_expr EXPR_GT simple_expr |
143
|
749 { $$ = new tree_binary_expression |
|
750 ($1, $3, tree::cmp_gt, $2->line (), $2->column ()); } |
1
|
751 | simple_expr EXPR_NE simple_expr |
143
|
752 { $$ = new tree_binary_expression |
|
753 ($1, $3, tree::cmp_ne, $2->line (), $2->column ()); } |
1
|
754 | simple_expr EXPR_AND simple_expr |
143
|
755 { $$ = new tree_binary_expression |
|
756 ($1, $3, tree::and, $2->line (), $2->column ()); } |
1
|
757 | simple_expr EXPR_OR simple_expr |
143
|
758 { $$ = new tree_binary_expression |
|
759 ($1, $3, tree::or, $2->line (), $2->column ()); } |
1
|
760 ; |
|
761 |
|
762 simple_expr1 : NUM |
143
|
763 { $$ = new tree_constant ($1->number ()); } |
1
|
764 | IMAG_NUM |
143
|
765 { $$ = new tree_constant (Complex (0.0, $1->number ())); } |
1
|
766 | TEXT |
143
|
767 { $$ = new tree_constant ($1->string ()); } |
1
|
768 | word_list_cmd |
|
769 { $$ = $1; } |
|
770 | '(' expression ')' |
|
771 { |
|
772 if ($2->is_assignment_expression ()) |
|
773 ((tree_assignment_expression *) $2) -> in_parens++; |
|
774 $$ = $2; |
|
775 } |
|
776 | variable |
|
777 { $$ = $1; } |
|
778 | matrix |
|
779 { $$ = $1; } |
240
|
780 | '[' ']' |
|
781 { |
|
782 mlnm.pop (); |
|
783 $$ = new tree_constant (Matrix ()); |
|
784 } |
|
785 | '[' ';' ']' |
|
786 { |
|
787 mlnm.pop (); |
|
788 $$ = new tree_constant (Matrix ()); |
|
789 } |
1
|
790 | colon_expr |
|
791 { $$ = $1; } |
|
792 | PLUS_PLUS identifier %prec UNARY |
143
|
793 { $$ = new tree_prefix_expression |
|
794 ($2, tree::increment, $1->line (), $1->column ()); } |
1
|
795 | MINUS_MINUS identifier %prec UNARY |
143
|
796 { $$ = new tree_prefix_expression |
|
797 ($2, tree::decrement, $1->line (), $1->column ()); } |
1
|
798 | EXPR_NOT simple_expr |
143
|
799 { $$ = new tree_unary_expression |
|
800 ($2, tree::not, $1->line (), $1->column ()); } |
1
|
801 | '+' simple_expr %prec UNARY |
|
802 { $$ = $2; } |
|
803 | '-' simple_expr %prec UNARY |
143
|
804 { $$ = new tree_unary_expression |
|
805 ($2, tree::uminus, $1->line (), $1->column ()); } |
1
|
806 ; |
|
807 |
|
808 colon_expr : simple_expr ':' simple_expr |
143
|
809 { $$ = new tree_colon_expression |
|
810 ($1, $3, $2->line (), $2->column ()); } |
1
|
811 | colon_expr ':' simple_expr |
|
812 { |
|
813 $$ = $1->chain ($3); |
|
814 if ($$ == (tree_colon_expression *) NULL) |
|
815 { |
|
816 yyerror ("parse error"); |
|
817 ABORT_PARSE; |
|
818 } |
|
819 } |
|
820 ; |
|
821 |
|
822 word_list_cmd : identifier word_list |
|
823 { $$ = new tree_word_list_command ($1, $2); } |
|
824 ; |
|
825 |
|
826 word_list : word_list1 |
|
827 { $$ = $1->reverse (); } |
|
828 ; |
|
829 |
|
830 word_list1 : TEXT |
143
|
831 { $$ = new tree_word_list ($1->string ()); } |
1
|
832 | word_list1 TEXT |
143
|
833 { $$ = $1->chain ($2->string ()); } |
1
|
834 ; |
|
835 |
|
836 // This is truly disgusting. |
|
837 |
|
838 g_symtab : // empty |
|
839 { curr_sym_tab = global_sym_tab; } |
|
840 ; |
|
841 |
|
842 local_symtab : // empty |
|
843 { curr_sym_tab = tmp_local_sym_tab; } |
|
844 ; |
|
845 |
|
846 safe : // empty |
|
847 { maybe_screwed = 0; } |
|
848 ; |
|
849 |
|
850 are_we_screwed : // empty |
|
851 { maybe_screwed = 1; } |
|
852 ; |
|
853 |
|
854 func_def : FCN g_symtab are_we_screwed func_def1 |
|
855 { |
|
856 curr_sym_tab = top_level_sym_tab; |
|
857 defining_func = 0; |
|
858 $$ = (tree_function *) NULL; |
|
859 } |
|
860 | FCN g_symtab are_we_screwed func_def2 |
|
861 { |
|
862 curr_sym_tab = top_level_sym_tab; |
|
863 defining_func = 0; |
|
864 $$ = (tree_function *) NULL; |
|
865 } |
|
866 ; |
|
867 |
|
868 func_def1 : SCREW safe g_symtab '=' func_def2 |
|
869 { |
143
|
870 tree_identifier *tmp = new tree_identifier |
|
871 ($1->sym_rec (), $1->line (), $1->column ()); |
1
|
872 tree_parameter_list *tpl = new tree_parameter_list (tmp); |
|
873 tpl = tpl->reverse (); |
|
874 tpl->mark_as_formal_parameters (); |
|
875 $$ = $5->define_ret_list (tpl); |
|
876 } |
|
877 | func_def1a ']' g_symtab '=' func_def2 |
|
878 { |
|
879 tree_parameter_list *tpl = $1->reverse (); |
|
880 tpl->mark_as_formal_parameters (); |
|
881 $$ = $5->define_ret_list (tpl); |
|
882 } |
|
883 ; |
|
884 |
|
885 func_def1a : '[' safe local_symtab identifier |
|
886 { $$ = new tree_parameter_list ($4); } |
|
887 | func_def1a ',' identifier |
|
888 { $$ = $1->chain ($3); } |
|
889 ; |
|
890 |
|
891 func_def2 : identifier safe local_symtab func_def3 |
|
892 { |
|
893 char *id_name = $1->name (); |
|
894 // if (is_text_function_name (id_name)) |
|
895 // { |
|
896 // yyerror ("parse error"); |
|
897 // error ("invalid use of reserved word %s", id_name); |
|
898 // ABORT_PARSE; |
|
899 // } |
|
900 |
|
901 // If input is coming from a file, issue a warning if the name of the |
|
902 // file does not match the name of the function stated in the file. |
|
903 // Matlab doesn't provide a diagnostic (it ignores the stated name). |
|
904 |
|
905 if (reading_m_file) |
|
906 { |
|
907 if (strcmp (curr_m_file_name, id_name) != 0) |
195
|
908 { |
|
909 warning ("function name `%s' does not agree\ |
1
|
910 with M-file name `%s.m'", id_name, curr_m_file_name); |
|
911 |
195
|
912 $1->rename (curr_m_file_name); |
197
|
913 id_name = $1->name (); |
195
|
914 } |
|
915 |
|
916 $4->stash_m_file_name (curr_m_file_name); |
|
917 $4->stash_m_file_time (time ((time_t *) NULL)); |
|
918 $4->mark_as_system_m_file (); |
1
|
919 } |
315
|
920 else if (! (input_from_tmp_history_file |
|
921 || input_from_startup_file) |
195
|
922 && reading_script_file |
|
923 && strcmp (curr_m_file_name, id_name) == 0) |
1
|
924 { |
195
|
925 warning ("function `%s' defined within\ |
1
|
926 script file `%s.m'", id_name, curr_m_file_name); |
|
927 } |
|
928 |
197
|
929 top_level_sym_tab->clear (id_name); |
|
930 |
143
|
931 $4->stash_function_name (id_name); |
195
|
932 |
|
933 $1->define ($4); |
|
934 $1->document (help_buf); |
|
935 |
1
|
936 $$ = $4; |
|
937 } |
|
938 ; |
|
939 |
|
940 func_def3 : param_list optsep opt_list fcn_end_or_eof |
|
941 { |
|
942 tree_function *fcn = new tree_function ($3, curr_sym_tab); |
|
943 $$ = fcn->define_param_list ($1); |
|
944 } |
|
945 | optsep opt_list fcn_end_or_eof |
|
946 { $$ = new tree_function ($2, curr_sym_tab); } |
|
947 ; |
|
948 |
|
949 fcn_end_or_eof : END |
|
950 { |
143
|
951 if (check_end ($1, token::function_end)) |
1
|
952 ABORT_PARSE; |
|
953 |
|
954 if (reading_m_file) |
|
955 check_for_garbage_after_fcn_def (); |
|
956 } |
|
957 | END_OF_INPUT |
|
958 { |
|
959 if (! (reading_m_file || reading_script_file)) |
|
960 YYABORT; |
|
961 } |
|
962 ; |
|
963 |
|
964 variable : identifier |
191
|
965 { |
|
966 $$ = new tree_index_expression |
|
967 ($1, $1->line (), $1->column ()); |
|
968 } |
1
|
969 | identifier '(' arg_list ')' |
191
|
970 { |
|
971 $$ = new tree_index_expression |
|
972 ($1, $3, $1->line (), $1->column ()); |
|
973 } |
1
|
974 | identifier '(' ')' |
|
975 { |
|
976 $$ = new tree_index_expression |
191
|
977 ($1, (tree_argument_list *) NULL, |
|
978 $1->line (), $1->column ()); |
1
|
979 } |
|
980 | identifier '[' |
|
981 { |
|
982 yyerror ("parse error"); |
|
983 error ("use `(\' and `)\' as index operators, not\ |
|
984 `[\' and `]\'"); |
|
985 $$ = (tree_index_expression *) NULL; |
|
986 ABORT_PARSE; |
|
987 } |
|
988 ; |
|
989 |
240
|
990 param_list : '(' ')' |
1
|
991 { |
240
|
992 quote_is_transpose = 0; |
|
993 $$ = (tree_parameter_list *) NULL; |
|
994 } |
|
995 | param_list1 ')' |
|
996 { |
|
997 quote_is_transpose = 0; |
1
|
998 tree_parameter_list *tmp = $1->reverse (); |
|
999 tmp->mark_as_formal_parameters (); |
|
1000 $$ = tmp; |
|
1001 } |
206
|
1002 | param_list1 ',' ELLIPSIS ')' |
|
1003 { |
240
|
1004 quote_is_transpose = 0; |
206
|
1005 tree_parameter_list *tmp = $1->reverse (); |
|
1006 tmp->mark_as_formal_parameters (); |
|
1007 tmp->mark_varargs (); |
|
1008 $$ = tmp; |
|
1009 } |
1
|
1010 |
|
1011 param_list1 : '(' identifier |
|
1012 { $$ = new tree_parameter_list ($2); } |
|
1013 | param_list1 ',' identifier |
|
1014 { $$ = $1->chain ($3); } |
|
1015 | '(' error |
|
1016 { |
206
|
1017 yyerror ("parse error"); |
|
1018 error ("invalid parameter list"); |
|
1019 ABORT_PARSE; |
1
|
1020 } |
|
1021 | param_list1 ',' error |
|
1022 { |
206
|
1023 yyerror ("parse error"); |
|
1024 error ("invalid parameter list"); |
|
1025 ABORT_PARSE; |
1
|
1026 } |
|
1027 ; |
|
1028 |
|
1029 identifier : NAME |
143
|
1030 { $$ = new tree_identifier |
|
1031 ($1->sym_rec (), $1->line (), $1->column ()); } |
1
|
1032 |
|
1033 arg_list : arg_list1 |
|
1034 { $$ = $1->reverse (); } |
|
1035 ; |
|
1036 |
|
1037 arg_list1 : ':' |
|
1038 { |
|
1039 tree_constant *colon; |
|
1040 colon = new tree_constant (tree_constant_rep::magic_colon); |
|
1041 $$ = new tree_argument_list (colon); |
|
1042 } |
|
1043 | arg_list1 ',' ':' |
|
1044 { |
|
1045 tree_constant *colon; |
|
1046 colon = new tree_constant (tree_constant_rep::magic_colon); |
|
1047 $$ = $1->chain (colon); |
|
1048 if ($$ == NULL_TREE) |
|
1049 { |
|
1050 yyerror ("parse error"); |
|
1051 ABORT_PARSE; |
|
1052 } |
|
1053 } |
|
1054 | expression |
|
1055 { $$ = new tree_argument_list ($1); } |
|
1056 | arg_list1 ',' expression |
|
1057 { |
|
1058 $$ = $1->chain ($3); |
|
1059 if ($$ == NULL_TREE) |
|
1060 { |
|
1061 yyerror ("parse error"); |
|
1062 ABORT_PARSE; |
|
1063 } |
|
1064 } |
|
1065 ; |
|
1066 |
240
|
1067 matrix : '[' screwed_again rows ']' |
1
|
1068 { |
|
1069 mlnm.pop (); |
|
1070 maybe_screwed_again--; |
|
1071 tree_matrix *tmp = ml.pop (); |
|
1072 $$ = tmp->reverse (); |
|
1073 } |
|
1074 ; |
|
1075 |
|
1076 rows : matrix_row |
|
1077 | rows ';' // Ignore trailing semicolon. |
|
1078 | rows ';' matrix_row |
|
1079 ; |
|
1080 |
|
1081 matrix_row : expression // First element on row. |
|
1082 { |
|
1083 if (mlnm.top ()) |
|
1084 { |
|
1085 mlnm.pop (); |
|
1086 mlnm.push (0); |
143
|
1087 tree_matrix *tmp = new tree_matrix ($1, tree::md_none); |
1
|
1088 ml.push (tmp); |
|
1089 } |
|
1090 else |
|
1091 { |
|
1092 tree_matrix *tmp = ml.pop (); |
|
1093 tmp = tmp->chain ($1, tree::md_down); |
|
1094 ml.push (tmp); |
|
1095 } |
|
1096 } |
|
1097 | matrix_row ',' // Ignore trailing comma. |
|
1098 | matrix_row ',' expression |
|
1099 { |
|
1100 tree_matrix *tmp = ml.pop (); |
|
1101 tmp = tmp->chain ($3, tree::md_right); |
|
1102 ml.push (tmp); |
|
1103 } |
|
1104 ; |
|
1105 |
|
1106 %% |
|
1107 |
|
1108 static void |
|
1109 yyerror (char *s) |
|
1110 { |
|
1111 char *line = current_input_line; |
143
|
1112 int err_col = current_input_column - 1; |
1
|
1113 if (err_col == 0) |
|
1114 err_col = strlen (current_input_line) + 1; |
|
1115 |
|
1116 // Print a message like `parse error'. |
|
1117 fprintf (stderr, "\n%s", s); |
|
1118 |
|
1119 // Maybe print the line number and file name. |
|
1120 if (reading_m_file || reading_script_file) |
|
1121 fprintf (stderr, " near line %d of file %s.m", input_line_number, |
|
1122 curr_m_file_name); |
|
1123 |
|
1124 int len = strlen (line); |
|
1125 if (line[len-1] == '\n') |
|
1126 { |
|
1127 len--; |
|
1128 line[len] = '\0'; |
|
1129 } |
|
1130 |
|
1131 // Print the line, maybe with a pointer near the error token. |
|
1132 if (err_col > len) |
|
1133 fprintf (stderr, ":\n\n %s\n\n", line); |
|
1134 else |
|
1135 fprintf (stderr, ":\n\n %s\n %*s\n\n", line, err_col, "^"); |
|
1136 } |
|
1137 |
143
|
1138 static int |
|
1139 check_end (token *tok, token::end_tok_type expected) |
|
1140 { |
|
1141 token::end_tok_type ettype = tok->ettype (); |
|
1142 if (ettype != expected && ettype != token::simple_end) |
|
1143 { |
|
1144 yyerror ("parse error"); |
|
1145 |
|
1146 int l = tok->line (); |
|
1147 int c = tok->column (); |
|
1148 |
|
1149 switch (expected) |
|
1150 { |
|
1151 case token::for_end: |
|
1152 end_error ("for", ettype, l, c); |
|
1153 break; |
|
1154 case token::function_end: |
|
1155 end_error ("function", ettype, l, c); |
|
1156 break; |
|
1157 case token::if_end: |
|
1158 end_error ("if", ettype, l, c); |
|
1159 break; |
|
1160 case token::while_end: |
|
1161 end_error ("while", ettype, l, c); |
|
1162 break; |
|
1163 default: |
|
1164 panic_impossible (); |
|
1165 break; |
|
1166 } |
|
1167 return 1; |
|
1168 } |
|
1169 else |
|
1170 return 0; |
|
1171 } |
|
1172 |
1
|
1173 static void |
143
|
1174 end_error (char *type, token::end_tok_type ettype, int l, int c) |
1
|
1175 { |
143
|
1176 static char *fmt = "%s command matched by `%s' near line %d column %d"; |
|
1177 |
1
|
1178 switch (ettype) |
|
1179 { |
143
|
1180 case token::simple_end: |
|
1181 error (fmt, type, "end", l, c); |
1
|
1182 break; |
143
|
1183 case token::for_end: |
|
1184 error (fmt, type, "endfor", l, c); |
1
|
1185 break; |
143
|
1186 case token::function_end: |
|
1187 error (fmt, type, "endfunction", l, c); |
1
|
1188 break; |
143
|
1189 case token::if_end: |
|
1190 error (fmt, type, "endif", l, c); |
1
|
1191 break; |
143
|
1192 case token::while_end: |
|
1193 error (fmt, type, "endwhile", l, c); |
1
|
1194 break; |
|
1195 default: |
|
1196 panic_impossible (); |
|
1197 break; |
|
1198 } |
|
1199 } |
|
1200 |
|
1201 /* |
|
1202 * Need to make sure that the expression isn't already an identifier |
|
1203 * that has a name, or an assignment expression. |
|
1204 * |
|
1205 * Note that an expression can't be just an identifier anymore -- it |
|
1206 * must at least be an index expression (see the definition of the |
|
1207 * non-terminal `variable' above). |
183
|
1208 * |
|
1209 * XXX FIXME XXX. This isn't quite sufficient. For example, try the |
|
1210 * command `x = 4, x' for `x' previously undefined. |
195
|
1211 * |
|
1212 * XXX FIXME XXX -- we should probably delay doing this until eval-time. |
1
|
1213 */ |
|
1214 tree * |
|
1215 maybe_convert_to_ans_assign (tree *expr) |
|
1216 { |
|
1217 if (expr->is_index_expression ()) |
|
1218 { |
195
|
1219 expr->mark_for_possible_ans_assign (); |
|
1220 return expr; |
|
1221 } |
|
1222 else if (expr->is_assignment_expression () |
|
1223 || expr->is_prefix_expression ()) |
|
1224 { |
|
1225 return expr; |
|
1226 } |
|
1227 else |
|
1228 { |
|
1229 symbol_record *sr = global_sym_tab->lookup ("ans", 1, 0); |
1
|
1230 |
195
|
1231 assert (sr != (symbol_record *) NULL); |
|
1232 |
|
1233 tree_identifier *ans = new tree_identifier (sr); |
|
1234 |
|
1235 return new tree_simple_assignment_expression (ans, expr); |
1
|
1236 } |
|
1237 } |
|
1238 |
|
1239 void |
|
1240 maybe_warn_assign_as_truth_value (tree *expr) |
|
1241 { |
|
1242 if (user_pref.warn_assign_as_truth_value |
|
1243 && expr->is_assignment_expression () |
|
1244 && ((tree_assignment_expression *) expr) -> in_parens < 2) |
|
1245 { |
|
1246 warning ("suggest parenthesis around assignment used as truth value"); |
|
1247 } |
|
1248 } |