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