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