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