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