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