143
|
1 /* parse.y -*- text -*- |
1
|
2 |
1009
|
3 Copyright (C) 1992, 1993, 1994, 1995 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 |
767
|
25 // C decarations. |
|
26 |
1
|
27 %{ |
|
28 #define YYDEBUG 1 |
|
29 |
240
|
30 #ifdef HAVE_CONFIG_H |
1229
|
31 #include <config.h> |
240
|
32 #endif |
|
33 |
581
|
34 #include <strstream.h> |
|
35 |
1
|
36 #include "SLStack.h" |
|
37 |
|
38 #include "Matrix.h" |
|
39 |
168
|
40 #include "octave-hist.h" |
1
|
41 #include "user-prefs.h" |
584
|
42 #include "tree-base.h" |
|
43 #include "tree-expr.h" |
|
44 #include "tree-cmd.h" |
581
|
45 #include "tree-const.h" |
|
46 #include "tree-misc.h" |
|
47 #include "variables.h" |
|
48 #include "tree-plot.h" |
|
49 #include "octave.h" |
|
50 #include "symtab.h" |
|
51 #include "parse.h" |
|
52 #include "token.h" |
|
53 #include "error.h" |
|
54 #include "pager.h" |
1
|
55 #include "input.h" |
|
56 #include "utils.h" |
|
57 #include "lex.h" |
|
58 |
|
59 // Nonzero means we're in the middle of defining a function. |
|
60 int defining_func = 0; |
|
61 |
|
62 // Nonzero means we're in the middle of defining a loop. |
|
63 int looping = 0; |
|
64 |
|
65 // Nonzero means we're in the middle of defining a conditional expression. |
|
66 int iffing = 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 = 0; |
|
71 |
|
72 // Nonzero means we need to do some extra lookahead to avoid being |
|
73 // screwed by bogus function syntax. |
|
74 int maybe_screwed_again = 0; |
|
75 |
|
76 // Temporary symbol table pointer used to cope with bogus function syntax. |
522
|
77 symbol_table *tmp_local_sym_tab = 0; |
1
|
78 |
|
79 // Stack to hold list of literal matrices. |
|
80 SLStack <tree_matrix *> ml; |
|
81 |
|
82 // A nonzero element corresponding to an element of ml means we just |
|
83 // started reading a new matrix. This should probably be part of a |
|
84 // new struct for matrix lists... |
|
85 SLStack <int> mlnm; |
|
86 |
|
87 // The current input line number. |
|
88 int input_line_number = 0; |
|
89 |
|
90 // The column of the current token. |
143
|
91 int current_input_column = 1; |
1
|
92 |
338
|
93 // Buffer for help text snagged from function files. |
991
|
94 char *help_buf = 0; |
1
|
95 |
|
96 // Nonzero means we're working on a plot command. |
|
97 int plotting = 0; |
|
98 |
113
|
99 // Nonzero means we've seen something that means we must be past the |
|
100 // range part of a plot command. |
|
101 int past_plot_range = 0; |
|
102 |
1
|
103 // Nonzero means we're looking at the range part of a plot command. |
|
104 int in_plot_range = 0; |
|
105 |
|
106 // Nonzero means we're looking at the using part of a plot command. |
|
107 int in_plot_using = 0; |
|
108 |
|
109 // Nonzero means we're looking at the style part of a plot command. |
|
110 int in_plot_style = 0; |
|
111 |
751
|
112 // Nonzero means we're looking at an indirect reference to a structure |
|
113 // element. |
|
114 int looking_at_indirect_ref = 0; |
|
115 |
496
|
116 // Forward declarations for some functions defined at the bottom of |
|
117 // the file. |
|
118 |
|
119 // Generic error messages. |
|
120 static void yyerror (char *s); |
1
|
121 |
578
|
122 // Error mesages for mismatched end tokens. |
143
|
123 static void end_error (char *type, token::end_tok_type ettype, int l, int c); |
1
|
124 |
578
|
125 // Check to see that end tokens are properly matched. |
496
|
126 static int check_end (token *tok, token::end_tok_type expected); |
1
|
127 |
496
|
128 // Try to figure out early if an expression should become an |
|
129 // assignment to the builtin variable ans. |
|
130 static tree_expression *maybe_convert_to_ans_assign (tree_expression *expr); |
|
131 |
|
132 // Maybe print a warning if an assignment expression is used as the |
|
133 // test in a logical expression. |
|
134 static void maybe_warn_assign_as_truth_value (tree_expression *expr); |
1
|
135 |
578
|
136 // Build a binary expression. |
751
|
137 static tree_expression *make_binary_op |
|
138 (int op, tree_expression *op1, token *tok_val, tree_expression *op2); |
578
|
139 |
|
140 // Build a prefix expression. |
751
|
141 static tree_expression *make_prefix_op |
|
142 (int op, tree_identifier *op1, token *tok_val); |
578
|
143 |
|
144 // Build a postfix expression. |
751
|
145 static tree_expression *make_postfix_op |
|
146 (int op, tree_identifier *op1, token *tok_val); |
578
|
147 |
|
148 // Build a binary expression. |
751
|
149 static tree_expression *make_unary_op |
|
150 (int op, tree_expression *op1, token *tok_val); |
578
|
151 |
666
|
152 // Make an expression that handles assignment of multiple values. |
751
|
153 static tree_expression *make_multi_val_ret |
1229
|
154 (tree_matrix *m, tree_expression *rhs, int l = -1, int c = -1); |
751
|
155 |
|
156 // Make an index expression. |
|
157 static tree_index_expression *make_index_expression |
|
158 (tree_indirect_ref *indir, tree_argument_list *args); |
666
|
159 |
1
|
160 #define ABORT_PARSE \ |
|
161 do \ |
|
162 { \ |
522
|
163 global_command = 0; \ |
1
|
164 yyerrok; \ |
|
165 if (interactive) \ |
|
166 YYACCEPT; \ |
|
167 else \ |
|
168 YYABORT; \ |
|
169 } \ |
|
170 while (0) |
|
171 |
|
172 %} |
|
173 |
666
|
174 // Bison declarations. |
|
175 |
1
|
176 %union |
|
177 { |
143
|
178 // The type of the basic tokens returned by the lexer. |
|
179 token *tok_val; |
|
180 |
|
181 // Types for the nonterminals we generate. |
1
|
182 tree *tree_type; |
496
|
183 tree_expression *tree_expression_type; |
1
|
184 tree_constant *tree_constant_type; |
|
185 tree_matrix *tree_matrix_type; |
|
186 tree_identifier *tree_identifier_type; |
751
|
187 tree_indirect_ref *tree_indirect_ref_type; |
1
|
188 tree_function *tree_function_type; |
|
189 tree_index_expression *tree_index_expression_type; |
|
190 tree_colon_expression *tree_colon_expression_type; |
|
191 tree_argument_list *tree_argument_list_type; |
|
192 tree_parameter_list *tree_parameter_list_type; |
|
193 tree_command *tree_command_type; |
|
194 tree_if_command *tree_if_command_type; |
578
|
195 tree_if_clause *tree_if_clause_type; |
|
196 tree_if_command_list *tree_if_command_list_type; |
|
197 tree_global *tree_global_type; |
|
198 tree_global_init_list *tree_global_init_list_type; |
195
|
199 tree_global_command *tree_global_command_type; |
578
|
200 tree_statement *tree_statement_type; |
|
201 tree_statement_list *tree_statement_list_type; |
1
|
202 tree_plot_command *tree_plot_command_type; |
578
|
203 subplot *subplot_type; |
|
204 subplot_list *subplot_list_type; |
|
205 plot_limits *plot_limits_type; |
|
206 plot_range *plot_range_type; |
|
207 subplot_using *subplot_using_type; |
|
208 subplot_style *subplot_style_type; |
1
|
209 } |
|
210 |
143
|
211 // Tokens with line and column information. |
|
212 %token <tok_val> '=' ':' '-' '+' '*' '/' |
428
|
213 %token <tok_val> EXPR_AND_AND EXPR_OR_OR |
143
|
214 %token <tok_val> EXPR_AND EXPR_OR EXPR_NOT |
|
215 %token <tok_val> EXPR_LT EXPR_LE EXPR_EQ EXPR_NE EXPR_GE EXPR_GT |
|
216 %token <tok_val> LEFTDIV EMUL EDIV ELEFTDIV QUOTE TRANSPOSE |
|
217 %token <tok_val> PLUS_PLUS MINUS_MINUS POW EPOW |
|
218 %token <tok_val> NUM IMAG_NUM |
169
|
219 %token <tok_val> NAME SCREW |
143
|
220 %token <tok_val> END |
|
221 %token <tok_val> PLOT |
|
222 %token <tok_val> TEXT STYLE |
191
|
223 %token <tok_val> FOR WHILE IF ELSEIF ELSE BREAK CONTINUE FUNC_RET |
924
|
224 %token <tok_val> UNWIND CLEANUP |
578
|
225 %token <tok_val> GLOBAL |
751
|
226 %token <tok_val> TEXT_ID |
1
|
227 |
143
|
228 // Other tokens. |
627
|
229 %token LEXICAL_ERROR |
191
|
230 %token FCN SCREW_TWO |
206
|
231 %token ELLIPSIS |
922
|
232 %token ALL_VA_ARGS |
206
|
233 %token END_OF_INPUT |
883
|
234 %token USING TITLE WITH COLON OPEN_BRACE CLOSE_BRACE CLEAR |
1
|
235 |
143
|
236 // Nonterminals we construct. |
578
|
237 %type <tree_type> input |
496
|
238 %type <tree_expression_type> expression simple_expr simple_expr1 |
|
239 %type <tree_expression_type> ans_expression title |
1
|
240 %type <tree_matrix_type> matrix |
|
241 %type <tree_identifier_type> identifier |
751
|
242 %type <tree_indirect_ref_type> indirect_ref indirect_ref1 |
578
|
243 %type <tree_function_type> func_def1 func_def2 func_def3 |
482
|
244 %type <tree_index_expression_type> variable word_list_cmd |
1
|
245 %type <tree_colon_expression_type> colon_expr |
578
|
246 %type <tree_argument_list_type> arg_list word_list |
723
|
247 %type <tree_parameter_list_type> param_list param_list1 |
|
248 %type <tree_parameter_list_type> return_list return_list1 |
578
|
249 %type <tree_command_type> command func_def |
|
250 %type <tree_if_command_type> if_command |
|
251 %type <tree_if_clause_type> elseif_clause else_clause |
|
252 %type <tree_if_command_list_type> if_cmd_list1 if_cmd_list |
|
253 %type <tree_global_type> global_decl2 |
|
254 %type <tree_global_init_list_type> global_decl1 |
|
255 %type <tree_global_command_type> global_decl |
|
256 %type <tree_statement_type> statement |
627
|
257 %type <tree_statement_list_type> simple_list simple_list1 list list1 |
|
258 %type <tree_statement_list_type> opt_list input1 |
1
|
259 %type <tree_plot_command_type> plot_command |
578
|
260 %type <subplot_type> plot_command2 plot_options |
|
261 %type <subplot_list_type> plot_command1 |
|
262 %type <plot_limits_type> ranges |
|
263 %type <plot_range_type> ranges1 |
|
264 %type <subplot_using_type> using using1 |
|
265 %type <subplot_style_type> style |
1
|
266 |
143
|
267 // Precedence and associativity. |
1
|
268 %left ';' ',' '\n' |
|
269 %right '=' |
428
|
270 %left EXPR_AND_AND EXPR_OR_OR |
1
|
271 %left EXPR_AND EXPR_OR |
|
272 %left EXPR_LT EXPR_LE EXPR_EQ EXPR_NE EXPR_GE EXPR_GT |
|
273 %left ':' |
|
274 %left '-' '+' |
|
275 %left '*' '/' LEFTDIV EMUL EDIV ELEFTDIV |
|
276 %left QUOTE TRANSPOSE |
|
277 %left UNARY PLUS_PLUS MINUS_MINUS EXPR_NOT |
|
278 %right POW EPOW |
|
279 |
169
|
280 // There are 19 shift/reduce conflicts, ok? |
|
281 %expect 19 |
143
|
282 |
|
283 // Where to start. |
1
|
284 %start input |
|
285 |
666
|
286 // Grammar rules. |
|
287 |
1
|
288 %% |
|
289 |
627
|
290 input : input1 |
1
|
291 { |
627
|
292 global_command = $1; |
1
|
293 promptflag = 1; |
|
294 YYACCEPT; |
|
295 } |
|
296 | END_OF_INPUT |
|
297 { |
522
|
298 global_command = 0; |
1
|
299 promptflag = 1; |
|
300 YYABORT; |
|
301 } |
627
|
302 | simple_list parse_error |
1091
|
303 { ABORT_PARSE; } |
627
|
304 | parse_error |
1091
|
305 { ABORT_PARSE; } |
627
|
306 ; |
|
307 |
|
308 input1 : '\n' |
|
309 { $$ = 0; } |
327
|
310 | simple_list |
627
|
311 { $$ = $1; } |
1
|
312 | simple_list '\n' |
627
|
313 { $$ = $1; } |
1
|
314 | simple_list END_OF_INPUT |
627
|
315 { $$ = $1; } |
|
316 ; |
|
317 |
|
318 parse_error : LEXICAL_ERROR |
1091
|
319 { yyerror ("parse error"); } |
1
|
320 | error |
|
321 ; |
|
322 |
|
323 simple_list : semi_comma |
522
|
324 { $$ = 0; } |
1
|
325 | comma_semi |
522
|
326 { $$ = 0; } |
1
|
327 | simple_list1 |
578
|
328 { $$ = $1; } |
1
|
329 | simple_list1 semi_comma |
|
330 { |
578
|
331 tree_statement *tmp = $1->rear (); |
|
332 tmp->set_print_flag (0); |
1
|
333 } |
|
334 | simple_list1 comma_semi |
578
|
335 { $$ = $1; } |
1
|
336 ; |
|
337 |
578
|
338 simple_list1 : statement |
|
339 { $$ = new tree_statement_list ($1); } |
|
340 | semi_comma statement |
|
341 { $$ = new tree_statement_list ($2); } |
|
342 | comma_semi statement |
|
343 { $$ = new tree_statement_list ($2); } |
|
344 | simple_list1 semi_comma statement |
1
|
345 { |
578
|
346 tree_statement *tmp = $1->rear (); |
|
347 tmp->set_print_flag (0); |
|
348 $1->append ($3); |
1
|
349 } |
578
|
350 | simple_list1 comma_semi statement |
|
351 { $1->append ($3); } |
1
|
352 ; |
|
353 |
|
354 semi_comma : ';' |
|
355 | semi_comma ',' |
|
356 | semi_comma ';' |
|
357 ; |
|
358 |
|
359 comma_semi : ',' |
|
360 | comma_semi ';' |
|
361 | comma_semi ',' |
|
362 ; |
|
363 |
|
364 comma_nl_sep : ',' |
|
365 | '\n' |
|
366 | comma_nl_sep sep |
|
367 ; |
|
368 |
|
369 semi_sep : ';' |
|
370 | semi_sep sep |
|
371 ; |
|
372 |
|
373 opt_list : // empty |
578
|
374 { $$ = new tree_statement_list (); } |
1
|
375 | list |
|
376 { $$ = $1; } |
496
|
377 ; |
1
|
378 |
|
379 list : list1 |
578
|
380 { $$ = $1; } |
1
|
381 | list1 comma_nl_sep |
578
|
382 { $$ = $1; } |
1
|
383 | list1 semi_sep |
|
384 { |
578
|
385 tree_statement *tmp = $1->rear (); |
|
386 tmp->set_print_flag (0); |
1
|
387 } |
|
388 ; |
|
389 |
578
|
390 list1 : statement |
440
|
391 { |
|
392 beginning_of_function = 0; |
578
|
393 $$ = new tree_statement_list ($1); |
440
|
394 } |
578
|
395 | list1 comma_nl_sep statement |
|
396 { $1->append ($3); } |
|
397 | list1 semi_sep statement |
1
|
398 { |
578
|
399 tree_statement *tmp = $1->rear (); |
|
400 tmp->set_print_flag (0); |
|
401 $1->append ($3); |
1
|
402 } |
|
403 ; |
|
404 |
578
|
405 statement : command |
|
406 { $$ = new tree_statement ($1); } |
1
|
407 | ans_expression |
578
|
408 { $$ = new tree_statement ($1); } |
883
|
409 | PLOT CLEAR |
|
410 { |
|
411 symbol_record *sr = lookup_by_name ("clearplot", 0); |
|
412 tree_identifier *id = new tree_identifier (sr); |
|
413 $$ = new tree_statement (id); |
|
414 } |
1
|
415 ; |
|
416 |
|
417 plot_command : PLOT plot_command1 |
|
418 { |
578
|
419 if (! $2 && $1->pttype () != token::replot) |
478
|
420 { |
|
421 yyerror ("must have something to plot"); |
|
422 ABORT_PARSE; |
|
423 } |
|
424 else |
|
425 { |
578
|
426 $$ = new tree_plot_command ($2, $1->pttype ()); |
478
|
427 plotting = 0; |
|
428 past_plot_range = 0; |
|
429 in_plot_range = 0; |
|
430 in_plot_using = 0; |
|
431 in_plot_style = 0; |
|
432 } |
1
|
433 } |
|
434 | PLOT ranges plot_command1 |
|
435 { |
476
|
436 if ($1->pttype () == token::replot) |
|
437 { |
|
438 yyerror ("cannot specify new ranges with replot"); |
|
439 ABORT_PARSE; |
|
440 } |
|
441 else |
|
442 { |
578
|
443 $$ = new tree_plot_command ($3, $2, $1->pttype ()); |
476
|
444 plotting = 0; |
|
445 past_plot_range = 0; |
|
446 in_plot_range = 0; |
|
447 in_plot_using = 0; |
|
448 in_plot_style = 0; |
|
449 } |
1
|
450 } |
|
451 ; |
|
452 |
|
453 ranges : ranges1 |
578
|
454 { $$ = new plot_limits ($1); } |
1
|
455 | ranges1 ranges1 |
578
|
456 { $$ = new plot_limits ($1, $2); } |
1
|
457 | ranges1 ranges1 ranges1 |
578
|
458 { $$ = new plot_limits ($1, $2, $3); } |
1
|
459 ; |
|
460 |
|
461 ranges1 : OPEN_BRACE expression COLON expression CLOSE_BRACE |
578
|
462 { $$ = new plot_range ($2, $4); } |
1
|
463 | OPEN_BRACE COLON expression CLOSE_BRACE |
578
|
464 { $$ = new plot_range (0, $3); } |
1
|
465 | OPEN_BRACE expression COLON CLOSE_BRACE |
578
|
466 { $$ = new plot_range ($2, 0); } |
1
|
467 | OPEN_BRACE COLON CLOSE_BRACE |
578
|
468 { $$ = new plot_range (); } |
1
|
469 | OPEN_BRACE CLOSE_BRACE |
578
|
470 { $$ = new plot_range (); } |
1
|
471 ; |
|
472 |
478
|
473 plot_command1 : // empty |
522
|
474 { $$ = 0; } |
478
|
475 | plot_command2 |
578
|
476 { $$ = new subplot_list ($1); } |
1
|
477 | plot_command1 ',' plot_command2 |
578
|
478 { $1->append ($3); } |
1
|
479 ; |
|
480 |
|
481 plot_command2 : expression |
578
|
482 { $$ = new subplot ($1); } |
1
|
483 | expression plot_options |
578
|
484 { |
|
485 $2->set_data ($1); |
|
486 $$ = $2; |
|
487 } |
1
|
488 ; |
|
489 |
|
490 plot_options : using |
578
|
491 { $$ = new subplot ($1, 0, 0); } |
1
|
492 | title |
578
|
493 { $$ = new subplot (0, $1, 0); } |
1
|
494 | style |
578
|
495 { $$ = new subplot (0, 0, $1); } |
1
|
496 | using title |
578
|
497 { $$ = new subplot ($1, $2, 0); } |
|
498 | title using |
|
499 { $$ = new subplot ($2, $1, 0); } |
|
500 | using style |
|
501 { $$ = new subplot ($1, 0, $2); } |
|
502 | style using |
|
503 { $$ = new subplot ($2, 0, $1); } |
|
504 | title style |
|
505 { $$ = new subplot (0, $1, $2); } |
|
506 | style title |
|
507 { $$ = new subplot (0, $2, $1); } |
|
508 | using title style |
|
509 { $$ = new subplot ($1, $2, $3); } |
|
510 | using style title |
|
511 { $$ = new subplot ($1, $3, $2); } |
|
512 | title using style |
|
513 { $$ = new subplot ($2, $1, $3); } |
|
514 | title style using |
|
515 { $$ = new subplot ($3, $1, $2); } |
|
516 | style using title |
|
517 { $$ = new subplot ($2, $3, $1); } |
|
518 | style title using |
|
519 { $$ = new subplot ($3, $2, $1); } |
1
|
520 ; |
|
521 |
|
522 using : using1 |
|
523 { |
496
|
524 in_plot_using = 0; |
1
|
525 $$ = $1; |
|
526 } |
|
527 | using1 expression |
|
528 { |
496
|
529 in_plot_using = 0; |
1
|
530 $$ = $1->set_format ($2); |
|
531 } |
|
532 ; |
|
533 |
|
534 using1 : USING expression |
|
535 { |
578
|
536 subplot_using *tmp = new subplot_using (); |
1
|
537 $$ = tmp->add_qualifier ($2); |
|
538 } |
|
539 | using1 COLON expression |
|
540 { $$ = $1->add_qualifier ($3); } |
|
541 ; |
|
542 |
|
543 title : TITLE expression |
|
544 { $$ = $2; } |
|
545 ; |
|
546 |
|
547 style : WITH STYLE |
578
|
548 { $$ = new subplot_style ($2->string ()); } |
1
|
549 | WITH STYLE expression |
578
|
550 { $$ = new subplot_style ($2->string (), $3); } |
1
|
551 | WITH STYLE expression bogus_syntax expression |
578
|
552 { $$ = new subplot_style ($2->string (), $3, $5); } |
1
|
553 ; |
|
554 |
|
555 bogus_syntax : // empty |
|
556 ; |
|
557 |
|
558 ans_expression : expression |
|
559 { $$ = maybe_convert_to_ans_assign ($1); } |
|
560 ; |
|
561 |
|
562 global_decl : GLOBAL global_decl1 |
578
|
563 { |
|
564 $$ = new tree_global_command ($2, $1->line (), |
|
565 $1->column ()); |
|
566 } |
1
|
567 ; |
|
568 |
578
|
569 global_decl1 : global_decl2 |
|
570 { $$ = new tree_global_init_list ($1); } |
|
571 | global_decl1 optcomma global_decl2 |
|
572 { $1->append ($3); } |
|
573 |
|
574 global_decl2 : identifier |
|
575 { $$ = new tree_global ($1); } |
|
576 | identifier '=' expression |
1
|
577 { |
578
|
578 tree_simple_assignment_expression *tmp_ass; |
|
579 tmp_ass = new tree_simple_assignment_expression |
581
|
580 ($1, $3, 0, 0, $2->line (), $2->column ()); |
578
|
581 $$ = new tree_global (tmp_ass); |
1
|
582 } |
|
583 ; |
|
584 |
|
585 optcomma : // empty |
|
586 | ',' |
|
587 { |
|
588 if (user_pref.warn_comma_in_global_decl) |
|
589 warning ("comma in global declaration not\ |
|
590 interpreted as a command separator"); |
|
591 } |
|
592 ; |
|
593 |
578
|
594 command : plot_command |
|
595 { $$ = $1; } |
|
596 | func_def |
|
597 { $$ = $1; } |
|
598 | global_decl |
|
599 { $$ = $1; } |
|
600 | if_command |
|
601 { |
|
602 iffing--; |
|
603 $$ = $1; |
|
604 } |
916
|
605 |
924
|
606 | UNWIND optsep opt_list CLEANUP optsep opt_list END |
916
|
607 { |
924
|
608 if (check_end ($7, token::unwind_protect_end)) |
916
|
609 ABORT_PARSE; |
|
610 |
924
|
611 $$ = new tree_unwind_protect_command ($3, $6, $1->line (), |
916
|
612 $1->column ()); |
|
613 } |
578
|
614 | WHILE expression optsep opt_list END |
1
|
615 { |
|
616 maybe_warn_assign_as_truth_value ($2); |
143
|
617 if (check_end ($5, token::while_end)) |
|
618 ABORT_PARSE; |
1
|
619 looping--; |
191
|
620 $$ = new tree_while_command ($2, $4, $1->line (), |
|
621 $1->column ()); |
1
|
622 } |
118
|
623 | FOR variable '=' expression optsep opt_list END |
1
|
624 { |
143
|
625 if (check_end ($7, token::for_end)) |
|
626 ABORT_PARSE; |
1
|
627 looping--; |
191
|
628 $$ = new tree_for_command ($2, $4, $6, |
|
629 $1->line (), $1->column ()); |
1
|
630 } |
1229
|
631 | FOR '[' screwed_again matrix_row SCREW_TWO '=' |
|
632 expression optsep opt_list END |
|
633 { |
|
634 if (check_end ($10, token::for_end)) |
|
635 ABORT_PARSE; |
|
636 looping--; |
|
637 tree_matrix *tmp = ml.pop (); |
|
638 tmp = tmp->reverse (); |
|
639 tree_return_list *id_list = tmp->to_return_list (); |
|
640 $$ = new tree_for_command (id_list, $7, $9, |
|
641 $1->line (), $1->column ()); |
|
642 } |
1
|
643 | BREAK |
|
644 { |
1060
|
645 if (! (looping || defining_func)) |
1
|
646 { |
1060
|
647 yyerror ("break: only meaningful within a loop\ |
|
648 or function body"); |
1
|
649 ABORT_PARSE; |
|
650 } |
191
|
651 $$ = new tree_break_command ($1->line (), $1->column ()); |
1
|
652 } |
|
653 | CONTINUE |
|
654 { |
1060
|
655 if (! looping) |
1
|
656 { |
1005
|
657 yyerror ("continue: only meaningful within a\ |
1
|
658 `for' or `while' loop"); |
|
659 ABORT_PARSE; |
|
660 } |
191
|
661 $$ = new tree_continue_command ($1->line (), |
|
662 $1->column ()); |
1
|
663 } |
|
664 | FUNC_RET |
|
665 { |
1060
|
666 if (! defining_func) |
1
|
667 { |
1005
|
668 yyerror ("return: only meaningful within a function"); |
1
|
669 ABORT_PARSE; |
|
670 } |
191
|
671 $$ = new tree_return_command ($1->line (), $1->column ()); |
1
|
672 } |
|
673 ; |
|
674 |
578
|
675 if_command : IF if_cmd_list END |
|
676 { |
|
677 if (check_end ($3, token::if_end)) |
|
678 ABORT_PARSE; |
|
679 $$ = new tree_if_command ($2, $1->line (), $1->column ()); |
|
680 } |
|
681 ; |
|
682 |
|
683 if_cmd_list : if_cmd_list1 |
|
684 { $$ = $1 } |
|
685 | if_cmd_list1 else_clause |
|
686 { $1->append ($2); } |
|
687 ; |
|
688 |
|
689 if_cmd_list1 : expression optsep opt_list |
|
690 { |
|
691 maybe_warn_assign_as_truth_value ($1); |
|
692 tree_if_clause *t = new tree_if_clause ($1, $3); |
|
693 $$ = new tree_if_command_list (t); |
|
694 } |
|
695 | if_cmd_list1 elseif_clause |
|
696 { $1->append ($2); } |
|
697 ; |
|
698 |
|
699 elseif_clause : ELSEIF optsep expression optsep opt_list |
1
|
700 { |
|
701 maybe_warn_assign_as_truth_value ($3); |
578
|
702 $$ = new tree_if_clause ($3, $5); |
1
|
703 } |
578
|
704 ; |
|
705 |
|
706 else_clause : ELSE optsep opt_list |
|
707 { $$ = new tree_if_clause ($3); } |
1
|
708 ; |
|
709 |
|
710 optsep : // empty |
|
711 | sep |
|
712 ; |
|
713 |
|
714 sep : ',' |
|
715 | ';' |
|
716 | '\n' |
|
717 | sep ',' |
|
718 | sep ';' |
|
719 | sep '\n' |
|
720 ; |
|
721 |
|
722 screwed_again : // empty |
|
723 { maybe_screwed_again++; } |
|
724 ; |
|
725 |
|
726 expression : variable '=' expression |
143
|
727 { $$ = new tree_simple_assignment_expression |
581
|
728 ($1, $3, 0, 0, $2->line (), $2->column ()); } |
1
|
729 | NUM '=' expression |
|
730 { |
1005
|
731 yyerror ("invalid assignment to a number"); |
522
|
732 $$ = 0; |
1
|
733 ABORT_PARSE; |
|
734 } |
666
|
735 | '[' screwed_again matrix_row SCREW_TWO '=' expression |
|
736 { |
1229
|
737 tree_matrix *tmp = ml.pop (); |
|
738 tmp = tmp->reverse (); |
|
739 $$ = make_multi_val_ret (tmp, $6, $5->line (), |
|
740 $5->column ()); |
666
|
741 if (! $$) |
|
742 ABORT_PARSE; |
|
743 } |
1
|
744 | simple_expr |
|
745 { $$ = $1; } |
|
746 ; |
|
747 |
|
748 simple_expr : simple_expr1 |
|
749 { $$ = $1; } |
|
750 | identifier PLUS_PLUS |
578
|
751 { $$ = make_postfix_op (PLUS_PLUS, $1, $2); } |
1
|
752 | identifier MINUS_MINUS |
578
|
753 { $$ = make_postfix_op (MINUS_MINUS, $1, $2); } |
1
|
754 | simple_expr QUOTE |
578
|
755 { $$ = make_unary_op (QUOTE, $1, $2); } |
1
|
756 | simple_expr TRANSPOSE |
578
|
757 { $$ = make_unary_op (TRANSPOSE, $1, $2); } |
1
|
758 | simple_expr POW simple_expr |
578
|
759 { $$ = make_binary_op (POW, $1, $2, $3); } |
1
|
760 | simple_expr EPOW simple_expr |
578
|
761 { $$ = make_binary_op (EPOW, $1, $2, $3); } |
1
|
762 | simple_expr '+' simple_expr |
578
|
763 { $$ = make_binary_op ('+', $1, $2, $3); } |
1
|
764 | simple_expr '-' simple_expr |
578
|
765 { $$ = make_binary_op ('-', $1, $2, $3); } |
1
|
766 | simple_expr '*' simple_expr |
578
|
767 { $$ = make_binary_op ('*', $1, $2, $3); } |
1
|
768 | simple_expr '/' simple_expr |
578
|
769 { $$ = make_binary_op ('/', $1, $2, $3); } |
1
|
770 | simple_expr EMUL simple_expr |
578
|
771 { $$ = make_binary_op (EMUL, $1, $2, $3); } |
1
|
772 | simple_expr EDIV simple_expr |
578
|
773 { $$ = make_binary_op (EDIV, $1, $2, $3); } |
1
|
774 | simple_expr LEFTDIV simple_expr |
578
|
775 { $$ = make_binary_op (LEFTDIV, $1, $2, $3); } |
1
|
776 | simple_expr ELEFTDIV simple_expr |
578
|
777 { $$ = make_binary_op (ELEFTDIV, $1, $2, $3); } |
1
|
778 | simple_expr EXPR_LT simple_expr |
578
|
779 { $$ = make_binary_op (EXPR_LT, $1, $2, $3); } |
1
|
780 | simple_expr EXPR_LE simple_expr |
578
|
781 { $$ = make_binary_op (EXPR_LE, $1, $2, $3); } |
1
|
782 | simple_expr EXPR_EQ simple_expr |
578
|
783 { $$ = make_binary_op (EXPR_EQ, $1, $2, $3); } |
1
|
784 | simple_expr EXPR_GE simple_expr |
578
|
785 { $$ = make_binary_op (EXPR_GE, $1, $2, $3); } |
1
|
786 | simple_expr EXPR_GT simple_expr |
578
|
787 { $$ = make_binary_op (EXPR_GT, $1, $2, $3); } |
1
|
788 | simple_expr EXPR_NE simple_expr |
578
|
789 { $$ = make_binary_op (EXPR_NE, $1, $2, $3); } |
428
|
790 | simple_expr EXPR_AND_AND simple_expr |
578
|
791 { $$ = make_binary_op (EXPR_AND_AND, $1, $2, $3); } |
428
|
792 | simple_expr EXPR_OR_OR simple_expr |
578
|
793 { $$ = make_binary_op (EXPR_OR_OR, $1, $2, $3); } |
1
|
794 | simple_expr EXPR_AND simple_expr |
578
|
795 { $$ = make_binary_op (EXPR_AND, $1, $2, $3); } |
1
|
796 | simple_expr EXPR_OR simple_expr |
578
|
797 { $$ = make_binary_op (EXPR_OR, $1, $2, $3); } |
1
|
798 ; |
|
799 |
|
800 simple_expr1 : NUM |
581
|
801 { |
|
802 tree_constant *tmp = new tree_constant ($1->number ()); |
|
803 tmp->stash_original_text ($1->text_rep ()); |
|
804 $$ = tmp; |
|
805 } |
1
|
806 | IMAG_NUM |
581
|
807 { |
|
808 Complex c (0.0, $1->number ()); |
|
809 tree_constant *tmp = new tree_constant (c); |
|
810 tmp->stash_original_text ($1->text_rep ()); |
|
811 $$ = tmp; |
|
812 } |
1
|
813 | TEXT |
143
|
814 { $$ = new tree_constant ($1->string ()); } |
1
|
815 | '(' expression ')' |
|
816 { |
581
|
817 $2->in_parens++; |
1
|
818 $$ = $2; |
|
819 } |
496
|
820 | word_list_cmd |
|
821 { $$ = $1; } |
1
|
822 | variable |
|
823 { $$ = $1; } |
|
824 | matrix |
|
825 { $$ = $1; } |
240
|
826 | '[' ']' |
|
827 { |
|
828 mlnm.pop (); |
|
829 $$ = new tree_constant (Matrix ()); |
|
830 } |
|
831 | '[' ';' ']' |
|
832 { |
|
833 mlnm.pop (); |
|
834 $$ = new tree_constant (Matrix ()); |
|
835 } |
1
|
836 | colon_expr |
|
837 { $$ = $1; } |
|
838 | PLUS_PLUS identifier %prec UNARY |
578
|
839 { $$ = make_prefix_op (PLUS_PLUS, $2, $1); } |
1
|
840 | MINUS_MINUS identifier %prec UNARY |
578
|
841 { $$ = make_prefix_op (MINUS_MINUS, $2, $1); } |
1
|
842 | EXPR_NOT simple_expr |
578
|
843 { $$ = make_unary_op (EXPR_NOT, $2, $1); } |
1
|
844 | '+' simple_expr %prec UNARY |
|
845 { $$ = $2; } |
|
846 | '-' simple_expr %prec UNARY |
578
|
847 { $$ = make_unary_op ('-', $2, $1); } |
1
|
848 ; |
|
849 |
|
850 colon_expr : simple_expr ':' simple_expr |
143
|
851 { $$ = new tree_colon_expression |
|
852 ($1, $3, $2->line (), $2->column ()); } |
1
|
853 | colon_expr ':' simple_expr |
|
854 { |
|
855 $$ = $1->chain ($3); |
522
|
856 if (! $$) |
1005
|
857 ABORT_PARSE; |
1
|
858 } |
|
859 ; |
|
860 |
|
861 word_list_cmd : identifier word_list |
482
|
862 { |
|
863 $$ = new tree_index_expression |
|
864 ($1, $2, $1->line (), $1->column ()); |
|
865 } |
1
|
866 ; |
|
867 |
578
|
868 word_list : TEXT |
482
|
869 { |
|
870 tree_constant *tmp = new tree_constant ($1->string ()); |
|
871 $$ = new tree_argument_list (tmp); |
|
872 } |
578
|
873 | word_list TEXT |
482
|
874 { |
|
875 tree_constant *tmp = new tree_constant ($2->string ()); |
578
|
876 $1->append (tmp); |
482
|
877 } |
1
|
878 ; |
|
879 |
|
880 // This is truly disgusting. |
|
881 |
|
882 g_symtab : // empty |
|
883 { curr_sym_tab = global_sym_tab; } |
|
884 ; |
|
885 |
|
886 local_symtab : // empty |
|
887 { curr_sym_tab = tmp_local_sym_tab; } |
|
888 ; |
|
889 |
|
890 safe : // empty |
|
891 { maybe_screwed = 0; } |
|
892 ; |
|
893 |
|
894 are_we_screwed : // empty |
|
895 { maybe_screwed = 1; } |
|
896 ; |
|
897 |
|
898 func_def : FCN g_symtab are_we_screwed func_def1 |
|
899 { |
|
900 curr_sym_tab = top_level_sym_tab; |
|
901 defining_func = 0; |
522
|
902 $$ = 0; |
1
|
903 } |
|
904 | FCN g_symtab are_we_screwed func_def2 |
|
905 { |
|
906 curr_sym_tab = top_level_sym_tab; |
|
907 defining_func = 0; |
522
|
908 $$ = 0; |
1
|
909 } |
|
910 ; |
|
911 |
|
912 func_def1 : SCREW safe g_symtab '=' func_def2 |
|
913 { |
143
|
914 tree_identifier *tmp = new tree_identifier |
|
915 ($1->sym_rec (), $1->line (), $1->column ()); |
1
|
916 tree_parameter_list *tpl = new tree_parameter_list (tmp); |
|
917 tpl->mark_as_formal_parameters (); |
|
918 $$ = $5->define_ret_list (tpl); |
|
919 } |
723
|
920 | return_list g_symtab '=' func_def2 |
1
|
921 { |
578
|
922 $1->mark_as_formal_parameters (); |
723
|
923 $$ = $4->define_ret_list ($1); |
1
|
924 } |
|
925 ; |
|
926 |
723
|
927 return_list_x : '[' safe local_symtab |
|
928 ; |
|
929 |
|
930 return_list : return_list_x ']' |
|
931 { $$ = new tree_parameter_list (); } |
|
932 | return_list_x ELLIPSIS ']' |
|
933 { |
|
934 tree_parameter_list *tmp = new tree_parameter_list (); |
|
935 tmp->mark_varargs_only (); |
|
936 $$ = tmp; |
|
937 } |
|
938 | return_list1 ']' |
|
939 { $$ = $1; } |
|
940 | return_list1 ',' ELLIPSIS ']' |
|
941 { |
|
942 $1->mark_varargs (); |
|
943 $$ = $1; |
|
944 } |
|
945 ; |
|
946 |
|
947 return_list1 : return_list_x identifier |
|
948 { $$ = new tree_parameter_list ($2); } |
|
949 | return_list_x error |
|
950 { |
1005
|
951 yyerror ("invalid function return list"); |
723
|
952 ABORT_PARSE; |
|
953 } |
|
954 | return_list1 ',' identifier |
578
|
955 { $1->append ($3); } |
1
|
956 ; |
|
957 |
|
958 func_def2 : identifier safe local_symtab func_def3 |
|
959 { |
|
960 char *id_name = $1->name (); |
|
961 // if (is_text_function_name (id_name)) |
|
962 // { |
1005
|
963 // yyerror ("invalid use of reserved word %s", id_name); |
1
|
964 // ABORT_PARSE; |
|
965 // } |
|
966 |
|
967 // If input is coming from a file, issue a warning if the name of the |
|
968 // file does not match the name of the function stated in the file. |
|
969 // Matlab doesn't provide a diagnostic (it ignores the stated name). |
|
970 |
908
|
971 $4->stash_function_name (id_name); |
|
972 |
338
|
973 if (reading_fcn_file) |
1
|
974 { |
338
|
975 if (strcmp (curr_fcn_file_name, id_name) != 0) |
195
|
976 { |
1038
|
977 if (user_pref.warn_function_name_clash) |
|
978 warning ("function name `%s' does not agree\ |
341
|
979 with function file name `%s.m'", id_name, curr_fcn_file_name); |
1
|
980 |
572
|
981 global_sym_tab->rename (id_name, |
|
982 curr_fcn_file_name); |
|
983 |
773
|
984 if (error_state) |
|
985 ABORT_PARSE; |
|
986 |
197
|
987 id_name = $1->name (); |
195
|
988 } |
|
989 |
908
|
990 $4->stash_function_name (id_name); |
905
|
991 $4->stash_fcn_file_name (); |
522
|
992 $4->stash_fcn_file_time (time (0)); |
338
|
993 $4->mark_as_system_fcn_file (); |
1
|
994 } |
315
|
995 else if (! (input_from_tmp_history_file |
|
996 || input_from_startup_file) |
195
|
997 && reading_script_file |
338
|
998 && strcmp (curr_fcn_file_name, id_name) == 0) |
1
|
999 { |
195
|
1000 warning ("function `%s' defined within\ |
341
|
1001 script file `%s.m'", id_name, curr_fcn_file_name); |
1
|
1002 } |
|
1003 |
197
|
1004 top_level_sym_tab->clear (id_name); |
|
1005 |
195
|
1006 $1->define ($4); |
|
1007 $1->document (help_buf); |
|
1008 |
1
|
1009 $$ = $4; |
|
1010 } |
|
1011 ; |
|
1012 |
|
1013 func_def3 : param_list optsep opt_list fcn_end_or_eof |
|
1014 { |
|
1015 tree_function *fcn = new tree_function ($3, curr_sym_tab); |
|
1016 $$ = fcn->define_param_list ($1); |
|
1017 } |
|
1018 | optsep opt_list fcn_end_or_eof |
|
1019 { $$ = new tree_function ($2, curr_sym_tab); } |
|
1020 ; |
|
1021 |
|
1022 fcn_end_or_eof : END |
|
1023 { |
143
|
1024 if (check_end ($1, token::function_end)) |
1
|
1025 ABORT_PARSE; |
|
1026 |
338
|
1027 if (reading_fcn_file) |
1
|
1028 check_for_garbage_after_fcn_def (); |
|
1029 } |
|
1030 | END_OF_INPUT |
|
1031 { |
338
|
1032 if (! (reading_fcn_file || reading_script_file)) |
1
|
1033 YYABORT; |
|
1034 } |
|
1035 ; |
|
1036 |
751
|
1037 indirect_ref : indirect_ref1 |
191
|
1038 { |
751
|
1039 looking_at_indirect_ref = 0; |
|
1040 $$ = $1; |
191
|
1041 } |
751
|
1042 |
|
1043 indirect_ref1 : identifier |
191
|
1044 { |
751
|
1045 $$ = new tree_indirect_ref ($1, $1->line (), |
|
1046 $1->column ()); |
191
|
1047 } |
751
|
1048 | indirect_ref1 '.' { looking_at_indirect_ref = 1; } TEXT_ID |
|
1049 { $$ = $1->chain ($4->string ()); } |
|
1050 ; |
|
1051 |
|
1052 variable : indirect_ref |
|
1053 { $$ = make_index_expression ($1, 0); } |
|
1054 | indirect_ref '(' ')' |
|
1055 { $$ = make_index_expression ($1, 0); } |
|
1056 | indirect_ref '(' arg_list ')' |
|
1057 { $$ = make_index_expression ($1, $3); } |
|
1058 | indirect_ref '[' |
1
|
1059 { |
1005
|
1060 yyerror ("use `(\' and `)\' as index operators, not\ |
1
|
1061 `[\' and `]\'"); |
522
|
1062 $$ = 0; |
1
|
1063 ABORT_PARSE; |
|
1064 } |
|
1065 ; |
|
1066 |
240
|
1067 param_list : '(' ')' |
1
|
1068 { |
240
|
1069 quote_is_transpose = 0; |
522
|
1070 $$ = 0; |
240
|
1071 } |
504
|
1072 | '(' ELLIPSIS ')' |
|
1073 { |
|
1074 quote_is_transpose = 0; |
|
1075 tree_parameter_list *tmp = new tree_parameter_list (); |
|
1076 tmp->mark_varargs_only (); |
|
1077 $$ = tmp; |
|
1078 } |
240
|
1079 | param_list1 ')' |
|
1080 { |
|
1081 quote_is_transpose = 0; |
578
|
1082 $1->mark_as_formal_parameters (); |
1
|
1083 } |
206
|
1084 | param_list1 ',' ELLIPSIS ')' |
|
1085 { |
240
|
1086 quote_is_transpose = 0; |
578
|
1087 $1->mark_as_formal_parameters (); |
|
1088 $1->mark_varargs (); |
206
|
1089 } |
504
|
1090 ; |
1
|
1091 |
|
1092 param_list1 : '(' identifier |
|
1093 { $$ = new tree_parameter_list ($2); } |
|
1094 | param_list1 ',' identifier |
578
|
1095 { $1->append ($3); } |
1
|
1096 | '(' error |
|
1097 { |
1005
|
1098 yyerror ("invalid parameter list"); |
206
|
1099 ABORT_PARSE; |
1
|
1100 } |
|
1101 | param_list1 ',' error |
|
1102 { |
1005
|
1103 yyerror ("invalid parameter list"); |
206
|
1104 ABORT_PARSE; |
1
|
1105 } |
|
1106 ; |
|
1107 |
|
1108 identifier : NAME |
504
|
1109 { |
|
1110 $$ = new tree_identifier |
|
1111 ($1->sym_rec (), $1->line (), $1->column ()); |
|
1112 } |
|
1113 ; |
1
|
1114 |
578
|
1115 arg_list : ':' |
1
|
1116 { |
|
1117 tree_constant *colon; |
620
|
1118 tree_constant::magic_colon t; |
|
1119 colon = new tree_constant (t); |
1
|
1120 $$ = new tree_argument_list (colon); |
|
1121 } |
922
|
1122 | expression |
|
1123 { $$ = new tree_argument_list ($1); } |
|
1124 | ALL_VA_ARGS |
|
1125 { |
|
1126 tree_constant *all_va_args; |
|
1127 tree_constant::all_va_args t; |
|
1128 all_va_args = new tree_constant (t); |
|
1129 $$ = new tree_argument_list (all_va_args); |
|
1130 } |
578
|
1131 | arg_list ',' ':' |
1
|
1132 { |
|
1133 tree_constant *colon; |
620
|
1134 tree_constant::magic_colon t; |
|
1135 colon = new tree_constant (t); |
578
|
1136 $1->append (colon); |
1
|
1137 } |
578
|
1138 | arg_list ',' expression |
|
1139 { $1->append ($3); } |
922
|
1140 | arg_list ',' ALL_VA_ARGS |
|
1141 { |
|
1142 tree_constant *all_va_args; |
|
1143 tree_constant::all_va_args t; |
|
1144 all_va_args = new tree_constant (t); |
|
1145 $1->append (all_va_args); |
|
1146 } |
1
|
1147 ; |
|
1148 |
240
|
1149 matrix : '[' screwed_again rows ']' |
1
|
1150 { |
|
1151 mlnm.pop (); |
|
1152 maybe_screwed_again--; |
|
1153 tree_matrix *tmp = ml.pop (); |
|
1154 $$ = tmp->reverse (); |
|
1155 } |
|
1156 ; |
|
1157 |
1084
|
1158 rows : rows1 |
|
1159 | rows1 ';' // Ignore trailing semicolon. |
1
|
1160 ; |
|
1161 |
1084
|
1162 rows1 : matrix_row |
|
1163 | rows1 ';' matrix_row |
|
1164 ; |
|
1165 |
|
1166 matrix_row : matrix_row1 |
|
1167 | matrix_row1 ',' // Ignore trailing comma. |
|
1168 ; |
|
1169 |
|
1170 matrix_row1 : expression // First element on row. |
1
|
1171 { |
|
1172 if (mlnm.top ()) |
|
1173 { |
|
1174 mlnm.pop (); |
|
1175 mlnm.push (0); |
578
|
1176 tree_matrix *tmp = new tree_matrix |
|
1177 ($1, tree_matrix::md_none); |
1
|
1178 ml.push (tmp); |
|
1179 } |
|
1180 else |
|
1181 { |
|
1182 tree_matrix *tmp = ml.pop (); |
578
|
1183 tmp = tmp->chain ($1, tree_matrix::md_down); |
1
|
1184 ml.push (tmp); |
|
1185 } |
|
1186 } |
1084
|
1187 | matrix_row1 ',' expression |
1
|
1188 { |
|
1189 tree_matrix *tmp = ml.pop (); |
578
|
1190 tmp = tmp->chain ($3, tree_matrix::md_right); |
1
|
1191 ml.push (tmp); |
|
1192 } |
|
1193 ; |
|
1194 |
|
1195 %% |
|
1196 |
666
|
1197 // Generic error messages. |
|
1198 |
1
|
1199 static void |
|
1200 yyerror (char *s) |
|
1201 { |
|
1202 char *line = current_input_line; |
143
|
1203 int err_col = current_input_column - 1; |
1
|
1204 |
581
|
1205 ostrstream output_buf; |
1
|
1206 |
1005
|
1207 if (reading_fcn_file || reading_script_file) |
|
1208 output_buf << "parse error near line " << input_line_number |
|
1209 << " of file " << curr_fcn_file_name << ".m:"; |
|
1210 else |
|
1211 output_buf << "parse error:"; |
581
|
1212 |
1005
|
1213 if (s && strcmp (s, "parse error") != 0) |
|
1214 output_buf << "\n\n " << s; |
|
1215 |
|
1216 output_buf << "\n\n"; |
1
|
1217 |
522
|
1218 if (line) |
1
|
1219 { |
335
|
1220 int len = strlen (line); |
1060
|
1221 |
335
|
1222 if (line[len-1] == '\n') |
|
1223 { |
|
1224 len--; |
|
1225 line[len] = '\0'; |
|
1226 } |
1005
|
1227 |
335
|
1228 // Print the line, maybe with a pointer near the error token. |
1005
|
1229 |
1060
|
1230 output_buf << ">>> " << line << "\n"; |
|
1231 |
|
1232 if (err_col == 0) |
|
1233 err_col = len; |
|
1234 |
|
1235 for (int i = 0; i < err_col + 3; i++) |
|
1236 output_buf << " "; |
|
1237 |
|
1238 output_buf << "^"; |
1
|
1239 } |
1005
|
1240 |
|
1241 output_buf << "\n" << ends; |
581
|
1242 |
1005
|
1243 char *msg = output_buf.str (); |
|
1244 |
1090
|
1245 parse_error ("%s", msg); |
1005
|
1246 |
|
1247 delete [] msg; |
1
|
1248 } |
|
1249 |
666
|
1250 // Error mesages for mismatched end tokens. |
|
1251 |
496
|
1252 static void |
|
1253 end_error (char *type, token::end_tok_type ettype, int l, int c) |
|
1254 { |
769
|
1255 static char *fmt = "`%s' command matched by `%s' near line %d column %d"; |
496
|
1256 |
|
1257 switch (ettype) |
|
1258 { |
|
1259 case token::simple_end: |
|
1260 error (fmt, type, "end", l, c); |
|
1261 break; |
777
|
1262 |
496
|
1263 case token::for_end: |
|
1264 error (fmt, type, "endfor", l, c); |
|
1265 break; |
777
|
1266 |
496
|
1267 case token::function_end: |
|
1268 error (fmt, type, "endfunction", l, c); |
|
1269 break; |
777
|
1270 |
496
|
1271 case token::if_end: |
|
1272 error (fmt, type, "endif", l, c); |
|
1273 break; |
777
|
1274 |
496
|
1275 case token::while_end: |
|
1276 error (fmt, type, "endwhile", l, c); |
|
1277 break; |
777
|
1278 |
496
|
1279 default: |
|
1280 panic_impossible (); |
|
1281 break; |
|
1282 } |
|
1283 } |
|
1284 |
666
|
1285 // Check to see that end tokens are properly matched. |
|
1286 |
143
|
1287 static int |
|
1288 check_end (token *tok, token::end_tok_type expected) |
|
1289 { |
|
1290 token::end_tok_type ettype = tok->ettype (); |
|
1291 if (ettype != expected && ettype != token::simple_end) |
|
1292 { |
|
1293 yyerror ("parse error"); |
|
1294 |
|
1295 int l = tok->line (); |
|
1296 int c = tok->column (); |
|
1297 |
|
1298 switch (expected) |
|
1299 { |
|
1300 case token::for_end: |
|
1301 end_error ("for", ettype, l, c); |
|
1302 break; |
777
|
1303 |
143
|
1304 case token::function_end: |
|
1305 end_error ("function", ettype, l, c); |
|
1306 break; |
777
|
1307 |
143
|
1308 case token::if_end: |
|
1309 end_error ("if", ettype, l, c); |
|
1310 break; |
777
|
1311 |
143
|
1312 case token::while_end: |
|
1313 end_error ("while", ettype, l, c); |
|
1314 break; |
777
|
1315 |
143
|
1316 default: |
|
1317 panic_impossible (); |
|
1318 break; |
|
1319 } |
|
1320 return 1; |
|
1321 } |
|
1322 else |
|
1323 return 0; |
|
1324 } |
|
1325 |
666
|
1326 // Try to figure out early if an expression should become an |
|
1327 // assignment to the builtin variable ans. |
|
1328 // |
|
1329 // Need to make sure that the expression isn't already an identifier |
|
1330 // that has a name, or an assignment expression. |
|
1331 // |
|
1332 // Note that an expression can't be just an identifier anymore -- it |
|
1333 // must at least be an index expression (see the definition of the |
|
1334 // non-terminal `variable' above). |
|
1335 // |
|
1336 // XXX FIXME XXX. This isn't quite sufficient. For example, try the |
|
1337 // command `x = 4, x' for `x' previously undefined. |
|
1338 // |
|
1339 // XXX FIXME XXX -- we should probably delay doing this until eval-time. |
|
1340 |
496
|
1341 static tree_expression * |
|
1342 maybe_convert_to_ans_assign (tree_expression *expr) |
1
|
1343 { |
|
1344 if (expr->is_index_expression ()) |
|
1345 { |
195
|
1346 expr->mark_for_possible_ans_assign (); |
|
1347 return expr; |
|
1348 } |
|
1349 else if (expr->is_assignment_expression () |
|
1350 || expr->is_prefix_expression ()) |
|
1351 { |
|
1352 return expr; |
|
1353 } |
|
1354 else |
|
1355 { |
1162
|
1356 static symbol_record *sr = global_sym_tab->lookup ("ans", 1, 0); |
|
1357 static tree_identifier ans_id (sr); |
1
|
1358 |
1162
|
1359 return new tree_simple_assignment_expression (&ans_id, expr, 1, 1); |
1
|
1360 } |
|
1361 } |
|
1362 |
666
|
1363 // Maybe print a warning if an assignment expression is used as the |
|
1364 // test in a logical expression. |
|
1365 |
496
|
1366 static void |
|
1367 maybe_warn_assign_as_truth_value (tree_expression *expr) |
1
|
1368 { |
|
1369 if (user_pref.warn_assign_as_truth_value |
|
1370 && expr->is_assignment_expression () |
581
|
1371 && expr->in_parens < 2) |
1
|
1372 { |
|
1373 warning ("suggest parenthesis around assignment used as truth value"); |
|
1374 } |
|
1375 } |
578
|
1376 |
666
|
1377 // Build a binary expression. |
|
1378 |
578
|
1379 static tree_expression * |
|
1380 make_binary_op (int op, tree_expression *op1, token *tok_val, |
|
1381 tree_expression *op2) |
|
1382 { |
|
1383 tree_expression::type t; |
|
1384 switch (op) |
|
1385 { |
|
1386 case POW: |
|
1387 t = tree_expression::power; |
|
1388 break; |
777
|
1389 |
578
|
1390 case EPOW: |
|
1391 t = tree_expression::elem_pow; |
|
1392 break; |
777
|
1393 |
578
|
1394 case '+': |
|
1395 t = tree_expression::add; |
|
1396 break; |
777
|
1397 |
578
|
1398 case '-': |
|
1399 t = tree_expression::subtract; |
|
1400 break; |
777
|
1401 |
578
|
1402 case '*': |
|
1403 t = tree_expression::multiply; |
|
1404 break; |
777
|
1405 |
578
|
1406 case '/': |
|
1407 t = tree_expression::divide; |
|
1408 break; |
777
|
1409 |
578
|
1410 case EMUL: |
|
1411 t = tree_expression::el_mul; |
|
1412 break; |
777
|
1413 |
578
|
1414 case EDIV: |
|
1415 t = tree_expression::el_div; |
|
1416 break; |
777
|
1417 |
578
|
1418 case LEFTDIV: |
|
1419 t = tree_expression::leftdiv; |
|
1420 break; |
777
|
1421 |
578
|
1422 case ELEFTDIV: |
|
1423 t = tree_expression::el_leftdiv; |
|
1424 break; |
777
|
1425 |
578
|
1426 case EXPR_LT: |
|
1427 t = tree_expression::cmp_lt; |
|
1428 break; |
777
|
1429 |
578
|
1430 case EXPR_LE: |
|
1431 t = tree_expression::cmp_le; |
|
1432 break; |
777
|
1433 |
578
|
1434 case EXPR_EQ: |
|
1435 t = tree_expression::cmp_eq; |
|
1436 break; |
777
|
1437 |
578
|
1438 case EXPR_GE: |
|
1439 t = tree_expression::cmp_ge; |
|
1440 break; |
777
|
1441 |
578
|
1442 case EXPR_GT: |
|
1443 t = tree_expression::cmp_gt; |
|
1444 break; |
777
|
1445 |
578
|
1446 case EXPR_NE: |
|
1447 t = tree_expression::cmp_ne; |
|
1448 break; |
777
|
1449 |
578
|
1450 case EXPR_AND_AND: |
|
1451 t = tree_expression::and_and; |
|
1452 break; |
777
|
1453 |
578
|
1454 case EXPR_OR_OR: |
|
1455 t = tree_expression::or_or; |
|
1456 break; |
777
|
1457 |
578
|
1458 case EXPR_AND: |
|
1459 t = tree_expression::and; |
|
1460 break; |
777
|
1461 |
578
|
1462 case EXPR_OR: |
|
1463 t = tree_expression::or; |
|
1464 break; |
777
|
1465 |
578
|
1466 default: |
|
1467 panic_impossible (); |
|
1468 break; |
|
1469 } |
|
1470 |
|
1471 int l = tok_val->line (); |
|
1472 int c = tok_val->column (); |
|
1473 |
|
1474 return new tree_binary_expression (op1, op2, t, l, c); |
|
1475 } |
|
1476 |
666
|
1477 // Build a prefix expression. |
|
1478 |
578
|
1479 static tree_expression * |
|
1480 make_prefix_op (int op, tree_identifier *op1, token *tok_val) |
|
1481 { |
|
1482 tree_expression::type t; |
|
1483 switch (op) |
|
1484 { |
|
1485 case PLUS_PLUS: |
|
1486 t = tree_expression::increment; |
|
1487 break; |
777
|
1488 |
578
|
1489 case MINUS_MINUS: |
|
1490 t = tree_expression::decrement; |
|
1491 break; |
777
|
1492 |
578
|
1493 default: |
|
1494 panic_impossible (); |
|
1495 break; |
|
1496 } |
|
1497 |
|
1498 int l = tok_val->line (); |
|
1499 int c = tok_val->column (); |
|
1500 |
|
1501 return new tree_prefix_expression (op1, t, l, c); |
|
1502 } |
|
1503 |
666
|
1504 // Build a postfix expression. |
|
1505 |
578
|
1506 static tree_expression * |
|
1507 make_postfix_op (int op, tree_identifier *op1, token *tok_val) |
|
1508 { |
|
1509 tree_expression::type t; |
|
1510 switch (op) |
|
1511 { |
|
1512 case PLUS_PLUS: |
|
1513 t = tree_expression::increment; |
|
1514 break; |
777
|
1515 |
578
|
1516 case MINUS_MINUS: |
|
1517 t = tree_expression::decrement; |
|
1518 break; |
777
|
1519 |
578
|
1520 default: |
|
1521 panic_impossible (); |
|
1522 break; |
|
1523 } |
|
1524 |
|
1525 int l = tok_val->line (); |
|
1526 int c = tok_val->column (); |
|
1527 |
|
1528 return new tree_postfix_expression (op1, t, l, c); |
|
1529 } |
|
1530 |
666
|
1531 // Build a unary expression. |
|
1532 |
578
|
1533 static tree_expression * |
|
1534 make_unary_op (int op, tree_expression *op1, token *tok_val) |
|
1535 { |
|
1536 tree_expression::type t; |
|
1537 switch (op) |
|
1538 { |
|
1539 case QUOTE: |
|
1540 t = tree_expression::hermitian; |
|
1541 break; |
777
|
1542 |
578
|
1543 case TRANSPOSE: |
|
1544 t = tree_expression::transpose; |
|
1545 break; |
777
|
1546 |
578
|
1547 case EXPR_NOT: |
|
1548 t = tree_expression::not; |
|
1549 break; |
777
|
1550 |
578
|
1551 case '-': |
|
1552 t = tree_expression::uminus; |
|
1553 break; |
777
|
1554 |
578
|
1555 default: |
|
1556 panic_impossible (); |
|
1557 break; |
|
1558 } |
|
1559 |
|
1560 int l = tok_val->line (); |
|
1561 int c = tok_val->column (); |
|
1562 |
|
1563 return new tree_unary_expression (op1, t, l, c); |
|
1564 } |
666
|
1565 |
|
1566 // Make an expression that handles assignment of multiple values. |
|
1567 |
|
1568 static tree_expression * |
1229
|
1569 make_multi_val_ret (tree_matrix *m, tree_expression *rhs, int l, int c) |
666
|
1570 { |
|
1571 // Convert the matrix list to a list of identifiers. If that fails, |
|
1572 // we can abort here, without losing anything -- no other possible |
|
1573 // syntax is valid if we've seen the equals sign as the next token |
|
1574 // after the `]'. |
|
1575 |
|
1576 tree_expression *retval = 0; |
|
1577 |
|
1578 maybe_screwed_again--; |
|
1579 |
1229
|
1580 tree_return_list *id_list = m->to_return_list (); |
666
|
1581 |
|
1582 if (id_list) |
|
1583 { |
|
1584 int list_len = id_list->length (); |
|
1585 |
|
1586 if (list_len == 1) |
|
1587 { |
|
1588 tree_index_expression *lhs = id_list->remove_front (); |
947
|
1589 retval = new tree_simple_assignment_expression (lhs, rhs, |
|
1590 0, 0, l, c); |
666
|
1591 |
|
1592 } |
|
1593 else if (list_len > 1) |
|
1594 { |
|
1595 if (rhs->is_multi_val_ret_expression ()) |
|
1596 { |
|
1597 tree_multi_val_ret *t = (tree_multi_val_ret *) rhs; |
1229
|
1598 retval = new tree_multi_assignment_expression (id_list, t, |
|
1599 0, l, c); |
666
|
1600 } |
|
1601 else |
1005
|
1602 yyerror ("RHS must be an expression that returns multiple values"); |
666
|
1603 } |
|
1604 else |
|
1605 panic_impossible (); |
|
1606 } |
|
1607 else |
1005
|
1608 yyerror ("invalid identifier list for assignment"); |
666
|
1609 |
|
1610 return retval; |
|
1611 } |
751
|
1612 |
|
1613 static tree_index_expression * |
|
1614 make_index_expression (tree_indirect_ref *indir, tree_argument_list *args) |
|
1615 { |
|
1616 tree_index_expression *retval = 0; |
|
1617 |
|
1618 int l = indir->line (); |
|
1619 int c = indir->column (); |
|
1620 |
|
1621 if (indir->is_identifier_only ()) |
|
1622 { |
|
1623 indir->preserve_identifier (); |
|
1624 retval = new tree_index_expression (indir->ident (), args, l, c); |
|
1625 delete indir; |
|
1626 } |
|
1627 else |
|
1628 retval = new tree_index_expression (indir, args, l, c); |
|
1629 |
|
1630 return retval; |
|
1631 } |