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