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