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