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