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