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