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