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