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