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