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