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