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 |
|
1149 rows : matrix_row |
|
1150 | rows ';' // Ignore trailing semicolon. |
|
1151 | rows ';' matrix_row |
|
1152 ; |
|
1153 |
|
1154 matrix_row : expression // First element on row. |
|
1155 { |
|
1156 if (mlnm.top ()) |
|
1157 { |
|
1158 mlnm.pop (); |
|
1159 mlnm.push (0); |
578
|
1160 tree_matrix *tmp = new tree_matrix |
|
1161 ($1, tree_matrix::md_none); |
1
|
1162 ml.push (tmp); |
|
1163 } |
|
1164 else |
|
1165 { |
|
1166 tree_matrix *tmp = ml.pop (); |
578
|
1167 tmp = tmp->chain ($1, tree_matrix::md_down); |
1
|
1168 ml.push (tmp); |
|
1169 } |
|
1170 } |
|
1171 | matrix_row ',' // Ignore trailing comma. |
|
1172 | matrix_row ',' expression |
|
1173 { |
|
1174 tree_matrix *tmp = ml.pop (); |
578
|
1175 tmp = tmp->chain ($3, tree_matrix::md_right); |
1
|
1176 ml.push (tmp); |
|
1177 } |
|
1178 ; |
|
1179 |
|
1180 %% |
|
1181 |
666
|
1182 // Generic error messages. |
|
1183 |
1
|
1184 static void |
|
1185 yyerror (char *s) |
|
1186 { |
|
1187 char *line = current_input_line; |
143
|
1188 int err_col = current_input_column - 1; |
1
|
1189 |
581
|
1190 ostrstream output_buf; |
1
|
1191 |
1005
|
1192 if (reading_fcn_file || reading_script_file) |
|
1193 output_buf << "parse error near line " << input_line_number |
|
1194 << " of file " << curr_fcn_file_name << ".m:"; |
|
1195 else |
|
1196 output_buf << "parse error:"; |
581
|
1197 |
1005
|
1198 if (s && strcmp (s, "parse error") != 0) |
|
1199 output_buf << "\n\n " << s; |
|
1200 |
|
1201 output_buf << "\n\n"; |
1
|
1202 |
522
|
1203 if (line) |
1
|
1204 { |
335
|
1205 int len = strlen (line); |
1060
|
1206 |
335
|
1207 if (line[len-1] == '\n') |
|
1208 { |
|
1209 len--; |
|
1210 line[len] = '\0'; |
|
1211 } |
1005
|
1212 |
335
|
1213 // Print the line, maybe with a pointer near the error token. |
1005
|
1214 |
1060
|
1215 output_buf << ">>> " << line << "\n"; |
|
1216 |
|
1217 if (err_col == 0) |
|
1218 err_col = len; |
|
1219 |
|
1220 for (int i = 0; i < err_col + 3; i++) |
|
1221 output_buf << " "; |
|
1222 |
|
1223 output_buf << "^"; |
1
|
1224 } |
1005
|
1225 |
|
1226 output_buf << "\n" << ends; |
581
|
1227 |
1005
|
1228 char *msg = output_buf.str (); |
|
1229 |
|
1230 parse_error (msg); |
|
1231 |
|
1232 delete [] msg; |
1
|
1233 } |
|
1234 |
666
|
1235 // Error mesages for mismatched end tokens. |
|
1236 |
496
|
1237 static void |
|
1238 end_error (char *type, token::end_tok_type ettype, int l, int c) |
|
1239 { |
769
|
1240 static char *fmt = "`%s' command matched by `%s' near line %d column %d"; |
496
|
1241 |
|
1242 switch (ettype) |
|
1243 { |
|
1244 case token::simple_end: |
|
1245 error (fmt, type, "end", l, c); |
|
1246 break; |
777
|
1247 |
496
|
1248 case token::for_end: |
|
1249 error (fmt, type, "endfor", l, c); |
|
1250 break; |
777
|
1251 |
496
|
1252 case token::function_end: |
|
1253 error (fmt, type, "endfunction", l, c); |
|
1254 break; |
777
|
1255 |
496
|
1256 case token::if_end: |
|
1257 error (fmt, type, "endif", l, c); |
|
1258 break; |
777
|
1259 |
496
|
1260 case token::while_end: |
|
1261 error (fmt, type, "endwhile", l, c); |
|
1262 break; |
777
|
1263 |
496
|
1264 default: |
|
1265 panic_impossible (); |
|
1266 break; |
|
1267 } |
|
1268 } |
|
1269 |
666
|
1270 // Check to see that end tokens are properly matched. |
|
1271 |
143
|
1272 static int |
|
1273 check_end (token *tok, token::end_tok_type expected) |
|
1274 { |
|
1275 token::end_tok_type ettype = tok->ettype (); |
|
1276 if (ettype != expected && ettype != token::simple_end) |
|
1277 { |
|
1278 yyerror ("parse error"); |
|
1279 |
|
1280 int l = tok->line (); |
|
1281 int c = tok->column (); |
|
1282 |
|
1283 switch (expected) |
|
1284 { |
|
1285 case token::for_end: |
|
1286 end_error ("for", ettype, l, c); |
|
1287 break; |
777
|
1288 |
143
|
1289 case token::function_end: |
|
1290 end_error ("function", ettype, l, c); |
|
1291 break; |
777
|
1292 |
143
|
1293 case token::if_end: |
|
1294 end_error ("if", ettype, l, c); |
|
1295 break; |
777
|
1296 |
143
|
1297 case token::while_end: |
|
1298 end_error ("while", ettype, l, c); |
|
1299 break; |
777
|
1300 |
143
|
1301 default: |
|
1302 panic_impossible (); |
|
1303 break; |
|
1304 } |
|
1305 return 1; |
|
1306 } |
|
1307 else |
|
1308 return 0; |
|
1309 } |
|
1310 |
666
|
1311 // Try to figure out early if an expression should become an |
|
1312 // assignment to the builtin variable ans. |
|
1313 // |
|
1314 // Need to make sure that the expression isn't already an identifier |
|
1315 // that has a name, or an assignment expression. |
|
1316 // |
|
1317 // Note that an expression can't be just an identifier anymore -- it |
|
1318 // must at least be an index expression (see the definition of the |
|
1319 // non-terminal `variable' above). |
|
1320 // |
|
1321 // XXX FIXME XXX. This isn't quite sufficient. For example, try the |
|
1322 // command `x = 4, x' for `x' previously undefined. |
|
1323 // |
|
1324 // XXX FIXME XXX -- we should probably delay doing this until eval-time. |
|
1325 |
496
|
1326 static tree_expression * |
|
1327 maybe_convert_to_ans_assign (tree_expression *expr) |
1
|
1328 { |
|
1329 if (expr->is_index_expression ()) |
|
1330 { |
195
|
1331 expr->mark_for_possible_ans_assign (); |
|
1332 return expr; |
|
1333 } |
|
1334 else if (expr->is_assignment_expression () |
|
1335 || expr->is_prefix_expression ()) |
|
1336 { |
|
1337 return expr; |
|
1338 } |
|
1339 else |
|
1340 { |
|
1341 symbol_record *sr = global_sym_tab->lookup ("ans", 1, 0); |
1
|
1342 |
522
|
1343 assert (sr); |
195
|
1344 |
|
1345 tree_identifier *ans = new tree_identifier (sr); |
|
1346 |
581
|
1347 return new tree_simple_assignment_expression (ans, expr, 0, 1); |
1
|
1348 } |
|
1349 } |
|
1350 |
666
|
1351 // Maybe print a warning if an assignment expression is used as the |
|
1352 // test in a logical expression. |
|
1353 |
496
|
1354 static void |
|
1355 maybe_warn_assign_as_truth_value (tree_expression *expr) |
1
|
1356 { |
|
1357 if (user_pref.warn_assign_as_truth_value |
|
1358 && expr->is_assignment_expression () |
581
|
1359 && expr->in_parens < 2) |
1
|
1360 { |
|
1361 warning ("suggest parenthesis around assignment used as truth value"); |
|
1362 } |
|
1363 } |
578
|
1364 |
666
|
1365 // Build a binary expression. |
|
1366 |
578
|
1367 static tree_expression * |
|
1368 make_binary_op (int op, tree_expression *op1, token *tok_val, |
|
1369 tree_expression *op2) |
|
1370 { |
|
1371 tree_expression::type t; |
|
1372 switch (op) |
|
1373 { |
|
1374 case POW: |
|
1375 t = tree_expression::power; |
|
1376 break; |
777
|
1377 |
578
|
1378 case EPOW: |
|
1379 t = tree_expression::elem_pow; |
|
1380 break; |
777
|
1381 |
578
|
1382 case '+': |
|
1383 t = tree_expression::add; |
|
1384 break; |
777
|
1385 |
578
|
1386 case '-': |
|
1387 t = tree_expression::subtract; |
|
1388 break; |
777
|
1389 |
578
|
1390 case '*': |
|
1391 t = tree_expression::multiply; |
|
1392 break; |
777
|
1393 |
578
|
1394 case '/': |
|
1395 t = tree_expression::divide; |
|
1396 break; |
777
|
1397 |
578
|
1398 case EMUL: |
|
1399 t = tree_expression::el_mul; |
|
1400 break; |
777
|
1401 |
578
|
1402 case EDIV: |
|
1403 t = tree_expression::el_div; |
|
1404 break; |
777
|
1405 |
578
|
1406 case LEFTDIV: |
|
1407 t = tree_expression::leftdiv; |
|
1408 break; |
777
|
1409 |
578
|
1410 case ELEFTDIV: |
|
1411 t = tree_expression::el_leftdiv; |
|
1412 break; |
777
|
1413 |
578
|
1414 case EXPR_LT: |
|
1415 t = tree_expression::cmp_lt; |
|
1416 break; |
777
|
1417 |
578
|
1418 case EXPR_LE: |
|
1419 t = tree_expression::cmp_le; |
|
1420 break; |
777
|
1421 |
578
|
1422 case EXPR_EQ: |
|
1423 t = tree_expression::cmp_eq; |
|
1424 break; |
777
|
1425 |
578
|
1426 case EXPR_GE: |
|
1427 t = tree_expression::cmp_ge; |
|
1428 break; |
777
|
1429 |
578
|
1430 case EXPR_GT: |
|
1431 t = tree_expression::cmp_gt; |
|
1432 break; |
777
|
1433 |
578
|
1434 case EXPR_NE: |
|
1435 t = tree_expression::cmp_ne; |
|
1436 break; |
777
|
1437 |
578
|
1438 case EXPR_AND_AND: |
|
1439 t = tree_expression::and_and; |
|
1440 break; |
777
|
1441 |
578
|
1442 case EXPR_OR_OR: |
|
1443 t = tree_expression::or_or; |
|
1444 break; |
777
|
1445 |
578
|
1446 case EXPR_AND: |
|
1447 t = tree_expression::and; |
|
1448 break; |
777
|
1449 |
578
|
1450 case EXPR_OR: |
|
1451 t = tree_expression::or; |
|
1452 break; |
777
|
1453 |
578
|
1454 default: |
|
1455 panic_impossible (); |
|
1456 break; |
|
1457 } |
|
1458 |
|
1459 int l = tok_val->line (); |
|
1460 int c = tok_val->column (); |
|
1461 |
|
1462 return new tree_binary_expression (op1, op2, t, l, c); |
|
1463 } |
|
1464 |
666
|
1465 // Build a prefix expression. |
|
1466 |
578
|
1467 static tree_expression * |
|
1468 make_prefix_op (int op, tree_identifier *op1, token *tok_val) |
|
1469 { |
|
1470 tree_expression::type t; |
|
1471 switch (op) |
|
1472 { |
|
1473 case PLUS_PLUS: |
|
1474 t = tree_expression::increment; |
|
1475 break; |
777
|
1476 |
578
|
1477 case MINUS_MINUS: |
|
1478 t = tree_expression::decrement; |
|
1479 break; |
777
|
1480 |
578
|
1481 default: |
|
1482 panic_impossible (); |
|
1483 break; |
|
1484 } |
|
1485 |
|
1486 int l = tok_val->line (); |
|
1487 int c = tok_val->column (); |
|
1488 |
|
1489 return new tree_prefix_expression (op1, t, l, c); |
|
1490 } |
|
1491 |
666
|
1492 // Build a postfix expression. |
|
1493 |
578
|
1494 static tree_expression * |
|
1495 make_postfix_op (int op, tree_identifier *op1, token *tok_val) |
|
1496 { |
|
1497 tree_expression::type t; |
|
1498 switch (op) |
|
1499 { |
|
1500 case PLUS_PLUS: |
|
1501 t = tree_expression::increment; |
|
1502 break; |
777
|
1503 |
578
|
1504 case MINUS_MINUS: |
|
1505 t = tree_expression::decrement; |
|
1506 break; |
777
|
1507 |
578
|
1508 default: |
|
1509 panic_impossible (); |
|
1510 break; |
|
1511 } |
|
1512 |
|
1513 int l = tok_val->line (); |
|
1514 int c = tok_val->column (); |
|
1515 |
|
1516 return new tree_postfix_expression (op1, t, l, c); |
|
1517 } |
|
1518 |
666
|
1519 // Build a unary expression. |
|
1520 |
578
|
1521 static tree_expression * |
|
1522 make_unary_op (int op, tree_expression *op1, token *tok_val) |
|
1523 { |
|
1524 tree_expression::type t; |
|
1525 switch (op) |
|
1526 { |
|
1527 case QUOTE: |
|
1528 t = tree_expression::hermitian; |
|
1529 break; |
777
|
1530 |
578
|
1531 case TRANSPOSE: |
|
1532 t = tree_expression::transpose; |
|
1533 break; |
777
|
1534 |
578
|
1535 case EXPR_NOT: |
|
1536 t = tree_expression::not; |
|
1537 break; |
777
|
1538 |
578
|
1539 case '-': |
|
1540 t = tree_expression::uminus; |
|
1541 break; |
777
|
1542 |
578
|
1543 default: |
|
1544 panic_impossible (); |
|
1545 break; |
|
1546 } |
|
1547 |
|
1548 int l = tok_val->line (); |
|
1549 int c = tok_val->column (); |
|
1550 |
|
1551 return new tree_unary_expression (op1, t, l, c); |
|
1552 } |
666
|
1553 |
|
1554 // Make an expression that handles assignment of multiple values. |
|
1555 |
|
1556 static tree_expression * |
|
1557 make_multi_val_ret (tree_expression *rhs, int l, int c) |
|
1558 { |
|
1559 // Convert the matrix list to a list of identifiers. If that fails, |
|
1560 // we can abort here, without losing anything -- no other possible |
|
1561 // syntax is valid if we've seen the equals sign as the next token |
|
1562 // after the `]'. |
|
1563 |
|
1564 tree_expression *retval = 0; |
|
1565 |
|
1566 maybe_screwed_again--; |
|
1567 |
|
1568 tree_matrix *tmp = ml.pop (); |
|
1569 |
|
1570 tmp = tmp->reverse (); |
|
1571 |
|
1572 tree_return_list *id_list = tmp->to_return_list (); |
|
1573 |
|
1574 if (id_list) |
|
1575 { |
|
1576 int list_len = id_list->length (); |
|
1577 |
|
1578 if (list_len == 1) |
|
1579 { |
|
1580 tree_index_expression *lhs = id_list->remove_front (); |
947
|
1581 retval = new tree_simple_assignment_expression (lhs, rhs, |
|
1582 0, 0, l, c); |
666
|
1583 |
|
1584 } |
|
1585 else if (list_len > 1) |
|
1586 { |
|
1587 if (rhs->is_multi_val_ret_expression ()) |
|
1588 { |
|
1589 tree_multi_val_ret *t = (tree_multi_val_ret *) rhs; |
|
1590 retval = new tree_multi_assignment_expression (id_list, t, l, c); |
|
1591 } |
|
1592 else |
1005
|
1593 yyerror ("RHS must be an expression that returns multiple values"); |
666
|
1594 } |
|
1595 else |
|
1596 panic_impossible (); |
|
1597 } |
|
1598 else |
1005
|
1599 yyerror ("invalid identifier list for assignment"); |
666
|
1600 |
|
1601 return retval; |
|
1602 } |
751
|
1603 |
|
1604 static tree_index_expression * |
|
1605 make_index_expression (tree_indirect_ref *indir, tree_argument_list *args) |
|
1606 { |
|
1607 tree_index_expression *retval = 0; |
|
1608 |
|
1609 int l = indir->line (); |
|
1610 int c = indir->column (); |
|
1611 |
|
1612 if (indir->is_identifier_only ()) |
|
1613 { |
|
1614 indir->preserve_identifier (); |
|
1615 retval = new tree_index_expression (indir->ident (), args, l, c); |
|
1616 delete indir; |
|
1617 } |
|
1618 else |
|
1619 retval = new tree_index_expression (indir, args, l, c); |
|
1620 |
|
1621 return retval; |
|
1622 } |