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. |
191
|
219 %token FCN SCREW_TWO |
206
|
220 %token ELLIPSIS |
|
221 %token END_OF_INPUT |
1
|
222 %token USING TITLE WITH COLON OPEN_BRACE CLOSE_BRACE |
|
223 |
143
|
224 // Nonterminals we construct. |
578
|
225 %type <tree_type> input |
496
|
226 %type <tree_expression_type> expression simple_expr simple_expr1 |
|
227 %type <tree_expression_type> ans_expression title |
1
|
228 %type <tree_matrix_type> matrix |
|
229 %type <tree_identifier_type> identifier |
578
|
230 %type <tree_function_type> func_def1 func_def2 func_def3 |
482
|
231 %type <tree_index_expression_type> variable word_list_cmd |
1
|
232 %type <tree_colon_expression_type> colon_expr |
578
|
233 %type <tree_argument_list_type> arg_list word_list |
1
|
234 %type <tree_parameter_list_type> param_list param_list1 func_def1a |
578
|
235 %type <tree_command_type> command func_def |
|
236 %type <tree_if_command_type> if_command |
|
237 %type <tree_if_clause_type> elseif_clause else_clause |
|
238 %type <tree_if_command_list_type> if_cmd_list1 if_cmd_list |
|
239 %type <tree_global_type> global_decl2 |
|
240 %type <tree_global_init_list_type> global_decl1 |
|
241 %type <tree_global_command_type> global_decl |
|
242 %type <tree_statement_type> statement |
|
243 %type <tree_statement_list_type> simple_list simple_list1 list list1 opt_list |
1
|
244 %type <tree_plot_command_type> plot_command |
578
|
245 %type <subplot_type> plot_command2 plot_options |
|
246 %type <subplot_list_type> plot_command1 |
|
247 %type <plot_limits_type> ranges |
|
248 %type <plot_range_type> ranges1 |
|
249 %type <subplot_using_type> using using1 |
|
250 %type <subplot_style_type> style |
1
|
251 |
143
|
252 // Precedence and associativity. |
1
|
253 %left ';' ',' '\n' |
|
254 %right '=' |
428
|
255 %left EXPR_AND_AND EXPR_OR_OR |
1
|
256 %left EXPR_AND EXPR_OR |
|
257 %left EXPR_LT EXPR_LE EXPR_EQ EXPR_NE EXPR_GE EXPR_GT |
|
258 %left ':' |
|
259 %left '-' '+' |
|
260 %left '*' '/' LEFTDIV EMUL EDIV ELEFTDIV |
|
261 %left QUOTE TRANSPOSE |
|
262 %left UNARY PLUS_PLUS MINUS_MINUS EXPR_NOT |
|
263 %right POW EPOW |
|
264 |
169
|
265 // There are 19 shift/reduce conflicts, ok? |
|
266 %expect 19 |
143
|
267 |
|
268 // Where to start. |
1
|
269 %start input |
|
270 |
|
271 /* |
|
272 * Grammar rules. |
|
273 */ |
|
274 %% |
|
275 |
|
276 input : '\n' |
|
277 { |
522
|
278 global_command = 0; |
1
|
279 promptflag = 1; |
|
280 YYACCEPT; |
|
281 } |
|
282 | END_OF_INPUT |
|
283 { |
522
|
284 global_command = 0; |
1
|
285 promptflag = 1; |
|
286 YYABORT; |
|
287 } |
327
|
288 | simple_list |
|
289 { |
|
290 global_command = $1; |
|
291 promptflag = 1; |
|
292 YYACCEPT; |
|
293 } |
1
|
294 | simple_list '\n' |
|
295 { |
|
296 global_command = $1; |
|
297 promptflag = 1; |
|
298 YYACCEPT; |
|
299 } |
|
300 | simple_list END_OF_INPUT |
|
301 { |
|
302 global_command = $1; |
|
303 promptflag = 1; |
|
304 YYACCEPT; |
|
305 } |
|
306 | error |
|
307 { ABORT_PARSE; } |
|
308 | error '\n' |
|
309 { ABORT_PARSE; } |
|
310 | error END_OF_INPUT |
|
311 { ABORT_PARSE; } |
|
312 | simple_list error |
|
313 { ABORT_PARSE; } |
327
|
314 | simple_list error '\n' |
|
315 { ABORT_PARSE; } |
1
|
316 | simple_list error END_OF_INPUT |
|
317 { ABORT_PARSE; } |
|
318 ; |
|
319 |
|
320 simple_list : semi_comma |
522
|
321 { $$ = 0; } |
1
|
322 | comma_semi |
522
|
323 { $$ = 0; } |
1
|
324 | simple_list1 |
578
|
325 { $$ = $1; } |
1
|
326 | simple_list1 semi_comma |
|
327 { |
578
|
328 tree_statement *tmp = $1->rear (); |
|
329 tmp->set_print_flag (0); |
1
|
330 } |
|
331 | simple_list1 comma_semi |
578
|
332 { $$ = $1; } |
1
|
333 ; |
|
334 |
578
|
335 simple_list1 : statement |
|
336 { $$ = new tree_statement_list ($1); } |
|
337 | semi_comma statement |
|
338 { $$ = new tree_statement_list ($2); } |
|
339 | comma_semi statement |
|
340 { $$ = new tree_statement_list ($2); } |
|
341 | simple_list1 semi_comma statement |
1
|
342 { |
578
|
343 tree_statement *tmp = $1->rear (); |
|
344 tmp->set_print_flag (0); |
|
345 $1->append ($3); |
1
|
346 } |
578
|
347 | simple_list1 comma_semi statement |
|
348 { $1->append ($3); } |
1
|
349 ; |
|
350 |
|
351 semi_comma : ';' |
|
352 | semi_comma ',' |
|
353 | semi_comma ';' |
|
354 ; |
|
355 |
|
356 comma_semi : ',' |
|
357 | comma_semi ';' |
|
358 | comma_semi ',' |
|
359 ; |
|
360 |
|
361 comma_nl_sep : ',' |
|
362 | '\n' |
|
363 | comma_nl_sep sep |
|
364 ; |
|
365 |
|
366 semi_sep : ';' |
|
367 | semi_sep sep |
|
368 ; |
|
369 |
|
370 opt_list : // empty |
578
|
371 { $$ = new tree_statement_list (); } |
1
|
372 | list |
|
373 { $$ = $1; } |
496
|
374 ; |
1
|
375 |
|
376 list : list1 |
578
|
377 { $$ = $1; } |
1
|
378 | list1 comma_nl_sep |
578
|
379 { $$ = $1; } |
1
|
380 | list1 semi_sep |
|
381 { |
578
|
382 tree_statement *tmp = $1->rear (); |
|
383 tmp->set_print_flag (0); |
1
|
384 } |
|
385 ; |
|
386 |
578
|
387 list1 : statement |
440
|
388 { |
|
389 beginning_of_function = 0; |
578
|
390 $$ = new tree_statement_list ($1); |
440
|
391 } |
578
|
392 | list1 comma_nl_sep statement |
|
393 { $1->append ($3); } |
|
394 | list1 semi_sep statement |
1
|
395 { |
578
|
396 tree_statement *tmp = $1->rear (); |
|
397 tmp->set_print_flag (0); |
|
398 $1->append ($3); |
1
|
399 } |
|
400 ; |
|
401 |
578
|
402 statement : command |
|
403 { $$ = new tree_statement ($1); } |
1
|
404 | ans_expression |
578
|
405 { $$ = new tree_statement ($1); } |
1
|
406 ; |
|
407 |
|
408 plot_command : PLOT plot_command1 |
|
409 { |
578
|
410 if (! $2 && $1->pttype () != token::replot) |
478
|
411 { |
|
412 yyerror ("must have something to plot"); |
|
413 ABORT_PARSE; |
|
414 } |
|
415 else |
|
416 { |
578
|
417 $$ = new tree_plot_command ($2, $1->pttype ()); |
478
|
418 plotting = 0; |
|
419 past_plot_range = 0; |
|
420 in_plot_range = 0; |
|
421 in_plot_using = 0; |
|
422 in_plot_style = 0; |
|
423 } |
1
|
424 } |
|
425 | PLOT ranges plot_command1 |
|
426 { |
476
|
427 if ($1->pttype () == token::replot) |
|
428 { |
|
429 yyerror ("cannot specify new ranges with replot"); |
|
430 ABORT_PARSE; |
|
431 } |
|
432 else |
|
433 { |
578
|
434 $$ = new tree_plot_command ($3, $2, $1->pttype ()); |
476
|
435 plotting = 0; |
|
436 past_plot_range = 0; |
|
437 in_plot_range = 0; |
|
438 in_plot_using = 0; |
|
439 in_plot_style = 0; |
|
440 } |
1
|
441 } |
|
442 ; |
|
443 |
|
444 ranges : ranges1 |
578
|
445 { $$ = new plot_limits ($1); } |
1
|
446 | ranges1 ranges1 |
578
|
447 { $$ = new plot_limits ($1, $2); } |
1
|
448 | ranges1 ranges1 ranges1 |
578
|
449 { $$ = new plot_limits ($1, $2, $3); } |
1
|
450 ; |
|
451 |
|
452 ranges1 : OPEN_BRACE expression COLON expression CLOSE_BRACE |
578
|
453 { $$ = new plot_range ($2, $4); } |
1
|
454 | OPEN_BRACE COLON expression CLOSE_BRACE |
578
|
455 { $$ = new plot_range (0, $3); } |
1
|
456 | OPEN_BRACE expression COLON CLOSE_BRACE |
578
|
457 { $$ = new plot_range ($2, 0); } |
1
|
458 | OPEN_BRACE COLON CLOSE_BRACE |
578
|
459 { $$ = new plot_range (); } |
1
|
460 | OPEN_BRACE CLOSE_BRACE |
578
|
461 { $$ = new plot_range (); } |
1
|
462 ; |
|
463 |
478
|
464 plot_command1 : // empty |
522
|
465 { $$ = 0; } |
478
|
466 | plot_command2 |
578
|
467 { $$ = new subplot_list ($1); } |
1
|
468 | plot_command1 ',' plot_command2 |
578
|
469 { $1->append ($3); } |
1
|
470 ; |
|
471 |
|
472 plot_command2 : expression |
578
|
473 { $$ = new subplot ($1); } |
1
|
474 | expression plot_options |
578
|
475 { |
|
476 $2->set_data ($1); |
|
477 $$ = $2; |
|
478 } |
1
|
479 ; |
|
480 |
|
481 plot_options : using |
578
|
482 { $$ = new subplot ($1, 0, 0); } |
1
|
483 | title |
578
|
484 { $$ = new subplot (0, $1, 0); } |
1
|
485 | style |
578
|
486 { $$ = new subplot (0, 0, $1); } |
1
|
487 | using title |
578
|
488 { $$ = new subplot ($1, $2, 0); } |
|
489 | title using |
|
490 { $$ = new subplot ($2, $1, 0); } |
|
491 | using style |
|
492 { $$ = new subplot ($1, 0, $2); } |
|
493 | style using |
|
494 { $$ = new subplot ($2, 0, $1); } |
|
495 | title style |
|
496 { $$ = new subplot (0, $1, $2); } |
|
497 | style title |
|
498 { $$ = new subplot (0, $2, $1); } |
|
499 | using title style |
|
500 { $$ = new subplot ($1, $2, $3); } |
|
501 | using style title |
|
502 { $$ = new subplot ($1, $3, $2); } |
|
503 | title using style |
|
504 { $$ = new subplot ($2, $1, $3); } |
|
505 | title style using |
|
506 { $$ = new subplot ($3, $1, $2); } |
|
507 | style using title |
|
508 { $$ = new subplot ($2, $3, $1); } |
|
509 | style title using |
|
510 { $$ = new subplot ($3, $2, $1); } |
1
|
511 ; |
|
512 |
|
513 using : using1 |
|
514 { |
496
|
515 in_plot_using = 0; |
1
|
516 $$ = $1; |
|
517 } |
|
518 | using1 expression |
|
519 { |
496
|
520 in_plot_using = 0; |
1
|
521 $$ = $1->set_format ($2); |
|
522 } |
|
523 ; |
|
524 |
|
525 using1 : USING expression |
|
526 { |
578
|
527 subplot_using *tmp = new subplot_using (); |
1
|
528 $$ = tmp->add_qualifier ($2); |
|
529 } |
|
530 | using1 COLON expression |
|
531 { $$ = $1->add_qualifier ($3); } |
|
532 ; |
|
533 |
|
534 title : TITLE expression |
|
535 { $$ = $2; } |
|
536 ; |
|
537 |
|
538 style : WITH STYLE |
578
|
539 { $$ = new subplot_style ($2->string ()); } |
1
|
540 | WITH STYLE expression |
578
|
541 { $$ = new subplot_style ($2->string (), $3); } |
1
|
542 | WITH STYLE expression bogus_syntax expression |
578
|
543 { $$ = new subplot_style ($2->string (), $3, $5); } |
1
|
544 ; |
|
545 |
|
546 bogus_syntax : // empty |
|
547 ; |
|
548 |
|
549 ans_expression : expression |
|
550 { $$ = maybe_convert_to_ans_assign ($1); } |
|
551 ; |
|
552 |
|
553 global_decl : GLOBAL global_decl1 |
578
|
554 { |
|
555 $$ = new tree_global_command ($2, $1->line (), |
|
556 $1->column ()); |
|
557 } |
1
|
558 ; |
|
559 |
578
|
560 global_decl1 : global_decl2 |
|
561 { $$ = new tree_global_init_list ($1); } |
|
562 | global_decl1 optcomma global_decl2 |
|
563 { $1->append ($3); } |
|
564 |
|
565 global_decl2 : identifier |
|
566 { $$ = new tree_global ($1); } |
|
567 | identifier '=' expression |
1
|
568 { |
578
|
569 tree_simple_assignment_expression *tmp_ass; |
|
570 tmp_ass = new tree_simple_assignment_expression |
581
|
571 ($1, $3, 0, 0, $2->line (), $2->column ()); |
578
|
572 $$ = new tree_global (tmp_ass); |
1
|
573 } |
|
574 ; |
|
575 |
|
576 optcomma : // empty |
|
577 | ',' |
|
578 { |
|
579 if (user_pref.warn_comma_in_global_decl) |
|
580 warning ("comma in global declaration not\ |
|
581 interpreted as a command separator"); |
|
582 } |
|
583 ; |
|
584 |
578
|
585 command : plot_command |
|
586 { $$ = $1; } |
|
587 | func_def |
|
588 { $$ = $1; } |
|
589 | global_decl |
|
590 { $$ = $1; } |
|
591 | if_command |
|
592 { |
|
593 iffing--; |
|
594 $$ = $1; |
|
595 } |
|
596 | WHILE expression optsep opt_list END |
1
|
597 { |
|
598 maybe_warn_assign_as_truth_value ($2); |
143
|
599 if (check_end ($5, token::while_end)) |
|
600 ABORT_PARSE; |
1
|
601 looping--; |
191
|
602 $$ = new tree_while_command ($2, $4, $1->line (), |
|
603 $1->column ()); |
1
|
604 } |
118
|
605 | FOR variable '=' expression optsep opt_list END |
1
|
606 { |
143
|
607 if (check_end ($7, token::for_end)) |
|
608 ABORT_PARSE; |
1
|
609 looping--; |
191
|
610 $$ = new tree_for_command ($2, $4, $6, |
|
611 $1->line (), $1->column ()); |
1
|
612 } |
|
613 | BREAK |
|
614 { |
|
615 if (!looping) |
|
616 { |
|
617 yyerror ("parse error"); |
|
618 error ("break: only meaningful within a `for'\ |
|
619 or `while' loop"); |
|
620 ABORT_PARSE; |
|
621 } |
191
|
622 $$ = new tree_break_command ($1->line (), $1->column ()); |
1
|
623 } |
|
624 | CONTINUE |
|
625 { |
|
626 if (!looping) |
|
627 { |
|
628 yyerror ("parse error"); |
|
629 error ("continue: only meaningful within a\ |
|
630 `for' or `while' loop"); |
|
631 ABORT_PARSE; |
|
632 } |
191
|
633 $$ = new tree_continue_command ($1->line (), |
|
634 $1->column ()); |
1
|
635 } |
|
636 | FUNC_RET |
|
637 { |
|
638 if (!defining_func) |
|
639 { |
|
640 yyerror ("parse error"); |
|
641 error ("return: only meaningful within a function"); |
|
642 ABORT_PARSE; |
|
643 } |
191
|
644 $$ = new tree_return_command ($1->line (), $1->column ()); |
1
|
645 } |
|
646 ; |
|
647 |
578
|
648 if_command : IF if_cmd_list END |
|
649 { |
|
650 if (check_end ($3, token::if_end)) |
|
651 ABORT_PARSE; |
|
652 $$ = new tree_if_command ($2, $1->line (), $1->column ()); |
|
653 } |
|
654 ; |
|
655 |
|
656 if_cmd_list : if_cmd_list1 |
|
657 { $$ = $1 } |
|
658 | if_cmd_list1 else_clause |
|
659 { $1->append ($2); } |
|
660 ; |
|
661 |
|
662 if_cmd_list1 : expression optsep opt_list |
|
663 { |
|
664 maybe_warn_assign_as_truth_value ($1); |
|
665 tree_if_clause *t = new tree_if_clause ($1, $3); |
|
666 $$ = new tree_if_command_list (t); |
|
667 } |
|
668 | if_cmd_list1 elseif_clause |
|
669 { $1->append ($2); } |
|
670 ; |
|
671 |
|
672 elseif_clause : ELSEIF optsep expression optsep opt_list |
1
|
673 { |
|
674 maybe_warn_assign_as_truth_value ($3); |
578
|
675 $$ = new tree_if_clause ($3, $5); |
1
|
676 } |
578
|
677 ; |
|
678 |
|
679 else_clause : ELSE optsep opt_list |
|
680 { $$ = new tree_if_clause ($3); } |
1
|
681 ; |
|
682 |
|
683 optsep : // empty |
|
684 | sep |
|
685 ; |
|
686 |
|
687 sep : ',' |
|
688 | ';' |
|
689 | '\n' |
|
690 | sep ',' |
|
691 | sep ';' |
|
692 | sep '\n' |
|
693 ; |
|
694 |
|
695 screwed_again : // empty |
|
696 { maybe_screwed_again++; } |
|
697 ; |
|
698 |
|
699 expression : variable '=' expression |
143
|
700 { $$ = new tree_simple_assignment_expression |
581
|
701 ($1, $3, 0, 0, $2->line (), $2->column ()); } |
1
|
702 | '[' screwed_again matrix_row SCREW_TWO '=' expression |
|
703 { |
|
704 |
|
705 // Will need a way to convert the matrix list to a list of |
143
|
706 // identifiers. If that fails, we can abort here, without losing |
1
|
707 // anything -- no other possible syntax is valid if we've seen the |
|
708 // equals sign as the next token after the `]'. |
|
709 |
522
|
710 $$ = 0; |
1
|
711 maybe_screwed_again--; |
|
712 tree_matrix *tmp = ml.pop (); |
|
713 tmp = tmp->reverse (); |
|
714 tree_return_list *id_list = tmp->to_return_list (); |
522
|
715 if (id_list) |
|
716 { |
|
717 $$ = new tree_multi_assignment_expression |
|
718 (id_list, $6, $5->line (), $5->column ()); |
|
719 } |
|
720 else |
1
|
721 { |
|
722 yyerror ("parse error"); |
143
|
723 error ("invalid identifier list for assignment"); |
522
|
724 $$ = 0; |
1
|
725 ABORT_PARSE; |
|
726 } |
|
727 } |
|
728 | NUM '=' expression |
|
729 { |
|
730 yyerror ("parse error"); |
|
731 error ("invalid assignment to a number"); |
522
|
732 $$ = 0; |
1
|
733 ABORT_PARSE; |
|
734 } |
|
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 } |
|
914 | func_def1a ']' g_symtab '=' func_def2 |
|
915 { |
578
|
916 $1->mark_as_formal_parameters (); |
|
917 $$ = $5->define_ret_list ($1); |
1
|
918 } |
|
919 ; |
|
920 |
|
921 func_def1a : '[' safe local_symtab identifier |
|
922 { $$ = new tree_parameter_list ($4); } |
|
923 | func_def1a ',' identifier |
578
|
924 { $1->append ($3); } |
1
|
925 ; |
|
926 |
|
927 func_def2 : identifier safe local_symtab func_def3 |
|
928 { |
|
929 char *id_name = $1->name (); |
|
930 // if (is_text_function_name (id_name)) |
|
931 // { |
|
932 // yyerror ("parse error"); |
|
933 // error ("invalid use of reserved word %s", id_name); |
|
934 // ABORT_PARSE; |
|
935 // } |
|
936 |
|
937 // If input is coming from a file, issue a warning if the name of the |
|
938 // file does not match the name of the function stated in the file. |
|
939 // Matlab doesn't provide a diagnostic (it ignores the stated name). |
|
940 |
338
|
941 if (reading_fcn_file) |
1
|
942 { |
338
|
943 if (strcmp (curr_fcn_file_name, id_name) != 0) |
195
|
944 { |
|
945 warning ("function name `%s' does not agree\ |
341
|
946 with function file name `%s.m'", id_name, curr_fcn_file_name); |
1
|
947 |
572
|
948 global_sym_tab->rename (id_name, |
|
949 curr_fcn_file_name); |
|
950 |
197
|
951 id_name = $1->name (); |
195
|
952 } |
|
953 |
338
|
954 $4->stash_fcn_file_name (curr_fcn_file_name); |
522
|
955 $4->stash_fcn_file_time (time (0)); |
338
|
956 $4->mark_as_system_fcn_file (); |
1
|
957 } |
315
|
958 else if (! (input_from_tmp_history_file |
|
959 || input_from_startup_file) |
195
|
960 && reading_script_file |
338
|
961 && strcmp (curr_fcn_file_name, id_name) == 0) |
1
|
962 { |
195
|
963 warning ("function `%s' defined within\ |
341
|
964 script file `%s.m'", id_name, curr_fcn_file_name); |
1
|
965 } |
|
966 |
197
|
967 top_level_sym_tab->clear (id_name); |
|
968 |
143
|
969 $4->stash_function_name (id_name); |
195
|
970 |
|
971 $1->define ($4); |
|
972 $1->document (help_buf); |
|
973 |
1
|
974 $$ = $4; |
|
975 } |
|
976 ; |
|
977 |
|
978 func_def3 : param_list optsep opt_list fcn_end_or_eof |
|
979 { |
|
980 tree_function *fcn = new tree_function ($3, curr_sym_tab); |
|
981 $$ = fcn->define_param_list ($1); |
|
982 } |
|
983 | optsep opt_list fcn_end_or_eof |
|
984 { $$ = new tree_function ($2, curr_sym_tab); } |
|
985 ; |
|
986 |
|
987 fcn_end_or_eof : END |
|
988 { |
143
|
989 if (check_end ($1, token::function_end)) |
1
|
990 ABORT_PARSE; |
|
991 |
338
|
992 if (reading_fcn_file) |
1
|
993 check_for_garbage_after_fcn_def (); |
|
994 } |
|
995 | END_OF_INPUT |
|
996 { |
338
|
997 if (! (reading_fcn_file || reading_script_file)) |
1
|
998 YYABORT; |
|
999 } |
|
1000 ; |
|
1001 |
|
1002 variable : identifier |
191
|
1003 { |
|
1004 $$ = new tree_index_expression |
|
1005 ($1, $1->line (), $1->column ()); |
|
1006 } |
1
|
1007 | identifier '(' arg_list ')' |
191
|
1008 { |
|
1009 $$ = new tree_index_expression |
|
1010 ($1, $3, $1->line (), $1->column ()); |
|
1011 } |
1
|
1012 | identifier '(' ')' |
|
1013 { |
522
|
1014 $$ = new tree_index_expression ($1, 0, $1->line (), |
|
1015 $1->column ()); |
1
|
1016 } |
|
1017 | identifier '[' |
|
1018 { |
|
1019 yyerror ("parse error"); |
|
1020 error ("use `(\' and `)\' as index operators, not\ |
|
1021 `[\' and `]\'"); |
522
|
1022 $$ = 0; |
1
|
1023 ABORT_PARSE; |
|
1024 } |
|
1025 ; |
|
1026 |
240
|
1027 param_list : '(' ')' |
1
|
1028 { |
240
|
1029 quote_is_transpose = 0; |
522
|
1030 $$ = 0; |
240
|
1031 } |
504
|
1032 | '(' ELLIPSIS ')' |
|
1033 { |
|
1034 quote_is_transpose = 0; |
|
1035 tree_parameter_list *tmp = new tree_parameter_list (); |
|
1036 tmp->mark_varargs_only (); |
|
1037 $$ = tmp; |
|
1038 } |
240
|
1039 | param_list1 ')' |
|
1040 { |
|
1041 quote_is_transpose = 0; |
578
|
1042 $1->mark_as_formal_parameters (); |
1
|
1043 } |
206
|
1044 | param_list1 ',' ELLIPSIS ')' |
|
1045 { |
240
|
1046 quote_is_transpose = 0; |
578
|
1047 $1->mark_as_formal_parameters (); |
|
1048 $1->mark_varargs (); |
206
|
1049 } |
504
|
1050 ; |
1
|
1051 |
|
1052 param_list1 : '(' identifier |
|
1053 { $$ = new tree_parameter_list ($2); } |
|
1054 | param_list1 ',' identifier |
578
|
1055 { $1->append ($3); } |
1
|
1056 | '(' error |
|
1057 { |
206
|
1058 yyerror ("parse error"); |
|
1059 error ("invalid parameter list"); |
|
1060 ABORT_PARSE; |
1
|
1061 } |
|
1062 | param_list1 ',' error |
|
1063 { |
206
|
1064 yyerror ("parse error"); |
|
1065 error ("invalid parameter list"); |
|
1066 ABORT_PARSE; |
1
|
1067 } |
|
1068 ; |
|
1069 |
|
1070 identifier : NAME |
504
|
1071 { |
|
1072 $$ = new tree_identifier |
|
1073 ($1->sym_rec (), $1->line (), $1->column ()); |
|
1074 } |
|
1075 ; |
1
|
1076 |
578
|
1077 arg_list : ':' |
1
|
1078 { |
|
1079 tree_constant *colon; |
|
1080 colon = new tree_constant (tree_constant_rep::magic_colon); |
|
1081 $$ = new tree_argument_list (colon); |
|
1082 } |
578
|
1083 | arg_list ',' ':' |
1
|
1084 { |
|
1085 tree_constant *colon; |
|
1086 colon = new tree_constant (tree_constant_rep::magic_colon); |
578
|
1087 $1->append (colon); |
1
|
1088 } |
|
1089 | expression |
|
1090 { $$ = new tree_argument_list ($1); } |
578
|
1091 | arg_list ',' expression |
|
1092 { $1->append ($3); } |
1
|
1093 ; |
|
1094 |
240
|
1095 matrix : '[' screwed_again rows ']' |
1
|
1096 { |
|
1097 mlnm.pop (); |
|
1098 maybe_screwed_again--; |
|
1099 tree_matrix *tmp = ml.pop (); |
|
1100 $$ = tmp->reverse (); |
|
1101 } |
|
1102 ; |
|
1103 |
|
1104 rows : matrix_row |
|
1105 | rows ';' // Ignore trailing semicolon. |
|
1106 | rows ';' matrix_row |
|
1107 ; |
|
1108 |
|
1109 matrix_row : expression // First element on row. |
|
1110 { |
|
1111 if (mlnm.top ()) |
|
1112 { |
|
1113 mlnm.pop (); |
|
1114 mlnm.push (0); |
578
|
1115 tree_matrix *tmp = new tree_matrix |
|
1116 ($1, tree_matrix::md_none); |
1
|
1117 ml.push (tmp); |
|
1118 } |
|
1119 else |
|
1120 { |
|
1121 tree_matrix *tmp = ml.pop (); |
578
|
1122 tmp = tmp->chain ($1, tree_matrix::md_down); |
1
|
1123 ml.push (tmp); |
|
1124 } |
|
1125 } |
|
1126 | matrix_row ',' // Ignore trailing comma. |
|
1127 | matrix_row ',' expression |
|
1128 { |
|
1129 tree_matrix *tmp = ml.pop (); |
578
|
1130 tmp = tmp->chain ($3, tree_matrix::md_right); |
1
|
1131 ml.push (tmp); |
|
1132 } |
|
1133 ; |
|
1134 |
|
1135 %% |
|
1136 |
|
1137 static void |
|
1138 yyerror (char *s) |
|
1139 { |
|
1140 char *line = current_input_line; |
143
|
1141 int err_col = current_input_column - 1; |
522
|
1142 if (err_col == 0 && line) |
335
|
1143 err_col = strlen (line) + 1; |
1
|
1144 |
581
|
1145 // Print a message like `parse error', maybe printing the line number |
|
1146 // and file name. |
|
1147 |
|
1148 ostrstream output_buf; |
1
|
1149 |
581
|
1150 output_buf.form ("\n%s", s); |
|
1151 |
338
|
1152 if (reading_fcn_file || reading_script_file) |
581
|
1153 output_buf.form (" near line %d of file %s.m", input_line_number, |
|
1154 curr_fcn_file_name); |
1
|
1155 |
522
|
1156 if (line) |
1
|
1157 { |
335
|
1158 int len = strlen (line); |
|
1159 if (line[len-1] == '\n') |
|
1160 { |
|
1161 len--; |
|
1162 line[len] = '\0'; |
|
1163 } |
|
1164 // Print the line, maybe with a pointer near the error token. |
|
1165 if (err_col > len) |
581
|
1166 output_buf.form (":\n\n %s\n\n", line); |
335
|
1167 else |
581
|
1168 output_buf.form (":\n\n %s\n %*s\n\n", line, err_col, "^"); |
1
|
1169 } |
336
|
1170 else |
581
|
1171 output_buf << "\n\n"; |
|
1172 |
|
1173 maybe_page_output (output_buf); |
1
|
1174 } |
|
1175 |
496
|
1176 static void |
|
1177 end_error (char *type, token::end_tok_type ettype, int l, int c) |
|
1178 { |
|
1179 static char *fmt = "%s command matched by `%s' near line %d column %d"; |
|
1180 |
|
1181 switch (ettype) |
|
1182 { |
|
1183 case token::simple_end: |
|
1184 error (fmt, type, "end", l, c); |
|
1185 break; |
|
1186 case token::for_end: |
|
1187 error (fmt, type, "endfor", l, c); |
|
1188 break; |
|
1189 case token::function_end: |
|
1190 error (fmt, type, "endfunction", l, c); |
|
1191 break; |
|
1192 case token::if_end: |
|
1193 error (fmt, type, "endif", l, c); |
|
1194 break; |
|
1195 case token::while_end: |
|
1196 error (fmt, type, "endwhile", l, c); |
|
1197 break; |
|
1198 default: |
|
1199 panic_impossible (); |
|
1200 break; |
|
1201 } |
|
1202 } |
|
1203 |
143
|
1204 static int |
|
1205 check_end (token *tok, token::end_tok_type expected) |
|
1206 { |
|
1207 token::end_tok_type ettype = tok->ettype (); |
|
1208 if (ettype != expected && ettype != token::simple_end) |
|
1209 { |
|
1210 yyerror ("parse error"); |
|
1211 |
|
1212 int l = tok->line (); |
|
1213 int c = tok->column (); |
|
1214 |
|
1215 switch (expected) |
|
1216 { |
|
1217 case token::for_end: |
|
1218 end_error ("for", ettype, l, c); |
|
1219 break; |
|
1220 case token::function_end: |
|
1221 end_error ("function", ettype, l, c); |
|
1222 break; |
|
1223 case token::if_end: |
|
1224 end_error ("if", ettype, l, c); |
|
1225 break; |
|
1226 case token::while_end: |
|
1227 end_error ("while", ettype, l, c); |
|
1228 break; |
|
1229 default: |
|
1230 panic_impossible (); |
|
1231 break; |
|
1232 } |
|
1233 return 1; |
|
1234 } |
|
1235 else |
|
1236 return 0; |
|
1237 } |
|
1238 |
1
|
1239 /* |
|
1240 * Need to make sure that the expression isn't already an identifier |
|
1241 * that has a name, or an assignment expression. |
|
1242 * |
|
1243 * Note that an expression can't be just an identifier anymore -- it |
|
1244 * must at least be an index expression (see the definition of the |
|
1245 * non-terminal `variable' above). |
183
|
1246 * |
|
1247 * XXX FIXME XXX. This isn't quite sufficient. For example, try the |
|
1248 * command `x = 4, x' for `x' previously undefined. |
195
|
1249 * |
|
1250 * XXX FIXME XXX -- we should probably delay doing this until eval-time. |
1
|
1251 */ |
496
|
1252 static tree_expression * |
|
1253 maybe_convert_to_ans_assign (tree_expression *expr) |
1
|
1254 { |
|
1255 if (expr->is_index_expression ()) |
|
1256 { |
195
|
1257 expr->mark_for_possible_ans_assign (); |
|
1258 return expr; |
|
1259 } |
|
1260 else if (expr->is_assignment_expression () |
|
1261 || expr->is_prefix_expression ()) |
|
1262 { |
|
1263 return expr; |
|
1264 } |
|
1265 else |
|
1266 { |
|
1267 symbol_record *sr = global_sym_tab->lookup ("ans", 1, 0); |
1
|
1268 |
522
|
1269 assert (sr); |
195
|
1270 |
|
1271 tree_identifier *ans = new tree_identifier (sr); |
|
1272 |
581
|
1273 return new tree_simple_assignment_expression (ans, expr, 0, 1); |
1
|
1274 } |
|
1275 } |
|
1276 |
496
|
1277 static void |
|
1278 maybe_warn_assign_as_truth_value (tree_expression *expr) |
1
|
1279 { |
|
1280 if (user_pref.warn_assign_as_truth_value |
|
1281 && expr->is_assignment_expression () |
581
|
1282 && expr->in_parens < 2) |
1
|
1283 { |
|
1284 warning ("suggest parenthesis around assignment used as truth value"); |
|
1285 } |
|
1286 } |
578
|
1287 |
|
1288 static tree_expression * |
|
1289 make_binary_op (int op, tree_expression *op1, token *tok_val, |
|
1290 tree_expression *op2) |
|
1291 { |
|
1292 tree_expression::type t; |
|
1293 switch (op) |
|
1294 { |
|
1295 case POW: |
|
1296 t = tree_expression::power; |
|
1297 break; |
|
1298 case EPOW: |
|
1299 t = tree_expression::elem_pow; |
|
1300 break; |
|
1301 case '+': |
|
1302 t = tree_expression::add; |
|
1303 break; |
|
1304 case '-': |
|
1305 t = tree_expression::subtract; |
|
1306 break; |
|
1307 case '*': |
|
1308 t = tree_expression::multiply; |
|
1309 break; |
|
1310 case '/': |
|
1311 t = tree_expression::divide; |
|
1312 break; |
|
1313 case EMUL: |
|
1314 t = tree_expression::el_mul; |
|
1315 break; |
|
1316 case EDIV: |
|
1317 t = tree_expression::el_div; |
|
1318 break; |
|
1319 case LEFTDIV: |
|
1320 t = tree_expression::leftdiv; |
|
1321 break; |
|
1322 case ELEFTDIV: |
|
1323 t = tree_expression::el_leftdiv; |
|
1324 break; |
|
1325 case EXPR_LT: |
|
1326 t = tree_expression::cmp_lt; |
|
1327 break; |
|
1328 case EXPR_LE: |
|
1329 t = tree_expression::cmp_le; |
|
1330 break; |
|
1331 case EXPR_EQ: |
|
1332 t = tree_expression::cmp_eq; |
|
1333 break; |
|
1334 case EXPR_GE: |
|
1335 t = tree_expression::cmp_ge; |
|
1336 break; |
|
1337 case EXPR_GT: |
|
1338 t = tree_expression::cmp_gt; |
|
1339 break; |
|
1340 case EXPR_NE: |
|
1341 t = tree_expression::cmp_ne; |
|
1342 break; |
|
1343 case EXPR_AND_AND: |
|
1344 t = tree_expression::and_and; |
|
1345 break; |
|
1346 case EXPR_OR_OR: |
|
1347 t = tree_expression::or_or; |
|
1348 break; |
|
1349 case EXPR_AND: |
|
1350 t = tree_expression::and; |
|
1351 break; |
|
1352 case EXPR_OR: |
|
1353 t = tree_expression::or; |
|
1354 break; |
|
1355 default: |
|
1356 panic_impossible (); |
|
1357 break; |
|
1358 } |
|
1359 |
|
1360 int l = tok_val->line (); |
|
1361 int c = tok_val->column (); |
|
1362 |
|
1363 return new tree_binary_expression (op1, op2, t, l, c); |
|
1364 } |
|
1365 |
|
1366 static tree_expression * |
|
1367 make_prefix_op (int op, tree_identifier *op1, token *tok_val) |
|
1368 { |
|
1369 tree_expression::type t; |
|
1370 switch (op) |
|
1371 { |
|
1372 case PLUS_PLUS: |
|
1373 t = tree_expression::increment; |
|
1374 break; |
|
1375 case MINUS_MINUS: |
|
1376 t = tree_expression::decrement; |
|
1377 break; |
|
1378 default: |
|
1379 panic_impossible (); |
|
1380 break; |
|
1381 } |
|
1382 |
|
1383 int l = tok_val->line (); |
|
1384 int c = tok_val->column (); |
|
1385 |
|
1386 return new tree_prefix_expression (op1, t, l, c); |
|
1387 } |
|
1388 |
|
1389 static tree_expression * |
|
1390 make_postfix_op (int op, tree_identifier *op1, token *tok_val) |
|
1391 { |
|
1392 tree_expression::type t; |
|
1393 switch (op) |
|
1394 { |
|
1395 case PLUS_PLUS: |
|
1396 t = tree_expression::increment; |
|
1397 break; |
|
1398 case MINUS_MINUS: |
|
1399 t = tree_expression::decrement; |
|
1400 break; |
|
1401 default: |
|
1402 panic_impossible (); |
|
1403 break; |
|
1404 } |
|
1405 |
|
1406 int l = tok_val->line (); |
|
1407 int c = tok_val->column (); |
|
1408 |
|
1409 return new tree_postfix_expression (op1, t, l, c); |
|
1410 } |
|
1411 |
|
1412 static tree_expression * |
|
1413 make_unary_op (int op, tree_expression *op1, token *tok_val) |
|
1414 { |
|
1415 tree_expression::type t; |
|
1416 switch (op) |
|
1417 { |
|
1418 case QUOTE: |
|
1419 t = tree_expression::hermitian; |
|
1420 break; |
|
1421 case TRANSPOSE: |
|
1422 t = tree_expression::transpose; |
|
1423 break; |
|
1424 case EXPR_NOT: |
|
1425 t = tree_expression::not; |
|
1426 break; |
|
1427 case '-': |
|
1428 t = tree_expression::uminus; |
|
1429 break; |
|
1430 default: |
|
1431 panic_impossible (); |
|
1432 break; |
|
1433 } |
|
1434 |
|
1435 int l = tok_val->line (); |
|
1436 int c = tok_val->column (); |
|
1437 |
|
1438 return new tree_unary_expression (op1, t, l, c); |
|
1439 } |