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 |
3178
|
34 #include <cassert> |
3156
|
35 #include <cstdio> |
|
36 |
2427
|
37 #ifdef YYBYACC |
|
38 #include <cstdlib> |
|
39 #endif |
|
40 |
3928
|
41 #include "Cell.h" |
1
|
42 #include "Matrix.h" |
3021
|
43 #include "cmd-edit.h" |
|
44 #include "cmd-hist.h" |
|
45 #include "file-ops.h" |
|
46 #include "file-stat.h" |
4171
|
47 #include "lo-sstream.h" |
4243
|
48 #include "oct-env.h" |
3712
|
49 #include "oct-time.h" |
4171
|
50 #include "quit.h" |
1
|
51 |
3665
|
52 #include "comment-list.h" |
4243
|
53 #include "defaults.h" |
2166
|
54 #include "defun.h" |
4243
|
55 #include "dirfns.h" |
3021
|
56 #include "dynamic-ld.h" |
1351
|
57 #include "error.h" |
|
58 #include "input.h" |
|
59 #include "lex.h" |
1743
|
60 #include "oct-hist.h" |
2970
|
61 #include "ov-usr-fcn.h" |
1670
|
62 #include "toplev.h" |
1351
|
63 #include "pager.h" |
|
64 #include "parse.h" |
2987
|
65 #include "pt-all.h" |
1351
|
66 #include "symtab.h" |
|
67 #include "token.h" |
3021
|
68 #include "unwind-prot.h" |
1
|
69 #include "utils.h" |
1351
|
70 #include "variables.h" |
1
|
71 |
3021
|
72 // TRUE means we print |
|
73 static bool Vdefault_eval_print_flag = true; |
|
74 |
2166
|
75 // If TRUE, generate a warning for the assignment in things like |
|
76 // |
|
77 // octave> if (a = 2 < n) |
|
78 // |
|
79 // but not |
|
80 // |
|
81 // octave> if ((a = 2) < n) |
|
82 // |
|
83 static bool Vwarn_assign_as_truth_value; |
|
84 |
2764
|
85 // If TRUE, generate a warning for variable swich labels. |
|
86 static bool Vwarn_variable_switch_label; |
|
87 |
2166
|
88 // If TRUE, generate warning if declared function name disagrees with |
|
89 // the name of the file in which it is defined. |
|
90 static bool Vwarn_function_name_clash; |
|
91 |
3162
|
92 // TRUE means warn about function files that have time stamps in the future. |
|
93 bool Vwarn_future_time_stamp; |
|
94 |
2166
|
95 // If TRUE, generate warning if a statement in a function is not |
|
96 // terminated with a semicolon. Useful for checking functions that |
|
97 // should only produce output using explicit printing statements. |
|
98 static bool Vwarn_missing_semicolon; |
|
99 |
4023
|
100 // If TRUE, generate warning about the meaning of code changing due to |
|
101 // changes in precedence levels for various ops (typically for Matlab |
|
102 // compatibility). |
|
103 static bool Vwarn_precedence_change; |
|
104 |
1
|
105 // Temporary symbol table pointer used to cope with bogus function syntax. |
522
|
106 symbol_table *tmp_local_sym_tab = 0; |
1
|
107 |
|
108 // The current input line number. |
|
109 int input_line_number = 0; |
|
110 |
|
111 // The column of the current token. |
143
|
112 int current_input_column = 1; |
1
|
113 |
338
|
114 // Buffer for help text snagged from function files. |
3523
|
115 std::string help_buf; |
1
|
116 |
3665
|
117 // Buffer for comments appearing before a function statement. |
|
118 static std::string fcn_comment_header; |
|
119 |
3021
|
120 // TRUE means we are using readline. |
|
121 // (--no-line-editing) |
|
122 bool line_editing = true; |
|
123 |
|
124 // TRUE means we printed messages about reading startup files. |
|
125 bool reading_startup_message_printed = false; |
|
126 |
|
127 // TRUE means input is coming from startup file. |
|
128 bool input_from_startup_file = false; |
|
129 |
|
130 // TRUE means that input is coming from a file that was named on |
|
131 // the command line. |
|
132 bool input_from_command_line_file = true; |
|
133 |
3489
|
134 // TRUE means that we are in the process of evaluating a function |
|
135 // body. The parser might be called in that case if we are looking at |
|
136 // an eval() statement. |
|
137 bool evaluating_function_body = false; |
|
138 |
4238
|
139 // Keep a count of how many END tokens we expect. |
|
140 int end_tokens_expected = 0; |
|
141 |
3903
|
142 // Keep track of symbol table information when parsing functions. |
4238
|
143 std::stack<symbol_table*> symtab_context; |
|
144 |
|
145 // Name of parent function when parsing function files that might |
|
146 // contain nested functions. |
|
147 std::string parent_function_name; |
3903
|
148 |
496
|
149 // Forward declarations for some functions defined at the bottom of |
|
150 // the file. |
|
151 |
|
152 // Generic error messages. |
2970
|
153 static void |
|
154 yyerror (const char *s); |
1
|
155 |
578
|
156 // Error mesages for mismatched end tokens. |
2970
|
157 static void |
|
158 end_error (const char *type, token::end_tok_type ettype, int l, int c); |
1
|
159 |
578
|
160 // Check to see that end tokens are properly matched. |
2970
|
161 static bool |
|
162 end_token_ok (token *tok, token::end_tok_type expected); |
496
|
163 |
|
164 // Maybe print a warning if an assignment expression is used as the |
|
165 // test in a logical expression. |
2970
|
166 static void |
|
167 maybe_warn_assign_as_truth_value (tree_expression *expr); |
1
|
168 |
2764
|
169 // Maybe print a warning about switch labels that aren't constants. |
2970
|
170 static void |
|
171 maybe_warn_variable_switch_label (tree_expression *expr); |
2764
|
172 |
1623
|
173 // Create a plot command. |
2970
|
174 static tree_plot_command * |
|
175 make_plot_command (token *tok, plot_limits *range, subplot_list *list); |
1623
|
176 |
|
177 // Finish building a range. |
2970
|
178 static tree_expression * |
|
179 finish_colon_expression (tree_colon_expression *e); |
1623
|
180 |
1607
|
181 // Build a constant. |
2970
|
182 static tree_constant * |
|
183 make_constant (int op, token *tok_val); |
1607
|
184 |
578
|
185 // Build a binary expression. |
2970
|
186 static tree_expression * |
|
187 make_binary_op (int op, tree_expression *op1, token *tok_val, |
|
188 tree_expression *op2); |
578
|
189 |
2375
|
190 // Build a boolean expression. |
2970
|
191 static tree_expression * |
|
192 make_boolean_op (int op, tree_expression *op1, token *tok_val, |
|
193 tree_expression *op2); |
2375
|
194 |
578
|
195 // Build a prefix expression. |
2970
|
196 static tree_expression * |
|
197 make_prefix_op (int op, tree_expression *op1, token *tok_val); |
578
|
198 |
|
199 // Build a postfix expression. |
2970
|
200 static tree_expression * |
|
201 make_postfix_op (int op, tree_expression *op1, token *tok_val); |
578
|
202 |
1623
|
203 // Build an unwind-protect command. |
2970
|
204 static tree_command * |
|
205 make_unwind_command (token *unwind_tok, tree_statement_list *body, |
3665
|
206 tree_statement_list *cleanup, token *end_tok, |
|
207 octave_comment_list *lc, octave_comment_list *mc); |
1623
|
208 |
|
209 // Build a try-catch command. |
2970
|
210 static tree_command * |
|
211 make_try_command (token *try_tok, tree_statement_list *body, |
3665
|
212 tree_statement_list *cleanup, token *end_tok, |
|
213 octave_comment_list *lc, octave_comment_list *mc); |
1623
|
214 |
|
215 // Build a while command. |
2970
|
216 static tree_command * |
|
217 make_while_command (token *while_tok, tree_expression *expr, |
3665
|
218 tree_statement_list *body, token *end_tok, |
|
219 octave_comment_list *lc); |
1623
|
220 |
3484
|
221 // Build a do-until command. |
|
222 static tree_command * |
|
223 make_do_until_command (token *do_tok, tree_statement_list *body, |
3665
|
224 tree_expression *expr, octave_comment_list *lc); |
3484
|
225 |
1623
|
226 // Build a for command. |
2970
|
227 static tree_command * |
|
228 make_for_command (token *for_tok, tree_argument_list *lhs, |
|
229 tree_expression *expr, tree_statement_list *body, |
3665
|
230 token *end_tok, octave_comment_list *lc); |
1623
|
231 |
4207
|
232 // Build a break command. |
|
233 static tree_command * |
|
234 make_break_command (token *break_tok); |
|
235 |
|
236 // Build a continue command. |
|
237 static tree_command * |
|
238 make_continue_command (token *continue_tok); |
|
239 |
|
240 // Build a return command. |
|
241 static tree_command * |
|
242 make_return_command (token *return_tok); |
1623
|
243 |
|
244 // Start an if command. |
2970
|
245 static tree_if_command_list * |
|
246 start_if_command (tree_expression *expr, tree_statement_list *list); |
1623
|
247 |
|
248 // Finish an if command. |
2970
|
249 static tree_if_command * |
3665
|
250 finish_if_command (token *if_tok, tree_if_command_list *list, |
|
251 token *end_tok, octave_comment_list *lc); |
1623
|
252 |
|
253 // Build an elseif clause. |
2970
|
254 static tree_if_clause * |
3665
|
255 make_elseif_clause (tree_expression *expr, tree_statement_list *list, |
|
256 octave_comment_list *lc); |
1623
|
257 |
2764
|
258 // Finish a switch command. |
2970
|
259 static tree_switch_command * |
|
260 finish_switch_command (token *switch_tok, tree_expression *expr, |
3665
|
261 tree_switch_case_list *list, token *end_tok, |
|
262 octave_comment_list *lc); |
2764
|
263 |
|
264 // Build a switch case. |
2970
|
265 static tree_switch_case * |
3665
|
266 make_switch_case (tree_expression *expr, tree_statement_list *list, |
|
267 octave_comment_list *lc); |
2764
|
268 |
1623
|
269 // Build an assignment to a variable. |
2970
|
270 static tree_expression * |
|
271 make_assign_op (int op, tree_argument_list *lhs, token *eq_tok, |
|
272 tree_expression *rhs); |
1623
|
273 |
|
274 // Begin defining a function. |
2970
|
275 static octave_user_function * |
|
276 start_function (tree_parameter_list *param_list, tree_statement_list *body); |
1623
|
277 |
|
278 // Do most of the work for defining a function. |
2970
|
279 static octave_user_function * |
|
280 frob_function (tree_identifier *id, octave_user_function *fcn); |
1623
|
281 |
|
282 // Finish defining a function. |
2970
|
283 static octave_user_function * |
3665
|
284 finish_function (tree_identifier *id, octave_user_function *fcn, |
|
285 octave_comment_list *lc); |
1623
|
286 |
|
287 // Finish defining a function a different way. |
2970
|
288 static octave_user_function * |
3665
|
289 finish_function (tree_parameter_list *ret_list, |
|
290 octave_user_function *fcn, octave_comment_list *lc); |
751
|
291 |
2883
|
292 // Reset state after parsing function. |
2970
|
293 static void |
|
294 recover_from_parsing_function (void); |
2883
|
295 |
751
|
296 // Make an index expression. |
2970
|
297 static tree_index_expression * |
3933
|
298 make_index_expression (tree_expression *expr, |
|
299 tree_argument_list *args, char type); |
2970
|
300 |
|
301 // Make an indirect reference expression. |
3930
|
302 static tree_index_expression * |
3523
|
303 make_indirect_ref (tree_expression *expr, const std::string&); |
666
|
304 |
4131
|
305 // Make an indirect reference expression with dynamic field name. |
|
306 static tree_index_expression * |
|
307 make_indirect_ref (tree_expression *expr, tree_expression *field); |
|
308 |
2846
|
309 // Make a declaration command. |
2970
|
310 static tree_decl_command * |
|
311 make_decl_command (int tok, token *tok_val, tree_decl_init_list *lst); |
2846
|
312 |
1623
|
313 // Finish building a matrix list. |
2970
|
314 static tree_expression * |
|
315 finish_matrix (tree_matrix *m); |
1623
|
316 |
3351
|
317 // Finish building a cell list. |
|
318 static tree_expression * |
|
319 finish_cell (tree_cell *c); |
|
320 |
1511
|
321 // Maybe print a warning. Duh. |
2970
|
322 static void |
|
323 maybe_warn_missing_semi (tree_statement_list *); |
1511
|
324 |
2525
|
325 // Set the print flag for a statement based on the separator type. |
2970
|
326 static void |
|
327 set_stmt_print_flag (tree_statement_list *, char, bool); |
2525
|
328 |
1
|
329 #define ABORT_PARSE \ |
|
330 do \ |
|
331 { \ |
522
|
332 global_command = 0; \ |
1
|
333 yyerrok; \ |
4238
|
334 if (! symtab_context.empty ()) \ |
3929
|
335 { \ |
4238
|
336 curr_sym_tab = symtab_context.top (); \ |
|
337 symtab_context.pop (); \ |
3929
|
338 } \ |
2865
|
339 if (interactive || forced_interactive) \ |
1
|
340 YYACCEPT; \ |
|
341 else \ |
|
342 YYABORT; \ |
|
343 } \ |
|
344 while (0) |
|
345 |
|
346 %} |
|
347 |
666
|
348 // Bison declarations. |
|
349 |
1
|
350 %union |
|
351 { |
2891
|
352 // The type of the basic tokens returned by the lexer. |
143
|
353 token *tok_val; |
|
354 |
3665
|
355 // Comment strings that we need to deal with mid-rule. |
|
356 octave_comment_list *comment_type; |
|
357 |
2891
|
358 // Types for the nonterminals we generate. |
2525
|
359 char sep_type; |
1
|
360 tree *tree_type; |
1829
|
361 tree_matrix *tree_matrix_type; |
3351
|
362 tree_cell *tree_cell_type; |
496
|
363 tree_expression *tree_expression_type; |
2375
|
364 tree_constant *tree_constant_type; |
1
|
365 tree_identifier *tree_identifier_type; |
|
366 tree_index_expression *tree_index_expression_type; |
|
367 tree_colon_expression *tree_colon_expression_type; |
|
368 tree_argument_list *tree_argument_list_type; |
|
369 tree_parameter_list *tree_parameter_list_type; |
|
370 tree_command *tree_command_type; |
|
371 tree_if_command *tree_if_command_type; |
578
|
372 tree_if_clause *tree_if_clause_type; |
|
373 tree_if_command_list *tree_if_command_list_type; |
2764
|
374 tree_switch_command *tree_switch_command_type; |
|
375 tree_switch_case *tree_switch_case_type; |
|
376 tree_switch_case_list *tree_switch_case_list_type; |
2846
|
377 tree_decl_elt *tree_decl_elt_type; |
|
378 tree_decl_init_list *tree_decl_init_list_type; |
|
379 tree_decl_command *tree_decl_command_type; |
578
|
380 tree_statement *tree_statement_type; |
|
381 tree_statement_list *tree_statement_list_type; |
1
|
382 tree_plot_command *tree_plot_command_type; |
578
|
383 subplot *subplot_type; |
|
384 subplot_list *subplot_list_type; |
|
385 plot_limits *plot_limits_type; |
|
386 plot_range *plot_range_type; |
|
387 subplot_using *subplot_using_type; |
|
388 subplot_style *subplot_style_type; |
3165
|
389 subplot_axes *subplot_axes_type; |
2891
|
390 octave_user_function *octave_user_function_type; |
1
|
391 } |
|
392 |
143
|
393 // Tokens with line and column information. |
|
394 %token <tok_val> '=' ':' '-' '+' '*' '/' |
4018
|
395 %token <tok_val> ADD_EQ SUB_EQ MUL_EQ DIV_EQ LEFTDIV_EQ POW_EQ |
|
396 %token <tok_val> EMUL_EQ EDIV_EQ ELEFTDIV_EQ EPOW_EQ AND_EQ OR_EQ |
2899
|
397 %token <tok_val> LSHIFT_EQ RSHIFT_EQ LSHIFT RSHIFT |
428
|
398 %token <tok_val> EXPR_AND_AND EXPR_OR_OR |
143
|
399 %token <tok_val> EXPR_AND EXPR_OR EXPR_NOT |
|
400 %token <tok_val> EXPR_LT EXPR_LE EXPR_EQ EXPR_NE EXPR_GE EXPR_GT |
1276
|
401 %token <tok_val> LEFTDIV EMUL EDIV ELEFTDIV EPLUS EMINUS |
|
402 %token <tok_val> QUOTE TRANSPOSE |
143
|
403 %token <tok_val> PLUS_PLUS MINUS_MINUS POW EPOW |
|
404 %token <tok_val> NUM IMAG_NUM |
2970
|
405 %token <tok_val> STRUCT_ELT |
2883
|
406 %token <tok_val> NAME |
143
|
407 %token <tok_val> END |
|
408 %token <tok_val> PLOT |
4064
|
409 %token <tok_val> STRING STYLE AXES_TAG |
3484
|
410 %token <tok_val> FOR WHILE DO UNTIL |
1491
|
411 %token <tok_val> IF ELSEIF ELSE |
2764
|
412 %token <tok_val> SWITCH CASE OTHERWISE |
1491
|
413 %token <tok_val> BREAK CONTINUE FUNC_RET |
924
|
414 %token <tok_val> UNWIND CLEANUP |
1489
|
415 %token <tok_val> TRY CATCH |
2846
|
416 %token <tok_val> GLOBAL STATIC |
1
|
417 |
143
|
418 // Other tokens. |
2970
|
419 %token END_OF_INPUT LEXICAL_ERROR |
3973
|
420 %token FCN VARARGIN VARARGOUT ALL_VA_ARGS |
3165
|
421 %token USING TITLE WITH AXES COLON OPEN_BRACE CLOSE_BRACE CLEAR |
1
|
422 |
143
|
423 // Nonterminals we construct. |
3665
|
424 %type <comment_type> stash_comment function_beg |
2525
|
425 %type <sep_type> sep_no_nl opt_sep_no_nl sep opt_sep |
578
|
426 %type <tree_type> input |
2960
|
427 %type <tree_constant_type> constant magic_colon |
3351
|
428 %type <tree_matrix_type> matrix_rows matrix_rows1 |
|
429 %type <tree_cell_type> cell_rows cell_rows1 |
|
430 %type <tree_expression_type> title matrix cell |
4207
|
431 %type <tree_expression_type> primary_expr postfix_expr prefix_expr binary_expr |
|
432 %type <tree_expression_type> simple_expr colon_expr assign_expr expression |
4238
|
433 %type <tree_identifier_type> identifier fcn_name |
2891
|
434 %type <octave_user_function_type> function1 function2 function3 |
2970
|
435 %type <tree_index_expression_type> word_list_cmd |
|
436 %type <tree_colon_expression_type> colon_expr1 |
3351
|
437 %type <tree_argument_list_type> arg_list word_list assign_lhs |
|
438 %type <tree_argument_list_type> cell_or_matrix_row |
723
|
439 %type <tree_parameter_list_type> param_list param_list1 |
|
440 %type <tree_parameter_list_type> return_list return_list1 |
2970
|
441 %type <tree_command_type> command select_command loop_command |
4207
|
442 %type <tree_command_type> jump_command except_command function |
578
|
443 %type <tree_if_command_type> if_command |
|
444 %type <tree_if_clause_type> elseif_clause else_clause |
|
445 %type <tree_if_command_list_type> if_cmd_list1 if_cmd_list |
2764
|
446 %type <tree_switch_command_type> switch_command |
|
447 %type <tree_switch_case_type> switch_case default_case |
|
448 %type <tree_switch_case_list_type> case_list1 case_list |
2846
|
449 %type <tree_decl_elt_type> decl2 |
|
450 %type <tree_decl_init_list_type> decl1 |
|
451 %type <tree_decl_command_type> declaration |
578
|
452 %type <tree_statement_type> statement |
627
|
453 %type <tree_statement_list_type> simple_list simple_list1 list list1 |
2891
|
454 %type <tree_statement_list_type> opt_list input1 function4 |
1
|
455 %type <tree_plot_command_type> plot_command |
578
|
456 %type <subplot_type> plot_command2 plot_options |
|
457 %type <subplot_list_type> plot_command1 |
|
458 %type <plot_limits_type> ranges |
|
459 %type <plot_range_type> ranges1 |
|
460 %type <subplot_using_type> using using1 |
|
461 %type <subplot_style_type> style |
3165
|
462 %type <subplot_axes_type> axes |
1
|
463 |
143
|
464 // Precedence and associativity. |
1
|
465 %left ';' ',' '\n' |
4018
|
466 %right '=' ADD_EQ SUB_EQ MUL_EQ DIV_EQ LEFTDIV_EQ POW_EQ EMUL_EQ EDIV_EQ ELEFTDIV_EQ EPOW_EQ OR_EQ AND_EQ LSHIFT_EQ RSHIFT_EQ |
4023
|
467 %left EXPR_OR_OR |
|
468 %left EXPR_AND_AND |
|
469 %left EXPR_OR |
|
470 %left EXPR_AND |
1
|
471 %left EXPR_LT EXPR_LE EXPR_EQ EXPR_NE EXPR_GE EXPR_GT |
2899
|
472 %left LSHIFT RSHIFT |
1
|
473 %left ':' |
1276
|
474 %left '-' '+' EPLUS EMINUS |
1
|
475 %left '*' '/' LEFTDIV EMUL EDIV ELEFTDIV |
|
476 %left QUOTE TRANSPOSE |
|
477 %left UNARY PLUS_PLUS MINUS_MINUS EXPR_NOT |
|
478 %right POW EPOW |
3351
|
479 %left '(' '.' '{' |
1
|
480 |
143
|
481 // Where to start. |
1
|
482 %start input |
|
483 |
|
484 %% |
|
485 |
2970
|
486 // ============================== |
|
487 // Statements and statement lists |
|
488 // ============================== |
|
489 |
627
|
490 input : input1 |
1
|
491 { |
627
|
492 global_command = $1; |
1
|
493 promptflag = 1; |
|
494 YYACCEPT; |
|
495 } |
627
|
496 | simple_list parse_error |
1091
|
497 { ABORT_PARSE; } |
627
|
498 | parse_error |
1091
|
499 { ABORT_PARSE; } |
627
|
500 ; |
|
501 |
|
502 input1 : '\n' |
|
503 { $$ = 0; } |
3883
|
504 | END_OF_INPUT |
|
505 { |
|
506 parser_end_of_input = 1; |
|
507 $$ = 0; |
|
508 } |
327
|
509 | simple_list |
627
|
510 { $$ = $1; } |
1
|
511 | simple_list '\n' |
627
|
512 { $$ = $1; } |
1
|
513 | simple_list END_OF_INPUT |
627
|
514 { $$ = $1; } |
|
515 ; |
|
516 |
2525
|
517 simple_list : simple_list1 opt_sep_no_nl |
1
|
518 { |
2525
|
519 set_stmt_print_flag ($1, $2, false); |
1511
|
520 $$ = $1; |
1
|
521 } |
|
522 ; |
|
523 |
578
|
524 simple_list1 : statement |
|
525 { $$ = new tree_statement_list ($1); } |
2525
|
526 | simple_list1 sep_no_nl statement |
1
|
527 { |
2525
|
528 set_stmt_print_flag ($1, $2, false); |
578
|
529 $1->append ($3); |
1511
|
530 $$ = $1; |
1
|
531 } |
|
532 ; |
|
533 |
|
534 opt_list : // empty |
578
|
535 { $$ = new tree_statement_list (); } |
1
|
536 | list |
|
537 { $$ = $1; } |
496
|
538 ; |
1
|
539 |
2525
|
540 list : list1 opt_sep |
1511
|
541 { |
2525
|
542 set_stmt_print_flag ($1, $2, true); |
1829
|
543 $$ = $1; |
1
|
544 } |
|
545 ; |
|
546 |
578
|
547 list1 : statement |
440
|
548 { |
2857
|
549 lexer_flags.beginning_of_function = false; |
578
|
550 $$ = new tree_statement_list ($1); |
440
|
551 } |
2525
|
552 | list1 sep statement |
1511
|
553 { |
2525
|
554 set_stmt_print_flag ($1, $2, true); |
578
|
555 $1->append ($3); |
1511
|
556 $$ = $1; |
1
|
557 } |
|
558 ; |
|
559 |
2970
|
560 statement : expression |
3665
|
561 { |
|
562 octave_comment_list *comment |
|
563 = octave_comment_buffer::get_comment (); |
|
564 |
|
565 $$ = new tree_statement ($1, comment); |
|
566 } |
2970
|
567 | command |
3665
|
568 { |
|
569 octave_comment_list *comment |
|
570 = octave_comment_buffer::get_comment (); |
|
571 |
|
572 $$ = new tree_statement ($1, comment); |
|
573 } |
883
|
574 | PLOT CLEAR |
|
575 { |
|
576 symbol_record *sr = lookup_by_name ("clearplot", 0); |
|
577 tree_identifier *id = new tree_identifier (sr); |
3665
|
578 |
|
579 octave_comment_list *comment |
|
580 = octave_comment_buffer::get_comment (); |
|
581 |
|
582 $$ = new tree_statement (id, comment); |
883
|
583 } |
1
|
584 ; |
|
585 |
2970
|
586 // =========== |
|
587 // Expressions |
|
588 // =========== |
|
589 |
|
590 identifier : NAME |
|
591 { |
|
592 $$ = new tree_identifier |
|
593 ($1->sym_rec (), $1->line (), $1->column ()); |
|
594 } |
|
595 ; |
|
596 |
|
597 constant : NUM |
|
598 { $$ = make_constant (NUM, $1); } |
|
599 | IMAG_NUM |
|
600 { $$ = make_constant (IMAG_NUM, $1); } |
4064
|
601 | STRING |
|
602 { $$ = make_constant (STRING, $1); } |
2970
|
603 ; |
|
604 |
3189
|
605 in_matrix_or_assign_lhs |
|
606 : // empty |
|
607 { lexer_flags.looking_at_matrix_or_assign_lhs = true; } |
|
608 ; |
|
609 |
2970
|
610 matrix : '[' ']' |
|
611 { $$ = new tree_constant (octave_value (Matrix ())); } |
|
612 | '[' ';' ']' |
|
613 { $$ = new tree_constant (octave_value (Matrix ())); } |
3351
|
614 | '[' in_matrix_or_assign_lhs matrix_rows ']' |
3203
|
615 { |
|
616 $$ = finish_matrix ($3); |
|
617 lexer_flags.looking_at_matrix_or_assign_lhs = false; |
|
618 } |
2970
|
619 ; |
|
620 |
3351
|
621 matrix_rows : matrix_rows1 |
2970
|
622 { $$ = $1; } |
3351
|
623 | matrix_rows1 ';' // Ignore trailing semicolon. |
2970
|
624 { $$ = $1; } |
|
625 ; |
|
626 |
3351
|
627 matrix_rows1 : cell_or_matrix_row |
2970
|
628 { $$ = new tree_matrix ($1); } |
3351
|
629 | matrix_rows1 ';' cell_or_matrix_row |
2970
|
630 { |
|
631 $1->append ($3); |
|
632 $$ = $1; |
|
633 } |
|
634 ; |
|
635 |
3351
|
636 cell : '{' '}' |
3928
|
637 { $$ = new tree_constant (octave_value (Cell ())); } |
3351
|
638 | '{' ';' '}' |
3928
|
639 { $$ = new tree_constant (octave_value (Cell ())); } |
3351
|
640 | '{' cell_rows '}' |
|
641 { $$ = finish_cell ($2); } |
|
642 ; |
|
643 |
|
644 cell_rows : cell_rows1 |
|
645 { $$ = $1; } |
|
646 | cell_rows1 ';' // Ignore trailing semicolon. |
|
647 { $$ = $1; } |
|
648 ; |
|
649 |
|
650 cell_rows1 : cell_or_matrix_row |
|
651 { $$ = new tree_cell ($1); } |
|
652 | cell_rows1 ';' cell_or_matrix_row |
|
653 { |
|
654 $1->append ($3); |
|
655 $$ = $1; |
|
656 } |
|
657 ; |
|
658 |
|
659 cell_or_matrix_row |
|
660 : arg_list |
2970
|
661 { $$ = $1; } |
|
662 | arg_list ',' // Ignore trailing comma. |
|
663 { $$ = $1; } |
|
664 ; |
|
665 |
|
666 primary_expr : identifier |
|
667 { $$ = $1; } |
|
668 | constant |
|
669 { $$ = $1; } |
|
670 | matrix |
|
671 { $$ = $1; } |
3351
|
672 | cell |
|
673 { $$ = $1; } |
2970
|
674 | '(' expression ')' |
|
675 { $$ = $2->mark_in_parens (); } |
|
676 ; |
|
677 |
|
678 magic_colon : ':' |
|
679 { |
|
680 octave_value tmp (octave_value::magic_colon_t); |
|
681 $$ = new tree_constant (tmp); |
|
682 } |
|
683 ; |
|
684 |
|
685 arg_list : expression |
|
686 { $$ = new tree_argument_list ($1); } |
|
687 | magic_colon |
|
688 { $$ = new tree_argument_list ($1); } |
|
689 | ALL_VA_ARGS |
|
690 { |
|
691 octave_value tmp (octave_value::all_va_args_t); |
|
692 tree_constant *all_va_args = new tree_constant (tmp); |
|
693 $$ = new tree_argument_list (all_va_args); |
|
694 } |
|
695 | arg_list ',' magic_colon |
|
696 { |
|
697 $1->append ($3); |
|
698 $$ = $1; |
|
699 } |
|
700 | arg_list ',' expression |
|
701 { |
|
702 $1->append ($3); |
|
703 $$ = $1; |
|
704 } |
|
705 | arg_list ',' ALL_VA_ARGS |
|
706 { |
|
707 octave_value tmp (octave_value::all_va_args_t); |
|
708 tree_constant *all_va_args = new tree_constant (tmp); |
|
709 $1->append (all_va_args); |
|
710 $$ = $1; |
|
711 } |
|
712 ; |
|
713 |
4131
|
714 indirect_ref_op : '.' |
2970
|
715 { lexer_flags.looking_at_indirect_ref = true; } |
|
716 ; |
|
717 |
4234
|
718 begin_obj_idx : // empty |
4237
|
719 { lexer_flags.looking_at_object_index++; } |
4234
|
720 ; |
|
721 |
2970
|
722 postfix_expr : primary_expr |
|
723 { $$ = $1; } |
4234
|
724 | postfix_expr '(' begin_obj_idx ')' |
4236
|
725 { |
|
726 $$ = make_index_expression ($1, 0, '('); |
4237
|
727 lexer_flags.looking_at_object_index--; |
4236
|
728 } |
4234
|
729 | postfix_expr '(' begin_obj_idx arg_list ')' |
|
730 { |
|
731 $$ = make_index_expression ($1, $4, '('); |
4237
|
732 lexer_flags.looking_at_object_index--; |
4234
|
733 } |
|
734 | postfix_expr '{' begin_obj_idx '}' |
4236
|
735 { |
|
736 $$ = make_index_expression ($1, 0, '{'); |
4237
|
737 lexer_flags.looking_at_object_index--; |
4236
|
738 } |
4234
|
739 | postfix_expr '{' begin_obj_idx arg_list '}' |
|
740 { |
|
741 $$ = make_index_expression ($1, $4, '{'); |
4237
|
742 lexer_flags.looking_at_object_index--; |
4234
|
743 } |
2970
|
744 | postfix_expr PLUS_PLUS |
|
745 { $$ = make_postfix_op (PLUS_PLUS, $1, $2); } |
|
746 | postfix_expr MINUS_MINUS |
|
747 { $$ = make_postfix_op (MINUS_MINUS, $1, $2); } |
|
748 | postfix_expr QUOTE |
|
749 { $$ = make_postfix_op (QUOTE, $1, $2); } |
|
750 | postfix_expr TRANSPOSE |
|
751 { $$ = make_postfix_op (TRANSPOSE, $1, $2); } |
4131
|
752 | postfix_expr indirect_ref_op STRUCT_ELT |
|
753 { $$ = make_indirect_ref ($1, $3->text ()); } |
|
754 | postfix_expr indirect_ref_op '(' expression ')' |
|
755 { $$ = make_indirect_ref ($1, $4); } |
2970
|
756 ; |
|
757 |
|
758 prefix_expr : postfix_expr |
|
759 { $$ = $1; } |
3178
|
760 | binary_expr |
|
761 { $$ = $1; } |
2970
|
762 | PLUS_PLUS prefix_expr %prec UNARY |
|
763 { $$ = make_prefix_op (PLUS_PLUS, $2, $1); } |
|
764 | MINUS_MINUS prefix_expr %prec UNARY |
|
765 { $$ = make_prefix_op (MINUS_MINUS, $2, $1); } |
|
766 | EXPR_NOT prefix_expr %prec UNARY |
|
767 { $$ = make_prefix_op (EXPR_NOT, $2, $1); } |
|
768 | '+' prefix_expr %prec UNARY |
|
769 { $$ = $2; } |
|
770 | '-' prefix_expr %prec UNARY |
|
771 { $$ = make_prefix_op ('-', $2, $1); } |
|
772 ; |
|
773 |
3178
|
774 binary_expr : prefix_expr POW prefix_expr |
2970
|
775 { $$ = make_binary_op (POW, $1, $2, $3); } |
3178
|
776 | prefix_expr EPOW prefix_expr |
2970
|
777 { $$ = make_binary_op (EPOW, $1, $2, $3); } |
3178
|
778 | prefix_expr '+' prefix_expr |
2970
|
779 { $$ = make_binary_op ('+', $1, $2, $3); } |
3178
|
780 | prefix_expr '-' prefix_expr |
2970
|
781 { $$ = make_binary_op ('-', $1, $2, $3); } |
3178
|
782 | prefix_expr '*' prefix_expr |
2970
|
783 { $$ = make_binary_op ('*', $1, $2, $3); } |
3178
|
784 | prefix_expr '/' prefix_expr |
2970
|
785 { $$ = make_binary_op ('/', $1, $2, $3); } |
3178
|
786 | prefix_expr EPLUS prefix_expr |
2970
|
787 { $$ = make_binary_op ('+', $1, $2, $3); } |
3178
|
788 | prefix_expr EMINUS prefix_expr |
2970
|
789 { $$ = make_binary_op ('-', $1, $2, $3); } |
3178
|
790 | prefix_expr EMUL prefix_expr |
2970
|
791 { $$ = make_binary_op (EMUL, $1, $2, $3); } |
3178
|
792 | prefix_expr EDIV prefix_expr |
2970
|
793 { $$ = make_binary_op (EDIV, $1, $2, $3); } |
3178
|
794 | prefix_expr LEFTDIV prefix_expr |
2970
|
795 { $$ = make_binary_op (LEFTDIV, $1, $2, $3); } |
3178
|
796 | prefix_expr ELEFTDIV prefix_expr |
2970
|
797 { $$ = make_binary_op (ELEFTDIV, $1, $2, $3); } |
|
798 ; |
|
799 |
|
800 colon_expr : colon_expr1 |
|
801 { $$ = finish_colon_expression ($1); } |
|
802 ; |
|
803 |
3178
|
804 colon_expr1 : prefix_expr |
2970
|
805 { $$ = new tree_colon_expression ($1); } |
3178
|
806 | colon_expr1 ':' prefix_expr |
2970
|
807 { |
|
808 if (! ($$ = $1->append ($3))) |
|
809 ABORT_PARSE; |
|
810 } |
|
811 ; |
|
812 |
|
813 simple_expr : colon_expr |
|
814 { $$ = $1; } |
|
815 | simple_expr LSHIFT simple_expr |
|
816 { $$ = make_binary_op (LSHIFT, $1, $2, $3); } |
|
817 | simple_expr RSHIFT simple_expr |
|
818 { $$ = make_binary_op (RSHIFT, $1, $2, $3); } |
|
819 | simple_expr EXPR_LT simple_expr |
|
820 { $$ = make_binary_op (EXPR_LT, $1, $2, $3); } |
|
821 | simple_expr EXPR_LE simple_expr |
|
822 { $$ = make_binary_op (EXPR_LE, $1, $2, $3); } |
|
823 | simple_expr EXPR_EQ simple_expr |
|
824 { $$ = make_binary_op (EXPR_EQ, $1, $2, $3); } |
|
825 | simple_expr EXPR_GE simple_expr |
|
826 { $$ = make_binary_op (EXPR_GE, $1, $2, $3); } |
|
827 | simple_expr EXPR_GT simple_expr |
|
828 { $$ = make_binary_op (EXPR_GT, $1, $2, $3); } |
|
829 | simple_expr EXPR_NE simple_expr |
|
830 { $$ = make_binary_op (EXPR_NE, $1, $2, $3); } |
|
831 | simple_expr EXPR_AND simple_expr |
|
832 { $$ = make_binary_op (EXPR_AND, $1, $2, $3); } |
|
833 | simple_expr EXPR_OR simple_expr |
|
834 { $$ = make_binary_op (EXPR_OR, $1, $2, $3); } |
|
835 | simple_expr EXPR_AND_AND simple_expr |
|
836 { $$ = make_boolean_op (EXPR_AND_AND, $1, $2, $3); } |
|
837 | simple_expr EXPR_OR_OR simple_expr |
|
838 { $$ = make_boolean_op (EXPR_OR_OR, $1, $2, $3); } |
|
839 ; |
|
840 |
|
841 // Arrange for the lexer to return CLOSE_BRACE for `]' by looking ahead |
|
842 // one token for an assignment op. |
|
843 |
|
844 assign_lhs : simple_expr |
|
845 { $$ = new tree_argument_list ($1); } |
3189
|
846 | '[' in_matrix_or_assign_lhs arg_list CLOSE_BRACE |
|
847 { |
|
848 $$ = $3; |
|
849 lexer_flags.looking_at_matrix_or_assign_lhs = false; |
|
850 } |
2970
|
851 ; |
|
852 |
|
853 assign_expr : assign_lhs '=' expression |
|
854 { $$ = make_assign_op ('=', $1, $2, $3); } |
|
855 | assign_lhs ADD_EQ expression |
|
856 { $$ = make_assign_op (ADD_EQ, $1, $2, $3); } |
|
857 | assign_lhs SUB_EQ expression |
|
858 { $$ = make_assign_op (SUB_EQ, $1, $2, $3); } |
|
859 | assign_lhs MUL_EQ expression |
|
860 { $$ = make_assign_op (MUL_EQ, $1, $2, $3); } |
|
861 | assign_lhs DIV_EQ expression |
|
862 { $$ = make_assign_op (DIV_EQ, $1, $2, $3); } |
3204
|
863 | assign_lhs LEFTDIV_EQ expression |
|
864 { $$ = make_assign_op (LEFTDIV_EQ, $1, $2, $3); } |
4018
|
865 | assign_lhs POW_EQ expression |
|
866 { $$ = make_assign_op (POW_EQ, $1, $2, $3); } |
2970
|
867 | assign_lhs LSHIFT_EQ expression |
|
868 { $$ = make_assign_op (LSHIFT_EQ, $1, $2, $3); } |
|
869 | assign_lhs RSHIFT_EQ expression |
|
870 { $$ = make_assign_op (RSHIFT_EQ, $1, $2, $3); } |
|
871 | assign_lhs EMUL_EQ expression |
|
872 { $$ = make_assign_op (EMUL_EQ, $1, $2, $3); } |
|
873 | assign_lhs EDIV_EQ expression |
|
874 { $$ = make_assign_op (EDIV_EQ, $1, $2, $3); } |
3204
|
875 | assign_lhs ELEFTDIV_EQ expression |
|
876 { $$ = make_assign_op (ELEFTDIV_EQ, $1, $2, $3); } |
4018
|
877 | assign_lhs EPOW_EQ expression |
|
878 { $$ = make_assign_op (EPOW_EQ, $1, $2, $3); } |
2970
|
879 | assign_lhs AND_EQ expression |
|
880 { $$ = make_assign_op (AND_EQ, $1, $2, $3); } |
|
881 | assign_lhs OR_EQ expression |
|
882 { $$ = make_assign_op (OR_EQ, $1, $2, $3); } |
|
883 ; |
|
884 |
|
885 word_list_cmd : identifier word_list |
3933
|
886 { $$ = make_index_expression ($1, $2, '('); } |
2970
|
887 ; |
|
888 |
4064
|
889 word_list : STRING |
2970
|
890 { |
4064
|
891 tree_constant *tmp = make_constant (STRING, $1); |
2970
|
892 $$ = new tree_argument_list (tmp); |
|
893 } |
4064
|
894 | word_list STRING |
2970
|
895 { |
4064
|
896 tree_constant *tmp = make_constant (STRING, $2); |
2970
|
897 $1->append (tmp); |
|
898 $$ = $1; |
|
899 } |
|
900 ; |
|
901 |
|
902 expression : simple_expr |
|
903 { $$ = $1; } |
|
904 | word_list_cmd |
|
905 { $$ = $1; } |
|
906 | assign_expr |
|
907 { $$ = $1; } |
|
908 ; |
|
909 |
|
910 // ================================================ |
|
911 // Commands, declarations, and function definitions |
|
912 // ================================================ |
|
913 |
|
914 command : declaration |
|
915 { $$ = $1; } |
|
916 | select_command |
|
917 { $$ = $1; } |
|
918 | loop_command |
|
919 { $$ = $1; } |
4207
|
920 | jump_command |
|
921 { $$ = $1; } |
2970
|
922 | except_command |
|
923 { $$ = $1; } |
|
924 | function |
|
925 { $$ = $1; } |
|
926 | plot_command |
|
927 { $$ = $1; } |
|
928 ; |
|
929 |
|
930 // ===================== |
|
931 // Declaration statemnts |
|
932 // ===================== |
|
933 |
|
934 declaration : GLOBAL decl1 |
|
935 { $$ = make_decl_command (GLOBAL, $1, $2); } |
|
936 | STATIC decl1 |
|
937 { $$ = make_decl_command (STATIC, $1, $2); } |
|
938 ; |
|
939 |
|
940 decl1 : decl2 |
|
941 { $$ = new tree_decl_init_list ($1); } |
|
942 | decl1 decl2 |
|
943 { |
|
944 $1->append ($2); |
|
945 $$ = $1; |
|
946 } |
|
947 ; |
|
948 |
|
949 decl2 : identifier |
|
950 { $$ = new tree_decl_elt ($1); } |
|
951 | identifier '=' expression |
|
952 { $$ = new tree_decl_elt ($1, $3); } |
|
953 ; |
|
954 |
|
955 // ==================== |
|
956 // Selection statements |
|
957 // ==================== |
|
958 |
|
959 select_command : if_command |
|
960 { $$ = $1; } |
|
961 | switch_command |
|
962 { $$ = $1; } |
|
963 ; |
|
964 |
|
965 // ============ |
|
966 // If statement |
|
967 // ============ |
|
968 |
3665
|
969 if_command : IF stash_comment if_cmd_list END |
2970
|
970 { |
3665
|
971 if (! ($$ = finish_if_command ($1, $3, $4, $2))) |
2970
|
972 ABORT_PARSE; |
|
973 } |
|
974 ; |
|
975 |
|
976 if_cmd_list : if_cmd_list1 |
|
977 { $$ = $1; } |
|
978 | if_cmd_list1 else_clause |
|
979 { |
|
980 $1->append ($2); |
|
981 $$ = $1; |
|
982 } |
|
983 ; |
|
984 |
|
985 if_cmd_list1 : expression opt_sep opt_list |
|
986 { $$ = start_if_command ($1, $3); } |
|
987 | if_cmd_list1 elseif_clause |
|
988 { |
|
989 $1->append ($2); |
|
990 $$ = $1; |
|
991 } |
|
992 ; |
|
993 |
3665
|
994 elseif_clause : ELSEIF stash_comment opt_sep expression opt_sep opt_list |
|
995 { $$ = make_elseif_clause ($4, $6, $2); } |
2970
|
996 ; |
|
997 |
3665
|
998 else_clause : ELSE stash_comment opt_sep opt_list |
|
999 { |
|
1000 $$ = new tree_if_clause ($4, $2); |
|
1001 } |
2970
|
1002 ; |
|
1003 |
|
1004 // ================ |
|
1005 // Switch statement |
|
1006 // ================ |
|
1007 |
3665
|
1008 switch_command : SWITCH stash_comment expression opt_sep case_list END |
2970
|
1009 { |
3665
|
1010 if (! ($$ = finish_switch_command ($1, $3, $5, $6, $2))) |
2970
|
1011 ABORT_PARSE; |
|
1012 } |
|
1013 ; |
|
1014 |
4044
|
1015 case_list : // empty |
|
1016 { $$ = new tree_switch_case_list (); } |
|
1017 | case_list1 |
2970
|
1018 { $$ = $1; } |
|
1019 | case_list1 default_case |
|
1020 { |
|
1021 $1->append ($2); |
|
1022 $$ = $1; |
|
1023 } |
|
1024 ; |
|
1025 |
|
1026 case_list1 : switch_case |
|
1027 { $$ = new tree_switch_case_list ($1); } |
|
1028 | case_list1 switch_case |
|
1029 { |
|
1030 $1->append ($2); |
|
1031 $$ = $1; |
|
1032 } |
|
1033 ; |
|
1034 |
4025
|
1035 switch_case : CASE stash_comment opt_sep expression opt_sep opt_list |
3665
|
1036 { $$ = make_switch_case ($4, $6, $2); } |
2970
|
1037 ; |
|
1038 |
3665
|
1039 default_case : OTHERWISE stash_comment opt_sep opt_list |
|
1040 { |
|
1041 $$ = new tree_switch_case ($4, $2); |
|
1042 } |
2970
|
1043 ; |
|
1044 |
|
1045 // ======= |
|
1046 // Looping |
|
1047 // ======= |
|
1048 |
3665
|
1049 loop_command : WHILE stash_comment expression opt_sep opt_list END |
2970
|
1050 { |
3665
|
1051 if (! ($$ = make_while_command ($1, $3, $5, $6, $2))) |
2970
|
1052 ABORT_PARSE; |
|
1053 } |
3665
|
1054 | DO stash_comment opt_sep opt_list UNTIL expression |
3484
|
1055 { |
3665
|
1056 if (! ($$ = make_do_until_command ($1, $4, $6, $2))) |
3484
|
1057 ABORT_PARSE; |
|
1058 } |
3665
|
1059 | FOR stash_comment assign_lhs '=' expression opt_sep opt_list END |
2970
|
1060 { |
3665
|
1061 if (! ($$ = make_for_command ($1, $3, $5, $7, $8, $2))) |
2970
|
1062 ABORT_PARSE; |
|
1063 } |
|
1064 ; |
|
1065 |
|
1066 // ======= |
|
1067 // Jumping |
|
1068 // ======= |
|
1069 |
4207
|
1070 jump_command : BREAK |
2970
|
1071 { |
4207
|
1072 if (! ($$ = make_break_command ($1))) |
2970
|
1073 ABORT_PARSE; |
|
1074 } |
|
1075 | CONTINUE |
|
1076 { |
4207
|
1077 if (! ($$ = make_continue_command ($1))) |
2970
|
1078 ABORT_PARSE; |
|
1079 } |
|
1080 | FUNC_RET |
|
1081 { |
4207
|
1082 if (! ($$ = make_return_command ($1))) |
2970
|
1083 ABORT_PARSE; |
|
1084 } |
|
1085 ; |
|
1086 |
|
1087 // ========== |
|
1088 // Exceptions |
|
1089 // ========== |
|
1090 |
3665
|
1091 except_command : UNWIND stash_comment opt_sep opt_list CLEANUP |
|
1092 stash_comment opt_sep opt_list END |
2970
|
1093 { |
3665
|
1094 if (! ($$ = make_unwind_command ($1, $4, $8, $9, $2, $6))) |
2970
|
1095 ABORT_PARSE; |
|
1096 } |
3665
|
1097 | TRY stash_comment opt_sep opt_list CATCH |
|
1098 stash_comment opt_sep opt_list END |
2970
|
1099 { |
3665
|
1100 if (! ($$ = make_try_command ($1, $4, $8, $9, $2, $6))) |
2970
|
1101 ABORT_PARSE; |
|
1102 } |
|
1103 ; |
|
1104 |
|
1105 // =========================================== |
|
1106 // Some `subroutines' for function definitions |
|
1107 // =========================================== |
|
1108 |
3903
|
1109 save_symtab : // empty |
4238
|
1110 { symtab_context.push (curr_sym_tab); } |
3903
|
1111 ; |
|
1112 |
4009
|
1113 function_symtab : // empty |
|
1114 { curr_sym_tab = fbi_sym_tab; } |
2970
|
1115 ; |
|
1116 |
|
1117 local_symtab : // empty |
|
1118 { curr_sym_tab = tmp_local_sym_tab; } |
|
1119 ; |
|
1120 |
|
1121 in_return_list : // empty |
|
1122 { lexer_flags.looking_at_return_list = true; } |
|
1123 ; |
|
1124 |
|
1125 // =========================== |
|
1126 // List of function parameters |
|
1127 // =========================== |
|
1128 |
|
1129 param_list_beg : '(' |
|
1130 { lexer_flags.looking_at_parameter_list = true; } |
|
1131 ; |
|
1132 |
|
1133 param_list_end : ')' |
|
1134 { lexer_flags.looking_at_parameter_list = false; } |
|
1135 ; |
|
1136 |
|
1137 param_list : param_list_beg param_list_end |
|
1138 { |
|
1139 lexer_flags.quote_is_transpose = false; |
|
1140 $$ = 0; |
|
1141 } |
3973
|
1142 | param_list_beg VARARGIN param_list_end |
2970
|
1143 { |
|
1144 lexer_flags.quote_is_transpose = false; |
|
1145 tree_parameter_list *tmp = new tree_parameter_list (); |
|
1146 tmp->mark_varargs_only (); |
|
1147 $$ = tmp; |
|
1148 } |
|
1149 | param_list1 param_list_end |
|
1150 { |
|
1151 lexer_flags.quote_is_transpose = false; |
|
1152 $1->mark_as_formal_parameters (); |
|
1153 $$ = $1; |
|
1154 } |
3973
|
1155 | param_list1 ',' VARARGIN param_list_end |
2970
|
1156 { |
|
1157 lexer_flags.quote_is_transpose = false; |
|
1158 $1->mark_as_formal_parameters (); |
|
1159 $1->mark_varargs (); |
|
1160 $$ = $1; |
|
1161 } |
|
1162 ; |
|
1163 |
|
1164 param_list1 : param_list_beg identifier |
|
1165 { $$ = new tree_parameter_list ($2); } |
|
1166 | param_list1 ',' identifier |
|
1167 { |
|
1168 $1->append ($3); |
|
1169 $$ = $1; |
|
1170 } |
|
1171 | param_list_beg error |
|
1172 { |
|
1173 yyerror ("invalid parameter list"); |
|
1174 $$ = 0; |
|
1175 ABORT_PARSE; |
|
1176 } |
|
1177 | param_list1 ',' error |
|
1178 { |
|
1179 yyerror ("invalid parameter list"); |
|
1180 $$ = 0; |
|
1181 ABORT_PARSE; |
|
1182 } |
|
1183 ; |
|
1184 |
|
1185 // =================================== |
|
1186 // List of function return value names |
|
1187 // =================================== |
|
1188 |
3189
|
1189 return_list_beg : '[' in_return_list local_symtab |
2970
|
1190 ; |
|
1191 |
|
1192 return_list : return_list_beg return_list_end |
|
1193 { |
|
1194 lexer_flags.looking_at_return_list = false; |
|
1195 $$ = new tree_parameter_list (); |
|
1196 } |
3973
|
1197 | return_list_beg VARARGOUT return_list_end |
2970
|
1198 { |
|
1199 lexer_flags.looking_at_return_list = false; |
|
1200 tree_parameter_list *tmp = new tree_parameter_list (); |
|
1201 tmp->mark_varargs_only (); |
|
1202 $$ = tmp; |
|
1203 } |
|
1204 | return_list_beg return_list1 return_list_end |
|
1205 { |
|
1206 lexer_flags.looking_at_return_list = false; |
|
1207 $$ = $2; |
|
1208 } |
3973
|
1209 | return_list_beg return_list1 ',' VARARGOUT return_list_end |
2970
|
1210 { |
|
1211 lexer_flags.looking_at_return_list = false; |
|
1212 $2->mark_varargs (); |
|
1213 $$ = $2; |
|
1214 } |
|
1215 ; |
|
1216 |
|
1217 return_list1 : identifier |
|
1218 { $$ = new tree_parameter_list ($1); } |
|
1219 | return_list1 ',' identifier |
|
1220 { |
|
1221 $1->append ($3); |
|
1222 $$ = $1; |
|
1223 } |
|
1224 ; |
|
1225 |
4009
|
1226 return_list_end : function_symtab ']' |
2970
|
1227 ; |
|
1228 |
|
1229 // =================== |
|
1230 // Function definition |
|
1231 // =================== |
|
1232 |
4238
|
1233 function_beg : save_symtab FCN function_symtab stash_comment |
|
1234 { $$ = $4; } |
2970
|
1235 ; |
|
1236 |
|
1237 function : function_beg function2 |
|
1238 { |
3665
|
1239 $2->stash_leading_comment ($1); |
2970
|
1240 recover_from_parsing_function (); |
|
1241 $$ = 0; |
|
1242 } |
|
1243 | function_beg identifier function1 |
|
1244 { |
3665
|
1245 finish_function ($2, $3, $1); |
2970
|
1246 recover_from_parsing_function (); |
|
1247 $$ = 0; |
|
1248 } |
|
1249 | function_beg return_list function1 |
|
1250 { |
3665
|
1251 finish_function ($2, $3, $1); |
2970
|
1252 recover_from_parsing_function (); |
|
1253 $$ = 0; |
|
1254 } |
|
1255 ; |
|
1256 |
4009
|
1257 function1 : function_symtab '=' function2 |
2970
|
1258 { $$ = $3; } |
|
1259 ; |
|
1260 |
4238
|
1261 fcn_name : identifier local_symtab |
2970
|
1262 { |
4238
|
1263 std::string id_name = $1->name (); |
|
1264 |
|
1265 if (reading_fcn_file |
|
1266 && ! lexer_flags.parsing_nested_function) |
|
1267 parent_function_name = (curr_fcn_file_name == id_name) |
|
1268 ? id_name : curr_fcn_file_name; |
|
1269 |
|
1270 lexer_flags.parsed_function_name = true; |
|
1271 |
|
1272 $$ = $1; |
|
1273 } |
|
1274 ; |
|
1275 |
|
1276 function2 : fcn_name function3 |
|
1277 { |
|
1278 if (! ($$ = frob_function ($1, $2))) |
2970
|
1279 ABORT_PARSE; |
|
1280 } |
|
1281 ; |
|
1282 |
|
1283 function3 : param_list function4 |
|
1284 { $$ = start_function ($1, $2); } |
|
1285 | function4 |
|
1286 { $$ = start_function (0, $1); } |
|
1287 ; |
|
1288 |
|
1289 function4 : opt_sep opt_list function_end |
|
1290 { $$ = $2; } |
|
1291 ; |
|
1292 |
|
1293 function_end : END |
|
1294 { |
4238
|
1295 if (! end_token_ok ($1, token::function_end)) |
2970
|
1296 ABORT_PARSE; |
|
1297 } |
|
1298 | END_OF_INPUT |
|
1299 { |
4240
|
1300 if (lexer_flags.parsing_nested_function) |
|
1301 lexer_flags.parsing_nested_function = -1; |
4238
|
1302 |
3939
|
1303 if (! (reading_fcn_file || reading_script_file |
|
1304 || get_input_from_eval_string)) |
2970
|
1305 YYABORT; |
|
1306 } |
|
1307 ; |
|
1308 |
|
1309 // ======== |
|
1310 // Plotting |
|
1311 // ======== |
|
1312 |
3180
|
1313 plot_command : PLOT |
|
1314 { |
|
1315 if (! ($$ = make_plot_command ($1, 0, 0))) |
|
1316 ABORT_PARSE; |
|
1317 } |
|
1318 | PLOT ranges |
|
1319 { |
|
1320 if (! ($$ = make_plot_command ($1, $2, 0))) |
|
1321 ABORT_PARSE; |
|
1322 } |
|
1323 | PLOT plot_command1 |
1
|
1324 { |
1623
|
1325 if (! ($$ = make_plot_command ($1, 0, $2))) |
|
1326 ABORT_PARSE; |
1
|
1327 } |
|
1328 | PLOT ranges plot_command1 |
|
1329 { |
1623
|
1330 if (! ($$ = make_plot_command ($1, $2, $3))) |
|
1331 ABORT_PARSE; |
1
|
1332 } |
|
1333 ; |
|
1334 |
|
1335 ranges : ranges1 |
578
|
1336 { $$ = new plot_limits ($1); } |
1
|
1337 | ranges1 ranges1 |
578
|
1338 { $$ = new plot_limits ($1, $2); } |
1
|
1339 | ranges1 ranges1 ranges1 |
578
|
1340 { $$ = new plot_limits ($1, $2, $3); } |
1
|
1341 ; |
|
1342 |
|
1343 ranges1 : OPEN_BRACE expression COLON expression CLOSE_BRACE |
578
|
1344 { $$ = new plot_range ($2, $4); } |
1
|
1345 | OPEN_BRACE COLON expression CLOSE_BRACE |
578
|
1346 { $$ = new plot_range (0, $3); } |
1
|
1347 | OPEN_BRACE expression COLON CLOSE_BRACE |
578
|
1348 { $$ = new plot_range ($2, 0); } |
1
|
1349 | OPEN_BRACE COLON CLOSE_BRACE |
578
|
1350 { $$ = new plot_range (); } |
1
|
1351 | OPEN_BRACE CLOSE_BRACE |
578
|
1352 { $$ = new plot_range (); } |
1
|
1353 ; |
|
1354 |
3180
|
1355 plot_command1 : plot_command2 |
578
|
1356 { $$ = new subplot_list ($1); } |
1
|
1357 | plot_command1 ',' plot_command2 |
1829
|
1358 { |
|
1359 $1->append ($3); |
|
1360 $$ = $1; |
|
1361 } |
1
|
1362 ; |
|
1363 |
|
1364 plot_command2 : expression |
578
|
1365 { $$ = new subplot ($1); } |
1
|
1366 | expression plot_options |
3165
|
1367 { $$ = $2->add_data ($1); } |
1
|
1368 ; |
|
1369 |
|
1370 plot_options : using |
3165
|
1371 { |
|
1372 subplot *tmp = new subplot (); |
|
1373 $$ = tmp->add_clause ($1); |
|
1374 } |
1
|
1375 | title |
3165
|
1376 { |
|
1377 subplot *tmp = new subplot (); |
|
1378 $$ = tmp->add_clause ($1); |
|
1379 } |
1
|
1380 | style |
3165
|
1381 { |
|
1382 subplot *tmp = new subplot (); |
|
1383 $$ = tmp->add_clause ($1); |
|
1384 } |
|
1385 | axes |
|
1386 { |
|
1387 subplot *tmp = new subplot (); |
|
1388 $$ = tmp->add_clause ($1); |
|
1389 } |
|
1390 | plot_options using |
|
1391 { |
|
1392 if (! ($$ = $1->add_clause ($2))) |
|
1393 { |
|
1394 yyerror ("only one using option may be specified"); |
|
1395 ABORT_PARSE; |
|
1396 } |
|
1397 } |
|
1398 | plot_options title |
|
1399 { |
|
1400 if (! ($$ = $1->add_clause ($2))) |
|
1401 { |
|
1402 yyerror ("only one title option my be specified"); |
|
1403 ABORT_PARSE; |
|
1404 } |
|
1405 } |
|
1406 | plot_options style |
|
1407 { |
|
1408 if (! ($$ = $1->add_clause ($2))) |
|
1409 { |
|
1410 yyerror ("only one style option my be specified"); |
|
1411 ABORT_PARSE; |
|
1412 } |
|
1413 } |
|
1414 | plot_options axes |
|
1415 { |
|
1416 if (! ($$ = $1->add_clause ($2))) |
|
1417 { |
|
1418 yyerror ("only one axes option may be specified"); |
|
1419 ABORT_PARSE; |
|
1420 } |
|
1421 } |
|
1422 ; |
|
1423 |
|
1424 axes : AXES AXES_TAG |
|
1425 { |
|
1426 lexer_flags.in_plot_axes = false; |
|
1427 $$ = new subplot_axes ($2->text ()); |
|
1428 } |
1
|
1429 ; |
|
1430 |
|
1431 using : using1 |
|
1432 { |
2857
|
1433 lexer_flags.in_plot_using = false; |
1
|
1434 $$ = $1; |
|
1435 } |
|
1436 | using1 expression |
|
1437 { |
2857
|
1438 lexer_flags.in_plot_using = false; |
1
|
1439 $$ = $1->set_format ($2); |
|
1440 } |
|
1441 ; |
|
1442 |
|
1443 using1 : USING expression |
|
1444 { |
578
|
1445 subplot_using *tmp = new subplot_using (); |
1
|
1446 $$ = tmp->add_qualifier ($2); |
|
1447 } |
|
1448 | using1 COLON expression |
|
1449 { $$ = $1->add_qualifier ($3); } |
|
1450 ; |
|
1451 |
|
1452 title : TITLE expression |
|
1453 { $$ = $2; } |
|
1454 ; |
|
1455 |
|
1456 style : WITH STYLE |
1755
|
1457 { $$ = new subplot_style ($2->text ()); } |
1
|
1458 | WITH STYLE expression |
1755
|
1459 { $$ = new subplot_style ($2->text (), $3); } |
2542
|
1460 | WITH STYLE expression expression |
|
1461 { $$ = new subplot_style ($2->text (), $3, $4); } |
1
|
1462 ; |
|
1463 |
2970
|
1464 // ============= |
|
1465 // Miscellaneous |
|
1466 // ============= |
|
1467 |
3665
|
1468 stash_comment : // empty |
|
1469 { $$ = octave_comment_buffer::get_comment (); } |
|
1470 ; |
|
1471 |
2970
|
1472 parse_error : LEXICAL_ERROR |
|
1473 { yyerror ("parse error"); } |
|
1474 | error |
1
|
1475 ; |
|
1476 |
2525
|
1477 sep_no_nl : ',' |
|
1478 { $$ = ','; } |
|
1479 | ';' |
|
1480 { $$ = ';'; } |
|
1481 | sep_no_nl ',' |
|
1482 { $$ = $1; } |
|
1483 | sep_no_nl ';' |
|
1484 { $$ = $1; } |
|
1485 ; |
|
1486 |
|
1487 opt_sep_no_nl : // empty |
|
1488 { $$ = 0; } |
|
1489 | sep_no_nl |
|
1490 { $$ = $1; } |
|
1491 ; |
|
1492 |
|
1493 sep : ',' |
|
1494 { $$ = ','; } |
|
1495 | ';' |
|
1496 { $$ = ';'; } |
|
1497 | '\n' |
|
1498 { $$ = '\n'; } |
|
1499 | sep ',' |
|
1500 { $$ = $1; } |
|
1501 | sep ';' |
|
1502 { $$ = $1; } |
|
1503 | sep '\n' |
|
1504 { $$ = $1; } |
|
1505 ; |
|
1506 |
|
1507 opt_sep : // empty |
|
1508 { $$ = 0; } |
|
1509 | sep |
|
1510 { $$ = $1; } |
|
1511 ; |
|
1512 |
1
|
1513 %% |
|
1514 |
666
|
1515 // Generic error messages. |
|
1516 |
1
|
1517 static void |
2805
|
1518 yyerror (const char *s) |
1
|
1519 { |
143
|
1520 int err_col = current_input_column - 1; |
1
|
1521 |
4051
|
1522 OSSTREAM output_buf; |
1
|
1523 |
1005
|
1524 if (reading_fcn_file || reading_script_file) |
|
1525 output_buf << "parse error near line " << input_line_number |
1607
|
1526 << " of file " << curr_fcn_file_full_name; |
1005
|
1527 else |
|
1528 output_buf << "parse error:"; |
581
|
1529 |
1005
|
1530 if (s && strcmp (s, "parse error") != 0) |
|
1531 output_buf << "\n\n " << s; |
|
1532 |
|
1533 output_buf << "\n\n"; |
1
|
1534 |
1755
|
1535 if (! current_input_line.empty ()) |
1
|
1536 { |
1755
|
1537 size_t len = current_input_line.length (); |
1060
|
1538 |
1755
|
1539 if (current_input_line[len-1] == '\n') |
|
1540 current_input_line.resize (len-1); |
1005
|
1541 |
4051
|
1542 // Print the line, maybe with a pointer near the error token. |
1005
|
1543 |
1755
|
1544 output_buf << ">>> " << current_input_line << "\n"; |
1060
|
1545 |
|
1546 if (err_col == 0) |
|
1547 err_col = len; |
|
1548 |
|
1549 for (int i = 0; i < err_col + 3; i++) |
|
1550 output_buf << " "; |
|
1551 |
|
1552 output_buf << "^"; |
1
|
1553 } |
1005
|
1554 |
4051
|
1555 output_buf << "\n" << OSSTREAM_ENDS; |
|
1556 |
|
1557 parse_error ("%s", OSSTREAM_C_STR (output_buf)); |
|
1558 |
|
1559 OSSTREAM_FREEZE (output_buf); |
1
|
1560 } |
|
1561 |
666
|
1562 // Error mesages for mismatched end tokens. |
|
1563 |
496
|
1564 static void |
2805
|
1565 end_error (const char *type, token::end_tok_type ettype, int l, int c) |
496
|
1566 { |
2805
|
1567 static const char *fmt |
|
1568 = "`%s' command matched by `%s' near line %d column %d"; |
496
|
1569 |
|
1570 switch (ettype) |
|
1571 { |
|
1572 case token::simple_end: |
|
1573 error (fmt, type, "end", l, c); |
|
1574 break; |
777
|
1575 |
496
|
1576 case token::for_end: |
|
1577 error (fmt, type, "endfor", l, c); |
|
1578 break; |
777
|
1579 |
496
|
1580 case token::function_end: |
|
1581 error (fmt, type, "endfunction", l, c); |
|
1582 break; |
777
|
1583 |
496
|
1584 case token::if_end: |
|
1585 error (fmt, type, "endif", l, c); |
|
1586 break; |
777
|
1587 |
3233
|
1588 case token::switch_end: |
|
1589 error (fmt, type, "endswitch", l, c); |
|
1590 break; |
|
1591 |
496
|
1592 case token::while_end: |
|
1593 error (fmt, type, "endwhile", l, c); |
|
1594 break; |
777
|
1595 |
1371
|
1596 case token::unwind_protect_end: |
|
1597 error (fmt, type, "end_unwind_protect", l, c); |
|
1598 break; |
|
1599 |
496
|
1600 default: |
|
1601 panic_impossible (); |
|
1602 break; |
|
1603 } |
|
1604 } |
|
1605 |
666
|
1606 // Check to see that end tokens are properly matched. |
|
1607 |
2857
|
1608 static bool |
|
1609 end_token_ok (token *tok, token::end_tok_type expected) |
143
|
1610 { |
2857
|
1611 bool retval = true; |
|
1612 |
143
|
1613 token::end_tok_type ettype = tok->ettype (); |
2857
|
1614 |
143
|
1615 if (ettype != expected && ettype != token::simple_end) |
|
1616 { |
2857
|
1617 retval = false; |
|
1618 |
143
|
1619 yyerror ("parse error"); |
|
1620 |
|
1621 int l = tok->line (); |
|
1622 int c = tok->column (); |
|
1623 |
|
1624 switch (expected) |
|
1625 { |
|
1626 case token::for_end: |
|
1627 end_error ("for", ettype, l, c); |
|
1628 break; |
777
|
1629 |
143
|
1630 case token::function_end: |
|
1631 end_error ("function", ettype, l, c); |
|
1632 break; |
777
|
1633 |
143
|
1634 case token::if_end: |
|
1635 end_error ("if", ettype, l, c); |
|
1636 break; |
777
|
1637 |
1489
|
1638 case token::try_catch_end: |
|
1639 end_error ("try", ettype, l, c); |
|
1640 break; |
|
1641 |
2764
|
1642 case token::switch_end: |
|
1643 end_error ("switch", ettype, l, c); |
|
1644 break; |
|
1645 |
1489
|
1646 case token::unwind_protect_end: |
|
1647 end_error ("unwind_protect", ettype, l, c); |
|
1648 break; |
|
1649 |
143
|
1650 case token::while_end: |
|
1651 end_error ("while", ettype, l, c); |
|
1652 break; |
777
|
1653 |
143
|
1654 default: |
|
1655 panic_impossible (); |
|
1656 break; |
|
1657 } |
|
1658 } |
2857
|
1659 |
|
1660 return retval; |
143
|
1661 } |
|
1662 |
666
|
1663 // Maybe print a warning if an assignment expression is used as the |
|
1664 // test in a logical expression. |
|
1665 |
496
|
1666 static void |
|
1667 maybe_warn_assign_as_truth_value (tree_expression *expr) |
1
|
1668 { |
2166
|
1669 if (Vwarn_assign_as_truth_value |
1
|
1670 && expr->is_assignment_expression () |
2961
|
1671 && expr->paren_count () < 2) |
1
|
1672 { |
|
1673 warning ("suggest parenthesis around assignment used as truth value"); |
|
1674 } |
|
1675 } |
578
|
1676 |
2764
|
1677 // Maybe print a warning about switch labels that aren't constants. |
|
1678 |
|
1679 static void |
|
1680 maybe_warn_variable_switch_label (tree_expression *expr) |
|
1681 { |
|
1682 if (Vwarn_variable_switch_label && ! expr->is_constant ()) |
|
1683 { |
|
1684 warning ("variable switch label"); |
|
1685 } |
|
1686 } |
|
1687 |
1623
|
1688 // Create a plot command. |
|
1689 |
|
1690 static tree_plot_command * |
|
1691 make_plot_command (token *tok, plot_limits *range, subplot_list *list) |
|
1692 { |
|
1693 if (range) |
|
1694 { |
|
1695 if (tok->pttype () == token::replot) |
|
1696 { |
|
1697 yyerror ("cannot specify new ranges with replot"); |
|
1698 return 0; |
|
1699 } |
|
1700 } |
|
1701 else if (! list && tok->pttype () != token::replot) |
|
1702 { |
|
1703 yyerror ("must have something to plot"); |
|
1704 return 0; |
|
1705 } |
|
1706 |
2857
|
1707 lexer_flags.plotting = false; |
|
1708 lexer_flags.past_plot_range = false; |
|
1709 lexer_flags.in_plot_range = false; |
|
1710 lexer_flags.in_plot_using = false; |
|
1711 lexer_flags.in_plot_style = false; |
1623
|
1712 |
|
1713 return new tree_plot_command (list, range, tok->pttype ()); |
|
1714 } |
|
1715 |
2533
|
1716 static tree_expression * |
|
1717 fold (tree_binary_expression *e) |
|
1718 { |
3110
|
1719 tree_expression *retval = e; |
|
1720 |
3292
|
1721 unwind_protect::begin_frame ("fold_binary_expression"); |
3110
|
1722 |
|
1723 unwind_protect_int (error_state); |
|
1724 |
3815
|
1725 unwind_protect_bool (discard_error_messages); |
|
1726 discard_error_messages = true; |
2533
|
1727 |
|
1728 tree_expression *op1 = e->lhs (); |
|
1729 tree_expression *op2 = e->rhs (); |
|
1730 |
|
1731 if (op1->is_constant () && op2->is_constant ()) |
|
1732 { |
2970
|
1733 octave_value tmp = e->rvalue (); |
2533
|
1734 |
3489
|
1735 if (! (error_state || warning_state)) |
2533
|
1736 { |
|
1737 tree_constant *tc_retval = new tree_constant (tmp); |
|
1738 |
4051
|
1739 OSSTREAM buf; |
2533
|
1740 |
|
1741 tree_print_code tpc (buf); |
|
1742 |
|
1743 e->accept (tpc); |
|
1744 |
4051
|
1745 buf << OSSTREAM_ENDS; |
|
1746 |
|
1747 tc_retval->stash_original_text (OSSTREAM_STR (buf)); |
|
1748 |
|
1749 OSSTREAM_FREEZE (buf); |
2533
|
1750 |
|
1751 delete e; |
|
1752 |
|
1753 retval = tc_retval; |
|
1754 } |
|
1755 } |
3110
|
1756 |
3292
|
1757 unwind_protect::run_frame ("fold_binary_expression"); |
|
1758 |
|
1759 return retval; |
|
1760 } |
|
1761 |
|
1762 static tree_expression * |
|
1763 fold (tree_unary_expression *e) |
|
1764 { |
|
1765 tree_expression *retval = e; |
|
1766 |
|
1767 unwind_protect::begin_frame ("fold_unary_expression"); |
|
1768 |
|
1769 unwind_protect_int (error_state); |
|
1770 |
3815
|
1771 unwind_protect_bool (discard_error_messages); |
|
1772 discard_error_messages = true; |
3292
|
1773 |
|
1774 tree_expression *op = e->operand (); |
|
1775 |
|
1776 if (op->is_constant ()) |
|
1777 { |
|
1778 octave_value tmp = e->rvalue (); |
|
1779 |
3489
|
1780 if (! (error_state || warning_state)) |
3292
|
1781 { |
|
1782 tree_constant *tc_retval = new tree_constant (tmp); |
|
1783 |
4051
|
1784 OSSTREAM buf; |
3292
|
1785 |
|
1786 tree_print_code tpc (buf); |
|
1787 |
|
1788 e->accept (tpc); |
|
1789 |
4051
|
1790 buf << OSSTREAM_ENDS; |
|
1791 |
|
1792 tc_retval->stash_original_text (OSSTREAM_STR (buf)); |
|
1793 |
|
1794 OSSTREAM_FREEZE (buf); |
3292
|
1795 |
|
1796 delete e; |
|
1797 |
|
1798 retval = tc_retval; |
|
1799 } |
|
1800 } |
|
1801 |
|
1802 unwind_protect::run_frame ("fold_unary_expression"); |
2533
|
1803 |
|
1804 return retval; |
|
1805 } |
|
1806 |
1623
|
1807 // Finish building a range. |
|
1808 |
|
1809 static tree_expression * |
|
1810 finish_colon_expression (tree_colon_expression *e) |
|
1811 { |
3110
|
1812 tree_expression *retval = e; |
|
1813 |
|
1814 unwind_protect::begin_frame ("finish_colon_expression"); |
|
1815 |
|
1816 unwind_protect_int (error_state); |
|
1817 |
3815
|
1818 unwind_protect_bool (discard_error_messages); |
|
1819 discard_error_messages = true; |
1623
|
1820 |
2533
|
1821 tree_expression *base = e->base (); |
|
1822 tree_expression *limit = e->limit (); |
|
1823 tree_expression *incr = e->increment (); |
|
1824 |
2970
|
1825 if (base) |
1623
|
1826 { |
2970
|
1827 if (limit) |
2533
|
1828 { |
2970
|
1829 if (base->is_constant () && limit->is_constant () |
|
1830 && (! incr || (incr && incr->is_constant ()))) |
|
1831 { |
|
1832 octave_value tmp = e->rvalue (); |
|
1833 |
3489
|
1834 if (! (error_state || warning_state)) |
2970
|
1835 { |
|
1836 tree_constant *tc_retval = new tree_constant (tmp); |
|
1837 |
4051
|
1838 OSSTREAM buf; |
2970
|
1839 |
|
1840 tree_print_code tpc (buf); |
|
1841 |
|
1842 e->accept (tpc); |
|
1843 |
4051
|
1844 buf << OSSTREAM_ENDS; |
|
1845 |
|
1846 tc_retval->stash_original_text (OSSTREAM_STR (buf)); |
|
1847 |
|
1848 OSSTREAM_FREEZE (buf); |
2970
|
1849 |
|
1850 delete e; |
|
1851 |
|
1852 retval = tc_retval; |
|
1853 } |
|
1854 } |
2533
|
1855 } |
|
1856 else |
2970
|
1857 { |
2990
|
1858 e->preserve_base (); |
|
1859 delete e; |
2970
|
1860 |
|
1861 // XXX FIXME XXX -- need to attempt constant folding here |
|
1862 // too (we need a generic way to do that). |
|
1863 retval = base; |
|
1864 } |
1623
|
1865 } |
|
1866 |
3110
|
1867 unwind_protect::run_frame ("finish_colon_expression"); |
|
1868 |
1623
|
1869 return retval; |
|
1870 } |
|
1871 |
1607
|
1872 // Make a constant. |
|
1873 |
2375
|
1874 static tree_constant * |
1607
|
1875 make_constant (int op, token *tok_val) |
|
1876 { |
|
1877 int l = tok_val->line (); |
|
1878 int c = tok_val->column (); |
|
1879 |
3216
|
1880 tree_constant *retval = 0; |
1607
|
1881 |
|
1882 switch (op) |
|
1883 { |
|
1884 case NUM: |
2533
|
1885 { |
2883
|
1886 octave_value tmp (tok_val->number ()); |
|
1887 retval = new tree_constant (tmp, l, c); |
2533
|
1888 retval->stash_original_text (tok_val->text_rep ()); |
|
1889 } |
1607
|
1890 break; |
|
1891 |
|
1892 case IMAG_NUM: |
|
1893 { |
2883
|
1894 octave_value tmp (Complex (0.0, tok_val->number ())); |
|
1895 retval = new tree_constant (tmp, l, c); |
1607
|
1896 retval->stash_original_text (tok_val->text_rep ()); |
|
1897 } |
|
1898 break; |
|
1899 |
4064
|
1900 case STRING: |
2883
|
1901 { |
|
1902 octave_value tmp (tok_val->text ()); |
|
1903 retval = new tree_constant (tmp, l, c); |
|
1904 } |
1607
|
1905 break; |
|
1906 |
|
1907 default: |
|
1908 panic_impossible (); |
|
1909 break; |
|
1910 } |
|
1911 |
|
1912 return retval; |
|
1913 } |
|
1914 |
666
|
1915 // Build a binary expression. |
|
1916 |
578
|
1917 static tree_expression * |
|
1918 make_binary_op (int op, tree_expression *op1, token *tok_val, |
|
1919 tree_expression *op2) |
|
1920 { |
2883
|
1921 octave_value::binary_op t = octave_value::unknown_binary_op; |
1623
|
1922 |
578
|
1923 switch (op) |
|
1924 { |
|
1925 case POW: |
3525
|
1926 t = octave_value::op_pow; |
578
|
1927 break; |
777
|
1928 |
578
|
1929 case EPOW: |
3525
|
1930 t = octave_value::op_el_pow; |
578
|
1931 break; |
777
|
1932 |
578
|
1933 case '+': |
3525
|
1934 t = octave_value::op_add; |
578
|
1935 break; |
777
|
1936 |
578
|
1937 case '-': |
3525
|
1938 t = octave_value::op_sub; |
578
|
1939 break; |
777
|
1940 |
578
|
1941 case '*': |
3525
|
1942 t = octave_value::op_mul; |
578
|
1943 break; |
777
|
1944 |
578
|
1945 case '/': |
3525
|
1946 t = octave_value::op_div; |
578
|
1947 break; |
777
|
1948 |
578
|
1949 case EMUL: |
3525
|
1950 t = octave_value::op_el_mul; |
578
|
1951 break; |
777
|
1952 |
578
|
1953 case EDIV: |
3525
|
1954 t = octave_value::op_el_div; |
578
|
1955 break; |
777
|
1956 |
578
|
1957 case LEFTDIV: |
3525
|
1958 t = octave_value::op_ldiv; |
578
|
1959 break; |
777
|
1960 |
578
|
1961 case ELEFTDIV: |
3525
|
1962 t = octave_value::op_el_ldiv; |
578
|
1963 break; |
777
|
1964 |
2899
|
1965 case LSHIFT: |
3525
|
1966 t = octave_value::op_lshift; |
2899
|
1967 break; |
|
1968 |
|
1969 case RSHIFT: |
3525
|
1970 t = octave_value::op_rshift; |
2899
|
1971 break; |
|
1972 |
578
|
1973 case EXPR_LT: |
3525
|
1974 t = octave_value::op_lt; |
578
|
1975 break; |
777
|
1976 |
578
|
1977 case EXPR_LE: |
3525
|
1978 t = octave_value::op_le; |
578
|
1979 break; |
777
|
1980 |
578
|
1981 case EXPR_EQ: |
3525
|
1982 t = octave_value::op_eq; |
578
|
1983 break; |
777
|
1984 |
578
|
1985 case EXPR_GE: |
3525
|
1986 t = octave_value::op_ge; |
578
|
1987 break; |
777
|
1988 |
578
|
1989 case EXPR_GT: |
3525
|
1990 t = octave_value::op_gt; |
578
|
1991 break; |
777
|
1992 |
578
|
1993 case EXPR_NE: |
3525
|
1994 t = octave_value::op_ne; |
578
|
1995 break; |
777
|
1996 |
578
|
1997 case EXPR_AND: |
3525
|
1998 t = octave_value::op_el_and; |
578
|
1999 break; |
777
|
2000 |
578
|
2001 case EXPR_OR: |
3525
|
2002 t = octave_value::op_el_or; |
4023
|
2003 if (Vwarn_precedence_change |
4024
|
2004 && op2->paren_count () == 0 && op2->is_binary_expression ()) |
4023
|
2005 { |
|
2006 tree_binary_expression *e |
|
2007 = dynamic_cast<tree_binary_expression *> (op2); |
|
2008 |
|
2009 if (e->op_type () == octave_value::op_el_and) |
|
2010 warning ("meaning may have changed due to change in precedence for & and | operators"); |
|
2011 } |
578
|
2012 break; |
777
|