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