1829
|
1 /* |
1
|
2 |
1884
|
3 Copyright (C) 1996 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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
|
23 // Parser for Octave. |
|
24 |
767
|
25 // C decarations. |
|
26 |
1
|
27 %{ |
|
28 #define YYDEBUG 1 |
|
29 |
240
|
30 #ifdef HAVE_CONFIG_H |
1229
|
31 #include <config.h> |
240
|
32 #endif |
|
33 |
2427
|
34 #ifdef YYBYACC |
|
35 #include <cstdlib> |
|
36 #endif |
|
37 |
581
|
38 #include <strstream.h> |
|
39 |
1
|
40 #include "Matrix.h" |
|
41 |
2166
|
42 #include "defun.h" |
1351
|
43 #include "error.h" |
|
44 #include "input.h" |
|
45 #include "lex.h" |
1743
|
46 #include "oct-hist.h" |
1670
|
47 #include "toplev.h" |
1351
|
48 #include "pager.h" |
|
49 #include "parse.h" |
1740
|
50 #include "pt-cmd.h" |
|
51 #include "pt-const.h" |
|
52 #include "pt-fcn.h" |
|
53 #include "pt-fvc.h" |
|
54 #include "pt-mat.h" |
|
55 #include "pt-mvr.h" |
|
56 #include "pt-exp.h" |
|
57 #include "pt-misc.h" |
|
58 #include "pt-plot.h" |
2533
|
59 #include "pt-pr-code.h" |
1351
|
60 #include "symtab.h" |
|
61 #include "token.h" |
1
|
62 #include "utils.h" |
1351
|
63 #include "variables.h" |
1
|
64 |
2166
|
65 // If TRUE, generate a warning for the assignment in things like |
|
66 // |
|
67 // octave> if (a = 2 < n) |
|
68 // |
|
69 // but not |
|
70 // |
|
71 // octave> if ((a = 2) < n) |
|
72 // |
|
73 static bool Vwarn_assign_as_truth_value; |
|
74 |
2764
|
75 // If TRUE, generate a warning for variable swich labels. |
|
76 static bool Vwarn_variable_switch_label; |
|
77 |
2166
|
78 // If TRUE, generate a warning for the comma in things like |
|
79 // |
|
80 // octave> global a, b = 2 |
|
81 // |
2846
|
82 static bool Vwarn_comma_in_declaration; |
2166
|
83 |
|
84 // If TRUE, generate warning if declared function name disagrees with |
|
85 // the name of the file in which it is defined. |
|
86 static bool Vwarn_function_name_clash; |
|
87 |
|
88 // If TRUE, generate warning if a statement in a function is not |
|
89 // terminated with a semicolon. Useful for checking functions that |
|
90 // should only produce output using explicit printing statements. |
|
91 static bool Vwarn_missing_semicolon; |
|
92 |
1
|
93 // Temporary symbol table pointer used to cope with bogus function syntax. |
522
|
94 symbol_table *tmp_local_sym_tab = 0; |
1
|
95 |
|
96 // The current input line number. |
|
97 int input_line_number = 0; |
|
98 |
|
99 // The column of the current token. |
143
|
100 int current_input_column = 1; |
1
|
101 |
338
|
102 // Buffer for help text snagged from function files. |
1755
|
103 string help_buf; |
1
|
104 |
496
|
105 // Forward declarations for some functions defined at the bottom of |
|
106 // the file. |
|
107 |
|
108 // Generic error messages. |
2805
|
109 static void yyerror (const char *s); |
1
|
110 |
578
|
111 // Error mesages for mismatched end tokens. |
2805
|
112 static void end_error |
|
113 (const char *type, token::end_tok_type ettype, int l, int c); |
1
|
114 |
578
|
115 // Check to see that end tokens are properly matched. |
496
|
116 static int check_end (token *tok, token::end_tok_type expected); |
1
|
117 |
496
|
118 // Try to figure out early if an expression should become an |
1623
|
119 // assignment to the built-in variable ans. |
496
|
120 static tree_expression *maybe_convert_to_ans_assign (tree_expression *expr); |
|
121 |
|
122 // Maybe print a warning if an assignment expression is used as the |
|
123 // test in a logical expression. |
|
124 static void maybe_warn_assign_as_truth_value (tree_expression *expr); |
1
|
125 |
2764
|
126 // Maybe print a warning about switch labels that aren't constants. |
|
127 static void maybe_warn_variable_switch_label (tree_expression *expr); |
|
128 |
1623
|
129 // Create a plot command. |
|
130 static tree_plot_command *make_plot_command |
|
131 (token *tok, plot_limits *range, subplot_list *list); |
|
132 |
|
133 // Finish building a range. |
|
134 static tree_expression *finish_colon_expression (tree_colon_expression *e); |
|
135 |
1607
|
136 // Build a constant. |
2375
|
137 static tree_constant *make_constant (int op, token *tok_val); |
1607
|
138 |
578
|
139 // Build a binary expression. |
751
|
140 static tree_expression *make_binary_op |
|
141 (int op, tree_expression *op1, token *tok_val, tree_expression *op2); |
578
|
142 |
2375
|
143 // Build a boolean expression. |
|
144 static tree_expression *make_boolean_op |
|
145 (int op, tree_expression *op1, token *tok_val, tree_expression *op2); |
|
146 |
578
|
147 // Build a prefix expression. |
751
|
148 static tree_expression *make_prefix_op |
|
149 (int op, tree_identifier *op1, token *tok_val); |
578
|
150 |
|
151 // Build a postfix expression. |
751
|
152 static tree_expression *make_postfix_op |
|
153 (int op, tree_identifier *op1, token *tok_val); |
578
|
154 |
|
155 // Build a binary expression. |
751
|
156 static tree_expression *make_unary_op |
|
157 (int op, tree_expression *op1, token *tok_val); |
578
|
158 |
1623
|
159 // Build an unwind-protect command. |
|
160 static tree_command *make_unwind_command |
|
161 (token *unwind_tok, tree_statement_list *body, |
|
162 tree_statement_list *cleanup, token *end_tok); |
|
163 |
|
164 // Build a try-catch command. |
|
165 static tree_command *make_try_command |
|
166 (token *try_tok, tree_statement_list *body, |
|
167 tree_statement_list *cleanup, token *end_tok); |
|
168 |
|
169 // Build a while command. |
|
170 static tree_command *make_while_command |
|
171 (token *while_tok, tree_expression *expr, |
|
172 tree_statement_list *body, token *end_tok); |
|
173 |
|
174 // Build a for command. |
|
175 static tree_command *make_for_command |
|
176 (token *for_tok, tree_index_expression *var, |
|
177 tree_expression *expr, tree_statement_list *body, |
|
178 token *end_tok); |
|
179 |
|
180 // Build a for command a different way. |
|
181 static tree_command *make_for_command |
1829
|
182 (token *for_tok, tree_matrix_row *mr, tree_expression *expr, |
1623
|
183 tree_statement_list *body, token *end_tok); |
|
184 |
|
185 // Build a break command. |
|
186 static tree_command *make_break_command (token *break_tok); |
|
187 |
|
188 // Build a continue command. |
|
189 static tree_command *make_continue_command (token *continue_tok); |
|
190 |
|
191 // Build a return command. |
|
192 static tree_command *make_return_command (token *return_tok); |
|
193 |
|
194 // Start an if command. |
|
195 static tree_if_command_list *start_if_command |
|
196 (tree_expression *expr, tree_statement_list *list); |
|
197 |
|
198 // Finish an if command. |
|
199 static tree_if_command *finish_if_command |
|
200 (token *if_tok, tree_if_command_list *list, token *end_tok); |
|
201 |
|
202 // Build an elseif clause. |
|
203 static tree_if_clause *make_elseif_clause |
|
204 (tree_expression *expr, tree_statement_list *list); |
|
205 |
2764
|
206 // Finish a switch command. |
|
207 static tree_switch_command *finish_switch_command |
|
208 (token *switch_tok, tree_expression *expr, |
|
209 tree_switch_case_list *list, token *end_tok); |
|
210 |
|
211 // Build a switch case. |
|
212 static tree_switch_case *make_switch_case |
|
213 (tree_expression *expr, tree_statement_list *list); |
|
214 |
1623
|
215 // Build an assignment to a variable. |
|
216 static tree_expression *make_simple_assignment |
|
217 (tree_index_expression *var, token *eq_tok, tree_expression *expr); |
|
218 |
666
|
219 // Make an expression that handles assignment of multiple values. |
751
|
220 static tree_expression *make_multi_val_ret |
1829
|
221 (tree_matrix_row *mr, tree_expression *rhs, token *eq_tok); |
1623
|
222 |
|
223 // Begin defining a function. |
|
224 static tree_function *start_function_def |
|
225 (tree_parameter_list *param_list, tree_statement_list *body); |
|
226 |
|
227 // Do most of the work for defining a function. |
|
228 static tree_function *frob_function_def |
|
229 (tree_identifier *id, tree_function *fcn); |
|
230 |
|
231 // Finish defining a function. |
|
232 static tree_function *finish_function_def (token *var, tree_function *fcn); |
|
233 |
|
234 // Finish defining a function a different way. |
|
235 static tree_function *finish_function_def |
|
236 (tree_parameter_list *ret_list, tree_function *fcn); |
751
|
237 |
|
238 // Make an index expression. |
|
239 static tree_index_expression *make_index_expression |
|
240 (tree_indirect_ref *indir, tree_argument_list *args); |
666
|
241 |
2846
|
242 // Make a declaration command. |
|
243 static tree_decl_command *make_decl_command |
|
244 (int tok, token *tok_val, tree_decl_init_list *lst); |
|
245 |
1623
|
246 // Finish building a matrix list. |
1829
|
247 static tree_expression *finish_matrix (tree_matrix *m); |
1623
|
248 |
1511
|
249 // Maybe print a warning. Duh. |
|
250 static void maybe_warn_missing_semi (tree_statement_list *); |
|
251 |
2846
|
252 // Maybe print a warning. Duh. |
|
253 static void maybe_warn_comma_in_decl (void); |
|
254 |
2525
|
255 // Set the print flag for a statement based on the separator type. |
|
256 static void set_stmt_print_flag (tree_statement_list *, char, bool); |
|
257 |
1
|
258 #define ABORT_PARSE \ |
|
259 do \ |
|
260 { \ |
522
|
261 global_command = 0; \ |
1
|
262 yyerrok; \ |
|
263 if (interactive) \ |
|
264 YYACCEPT; \ |
|
265 else \ |
|
266 YYABORT; \ |
|
267 } \ |
|
268 while (0) |
|
269 |
|
270 %} |
|
271 |
666
|
272 // Bison declarations. |
|
273 |
1
|
274 %union |
|
275 { |
143
|
276 // The type of the basic tokens returned by the lexer. |
|
277 token *tok_val; |
|
278 |
|
279 // Types for the nonterminals we generate. |
2525
|
280 char sep_type; |
1
|
281 tree *tree_type; |
1829
|
282 tree_matrix *tree_matrix_type; |
|
283 tree_matrix_row *tree_matrix_row_type; |
496
|
284 tree_expression *tree_expression_type; |
2375
|
285 tree_constant *tree_constant_type; |
1
|
286 tree_identifier *tree_identifier_type; |
751
|
287 tree_indirect_ref *tree_indirect_ref_type; |
1
|
288 tree_function *tree_function_type; |
|
289 tree_index_expression *tree_index_expression_type; |
|
290 tree_colon_expression *tree_colon_expression_type; |
|
291 tree_argument_list *tree_argument_list_type; |
|
292 tree_parameter_list *tree_parameter_list_type; |
|
293 tree_command *tree_command_type; |
|
294 tree_if_command *tree_if_command_type; |
578
|
295 tree_if_clause *tree_if_clause_type; |
|
296 tree_if_command_list *tree_if_command_list_type; |
2764
|
297 tree_switch_command *tree_switch_command_type; |
|
298 tree_switch_case *tree_switch_case_type; |
|
299 tree_switch_case_list *tree_switch_case_list_type; |
2846
|
300 tree_decl_elt *tree_decl_elt_type; |
|
301 tree_decl_init_list *tree_decl_init_list_type; |
|
302 tree_decl_command *tree_decl_command_type; |
578
|
303 tree_statement *tree_statement_type; |
|
304 tree_statement_list *tree_statement_list_type; |
1
|
305 tree_plot_command *tree_plot_command_type; |
578
|
306 subplot *subplot_type; |
|
307 subplot_list *subplot_list_type; |
|
308 plot_limits *plot_limits_type; |
|
309 plot_range *plot_range_type; |
|
310 subplot_using *subplot_using_type; |
|
311 subplot_style *subplot_style_type; |
1
|
312 } |
|
313 |
143
|
314 // Tokens with line and column information. |
|
315 %token <tok_val> '=' ':' '-' '+' '*' '/' |
428
|
316 %token <tok_val> EXPR_AND_AND EXPR_OR_OR |
143
|
317 %token <tok_val> EXPR_AND EXPR_OR EXPR_NOT |
|
318 %token <tok_val> EXPR_LT EXPR_LE EXPR_EQ EXPR_NE EXPR_GE EXPR_GT |
1276
|
319 %token <tok_val> LEFTDIV EMUL EDIV ELEFTDIV EPLUS EMINUS |
|
320 %token <tok_val> QUOTE TRANSPOSE |
143
|
321 %token <tok_val> PLUS_PLUS MINUS_MINUS POW EPOW |
|
322 %token <tok_val> NUM IMAG_NUM |
169
|
323 %token <tok_val> NAME SCREW |
143
|
324 %token <tok_val> END |
|
325 %token <tok_val> PLOT |
|
326 %token <tok_val> TEXT STYLE |
1491
|
327 %token <tok_val> FOR WHILE |
|
328 %token <tok_val> IF ELSEIF ELSE |
2764
|
329 %token <tok_val> SWITCH CASE OTHERWISE |
1491
|
330 %token <tok_val> BREAK CONTINUE FUNC_RET |
924
|
331 %token <tok_val> UNWIND CLEANUP |
1489
|
332 %token <tok_val> TRY CATCH |
2846
|
333 %token <tok_val> GLOBAL STATIC |
751
|
334 %token <tok_val> TEXT_ID |
1
|
335 |
143
|
336 // Other tokens. |
627
|
337 %token LEXICAL_ERROR |
191
|
338 %token FCN SCREW_TWO |
206
|
339 %token ELLIPSIS |
922
|
340 %token ALL_VA_ARGS |
206
|
341 %token END_OF_INPUT |
883
|
342 %token USING TITLE WITH COLON OPEN_BRACE CLOSE_BRACE CLEAR |
1
|
343 |
143
|
344 // Nonterminals we construct. |
2525
|
345 %type <sep_type> sep_no_nl opt_sep_no_nl sep opt_sep |
578
|
346 %type <tree_type> input |
1829
|
347 %type <tree_matrix_type> rows rows1 |
|
348 %type <tree_matrix_row_type> matrix_row matrix_row1 |
496
|
349 %type <tree_expression_type> expression simple_expr simple_expr1 |
1623
|
350 %type <tree_expression_type> ans_expression title matrix |
1
|
351 %type <tree_identifier_type> identifier |
751
|
352 %type <tree_indirect_ref_type> indirect_ref indirect_ref1 |
578
|
353 %type <tree_function_type> func_def1 func_def2 func_def3 |
482
|
354 %type <tree_index_expression_type> variable word_list_cmd |
1
|
355 %type <tree_colon_expression_type> colon_expr |
578
|
356 %type <tree_argument_list_type> arg_list word_list |
723
|
357 %type <tree_parameter_list_type> param_list param_list1 |
|
358 %type <tree_parameter_list_type> return_list return_list1 |
578
|
359 %type <tree_command_type> command func_def |
|
360 %type <tree_if_command_type> if_command |
|
361 %type <tree_if_clause_type> elseif_clause else_clause |
|
362 %type <tree_if_command_list_type> if_cmd_list1 if_cmd_list |
2764
|
363 %type <tree_switch_command_type> switch_command |
|
364 %type <tree_switch_case_type> switch_case default_case |
|
365 %type <tree_switch_case_list_type> case_list1 case_list |
2846
|
366 %type <tree_decl_elt_type> decl2 |
|
367 %type <tree_decl_init_list_type> decl1 |
|
368 %type <tree_decl_command_type> declaration |
578
|
369 %type <tree_statement_type> statement |
627
|
370 %type <tree_statement_list_type> simple_list simple_list1 list list1 |
|
371 %type <tree_statement_list_type> opt_list input1 |
1
|
372 %type <tree_plot_command_type> plot_command |
578
|
373 %type <subplot_type> plot_command2 plot_options |
|
374 %type <subplot_list_type> plot_command1 |
|
375 %type <plot_limits_type> ranges |
|
376 %type <plot_range_type> ranges1 |
|
377 %type <subplot_using_type> using using1 |
|
378 %type <subplot_style_type> style |
1
|
379 |
143
|
380 // Precedence and associativity. |
1
|
381 %left ';' ',' '\n' |
|
382 %right '=' |
428
|
383 %left EXPR_AND_AND EXPR_OR_OR |
1
|
384 %left EXPR_AND EXPR_OR |
|
385 %left EXPR_LT EXPR_LE EXPR_EQ EXPR_NE EXPR_GE EXPR_GT |
|
386 %left ':' |
1276
|
387 %left '-' '+' EPLUS EMINUS |
1
|
388 %left '*' '/' LEFTDIV EMUL EDIV ELEFTDIV |
|
389 %left QUOTE TRANSPOSE |
|
390 %left UNARY PLUS_PLUS MINUS_MINUS EXPR_NOT |
|
391 %right POW EPOW |
|
392 |
2524
|
393 // There are 18 shift/reduce conflicts, ok? But this only works with |
2427
|
394 // bison... |
2524
|
395 // %expect 18 |
143
|
396 |
|
397 // Where to start. |
1
|
398 %start input |
|
399 |
666
|
400 // Grammar rules. |
|
401 |
1
|
402 %% |
|
403 |
627
|
404 input : input1 |
1
|
405 { |
627
|
406 global_command = $1; |
1
|
407 promptflag = 1; |
|
408 YYACCEPT; |
|
409 } |
|
410 | END_OF_INPUT |
|
411 { |
522
|
412 global_command = 0; |
1
|
413 promptflag = 1; |
|
414 YYABORT; |
|
415 } |
627
|
416 | simple_list parse_error |
1091
|
417 { ABORT_PARSE; } |
627
|
418 | parse_error |
1091
|
419 { ABORT_PARSE; } |
627
|
420 ; |
|
421 |
|
422 input1 : '\n' |
|
423 { $$ = 0; } |
327
|
424 | simple_list |
627
|
425 { $$ = $1; } |
1
|
426 | simple_list '\n' |
627
|
427 { $$ = $1; } |
1
|
428 | simple_list END_OF_INPUT |
627
|
429 { $$ = $1; } |
|
430 ; |
|
431 |
|
432 parse_error : LEXICAL_ERROR |
1091
|
433 { yyerror ("parse error"); } |
1
|
434 | error |
|
435 ; |
|
436 |
2525
|
437 simple_list : simple_list1 opt_sep_no_nl |
1
|
438 { |
2525
|
439 set_stmt_print_flag ($1, $2, false); |
1511
|
440 $$ = $1; |
1
|
441 } |
|
442 ; |
|
443 |
578
|
444 simple_list1 : statement |
|
445 { $$ = new tree_statement_list ($1); } |
2525
|
446 | simple_list1 sep_no_nl statement |
1
|
447 { |
2525
|
448 set_stmt_print_flag ($1, $2, false); |
578
|
449 $1->append ($3); |
1511
|
450 $$ = $1; |
1
|
451 } |
|
452 ; |
|
453 |
|
454 opt_list : // empty |
578
|
455 { $$ = new tree_statement_list (); } |
1
|
456 | list |
|
457 { $$ = $1; } |
496
|
458 ; |
1
|
459 |
2525
|
460 list : list1 opt_sep |
1511
|
461 { |
2525
|
462 set_stmt_print_flag ($1, $2, true); |
1829
|
463 $$ = $1; |
1
|
464 } |
|
465 ; |
|
466 |
578
|
467 list1 : statement |
440
|
468 { |
1826
|
469 lexer_flags.beginning_of_function = 0; |
578
|
470 $$ = new tree_statement_list ($1); |
440
|
471 } |
2525
|
472 | list1 sep statement |
1511
|
473 { |
2525
|
474 set_stmt_print_flag ($1, $2, true); |
578
|
475 $1->append ($3); |
1511
|
476 $$ = $1; |
1
|
477 } |
|
478 ; |
|
479 |
578
|
480 statement : command |
|
481 { $$ = new tree_statement ($1); } |
1
|
482 | ans_expression |
578
|
483 { $$ = new tree_statement ($1); } |
883
|
484 | PLOT CLEAR |
|
485 { |
|
486 symbol_record *sr = lookup_by_name ("clearplot", 0); |
|
487 tree_identifier *id = new tree_identifier (sr); |
|
488 $$ = new tree_statement (id); |
|
489 } |
1
|
490 ; |
|
491 |
|
492 plot_command : PLOT plot_command1 |
|
493 { |
1623
|
494 if (! ($$ = make_plot_command ($1, 0, $2))) |
|
495 ABORT_PARSE; |
1
|
496 } |
|
497 | PLOT ranges plot_command1 |
|
498 { |
1623
|
499 if (! ($$ = make_plot_command ($1, $2, $3))) |
|
500 ABORT_PARSE; |
1
|
501 } |
|
502 ; |
|
503 |
|
504 ranges : ranges1 |
578
|
505 { $$ = new plot_limits ($1); } |
1
|
506 | ranges1 ranges1 |
578
|
507 { $$ = new plot_limits ($1, $2); } |
1
|
508 | ranges1 ranges1 ranges1 |
578
|
509 { $$ = new plot_limits ($1, $2, $3); } |
1
|
510 ; |
|
511 |
|
512 ranges1 : OPEN_BRACE expression COLON expression CLOSE_BRACE |
578
|
513 { $$ = new plot_range ($2, $4); } |
1
|
514 | OPEN_BRACE COLON expression CLOSE_BRACE |
578
|
515 { $$ = new plot_range (0, $3); } |
1
|
516 | OPEN_BRACE expression COLON CLOSE_BRACE |
578
|
517 { $$ = new plot_range ($2, 0); } |
1
|
518 | OPEN_BRACE COLON CLOSE_BRACE |
578
|
519 { $$ = new plot_range (); } |
1
|
520 | OPEN_BRACE CLOSE_BRACE |
578
|
521 { $$ = new plot_range (); } |
1
|
522 ; |
|
523 |
478
|
524 plot_command1 : // empty |
522
|
525 { $$ = 0; } |
478
|
526 | plot_command2 |
578
|
527 { $$ = new subplot_list ($1); } |
1
|
528 | plot_command1 ',' plot_command2 |
1829
|
529 { |
|
530 $1->append ($3); |
|
531 $$ = $1; |
|
532 } |
1
|
533 ; |
|
534 |
|
535 plot_command2 : expression |
578
|
536 { $$ = new subplot ($1); } |
1
|
537 | expression plot_options |
1623
|
538 { $$ = $2->set_data ($1); } |
1
|
539 ; |
|
540 |
|
541 plot_options : using |
578
|
542 { $$ = new subplot ($1, 0, 0); } |
1
|
543 | title |
578
|
544 { $$ = new subplot (0, $1, 0); } |
1
|
545 | style |
578
|
546 { $$ = new subplot (0, 0, $1); } |
1
|
547 | using title |
578
|
548 { $$ = new subplot ($1, $2, 0); } |
|
549 | title using |
|
550 { $$ = new subplot ($2, $1, 0); } |
|
551 | using style |
|
552 { $$ = new subplot ($1, 0, $2); } |
|
553 | style using |
|
554 { $$ = new subplot ($2, 0, $1); } |
|
555 | title style |
|
556 { $$ = new subplot (0, $1, $2); } |
|
557 | style title |
|
558 { $$ = new subplot (0, $2, $1); } |
|
559 | using title style |
|
560 { $$ = new subplot ($1, $2, $3); } |
|
561 | using style title |
|
562 { $$ = new subplot ($1, $3, $2); } |
|
563 | title using style |
|
564 { $$ = new subplot ($2, $1, $3); } |
|
565 | title style using |
|
566 { $$ = new subplot ($3, $1, $2); } |
|
567 | style using title |
|
568 { $$ = new subplot ($2, $3, $1); } |
|
569 | style title using |
|
570 { $$ = new subplot ($3, $2, $1); } |
1
|
571 ; |
|
572 |
|
573 using : using1 |
|
574 { |
1826
|
575 lexer_flags.in_plot_using = 0; |
1
|
576 $$ = $1; |
|
577 } |
|
578 | using1 expression |
|
579 { |
1826
|
580 lexer_flags.in_plot_using = 0; |
1
|
581 $$ = $1->set_format ($2); |
|
582 } |
|
583 ; |
|
584 |
|
585 using1 : USING expression |
|
586 { |
578
|
587 subplot_using *tmp = new subplot_using (); |
1
|
588 $$ = tmp->add_qualifier ($2); |
|
589 } |
|
590 | using1 COLON expression |
|
591 { $$ = $1->add_qualifier ($3); } |
|
592 ; |
|
593 |
|
594 title : TITLE expression |
|
595 { $$ = $2; } |
|
596 ; |
|
597 |
|
598 style : WITH STYLE |
1755
|
599 { $$ = new subplot_style ($2->text ()); } |
1
|
600 | WITH STYLE expression |
1755
|
601 { $$ = new subplot_style ($2->text (), $3); } |
2542
|
602 | WITH STYLE expression expression |
|
603 { $$ = new subplot_style ($2->text (), $3, $4); } |
1
|
604 ; |
|
605 |
|
606 ans_expression : expression |
|
607 { $$ = maybe_convert_to_ans_assign ($1); } |
|
608 ; |
|
609 |
2846
|
610 decl1 : decl2 |
|
611 { $$ = new tree_decl_init_list ($1); } |
|
612 | decl1 decl2 |
578
|
613 { |
2846
|
614 $1->append ($2); |
|
615 $$ = $1; |
578
|
616 } |
2846
|
617 | decl1 ',' decl2 |
1829
|
618 { |
2846
|
619 maybe_warn_comma_in_decl (); |
1829
|
620 $1->append ($3); |
|
621 $$ = $1; |
|
622 } |
2846
|
623 ; |
|
624 |
|
625 decl2 : identifier |
|
626 { $$ = new tree_decl_elt ($1); } |
578
|
627 | identifier '=' expression |
1
|
628 { |
578
|
629 tree_simple_assignment_expression *tmp_ass; |
|
630 tmp_ass = new tree_simple_assignment_expression |
581
|
631 ($1, $3, 0, 0, $2->line (), $2->column ()); |
2846
|
632 $$ = new tree_decl_elt (tmp_ass); |
1
|
633 } |
|
634 ; |
|
635 |
2846
|
636 declaration : GLOBAL decl1 |
|
637 { $$ = make_decl_command (GLOBAL, $1, $2); } |
|
638 | STATIC decl1 |
|
639 { $$ = make_decl_command (STATIC, $1, $2); } |
1
|
640 ; |
|
641 |
578
|
642 command : plot_command |
|
643 { $$ = $1; } |
|
644 | func_def |
|
645 { $$ = $1; } |
2846
|
646 | declaration |
578
|
647 { $$ = $1; } |
2764
|
648 | switch_command |
|
649 { $$ = $1; } |
578
|
650 | if_command |
2764
|
651 { $$ = $1; } |
2525
|
652 | UNWIND opt_sep opt_list CLEANUP opt_sep opt_list END |
916
|
653 { |
1623
|
654 if (! ($$ = make_unwind_command ($1, $3, $6, $7))) |
916
|
655 ABORT_PARSE; |
|
656 } |
2525
|
657 | TRY opt_sep opt_list CATCH opt_sep opt_list END |
1489
|
658 { |
1623
|
659 if (! ($$ = make_try_command ($1, $3, $6, $7))) |
1489
|
660 ABORT_PARSE; |
|
661 } |
2525
|
662 | WHILE expression opt_sep opt_list END |
1
|
663 { |
1623
|
664 if (! ($$ = make_while_command ($1, $2, $4, $5))) |
143
|
665 ABORT_PARSE; |
1
|
666 } |
2525
|
667 | FOR variable '=' expression opt_sep opt_list END |
1
|
668 { |
1623
|
669 if (! ($$ = make_for_command ($1, $2, $4, $6, $7))) |
143
|
670 ABORT_PARSE; |
1
|
671 } |
1229
|
672 | FOR '[' screwed_again matrix_row SCREW_TWO '=' |
2525
|
673 expression opt_sep opt_list END |
1229
|
674 { |
1829
|
675 if (! ($$ = make_for_command ($1, $4, $7, $9, $10))) |
1229
|
676 ABORT_PARSE; |
|
677 } |
1
|
678 | BREAK |
|
679 { |
1623
|
680 if (! ($$ = make_break_command ($1))) |
|
681 ABORT_PARSE; |
1
|
682 } |
|
683 | CONTINUE |
|
684 { |
1623
|
685 if (! ($$ = make_continue_command ($1))) |
|
686 ABORT_PARSE; |
1
|
687 } |
|
688 | FUNC_RET |
|
689 { |
1623
|
690 if (! ($$ = make_return_command ($1))) |
|
691 ABORT_PARSE; |
1
|
692 } |
|
693 ; |
|
694 |
578
|
695 if_command : IF if_cmd_list END |
|
696 { |
1623
|
697 if (! ($$ = finish_if_command ($1, $2, $3))) |
578
|
698 ABORT_PARSE; |
|
699 } |
|
700 ; |
|
701 |
|
702 if_cmd_list : if_cmd_list1 |
2427
|
703 { $$ = $1; } |
578
|
704 | if_cmd_list1 else_clause |
1829
|
705 { |
|
706 $1->append ($2); |
|
707 $$ = $1; |
|
708 } |
578
|
709 ; |
|
710 |
2525
|
711 if_cmd_list1 : expression opt_sep opt_list |
1623
|
712 { $$ = start_if_command ($1, $3); } |
578
|
713 | if_cmd_list1 elseif_clause |
1829
|
714 { |
|
715 $1->append ($2); |
|
716 $$ = $1; |
|
717 } |
578
|
718 ; |
|
719 |
2525
|
720 elseif_clause : ELSEIF opt_sep expression opt_sep opt_list |
1623
|
721 { $$ = make_elseif_clause ($3, $5); } |
578
|
722 ; |
|
723 |
2525
|
724 else_clause : ELSE opt_sep opt_list |
578
|
725 { $$ = new tree_if_clause ($3); } |
1
|
726 ; |
|
727 |
2764
|
728 switch_command : SWITCH expression opt_sep case_list END |
|
729 { |
|
730 if (! ($$ = finish_switch_command ($1, $2, $4, $5))) |
|
731 ABORT_PARSE; |
|
732 } |
|
733 ; |
|
734 |
|
735 case_list : case_list1 |
|
736 { $$ = $1; } |
|
737 | case_list1 default_case |
|
738 { |
|
739 $1->append ($2); |
|
740 $$ = $1; |
|
741 } |
|
742 ; |
|
743 |
|
744 case_list1 : switch_case |
|
745 { $$ = new tree_switch_case_list ($1); } |
|
746 | case_list1 switch_case |
|
747 { |
|
748 $1->append ($2); |
|
749 $$ = $1; |
|
750 } |
|
751 ; |
|
752 |
|
753 switch_case : CASE opt_sep expression opt_sep list |
|
754 { $$ = make_switch_case ($3, $5); } |
|
755 ; |
|
756 |
|
757 default_case : OTHERWISE opt_sep opt_list |
|
758 { $$ = new tree_switch_case ($3); } |
|
759 ; |
|
760 |
1
|
761 screwed_again : // empty |
1826
|
762 { lexer_flags.maybe_screwed_again++; } |
1
|
763 ; |
|
764 |
1623
|
765 expression : simple_expr |
|
766 { $$ = $1; } |
1
|
767 | NUM '=' expression |
|
768 { |
1005
|
769 yyerror ("invalid assignment to a number"); |
522
|
770 $$ = 0; |
1
|
771 ABORT_PARSE; |
|
772 } |
1623
|
773 ; |
|
774 |
|
775 // Now that we do some simple constant folding, we have to make sure |
|
776 // that we get something valid back make_binary_op and make_unary_op. |
|
777 |
|
778 simple_expr : simple_expr1 |
666
|
779 { |
1623
|
780 if (! ($$ = $1)) |
666
|
781 ABORT_PARSE; |
|
782 } |
1
|
783 ; |
|
784 |
1623
|
785 simple_expr1 : NUM |
|
786 { $$ = make_constant (NUM, $1); } |
|
787 | IMAG_NUM |
|
788 { $$ = make_constant (IMAG_NUM, $1); } |
|
789 | TEXT |
|
790 { $$ = make_constant (TEXT, $1); } |
|
791 | '(' simple_expr ')' |
|
792 { |
2124
|
793 $2->mark_in_parens (); |
1623
|
794 $$ = $2; |
|
795 } |
|
796 | word_list_cmd |
|
797 { $$ = $1; } |
|
798 | variable |
|
799 { $$ = $1; } |
2533
|
800 | colon_expr |
|
801 { $$ = finish_colon_expression ($1); } |
1623
|
802 | matrix |
1
|
803 { $$ = $1; } |
1623
|
804 | '[' ']' |
2375
|
805 { $$ = new tree_constant (Matrix ()); } |
1623
|
806 | '[' ';' ']' |
2375
|
807 { $$ = new tree_constant (Matrix ()); } |
1623
|
808 | PLUS_PLUS identifier %prec UNARY |
|
809 { $$ = make_prefix_op (PLUS_PLUS, $2, $1); } |
|
810 | MINUS_MINUS identifier %prec UNARY |
|
811 { $$ = make_prefix_op (MINUS_MINUS, $2, $1); } |
|
812 | EXPR_NOT simple_expr |
|
813 { $$ = make_unary_op (EXPR_NOT, $2, $1); } |
|
814 | '+' simple_expr %prec UNARY |
|
815 { $$ = $2; } |
|
816 | '-' simple_expr %prec UNARY |
|
817 { $$ = make_unary_op ('-', $2, $1); } |
|
818 | variable '=' simple_expr |
|
819 { $$ = make_simple_assignment ($1, $2, $3); } |
|
820 | '[' screwed_again matrix_row SCREW_TWO '=' simple_expr |
|
821 { |
1829
|
822 if (! ($$ = make_multi_val_ret ($3, $6, $5))) |
1623
|
823 ABORT_PARSE; |
|
824 } |
1
|
825 | identifier PLUS_PLUS |
578
|
826 { $$ = make_postfix_op (PLUS_PLUS, $1, $2); } |
1
|
827 | identifier MINUS_MINUS |
578
|
828 { $$ = make_postfix_op (MINUS_MINUS, $1, $2); } |
1
|
829 | simple_expr QUOTE |
578
|
830 { $$ = make_unary_op (QUOTE, $1, $2); } |
1
|
831 | simple_expr TRANSPOSE |
578
|
832 { $$ = make_unary_op (TRANSPOSE, $1, $2); } |
1
|
833 | simple_expr POW simple_expr |
578
|
834 { $$ = make_binary_op (POW, $1, $2, $3); } |
1
|
835 | simple_expr EPOW simple_expr |
578
|
836 { $$ = make_binary_op (EPOW, $1, $2, $3); } |
1
|
837 | simple_expr '+' simple_expr |
578
|
838 { $$ = make_binary_op ('+', $1, $2, $3); } |
1
|
839 | simple_expr '-' simple_expr |
578
|
840 { $$ = make_binary_op ('-', $1, $2, $3); } |
1
|
841 | simple_expr '*' simple_expr |
578
|
842 { $$ = make_binary_op ('*', $1, $2, $3); } |
1
|
843 | simple_expr '/' simple_expr |
578
|
844 { $$ = make_binary_op ('/', $1, $2, $3); } |
1276
|
845 | simple_expr EPLUS simple_expr |
|
846 { $$ = make_binary_op ('+', $1, $2, $3); } |
|
847 | simple_expr EMINUS simple_expr |
|
848 { $$ = make_binary_op ('-', $1, $2, $3); } |
1
|
849 | simple_expr EMUL simple_expr |
578
|
850 { $$ = make_binary_op (EMUL, $1, $2, $3); } |
1
|
851 | simple_expr EDIV simple_expr |
578
|
852 { $$ = make_binary_op (EDIV, $1, $2, $3); } |
1
|
853 | simple_expr LEFTDIV simple_expr |
578
|
854 { $$ = make_binary_op (LEFTDIV, $1, $2, $3); } |
1
|
855 | simple_expr ELEFTDIV simple_expr |
578
|
856 { $$ = make_binary_op (ELEFTDIV, $1, $2, $3); } |
1
|
857 | simple_expr EXPR_LT simple_expr |
578
|
858 { $$ = make_binary_op (EXPR_LT, $1, $2, $3); } |
1
|
859 | simple_expr EXPR_LE simple_expr |
578
|
860 { $$ = make_binary_op (EXPR_LE, $1, $2, $3); } |
1
|
861 | simple_expr EXPR_EQ simple_expr |
578
|
862 { $$ = make_binary_op (EXPR_EQ, $1, $2, $3); } |
1
|
863 | simple_expr EXPR_GE simple_expr |
578
|
864 { $$ = make_binary_op (EXPR_GE, $1, $2, $3); } |
1
|
865 | simple_expr EXPR_GT simple_expr |
578
|
866 { $$ = make_binary_op (EXPR_GT, $1, $2, $3); } |
1
|
867 | simple_expr EXPR_NE simple_expr |
578
|
868 { $$ = make_binary_op (EXPR_NE, $1, $2, $3); } |
1
|
869 | simple_expr EXPR_AND simple_expr |
578
|
870 { $$ = make_binary_op (EXPR_AND, $1, $2, $3); } |
1
|
871 | simple_expr EXPR_OR simple_expr |
578
|
872 { $$ = make_binary_op (EXPR_OR, $1, $2, $3); } |
2375
|
873 | simple_expr EXPR_AND_AND simple_expr |
|
874 { $$ = make_boolean_op (EXPR_AND_AND, $1, $2, $3); } |
|
875 | simple_expr EXPR_OR_OR simple_expr |
|
876 { $$ = make_boolean_op (EXPR_OR_OR, $1, $2, $3); } |
1
|
877 ; |
|
878 |
1623
|
879 colon_expr : simple_expr ':' simple_expr |
1
|
880 { |
1623
|
881 $$ = new tree_colon_expression |
|
882 ($1, $3, $2->line (), $2->column ()); |
240
|
883 } |
1
|
884 | colon_expr ':' simple_expr |
|
885 { |
1623
|
886 if (! ($$ = $1->chain ($3))) |
1005
|
887 ABORT_PARSE; |
1
|
888 } |
|
889 ; |
|
890 |
|
891 word_list_cmd : identifier word_list |
482
|
892 { |
|
893 $$ = new tree_index_expression |
1623
|
894 ($1, $2, $1->line (), $1->column ()); |
482
|
895 } |
1
|
896 ; |
|
897 |
578
|
898 word_list : TEXT |
482
|
899 { |
2375
|
900 tree_constant *tmp = make_constant (TEXT, $1); |
482
|
901 $$ = new tree_argument_list (tmp); |
|
902 } |
578
|
903 | word_list TEXT |
482
|
904 { |
2375
|
905 tree_constant *tmp = make_constant (TEXT, $2); |
578
|
906 $1->append (tmp); |
1829
|
907 $$ = $1; |
482
|
908 } |
1
|
909 ; |
|
910 |
|
911 // This is truly disgusting. |
|
912 |
|
913 g_symtab : // empty |
|
914 { curr_sym_tab = global_sym_tab; } |
|
915 ; |
|
916 |
2745
|
917 in_return_list : // empty |
|
918 { lexer_flags.looking_at_return_list = 1; } |
|
919 ; |
|
920 |
1
|
921 local_symtab : // empty |
|
922 { curr_sym_tab = tmp_local_sym_tab; } |
|
923 ; |
|
924 |
|
925 safe : // empty |
1826
|
926 { lexer_flags.maybe_screwed = 0; } |
1
|
927 ; |
|
928 |
|
929 are_we_screwed : // empty |
1826
|
930 { lexer_flags.maybe_screwed = 1; } |
1
|
931 ; |
|
932 |
|
933 func_def : FCN g_symtab are_we_screwed func_def1 |
|
934 { |
|
935 curr_sym_tab = top_level_sym_tab; |
1826
|
936 lexer_flags.defining_func = 0; |
522
|
937 $$ = 0; |
1
|
938 } |
|
939 | FCN g_symtab are_we_screwed func_def2 |
|
940 { |
|
941 curr_sym_tab = top_level_sym_tab; |
1826
|
942 lexer_flags.defining_func = 0; |
522
|
943 $$ = 0; |
1
|
944 } |
|
945 ; |
|
946 |
|
947 func_def1 : SCREW safe g_symtab '=' func_def2 |
1623
|
948 { $$ = finish_function_def ($1, $5); } |
723
|
949 | return_list g_symtab '=' func_def2 |
1623
|
950 { $$ = finish_function_def ($1, $4); } |
1
|
951 ; |
|
952 |
2745
|
953 return_list_x : '[' safe local_symtab in_return_list |
723
|
954 ; |
|
955 |
|
956 return_list : return_list_x ']' |
2745
|
957 { |
|
958 lexer_flags.looking_at_return_list = 0; |
|
959 $$ = new tree_parameter_list (); |
|
960 } |
723
|
961 | return_list_x ELLIPSIS ']' |
|
962 { |
2745
|
963 lexer_flags.looking_at_return_list = 0; |
723
|
964 tree_parameter_list *tmp = new tree_parameter_list (); |
|
965 tmp->mark_varargs_only (); |
|
966 $$ = tmp; |
|
967 } |
|
968 | return_list1 ']' |
2745
|
969 { |
|
970 lexer_flags.looking_at_return_list = 0; |
|
971 $$ = $1; |
|
972 } |
723
|
973 | return_list1 ',' ELLIPSIS ']' |
|
974 { |
2745
|
975 lexer_flags.looking_at_return_list = 0; |
723
|
976 $1->mark_varargs (); |
|
977 $$ = $1; |
|
978 } |
|
979 ; |
|
980 |
|
981 return_list1 : return_list_x identifier |
|
982 { $$ = new tree_parameter_list ($2); } |
|
983 | return_list_x error |
|
984 { |
1005
|
985 yyerror ("invalid function return list"); |
1829
|
986 $$ = 0; |
723
|
987 ABORT_PARSE; |
|
988 } |
|
989 | return_list1 ',' identifier |
1829
|
990 { |
|
991 $1->append ($3); |
|
992 $$ = $1; |
|
993 } |
1
|
994 ; |
|
995 |
|
996 func_def2 : identifier safe local_symtab func_def3 |
|
997 { |
1623
|
998 if (! ($$ = frob_function_def ($1, $4))) |
|
999 ABORT_PARSE; |
1
|
1000 } |
|
1001 ; |
|
1002 |
2525
|
1003 func_def3 : param_list opt_sep opt_list fcn_end_or_eof |
1623
|
1004 { $$ = start_function_def ($1, $3); } |
2525
|
1005 | opt_sep opt_list fcn_end_or_eof |
1623
|
1006 { $$ = start_function_def (0, $2); } |
1
|
1007 ; |
|
1008 |
|
1009 fcn_end_or_eof : END |
|
1010 { |
143
|
1011 if (check_end ($1, token::function_end)) |
1829
|
1012 ABORT_PARSE; |
1
|
1013 |
338
|
1014 if (reading_fcn_file) |
1
|
1015 check_for_garbage_after_fcn_def (); |
|
1016 } |
|
1017 | END_OF_INPUT |
|
1018 { |
338
|
1019 if (! (reading_fcn_file || reading_script_file)) |
1
|
1020 YYABORT; |
|
1021 } |
|
1022 ; |
|
1023 |
751
|
1024 indirect_ref : indirect_ref1 |
191
|
1025 { |
1826
|
1026 lexer_flags.looking_at_indirect_ref = 0; |
751
|
1027 $$ = $1; |
191
|
1028 } |
751
|
1029 |
|
1030 indirect_ref1 : identifier |
191
|
1031 { |
751
|
1032 $$ = new tree_indirect_ref ($1, $1->line (), |
|
1033 $1->column ()); |
191
|
1034 } |
1826
|
1035 | indirect_ref1 '.' |
|
1036 { lexer_flags.looking_at_indirect_ref = 1; } TEXT_ID |
2375
|
1037 { $$ = new tree_indirect_ref ($1, $4->text ()); } |
751
|
1038 ; |
|
1039 |
|
1040 variable : indirect_ref |
|
1041 { $$ = make_index_expression ($1, 0); } |
|
1042 | indirect_ref '(' ')' |
|
1043 { $$ = make_index_expression ($1, 0); } |
|
1044 | indirect_ref '(' arg_list ')' |
|
1045 { $$ = make_index_expression ($1, $3); } |
|
1046 | indirect_ref '[' |
1
|
1047 { |
1005
|
1048 yyerror ("use `(\' and `)\' as index operators, not\ |
1
|
1049 `[\' and `]\'"); |
522
|
1050 $$ = 0; |
1
|
1051 ABORT_PARSE; |
|
1052 } |
|
1053 ; |
|
1054 |
2818
|
1055 param_list_beg : '(' |
2745
|
1056 { lexer_flags.looking_at_parameter_list = 1; } |
|
1057 ; |
|
1058 |
2818
|
1059 param_list_end : ')' |
2825
|
1060 { lexer_flags.looking_at_parameter_list = 0; } |
2818
|
1061 ; |
|
1062 |
|
1063 param_list : param_list_beg param_list_end |
1
|
1064 { |
1826
|
1065 lexer_flags.quote_is_transpose = 0; |
522
|
1066 $$ = 0; |
240
|
1067 } |
2818
|
1068 | param_list_beg ELLIPSIS param_list_end |
504
|
1069 { |
1826
|
1070 lexer_flags.quote_is_transpose = 0; |
504
|
1071 tree_parameter_list *tmp = new tree_parameter_list (); |
|
1072 tmp->mark_varargs_only (); |
|
1073 $$ = tmp; |
|
1074 } |
2818
|
1075 | param_list1 param_list_end |
240
|
1076 { |
1826
|
1077 lexer_flags.quote_is_transpose = 0; |
578
|
1078 $1->mark_as_formal_parameters (); |
1829
|
1079 $$ = $1; |
1
|
1080 } |
2818
|
1081 | param_list1 ',' ELLIPSIS param_list_end |
206
|
1082 { |
1826
|
1083 lexer_flags.quote_is_transpose = 0; |
578
|
1084 $1->mark_as_formal_parameters (); |
|
1085 $1->mark_varargs (); |
1829
|
1086 $$ = $1; |
206
|
1087 } |
504
|
1088 ; |
1
|
1089 |
2818
|
1090 param_list1 : param_list_beg identifier |
|
1091 { $$ = new tree_parameter_list ($2); } |
1
|
1092 | param_list1 ',' identifier |
1829
|
1093 { |
|
1094 $1->append ($3); |
|
1095 $$ = $1; |
|
1096 } |
2818
|
1097 | param_list_beg error |
1
|
1098 { |
1005
|
1099 yyerror ("invalid parameter list"); |
1829
|
1100 $$ = 0; |
206
|
1101 ABORT_PARSE; |
1
|
1102 } |
|
1103 | param_list1 ',' error |
|
1104 { |
1005
|
1105 yyerror ("invalid parameter list"); |
1829
|
1106 $$ = 0; |
206
|
1107 ABORT_PARSE; |
1
|
1108 } |
|
1109 ; |
|
1110 |
|
1111 identifier : NAME |
504
|
1112 { |
|
1113 $$ = new tree_identifier |
|
1114 ($1->sym_rec (), $1->line (), $1->column ()); |
|
1115 } |
|
1116 ; |
1
|
1117 |
578
|
1118 arg_list : ':' |
1
|
1119 { |
2375
|
1120 tree_constant *colon = |
|
1121 new tree_constant (tree_constant::magic_colon_t); |
1
|
1122 $$ = new tree_argument_list (colon); |
|
1123 } |
922
|
1124 | expression |
|
1125 { $$ = new tree_argument_list ($1); } |
|
1126 | ALL_VA_ARGS |
|
1127 { |
2375
|
1128 tree_constant *all_va_args = |
|
1129 new tree_constant (tree_constant::all_va_args_t); |
922
|
1130 $$ = new tree_argument_list (all_va_args); |
|
1131 } |
578
|
1132 | arg_list ',' ':' |
1
|
1133 { |
2375
|
1134 tree_constant *colon = |
|
1135 new tree_constant (tree_constant::magic_colon_t); |
578
|
1136 $1->append (colon); |
1829
|
1137 $$ = $1; |
1
|
1138 } |
578
|
1139 | arg_list ',' expression |
1829
|
1140 { |
|
1141 $1->append ($3); |
|
1142 $$ = $1; |
|
1143 } |
922
|
1144 | arg_list ',' ALL_VA_ARGS |
|
1145 { |
2375
|
1146 tree_constant *all_va_args = |
|
1147 new tree_constant (tree_constant::all_va_args_t); |
922
|
1148 $1->append (all_va_args); |
1829
|
1149 $$ = $1; |
922
|
1150 } |
1
|
1151 ; |
|
1152 |
240
|
1153 matrix : '[' screwed_again rows ']' |
1829
|
1154 { $$ = finish_matrix ($3); } |
1
|
1155 ; |
|
1156 |
1084
|
1157 rows : rows1 |
1829
|
1158 { $$ = $1; } |
1084
|
1159 | rows1 ';' // Ignore trailing semicolon. |
1829
|
1160 { $$ = $1; } |
1
|
1161 ; |
|
1162 |
1084
|
1163 rows1 : matrix_row |
1829
|
1164 { $$ = new tree_matrix ($1); } |
1084
|
1165 | rows1 ';' matrix_row |
1829
|
1166 { |
|
1167 $1->append ($3); |
|
1168 $$ = $1; |
|
1169 } |
1084
|
1170 ; |
|
1171 |
|
1172 matrix_row : matrix_row1 |
1829
|
1173 { $$ = $1; } |
1084
|
1174 | matrix_row1 ',' // Ignore trailing comma. |
1829
|
1175 { $$ = $1; } |
1084
|
1176 ; |
|
1177 |
|
1178 matrix_row1 : expression // First element on row. |
1829
|
1179 { $$ = new tree_matrix_row ($1); } |
1084
|
1180 | matrix_row1 ',' expression |
1
|
1181 { |
1829
|
1182 $1->append ($3); |
|
1183 $$ = $1; |
1
|
1184 } |
|
1185 ; |
|
1186 |
2525
|
1187 sep_no_nl : ',' |
|
1188 { $$ = ','; } |
|
1189 | ';' |
|
1190 { $$ = ';'; } |
|
1191 | sep_no_nl ',' |
|
1192 { $$ = $1; } |
|
1193 | sep_no_nl ';' |
|
1194 { $$ = $1; } |
|
1195 ; |
|
1196 |
|
1197 opt_sep_no_nl : // empty |
|
1198 { $$ = 0; } |
|
1199 | sep_no_nl |
|
1200 { $$ = $1; } |
|
1201 ; |
|
1202 |
|
1203 sep : ',' |
|
1204 { $$ = ','; } |
|
1205 | ';' |
|
1206 { $$ = ';'; } |
|
1207 | '\n' |
|
1208 { $$ = '\n'; } |
|
1209 | sep ',' |
|
1210 { $$ = $1; } |
|
1211 | sep ';' |
|
1212 { $$ = $1; } |
|
1213 | sep '\n' |
|
1214 { $$ = $1; } |
|
1215 ; |
|
1216 |
|
1217 opt_sep : // empty |
|
1218 { $$ = 0; } |
|
1219 | sep |
|
1220 { $$ = $1; } |
|
1221 ; |
|
1222 |
1
|
1223 %% |
|
1224 |
666
|
1225 // Generic error messages. |
|
1226 |
1
|
1227 static void |
2805
|
1228 yyerror (const char *s) |
1
|
1229 { |
143
|
1230 int err_col = current_input_column - 1; |
1
|
1231 |
581
|
1232 ostrstream output_buf; |
1
|
1233 |
1005
|
1234 if (reading_fcn_file || reading_script_file) |
|
1235 output_buf << "parse error near line " << input_line_number |
1607
|
1236 << " of file " << curr_fcn_file_full_name; |
1005
|
1237 else |
|
1238 output_buf << "parse error:"; |
581
|
1239 |
1005
|
1240 if (s && strcmp (s, "parse error") != 0) |
|
1241 output_buf << "\n\n " << s; |
|
1242 |
|
1243 output_buf << "\n\n"; |
1
|
1244 |
1755
|
1245 if (! current_input_line.empty ()) |
1
|
1246 { |
1755
|
1247 size_t len = current_input_line.length (); |
1060
|
1248 |
1755
|
1249 if (current_input_line[len-1] == '\n') |
|
1250 current_input_line.resize (len-1); |
1005
|
1251 |
335
|
1252 // Print the line, maybe with a pointer near the error token. |
1005
|
1253 |
1755
|
1254 output_buf << ">>> " << current_input_line << "\n"; |
1060
|
1255 |
|
1256 if (err_col == 0) |
|
1257 err_col = len; |
|
1258 |
|
1259 for (int i = 0; i < err_col + 3; i++) |
|
1260 output_buf << " "; |
|
1261 |
|
1262 output_buf << "^"; |
1
|
1263 } |
1005
|
1264 |
|
1265 output_buf << "\n" << ends; |
581
|
1266 |
1005
|
1267 char *msg = output_buf.str (); |
|
1268 |
1090
|
1269 parse_error ("%s", msg); |
1005
|
1270 |
|
1271 delete [] msg; |
1
|
1272 } |
|
1273 |
666
|
1274 // Error mesages for mismatched end tokens. |
|
1275 |
496
|
1276 static void |
2805
|
1277 end_error (const char *type, token::end_tok_type ettype, int l, int c) |
496
|
1278 { |
2805
|
1279 static const char *fmt |
|
1280 = "`%s' command matched by `%s' near line %d column %d"; |
496
|
1281 |
|
1282 switch (ettype) |
|
1283 { |
|
1284 case token::simple_end: |
|
1285 error (fmt, type, "end", l, c); |
|
1286 break; |
777
|
1287 |
496
|
1288 case token::for_end: |
|
1289 error (fmt, type, "endfor", l, c); |
|
1290 break; |
777
|
1291 |
496
|
1292 case token::function_end: |
|
1293 error (fmt, type, "endfunction", l, c); |
|
1294 break; |
777
|
1295 |
496
|
1296 case token::if_end: |
|
1297 error (fmt, type, "endif", l, c); |
|
1298 break; |
777
|
1299 |
496
|
1300 case token::while_end: |
|
1301 error (fmt, type, "endwhile", l, c); |
|
1302 break; |
777
|
1303 |
1371
|
1304 case token::unwind_protect_end: |
|
1305 error (fmt, type, "end_unwind_protect", l, c); |
|
1306 break; |
|
1307 |
496
|
1308 default: |
|
1309 panic_impossible (); |
|
1310 break; |
|
1311 } |
|
1312 } |
|
1313 |
666
|
1314 // Check to see that end tokens are properly matched. |
|
1315 |
143
|
1316 static int |
|
1317 check_end (token *tok, token::end_tok_type expected) |
|
1318 { |
|
1319 token::end_tok_type ettype = tok->ettype (); |
|
1320 if (ettype != expected && ettype != token::simple_end) |
|
1321 { |
|
1322 yyerror ("parse error"); |
|
1323 |
|
1324 int l = tok->line (); |
|
1325 int c = tok->column (); |
|
1326 |
|
1327 switch (expected) |
|
1328 { |
|
1329 case token::for_end: |
|
1330 end_error ("for", ettype, l, c); |
|
1331 break; |
777
|
1332 |
143
|
1333 case token::function_end: |
|
1334 end_error ("function", ettype, l, c); |
|
1335 break; |
777
|
1336 |
143
|
1337 case token::if_end: |
|
1338 end_error ("if", ettype, l, c); |
|
1339 break; |
777
|
1340 |
1489
|
1341 case token::try_catch_end: |
|
1342 end_error ("try", ettype, l, c); |
|
1343 break; |
|
1344 |
2764
|
1345 case token::switch_end: |
|
1346 end_error ("switch", ettype, l, c); |
|
1347 break; |
|
1348 |
1489
|
1349 case token::unwind_protect_end: |
|
1350 end_error ("unwind_protect", ettype, l, c); |
|
1351 break; |
|
1352 |
143
|
1353 case token::while_end: |
|
1354 end_error ("while", ettype, l, c); |
|
1355 break; |
777
|
1356 |
143
|
1357 default: |
|
1358 panic_impossible (); |
|
1359 break; |
|
1360 } |
|
1361 return 1; |
|
1362 } |
|
1363 else |
|
1364 return 0; |
|
1365 } |
|
1366 |
666
|
1367 // Try to figure out early if an expression should become an |
1623
|
1368 // assignment to the built-in variable ans. |
666
|
1369 // |
2375
|
1370 // Need to make sure that the expression is not already an identifier |
666
|
1371 // that has a name, or an assignment expression. |
|
1372 // |
2375
|
1373 // Note that an expression can not be just an identifier now -- it |
666
|
1374 // must at least be an index expression (see the definition of the |
2375
|
1375 // non-terminal variable above). |
666
|
1376 |
496
|
1377 static tree_expression * |
|
1378 maybe_convert_to_ans_assign (tree_expression *expr) |
1
|
1379 { |
|
1380 if (expr->is_index_expression ()) |
|
1381 { |
195
|
1382 expr->mark_for_possible_ans_assign (); |
|
1383 return expr; |
|
1384 } |
|
1385 else if (expr->is_assignment_expression () |
|
1386 || expr->is_prefix_expression ()) |
|
1387 { |
|
1388 return expr; |
|
1389 } |
|
1390 else |
|
1391 { |
1266
|
1392 // XXX FIXME XXX -- making ans_id static, passing its address to |
|
1393 // tree_simple_assignment_expression along with a flag to not |
|
1394 // delete it seems to create a memory leak. Hmm. |
|
1395 |
1162
|
1396 static symbol_record *sr = global_sym_tab->lookup ("ans", 1, 0); |
1266
|
1397 tree_identifier *ans_id = new tree_identifier (sr); |
1
|
1398 |
1607
|
1399 int l = expr->line (); |
|
1400 int c = expr->column (); |
|
1401 |
|
1402 return new tree_simple_assignment_expression (ans_id, expr, 0, 1, l, c); |
1
|
1403 } |
|
1404 } |
|
1405 |
666
|
1406 // Maybe print a warning if an assignment expression is used as the |
|
1407 // test in a logical expression. |
|
1408 |
496
|
1409 static void |
|
1410 maybe_warn_assign_as_truth_value (tree_expression *expr) |
1
|
1411 { |
2166
|
1412 if (Vwarn_assign_as_truth_value |
1
|
1413 && expr->is_assignment_expression () |
2124
|
1414 && expr->is_in_parens () < 2) |
1
|
1415 { |
|
1416 warning ("suggest parenthesis around assignment used as truth value"); |
|
1417 } |
|
1418 } |
578
|
1419 |
2764
|
1420 // Maybe print a warning about switch labels that aren't constants. |
|
1421 |
|
1422 static void |
|
1423 maybe_warn_variable_switch_label (tree_expression *expr) |
|
1424 { |
|
1425 if (Vwarn_variable_switch_label && ! expr->is_constant ()) |
|
1426 { |
|
1427 warning ("variable switch label"); |
|
1428 } |
|
1429 } |
|
1430 |
1623
|
1431 // Create a plot command. |
|
1432 |
|
1433 static tree_plot_command * |
|
1434 make_plot_command (token *tok, plot_limits *range, subplot_list *list) |
|
1435 { |
|
1436 if (range) |
|
1437 { |
|
1438 if (tok->pttype () == token::replot) |
|
1439 { |
|
1440 yyerror ("cannot specify new ranges with replot"); |
|
1441 return 0; |
|
1442 } |
|
1443 } |
|
1444 else if (! list && tok->pttype () != token::replot) |
|
1445 { |
|
1446 yyerror ("must have something to plot"); |
|
1447 return 0; |
|
1448 } |
|
1449 |
1826
|
1450 lexer_flags.plotting = 0; |
|
1451 lexer_flags.past_plot_range = 0; |
|
1452 lexer_flags.in_plot_range = 0; |
|
1453 lexer_flags.in_plot_using = 0; |
|
1454 lexer_flags.in_plot_style = 0; |
1623
|
1455 |
|
1456 return new tree_plot_command (list, range, tok->pttype ()); |
|
1457 } |
|
1458 |
2533
|
1459 static tree_expression * |
|
1460 fold (tree_binary_expression *e) |
|
1461 { |
|
1462 tree_expression *retval = 0; |
|
1463 |
|
1464 tree_expression *op1 = e->lhs (); |
|
1465 tree_expression *op2 = e->rhs (); |
|
1466 |
|
1467 if (op1->is_constant () && op2->is_constant ()) |
|
1468 { |
|
1469 octave_value tmp = e->eval (0); |
|
1470 |
|
1471 if (! error_state) |
|
1472 { |
|
1473 tree_constant *tc_retval = new tree_constant (tmp); |
|
1474 |
|
1475 ostrstream buf; |
|
1476 |
|
1477 tree_print_code tpc (buf); |
|
1478 |
|
1479 e->accept (tpc); |
|
1480 |
|
1481 buf << ends; |
|
1482 |
|
1483 char *s = buf.str (); |
|
1484 |
|
1485 tc_retval->stash_original_text (s); |
|
1486 |
|
1487 delete [] s; |
|
1488 |
|
1489 delete e; |
|
1490 |
|
1491 retval = tc_retval; |
|
1492 } |
|
1493 else |
|
1494 delete e; |
|
1495 } |
|
1496 else |
|
1497 retval = e; |
|
1498 |
|
1499 return retval; |
|
1500 } |
|
1501 |
|
1502 static tree_expression * |
|
1503 fold (tree_unary_expression *e) |
|
1504 { |
|
1505 tree_expression *retval = 0; |
|
1506 |
|
1507 tree_expression *op1 = e->operand (); |
|
1508 |
|
1509 if (op1->is_constant ()) |
|
1510 { |
|
1511 octave_value tmp = e->eval (0); |
|
1512 |
|
1513 if (! error_state) |
|
1514 { |
|
1515 tree_constant *tc_retval = new tree_constant (tmp); |
|
1516 |
|
1517 ostrstream buf; |
|
1518 |
|
1519 tree_print_code tpc (buf); |
|
1520 |
|
1521 e->accept (tpc); |
|
1522 |
|
1523 buf << ends; |
|
1524 |
|
1525 char *s = buf.str (); |
|
1526 |
|
1527 tc_retval->stash_original_text (s); |
|
1528 |
|
1529 delete [] s; |
|
1530 |
|
1531 delete e; |
|
1532 |
|
1533 retval = tc_retval; |
|
1534 } |
|
1535 else |
|
1536 delete e; |
|
1537 } |
|
1538 else |
|
1539 retval = e; |
|
1540 |
|
1541 return retval; |
|
1542 } |
|
1543 |
1623
|
1544 // Finish building a range. |
|
1545 |
|
1546 static tree_expression * |
|
1547 finish_colon_expression (tree_colon_expression *e) |
|
1548 { |
|
1549 tree_expression *retval = 0; |
|
1550 |
2533
|
1551 tree_expression *base = e->base (); |
|
1552 tree_expression *limit = e->limit (); |
|
1553 tree_expression *incr = e->increment (); |
|
1554 |
|
1555 if (base->is_constant () && limit->is_constant () |
|
1556 && (! incr || (incr && incr->is_constant ()))) |
1623
|
1557 { |
2086
|
1558 octave_value tmp = e->eval (0); |
1623
|
1559 |
2533
|
1560 if (! error_state) |
|
1561 { |
|
1562 tree_constant *tc_retval = new tree_constant (tmp); |
|
1563 |
|
1564 ostrstream buf; |
|
1565 |
|
1566 tree_print_code tpc (buf); |
|
1567 |
|
1568 e->accept (tpc); |
|
1569 |
|
1570 buf << ends; |
1623
|
1571 |
2533
|
1572 char *s = buf.str (); |
|
1573 |
|
1574 tc_retval->stash_original_text (s); |
|
1575 |
|
1576 delete [] s; |
|
1577 |
|
1578 delete e; |
|
1579 |
|
1580 retval = tc_retval; |
|
1581 } |
|
1582 else |
|
1583 delete e; |
1623
|
1584 } |
|
1585 else |
|
1586 retval = e; |
|
1587 |
|
1588 return retval; |
|
1589 } |
|
1590 |
1607
|
1591 // Make a constant. |
|
1592 |
2375
|
1593 static tree_constant * |
1607
|
1594 make_constant (int op, token *tok_val) |
|
1595 { |
|
1596 int l = tok_val->line (); |
|
1597 int c = tok_val->column (); |
|
1598 |
2375
|
1599 tree_constant *retval; |
1607
|
1600 |
|
1601 switch (op) |
|
1602 { |
|
1603 case NUM: |
2533
|
1604 { |
|
1605 retval = new tree_constant (tok_val->number (), l, c); |
|
1606 retval->stash_original_text (tok_val->text_rep ()); |
|
1607 } |
1607
|
1608 break; |
|
1609 |
|
1610 case IMAG_NUM: |
|
1611 { |
|
1612 Complex C (0.0, tok_val->number ()); |
2375
|
1613 retval = new tree_constant (C, l, c); |
1607
|
1614 retval->stash_original_text (tok_val->text_rep ()); |
|
1615 } |
|
1616 break; |
|
1617 |
|
1618 case TEXT: |
2375
|
1619 retval = new tree_constant (tok_val->text (), l, c); |
1607
|
1620 break; |
|
1621 |
|
1622 default: |
|
1623 panic_impossible (); |
|
1624 break; |
|
1625 } |
|
1626 |
|
1627 return retval; |
|
1628 } |
|
1629 |
666
|
1630 // Build a binary expression. |
|
1631 |
578
|
1632 static tree_expression * |
|
1633 make_binary_op (int op, tree_expression *op1, token *tok_val, |
|
1634 tree_expression *op2) |
|
1635 { |
2375
|
1636 tree_binary_expression::type t; |
1623
|
1637 |
578
|
1638 switch (op) |
|
1639 { |
|
1640 case POW: |
2375
|
1641 t = tree_binary_expression::power; |
578
|
1642 break; |
777
|
1643 |
578
|
1644 case EPOW: |
2375
|
1645 t = tree_binary_expression::elem_pow; |
578
|
1646 break; |
777
|
1647 |
578
|
1648 case '+': |
2375
|
1649 t = tree_binary_expression::add; |
578
|
1650 break; |
777
|
1651 |
578
|
1652 case '-': |
2375
|
1653 t = tree_binary_expression::subtract; |
578
|
1654 break; |
777
|
1655 |
578
|
1656 case '*': |
2375
|
1657 t = tree_binary_expression::multiply; |
578
|
1658 break; |
777
|
1659 |
578
|
1660 case '/': |
2375
|
1661 t = tree_binary_expression::divide; |
578
|
1662 break; |
777
|
1663 |
578
|
1664 case EMUL: |
2375
|
1665 t = tree_binary_expression::el_mul; |
578
|
1666 break; |
777
|
1667 |
578
|
1668 case EDIV: |
2375
|
1669 t = tree_binary_expression::el_div; |
578
|
1670 break; |
777
|
1671 |
578
|
1672 case LEFTDIV: |
2375
|
1673 t = tree_binary_expression::leftdiv; |
578
|
1674 break; |
777
|
1675 |
578
|
1676 case ELEFTDIV: |
2375
|
1677 t = tree_binary_expression::el_leftdiv; |
578
|
1678 break; |
777
|
1679 |
578
|
1680 case EXPR_LT: |
2375
|
1681 t = tree_binary_expression::cmp_lt; |
578
|
1682 break; |
777
|
1683 |
578
|
1684 case EXPR_LE: |
2375
|
1685 t = tree_binary_expression::cmp_le; |
578
|
1686 break; |
777
|
1687 |
578
|
1688 case EXPR_EQ: |
2375
|
1689 t = tree_binary_expression::cmp_eq; |
578
|
1690 break; |
777
|
1691 |
578
|
1692 case EXPR_GE: |
2375
|
1693 t = tree_binary_expression::cmp_ge; |
578
|
1694 break; |
777
|
1695 |
578
|
1696 case EXPR_GT: |
2375
|
1697 t = tree_binary_expression::cmp_gt; |
578
|
1698 break; |
777
|
1699 |
578
|
1700 case EXPR_NE: |
2375
|
1701 t = tree_binary_expression::cmp_ne; |
578
|
1702 break; |
777
|
1703 |
578
|
1704 case EXPR_AND: |
2805
|
1705 t = tree_binary_expression::el_and; |
578
|
1706 break; |
777
|
1707 |
578
|
1708 case EXPR_OR: |
2805
|
1709 t = tree_binary_expression::el_or; |
578
|
1710 break; |
777
|
1711 |
578
|
1712 default: |
|
1713 panic_impossible (); |
|
1714 break; |
|
1715 } |
|
1716 |
|
1717 int l = tok_val->line (); |
|
1718 int c = tok_val->column (); |
|
1719 |
2533
|
1720 tree_binary_expression *e |
|
1721 = new tree_binary_expression (op1, op2, l, c, t); |
1623
|
1722 |
2533
|
1723 return fold (e); |
578
|
1724 } |
|
1725 |
2375
|
1726 // Build a boolean expression. |
666
|
1727 |
578
|
1728 static tree_expression * |
2375
|
1729 make_boolean_op (int op, tree_expression *op1, token *tok_val, |
|
1730 tree_expression *op2) |
578
|
1731 { |
2375
|
1732 tree_boolean_expression::type t; |
|
1733 |
578
|
1734 switch (op) |
|
1735 { |
2375
|
1736 case EXPR_AND_AND: |
2805
|
1737 t = tree_boolean_expression::bool_and; |
578
|
1738 break; |
777
|
1739 |
2375
|
1740 case EXPR_OR_OR: |
2805
|
1741 t = tree_boolean_expression::bool_or; |
578
|
1742 break; |
777
|
1743 |
578
|
1744 default: |
|
1745 panic_impossible (); |
|
1746 break; |
|
1747 } |
|
1748 |
|
1749 int l = tok_val->line (); |
|
1750 int c = tok_val->column (); |
|
1751 |
2533
|
1752 tree_boolean_expression *e |
|
1753 = new tree_boolean_expression (op1, op2, l, c, t); |
2375
|
1754 |
2533
|
1755 return fold (e); |
578
|
1756 } |
|
1757 |
2375
|
1758 // Build a prefix expression. |
666
|
1759 |
578
|
1760 static tree_expression * |
2375
|
1761 make_prefix_op (int op, tree_identifier *op1, token *tok_val) |
578
|
1762 { |
2375
|
1763 tree_prefix_expression::type t; |
|
1764 |
578
|
1765 switch (op) |
|
1766 { |
|
1767 case PLUS_PLUS: |
2375
|
1768 t = tree_prefix_expression::increment; |
578
|
1769 break; |
777
|
1770 |
578
|
1771 case MINUS_MINUS: |
2375
|
1772 t = tree_prefix_expression::decrement; |
578
|
1773 break; |
777
|
1774 |
578
|
1775 default: |
|
1776 panic_impossible (); |
|
1777 break; |
|
1778 } |
|
1779 |
|
1780 int l = tok_val->line (); |
|
1781 int c = tok_val->column (); |
|
1782 |
2375
|
1783 return new tree_prefix_expression (op1, l, c, t); |
578
|
1784 } |
|
1785 |
2375
|
1786 // Build a postfix expression. |
666
|
1787 |
578
|
1788 static tree_expression * |
2375
|
1789 make_postfix_op (int op, tree_identifier *op1, token *tok_val) |
578
|
1790 { |
2375
|
1791 tree_postfix_expression::type t; |
1623
|
1792 |
578
|
1793 switch (op) |
|
1794 { |
2375
|
1795 case PLUS_PLUS: |
|
1796 t = tree_postfix_expression::increment; |
578
|
1797 break; |
777
|
1798 |
2375
|
1799 case MINUS_MINUS: |
|
1800 t = tree_postfix_expression::decrement; |
578
|
1801 break; |
777
|
1802 |
578
|
1803 default: |
|
1804 panic_impossible (); |
|
1805 break; |
|
1806 } |
|
1807 |
|
1808 int l = tok_val->line (); |
|
1809 int c = tok_val->column (); |
|
1810 |
2375
|
1811 return new tree_postfix_expression (op1, l, c, t); |
|
1812 } |
|
1813 |
|
1814 // Build a unary expression. |
|
1815 |
|
1816 static tree_expression * |
|
1817 make_unary_op (int op, tree_expression *op1, token *tok_val) |
|
1818 { |
|
1819 tree_unary_expression::type t; |
|
1820 |
|
1821 switch (op) |
|
1822 { |
|
1823 case QUOTE: |
|
1824 t = tree_unary_expression::hermitian; |
|
1825 break; |
|
1826 |
|
1827 case TRANSPOSE: |
|
1828 t = tree_unary_expression::transpose; |
|
1829 break; |
|
1830 |
|
1831 case EXPR_NOT: |
2805
|
1832 t = tree_unary_expression::unot; |
2375
|
1833 break; |
|
1834 |
|
1835 case '-': |
|
1836 t = tree_unary_expression::uminus; |
|
1837 break; |
|
1838 |
|
1839 default: |
|
1840 panic_impossible (); |
|
1841 break; |
|
1842 } |
|
1843 |
|
1844 int l = tok_val->line (); |
|
1845 int c = tok_val->column (); |
|
1846 |
2533
|
1847 tree_unary_expression *e |
|
1848 = new tree_unary_expression (op1, l, c, t); |
1623
|
1849 |
2533
|
1850 return fold (e); |
1623
|
1851 } |
|
1852 |
|
1853 // Build an unwind-protect command. |
|
1854 |
|
1855 static tree_command * |
|
1856 make_unwind_command (token *unwind_tok, tree_statement_list *body, |
|
1857 tree_statement_list *cleanup, token *end_tok) |
|
1858 { |
|
1859 tree_command *retval = 0; |
|
1860 |
|
1861 if (! check_end (end_tok, token::unwind_protect_end)) |
|
1862 { |
|
1863 int l = unwind_tok->line (); |
|
1864 int c = unwind_tok->column (); |
|
1865 |
|
1866 retval = new tree_unwind_protect_command (body, cleanup, l, c); |
|
1867 } |
|
1868 |
|
1869 return retval; |
|
1870 } |
|
1871 |
|
1872 // Build a try-catch command. |
|
1873 |
|
1874 static tree_command * |
|
1875 make_try_command (token *try_tok, tree_statement_list *body, |
|
1876 tree_statement_list *cleanup, token *end_tok) |
|
1877 { |
|
1878 tree_command *retval = 0; |
|
1879 |
|
1880 if (! check_end (end_tok, token::try_catch_end)) |
|
1881 { |
|
1882 int l = try_tok->line (); |
|
1883 int c = try_tok->column (); |
|
1884 |
|
1885 retval = new tree_try_catch_command (body, cleanup, l, c); |
|
1886 } |
|
1887 |
|
1888 return retval; |
|
1889 } |
|
1890 |
|
1891 // Build a while command. |
|
1892 |
|
1893 static tree_command * |
|
1894 make_while_command (token *while_tok, tree_expression *expr, |
|
1895 tree_statement_list *body, token *end_tok) |
|
1896 { |
|
1897 tree_command *retval = 0; |
|
1898 |
|
1899 maybe_warn_assign_as_truth_value (expr); |
|
1900 |
|
1901 if (! check_end (end_tok, token::while_end)) |
|
1902 { |
1826
|
1903 lexer_flags.looping--; |
1623
|
1904 |
|
1905 int l = while_tok->line (); |
|
1906 int c = while_tok->column (); |
|
1907 |
|
1908 retval = new tree_while_command (expr, body, l, c); |
|
1909 } |
|
1910 |
|
1911 return retval; |
|
1912 } |
|
1913 |
|
1914 // Build a for command. |
|
1915 |
|
1916 static tree_command * |
|
1917 make_for_command (token *for_tok, tree_index_expression *var, |
|
1918 tree_expression *expr, tree_statement_list *body, |
|
1919 token *end_tok) |
|
1920 { |
|
1921 tree_command *retval = 0; |
|
1922 |
|
1923 if (! check_end (end_tok, token::for_end)) |
|
1924 { |
1826
|
1925 lexer_flags.looping--; |
1623
|
1926 |
|
1927 int l = for_tok->line (); |
|
1928 int c = for_tok->column (); |
|
1929 |
|
1930 retval = new tree_for_command (var, expr, body, l, c); |
|
1931 } |
|
1932 |
|
1933 return retval; |
|
1934 } |
|
1935 |
|
1936 // Build a for command a different way. |
|
1937 |
|
1938 static tree_command * |
1829
|
1939 make_for_command (token *for_tok, tree_matrix_row *mr, |
|
1940 tree_expression *expr, tree_statement_list *body, |
|
1941 token *end_tok) |
1623
|
1942 { |
|
1943 tree_command *retval = 0; |
|
1944 |
|
1945 if (! check_end (end_tok, token::for_end)) |
|
1946 { |
1826
|
1947 lexer_flags.looping--; |
1623
|
1948 |
1829
|
1949 tree_return_list *id_list = mr->to_return_list (); |
1623
|
1950 |
|
1951 int l = for_tok->line (); |
|
1952 int c = for_tok->column (); |
|
1953 |
|
1954 retval = new tree_for_command (id_list, expr, body, l, c); |
|
1955 } |
|
1956 |
|
1957 return retval; |
|
1958 } |
|
1959 |
|
1960 // Build a break command. |
|
1961 |
|
1962 static tree_command * |
|
1963 make_break_command (token *break_tok) |
|
1964 { |
|
1965 tree_command *retval = 0; |
|
1966 |
2620
|
1967 int l = break_tok->line (); |
|
1968 int c = break_tok->column (); |
|
1969 |
|
1970 if (lexer_flags.looping || lexer_flags.defining_func || reading_script_file) |
|
1971 retval = new tree_break_command (l, c); |
1623
|
1972 else |
2620
|
1973 retval = new tree_no_op_command ("break", l, c); |
1623
|
1974 |
|
1975 return retval; |
|
1976 } |
|
1977 |
|
1978 // Build a continue command. |
|
1979 |
|
1980 static tree_command * |
|
1981 make_continue_command (token *continue_tok) |
|
1982 { |
|
1983 tree_command *retval = 0; |
|
1984 |
2620
|
1985 int l = continue_tok->line (); |
|
1986 int c = continue_tok->column (); |
|
1987 |
|
1988 if (lexer_flags.looping) |
|
1989 retval = new tree_continue_command (l, c); |
1623
|
1990 else |
2620
|
1991 retval = new tree_no_op_command ("continue", l, c); |
1623
|
1992 |
|
1993 return retval; |
|
1994 } |
|
1995 |
|
1996 // Build a return command. |
|
1997 |
|
1998 static tree_command * |
|
1999 make_return_command (token *return_tok) |
|
2000 { |
|
2001 tree_command *retval = 0; |
|
2002 |
2620
|
2003 int l = return_tok->line (); |
|
2004 int c = return_tok->column (); |
|
2005 |
|
2006 if (lexer_flags.defining_func || reading_script_file) |
|
2007 retval = new tree_return_command (l, c); |
1623
|
2008 else |
2620
|
2009 retval = new tree_no_op_command ("return", l, c); |
1623
|
2010 |
|
2011 return retval; |
|
2012 } |
|
2013 |
|
2014 // Start an if command. |
|
2015 |
|
2016 static tree_if_command_list * |
|
2017 start_if_command (tree_expression *expr, tree_statement_list *list) |
|
2018 { |
|
2019 maybe_warn_assign_as_truth_value (expr); |
|
2020 |
|
2021 tree_if_clause *t = new tree_if_clause (expr, list); |
|
2022 |
|
2023 return new tree_if_command_list (t); |
|
2024 } |
|
2025 |
|
2026 // Finish an if command. |
|
2027 |
|
2028 static tree_if_command * |
|
2029 finish_if_command (token *if_tok, tree_if_command_list *list, |
|
2030 token *end_tok) |
|
2031 { |
|
2032 tree_if_command *retval = 0; |
|
2033 |
|
2034 if (! check_end (end_tok, token::if_end)) |
|
2035 { |
|
2036 int l = if_tok->line (); |
|
2037 int c = if_tok->column (); |
|
2038 |
|
2039 retval = new tree_if_command (list, l, c); |
|
2040 } |
|
2041 |
|
2042 return retval; |
|
2043 } |
|
2044 |
|
2045 // Build an elseif clause. |
|
2046 |
|
2047 static tree_if_clause * |
|
2048 make_elseif_clause (tree_expression *expr, tree_statement_list *list) |
|
2049 { |
|
2050 maybe_warn_assign_as_truth_value (expr); |
|
2051 |
|
2052 return new tree_if_clause (expr, list); |
|
2053 } |
|
2054 |
2764
|
2055 // Finish a switch command. |
|
2056 |
|
2057 static tree_switch_command * |
|
2058 finish_switch_command (token *switch_tok, tree_expression *expr, |
|
2059 tree_switch_case_list *list, token *end_tok) |
|
2060 { |
|
2061 tree_switch_command *retval = 0; |
|
2062 |
|
2063 if (! check_end (end_tok, token::switch_end)) |
|
2064 { |
|
2065 int l = switch_tok->line (); |
|
2066 int c = switch_tok->column (); |
|
2067 |
|
2068 retval = new tree_switch_command (expr, list, l, c); |
|
2069 } |
|
2070 |
|
2071 return retval; |
|
2072 } |
|
2073 |
|
2074 // Build a switch case. |
|
2075 |
|
2076 static tree_switch_case * |
|
2077 make_switch_case (tree_expression *expr, tree_statement_list *list) |
|
2078 { |
|
2079 maybe_warn_variable_switch_label (expr); |
|
2080 |
|
2081 return new tree_switch_case (expr, list); |
|
2082 } |
|
2083 |
1623
|
2084 // Build an assignment to a variable. |
|
2085 |
|
2086 static tree_expression * |
|
2087 make_simple_assignment (tree_index_expression *var, token *eq_tok, |
|
2088 tree_expression *expr) |
|
2089 { |
|
2090 int l = eq_tok->line (); |
|
2091 int c = eq_tok->column (); |
|
2092 |
|
2093 return new tree_simple_assignment_expression (var, expr, 0, 0, l, c); |
578
|
2094 } |
666
|
2095 |
|
2096 // Make an expression that handles assignment of multiple values. |
|
2097 |
|
2098 static tree_expression * |
1829
|
2099 make_multi_val_ret (tree_matrix_row *mr, tree_expression *rhs, token *eq_tok) |
666
|
2100 { |
|
2101 // Convert the matrix list to a list of identifiers. If that fails, |
|
2102 // we can abort here, without losing anything -- no other possible |
|
2103 // syntax is valid if we've seen the equals sign as the next token |
|
2104 // after the `]'. |
|
2105 |
|
2106 tree_expression *retval = 0; |
|
2107 |
1826
|
2108 lexer_flags.maybe_screwed_again--; |
666
|
2109 |
1829
|
2110 tree_return_list *id_list = mr->to_return_list (); |
666
|
2111 |
|
2112 if (id_list) |
|
2113 { |
|
2114 int list_len = id_list->length (); |
|
2115 |
|
2116 if (list_len == 1) |
|
2117 { |
|
2118 tree_index_expression *lhs = id_list->remove_front (); |
1623
|
2119 |
|
2120 int l = eq_tok->line (); |
|
2121 int c = eq_tok->column (); |
|
2122 |
947
|
2123 retval = new tree_simple_assignment_expression (lhs, rhs, |
|
2124 0, 0, l, c); |
666
|
2125 } |
|
2126 else if (list_len > 1) |
|
2127 { |
|
2128 if (rhs->is_multi_val_ret_expression ()) |
|
2129 { |
|
2130 tree_multi_val_ret *t = (tree_multi_val_ret *) rhs; |
1623
|
2131 |
|
2132 int l = eq_tok->line (); |
|
2133 int c = eq_tok->column (); |
|
2134 |
1229
|
2135 retval = new tree_multi_assignment_expression (id_list, t, |
|
2136 0, l, c); |
666
|
2137 } |
|
2138 else |
1005
|
2139 yyerror ("RHS must be an expression that returns multiple values"); |
666
|
2140 } |
|
2141 else |
|
2142 panic_impossible (); |
|
2143 } |
|
2144 else |
1005
|
2145 yyerror ("invalid identifier list for assignment"); |
666
|
2146 |
|
2147 return retval; |
|
2148 } |
751
|
2149 |
1623
|
2150 // Begin defining a function. |
|
2151 |
|
2152 static tree_function * |
|
2153 start_function_def (tree_parameter_list *param_list, |
|
2154 tree_statement_list *body) |
|
2155 { |
|
2156 body->mark_as_function_body (); |
|
2157 |
|
2158 tree_function *fcn = new tree_function (body, curr_sym_tab); |
|
2159 |
|
2160 fcn->define_param_list (param_list); |
|
2161 |
|
2162 return fcn; |
|
2163 } |
|
2164 |
|
2165 // Do most of the work for defining a function. |
|
2166 |
|
2167 static tree_function * |
|
2168 frob_function_def (tree_identifier *id, tree_function *fcn) |
|
2169 { |
1755
|
2170 string id_name = id->name (); |
1623
|
2171 |
|
2172 // If input is coming from a file, issue a warning if the name of |
|
2173 // the file does not match the name of the function stated in the |
|
2174 // file. Matlab doesn't provide a diagnostic (it ignores the stated |
|
2175 // name). |
|
2176 |
|
2177 fcn->stash_function_name (id_name); |
|
2178 |
|
2179 if (reading_fcn_file) |
|
2180 { |
1755
|
2181 if (curr_fcn_file_name != id_name) |
1623
|
2182 { |
2166
|
2183 if (Vwarn_function_name_clash) |
1623
|
2184 warning ("function name `%s' does not agree with function\ |
1755
|
2185 file name `%s'", id_name.c_str (), curr_fcn_file_full_name.c_str ()); |
1623
|
2186 |
|
2187 global_sym_tab->rename (id_name, curr_fcn_file_name); |
|
2188 |
|
2189 if (error_state) |
|
2190 return 0; |
|
2191 |
|
2192 id_name = id->name (); |
|
2193 } |
|
2194 |
|
2195 fcn->stash_function_name (id_name); |
|
2196 fcn->stash_fcn_file_name (); |
|
2197 fcn->stash_fcn_file_time (time (0)); |
|
2198 fcn->mark_as_system_fcn_file (); |
|
2199 } |
|
2200 else if (! (input_from_tmp_history_file || input_from_startup_file) |
|
2201 && reading_script_file |
1755
|
2202 && curr_fcn_file_name == id_name) |
1623
|
2203 { |
|
2204 warning ("function `%s' defined within script file `%s'", |
1755
|
2205 id_name.c_str (), curr_fcn_file_full_name.c_str ()); |
1623
|
2206 } |
|
2207 |
|
2208 top_level_sym_tab->clear (id_name); |
|
2209 |
2791
|
2210 symbol_record *sr = global_sym_tab->lookup (id_name, 0, 0); |
|
2211 |
|
2212 if (sr) |
|
2213 fcn->stash_symtab_ptr (sr); |
|
2214 else |
|
2215 panic_impossible (); |
|
2216 |
1623
|
2217 id->define (fcn); |
1755
|
2218 |
1623
|
2219 id->document (help_buf); |
|
2220 |
|
2221 return fcn; |
|
2222 } |
|
2223 |
|
2224 // Finish defining a function. |
|
2225 |
|
2226 static tree_function * |
|
2227 finish_function_def (token *var, tree_function *fcn) |
|
2228 { |
|
2229 symbol_record *sr = var->sym_rec (); |
|
2230 |
|
2231 int l = var->line (); |
|
2232 int c = var->column (); |
|
2233 |
|
2234 tree_identifier *tmp = new tree_identifier (sr, l, c); |
|
2235 |
|
2236 tree_parameter_list *tpl = new tree_parameter_list (tmp); |
|
2237 |
|
2238 tpl->mark_as_formal_parameters (); |
|
2239 |
|
2240 return fcn->define_ret_list (tpl); |
|
2241 } |
|
2242 |
|
2243 // Finish defining a function a different way. |
|
2244 |
|
2245 static tree_function * |
|
2246 finish_function_def (tree_parameter_list *ret_list, tree_function *fcn) |
|
2247 { |
|
2248 ret_list->mark_as_formal_parameters (); |
|
2249 |
|
2250 return fcn->define_ret_list (ret_list); |
|
2251 } |
|
2252 |
2846
|
2253 // Make an index expression. |
|
2254 |
751
|
2255 static tree_index_expression * |
|
2256 make_index_expression (tree_indirect_ref *indir, tree_argument_list *args) |
|
2257 { |
|
2258 tree_index_expression *retval = 0; |
|
2259 |
|
2260 int l = indir->line (); |
|
2261 int c = indir->column (); |
|
2262 |
|
2263 if (indir->is_identifier_only ()) |
|
2264 { |
|
2265 indir->preserve_identifier (); |
|
2266 retval = new tree_index_expression (indir->ident (), args, l, c); |
|
2267 delete indir; |
|
2268 } |
|
2269 else |
|
2270 retval = new tree_index_expression (indir, args, l, c); |
|
2271 |
|
2272 return retval; |
|
2273 } |
1511
|
2274 |
2846
|
2275 // Make a declaration command. |
|
2276 |
|
2277 static tree_decl_command * |
|
2278 make_decl_command (int tok, token *tok_val, tree_decl_init_list *lst) |
|
2279 { |
|
2280 tree_decl_command *retval = 0; |
|
2281 |
|
2282 int l = tok_val->line (); |
|
2283 int c = tok_val->column (); |
|
2284 |
|
2285 switch (tok) |
|
2286 { |
|
2287 case GLOBAL: |
|
2288 retval = new tree_global_command (lst, l, c); |
|
2289 break; |
|
2290 |
|
2291 case STATIC: |
|
2292 if (lexer_flags.defining_func) |
|
2293 retval = new tree_static_command (lst, l, c); |
|
2294 else |
|
2295 { |
|
2296 if (reading_script_file) |
|
2297 warning ("ignoring static declaration near line %d of file `%s'", |
|
2298 l, curr_fcn_file_full_name.c_str ()); |
|
2299 else |
|
2300 warning ("ignoring static declaration near line %d", l); |
|
2301 } |
|
2302 break; |
|
2303 |
|
2304 default: |
|
2305 panic_impossible (); |
|
2306 break; |
|
2307 } |
|
2308 |
|
2309 return retval; |
|
2310 } |
|
2311 |
1623
|
2312 // Finish building a matrix list. |
|
2313 |
|
2314 static tree_expression * |
1829
|
2315 finish_matrix (tree_matrix *m) |
1623
|
2316 { |
|
2317 tree_expression *retval = 0; |
|
2318 |
1826
|
2319 lexer_flags.maybe_screwed_again--; |
1623
|
2320 |
2533
|
2321 if (m->all_elements_are_constant ()) |
1829
|
2322 { |
2086
|
2323 octave_value tmp = m->eval (0); |
1623
|
2324 |
2533
|
2325 if (! error_state) |
|
2326 { |
|
2327 tree_constant *tc_retval = new tree_constant (tmp); |
|
2328 |
|
2329 ostrstream buf; |
|
2330 |
|
2331 tree_print_code tpc (buf); |
|
2332 |
|
2333 m->accept (tpc); |
|
2334 |
|
2335 buf << ends; |
1623
|
2336 |
2533
|
2337 char *s = buf.str (); |
|
2338 |
|
2339 tc_retval->stash_original_text (s); |
|
2340 |
|
2341 delete [] s; |
|
2342 |
|
2343 delete m; |
|
2344 |
|
2345 retval = tc_retval; |
|
2346 } |
|
2347 else |
|
2348 delete m; |
1623
|
2349 } |
|
2350 else |
1829
|
2351 retval = m; |
1623
|
2352 |
|
2353 return retval; |
|
2354 } |
|
2355 |
1511
|
2356 static void |
|
2357 maybe_warn_missing_semi (tree_statement_list *t) |
|
2358 { |
2166
|
2359 if (lexer_flags.defining_func && Vwarn_missing_semicolon) |
1511
|
2360 { |
|
2361 tree_statement *tmp = t->rear(); |
1607
|
2362 |
1511
|
2363 if (tmp->is_expression ()) |
1607
|
2364 warning ("missing semicolon near line %d, column %d in file `%s'", |
1755
|
2365 tmp->line (), tmp->column (), |
|
2366 curr_fcn_file_full_name.c_str ()); |
1511
|
2367 } |
|
2368 } |
1994
|
2369 |
2525
|
2370 static void |
|
2371 set_stmt_print_flag (tree_statement_list *list, char sep, |
|
2372 bool warn_missing_semi) |
|
2373 { |
|
2374 switch (sep) |
|
2375 { |
|
2376 case ';': |
|
2377 { |
|
2378 tree_statement *tmp = list->rear (); |
|
2379 tmp->set_print_flag (0); |
|
2380 } |
|
2381 break; |
|
2382 |
|
2383 case 0: |
|
2384 case ',': |
|
2385 case '\n': |
|
2386 if (warn_missing_semi) |
|
2387 maybe_warn_missing_semi (list); |
|
2388 break; |
|
2389 |
|
2390 default: |
|
2391 warning ("unrecognized separator type!"); |
|
2392 break; |
|
2393 } |
|
2394 } |
|
2395 |
2846
|
2396 static void |
|
2397 maybe_warn_comma_in_decl (void) |
|
2398 { |
|
2399 if (Vwarn_comma_in_declaration)\ |
|
2400 { |
|
2401 warning ("comma in declaration not interpreted as a command separator"); |
|
2402 |
|
2403 if (reading_fcn_file || reading_script_file) |
|
2404 warning ("near line %d of file `%s'", input_line_number, |
|
2405 curr_fcn_file_full_name.c_str ()); |
|
2406 } |
|
2407 } |
|
2408 |
2166
|
2409 static int |
|
2410 warn_assign_as_truth_value (void) |
|
2411 { |
|
2412 Vwarn_assign_as_truth_value |
|
2413 = check_preference ("warn_assign_as_truth_value"); |
|
2414 |
|
2415 return 0; |
|
2416 } |
|
2417 |
|
2418 static int |
2846
|
2419 warn_comma_in_declaration (void) |
2166
|
2420 { |
2846
|
2421 Vwarn_comma_in_declaration = check_preference ("warn_comma_in_declaration"); |
2166
|
2422 |
|
2423 return 0; |
|
2424 } |
|
2425 |
|
2426 static int |
|
2427 warn_function_name_clash (void) |
|
2428 { |
|
2429 Vwarn_function_name_clash = check_preference ("warn_function_name_clash"); |
|
2430 |
|
2431 return 0; |
|
2432 } |
|
2433 |
|
2434 static int |
|
2435 warn_missing_semicolon (void) |
|
2436 { |
|
2437 Vwarn_missing_semicolon = check_preference ("warn_missing_semicolon"); |
|
2438 |
|
2439 return 0; |
|
2440 } |
|
2441 |
2764
|
2442 static int |
|
2443 warn_variable_switch_label (void) |
|
2444 { |
|
2445 Vwarn_variable_switch_label |
|
2446 = check_preference ("warn_variable_switch_label"); |
|
2447 |
|
2448 return 0; |
|
2449 } |
|
2450 |
2166
|
2451 void |
|
2452 symbols_of_parse (void) |
|
2453 { |
|
2454 DEFVAR (warn_assign_as_truth_value, 1.0, 0, warn_assign_as_truth_value, |
|
2455 "produce warning for assignments used as truth values"); |
|
2456 |
2846
|
2457 DEFVAR (warn_comma_in_declaration, 1.0, 0, warn_comma_in_declaration, |
|
2458 "produce warning for commas in declaration statements"); |
2166
|
2459 |
|
2460 DEFVAR (warn_function_name_clash, 1.0, 0, warn_function_name_clash, |
|
2461 "produce warning if function name conflicts with file name"); |
|
2462 |
|
2463 DEFVAR (warn_missing_semicolon, 0.0, 0, warn_missing_semicolon, |
2764
|
2464 "produce a warning if a statement in a function file is not\n\ |
2166
|
2465 terminated with a semicolon"); |
2764
|
2466 |
|
2467 DEFVAR (warn_variable_switch_label, 0.0, 0, warn_variable_switch_label, |
|
2468 "produce warning for variables used as switch labels"); |
2166
|
2469 } |
|
2470 |
1994
|
2471 /* |
|
2472 ;;; Local Variables: *** |
|
2473 ;;; mode: text *** |
|
2474 ;;; End: *** |
|
2475 */ |