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