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