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