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 |
581
|
41 #include <strstream.h> |
|
42 |
3928
|
43 #include "Cell.h" |
1
|
44 #include "Matrix.h" |
3021
|
45 #include "cmd-edit.h" |
|
46 #include "cmd-hist.h" |
|
47 #include "file-ops.h" |
|
48 #include "file-stat.h" |
3712
|
49 #include "oct-time.h" |
1
|
50 |
3665
|
51 #include "comment-list.h" |
2166
|
52 #include "defun.h" |
3021
|
53 #include "dynamic-ld.h" |
1351
|
54 #include "error.h" |
|
55 #include "input.h" |
|
56 #include "lex.h" |
1743
|
57 #include "oct-hist.h" |
2970
|
58 #include "ov-usr-fcn.h" |
1670
|
59 #include "toplev.h" |
1351
|
60 #include "pager.h" |
|
61 #include "parse.h" |
2987
|
62 #include "pt-all.h" |
1351
|
63 #include "symtab.h" |
|
64 #include "token.h" |
3021
|
65 #include "unwind-prot.h" |
1
|
66 #include "utils.h" |
1351
|
67 #include "variables.h" |
1
|
68 |
3021
|
69 // TRUE means we print |
|
70 static bool Vdefault_eval_print_flag = true; |
|
71 |
2166
|
72 // If TRUE, generate a warning for the assignment in things like |
|
73 // |
|
74 // octave> if (a = 2 < n) |
|
75 // |
|
76 // but not |
|
77 // |
|
78 // octave> if ((a = 2) < n) |
|
79 // |
|
80 static bool Vwarn_assign_as_truth_value; |
|
81 |
2764
|
82 // If TRUE, generate a warning for variable swich labels. |
|
83 static bool Vwarn_variable_switch_label; |
|
84 |
2166
|
85 // If TRUE, generate warning if declared function name disagrees with |
|
86 // the name of the file in which it is defined. |
|
87 static bool Vwarn_function_name_clash; |
|
88 |
3162
|
89 // TRUE means warn about function files that have time stamps in the future. |
|
90 bool Vwarn_future_time_stamp; |
|
91 |
2166
|
92 // If TRUE, generate warning if a statement in a function is not |
|
93 // terminated with a semicolon. Useful for checking functions that |
|
94 // should only produce output using explicit printing statements. |
|
95 static bool Vwarn_missing_semicolon; |
|
96 |
4023
|
97 // If TRUE, generate warning about the meaning of code changing due to |
|
98 // changes in precedence levels for various ops (typically for Matlab |
|
99 // compatibility). |
|
100 static bool Vwarn_precedence_change; |
|
101 |
1
|
102 // Temporary symbol table pointer used to cope with bogus function syntax. |
522
|
103 symbol_table *tmp_local_sym_tab = 0; |
1
|
104 |
|
105 // The current input line number. |
|
106 int input_line_number = 0; |
|
107 |
|
108 // The column of the current token. |
143
|
109 int current_input_column = 1; |
1
|
110 |
338
|
111 // Buffer for help text snagged from function files. |
3523
|
112 std::string help_buf; |
1
|
113 |
3665
|
114 // Buffer for comments appearing before a function statement. |
|
115 static std::string fcn_comment_header; |
|
116 |
3021
|
117 // TRUE means we are using readline. |
|
118 // (--no-line-editing) |
|
119 bool line_editing = true; |
|
120 |
|
121 // TRUE means we printed messages about reading startup files. |
|
122 bool reading_startup_message_printed = false; |
|
123 |
|
124 // TRUE means input is coming from startup file. |
|
125 bool input_from_startup_file = false; |
|
126 |
|
127 // TRUE means that input is coming from a file that was named on |
|
128 // the command line. |
|
129 bool input_from_command_line_file = true; |
|
130 |
3489
|
131 // TRUE means that we are in the process of evaluating a function |
|
132 // body. The parser might be called in that case if we are looking at |
|
133 // an eval() statement. |
|
134 bool evaluating_function_body = false; |
|
135 |
3903
|
136 // Keep track of symbol table information when parsing functions. |
|
137 static symbol_table *symtab_context = 0; |
|
138 |
496
|
139 // Forward declarations for some functions defined at the bottom of |
|
140 // the file. |
|
141 |
|
142 // Generic error messages. |
2970
|
143 static void |
|
144 yyerror (const char *s); |
1
|
145 |
578
|
146 // Error mesages for mismatched end tokens. |
2970
|
147 static void |
|
148 end_error (const char *type, token::end_tok_type ettype, int l, int c); |
1
|
149 |
578
|
150 // Check to see that end tokens are properly matched. |
2970
|
151 static bool |
|
152 end_token_ok (token *tok, token::end_tok_type expected); |
496
|
153 |
|
154 // Maybe print a warning if an assignment expression is used as the |
|
155 // test in a logical expression. |
2970
|
156 static void |
|
157 maybe_warn_assign_as_truth_value (tree_expression *expr); |
1
|
158 |
2764
|
159 // Maybe print a warning about switch labels that aren't constants. |
2970
|
160 static void |
|
161 maybe_warn_variable_switch_label (tree_expression *expr); |
2764
|
162 |
1623
|
163 // Create a plot command. |
2970
|
164 static tree_plot_command * |
|
165 make_plot_command (token *tok, plot_limits *range, subplot_list *list); |
1623
|
166 |
|
167 // Finish building a range. |
2970
|
168 static tree_expression * |
|
169 finish_colon_expression (tree_colon_expression *e); |
1623
|
170 |
1607
|
171 // Build a constant. |
2970
|
172 static tree_constant * |
|
173 make_constant (int op, token *tok_val); |
1607
|
174 |
578
|
175 // Build a binary expression. |
2970
|
176 static tree_expression * |
|
177 make_binary_op (int op, tree_expression *op1, token *tok_val, |
|
178 tree_expression *op2); |
578
|
179 |
2375
|
180 // Build a boolean expression. |
2970
|
181 static tree_expression * |
|
182 make_boolean_op (int op, tree_expression *op1, token *tok_val, |
|
183 tree_expression *op2); |
2375
|
184 |
578
|
185 // Build a prefix expression. |
2970
|
186 static tree_expression * |
|
187 make_prefix_op (int op, tree_expression *op1, token *tok_val); |
578
|
188 |
|
189 // Build a postfix expression. |
2970
|
190 static tree_expression * |
|
191 make_postfix_op (int op, tree_expression *op1, token *tok_val); |
578
|
192 |
1623
|
193 // Build an unwind-protect command. |
2970
|
194 static tree_command * |
|
195 make_unwind_command (token *unwind_tok, tree_statement_list *body, |
3665
|
196 tree_statement_list *cleanup, token *end_tok, |
|
197 octave_comment_list *lc, octave_comment_list *mc); |
1623
|
198 |
|
199 // Build a try-catch command. |
2970
|
200 static tree_command * |
|
201 make_try_command (token *try_tok, tree_statement_list *body, |
3665
|
202 tree_statement_list *cleanup, token *end_tok, |
|
203 octave_comment_list *lc, octave_comment_list *mc); |
1623
|
204 |
|
205 // Build a while command. |
2970
|
206 static tree_command * |
|
207 make_while_command (token *while_tok, tree_expression *expr, |
3665
|
208 tree_statement_list *body, token *end_tok, |
|
209 octave_comment_list *lc); |
1623
|
210 |
3484
|
211 // Build a do-until command. |
|
212 static tree_command * |
|
213 make_do_until_command (token *do_tok, tree_statement_list *body, |
3665
|
214 tree_expression *expr, octave_comment_list *lc); |
3484
|
215 |
1623
|
216 // Build a for command. |
2970
|
217 static tree_command * |
|
218 make_for_command (token *for_tok, tree_argument_list *lhs, |
|
219 tree_expression *expr, tree_statement_list *body, |
3665
|
220 token *end_tok, octave_comment_list *lc); |
1623
|
221 |
|
222 // Build a break command. |
2970
|
223 static tree_command * |
|
224 make_break_command (token *break_tok); |
1623
|
225 |
|
226 // Build a continue command. |
2970
|
227 static tree_command * |
|
228 make_continue_command (token *continue_tok); |
1623
|
229 |
|
230 // Build a return command. |
2970
|
231 static tree_command * |
|
232 make_return_command (token *return_tok); |
1623
|
233 |
|
234 // Start an if command. |
2970
|
235 static tree_if_command_list * |
|
236 start_if_command (tree_expression *expr, tree_statement_list *list); |
1623
|
237 |
|
238 // Finish an if command. |
2970
|
239 static tree_if_command * |
3665
|
240 finish_if_command (token *if_tok, tree_if_command_list *list, |
|
241 token *end_tok, octave_comment_list *lc); |
1623
|
242 |
|
243 // Build an elseif clause. |
2970
|
244 static tree_if_clause * |
3665
|
245 make_elseif_clause (tree_expression *expr, tree_statement_list *list, |
|
246 octave_comment_list *lc); |
1623
|
247 |
2764
|
248 // Finish a switch command. |
2970
|
249 static tree_switch_command * |
|
250 finish_switch_command (token *switch_tok, tree_expression *expr, |
3665
|
251 tree_switch_case_list *list, token *end_tok, |
|
252 octave_comment_list *lc); |
2764
|
253 |
|
254 // Build a switch case. |
2970
|
255 static tree_switch_case * |
3665
|
256 make_switch_case (tree_expression *expr, tree_statement_list *list, |
|
257 octave_comment_list *lc); |
2764
|
258 |
1623
|
259 // Build an assignment to a variable. |
2970
|
260 static tree_expression * |
|
261 make_assign_op (int op, tree_argument_list *lhs, token *eq_tok, |
|
262 tree_expression *rhs); |
1623
|
263 |
|
264 // Begin defining a function. |
2970
|
265 static octave_user_function * |
|
266 start_function (tree_parameter_list *param_list, tree_statement_list *body); |
1623
|
267 |
|
268 // Do most of the work for defining a function. |
2970
|
269 static octave_user_function * |
|
270 frob_function (tree_identifier *id, octave_user_function *fcn); |
1623
|
271 |
|
272 // Finish defining a function. |
2970
|
273 static octave_user_function * |
3665
|
274 finish_function (tree_identifier *id, octave_user_function *fcn, |
|
275 octave_comment_list *lc); |
1623
|
276 |
|
277 // Finish defining a function a different way. |
2970
|
278 static octave_user_function * |
3665
|
279 finish_function (tree_parameter_list *ret_list, |
|
280 octave_user_function *fcn, octave_comment_list *lc); |
751
|
281 |
2883
|
282 // Reset state after parsing function. |
2970
|
283 static void |
|
284 recover_from_parsing_function (void); |
2883
|
285 |
751
|
286 // Make an index expression. |
2970
|
287 static tree_index_expression * |
3933
|
288 make_index_expression (tree_expression *expr, |
|
289 tree_argument_list *args, char type); |
2970
|
290 |
|
291 // Make an indirect reference expression. |
3930
|
292 static tree_index_expression * |
3523
|
293 make_indirect_ref (tree_expression *expr, const std::string&); |
666
|
294 |
2846
|
295 // Make a declaration command. |
2970
|
296 static tree_decl_command * |
|
297 make_decl_command (int tok, token *tok_val, tree_decl_init_list *lst); |
2846
|
298 |
1623
|
299 // Finish building a matrix list. |
2970
|
300 static tree_expression * |
|
301 finish_matrix (tree_matrix *m); |
1623
|
302 |
3351
|
303 // Finish building a cell list. |
|
304 static tree_expression * |
|
305 finish_cell (tree_cell *c); |
|
306 |
1511
|
307 // Maybe print a warning. Duh. |
2970
|
308 static void |
|
309 maybe_warn_missing_semi (tree_statement_list *); |
1511
|
310 |
2525
|
311 // Set the print flag for a statement based on the separator type. |
2970
|
312 static void |
|
313 set_stmt_print_flag (tree_statement_list *, char, bool); |
2525
|
314 |
1
|
315 #define ABORT_PARSE \ |
|
316 do \ |
|
317 { \ |
522
|
318 global_command = 0; \ |
1
|
319 yyerrok; \ |
3929
|
320 if (symtab_context) \ |
|
321 { \ |
|
322 curr_sym_tab = symtab_context; \ |
|
323 symtab_context = 0; \ |
|
324 } \ |
2865
|
325 if (interactive || forced_interactive) \ |
1
|
326 YYACCEPT; \ |
|
327 else \ |
|
328 YYABORT; \ |
|
329 } \ |
|
330 while (0) |
|
331 |
|
332 %} |
|
333 |
666
|
334 // Bison declarations. |
|
335 |
1
|
336 %union |
|
337 { |
2891
|
338 // The type of the basic tokens returned by the lexer. |
143
|
339 token *tok_val; |
|
340 |
3665
|
341 // Comment strings that we need to deal with mid-rule. |
|
342 octave_comment_list *comment_type; |
|
343 |
2891
|
344 // Types for the nonterminals we generate. |
2525
|
345 char sep_type; |
1
|
346 tree *tree_type; |
1829
|
347 tree_matrix *tree_matrix_type; |
3351
|
348 tree_cell *tree_cell_type; |
496
|
349 tree_expression *tree_expression_type; |
2375
|
350 tree_constant *tree_constant_type; |
1
|
351 tree_identifier *tree_identifier_type; |
|
352 tree_index_expression *tree_index_expression_type; |
|
353 tree_colon_expression *tree_colon_expression_type; |
|
354 tree_argument_list *tree_argument_list_type; |
|
355 tree_parameter_list *tree_parameter_list_type; |
|
356 tree_command *tree_command_type; |
|
357 tree_if_command *tree_if_command_type; |
578
|
358 tree_if_clause *tree_if_clause_type; |
|
359 tree_if_command_list *tree_if_command_list_type; |
2764
|
360 tree_switch_command *tree_switch_command_type; |
|
361 tree_switch_case *tree_switch_case_type; |
|
362 tree_switch_case_list *tree_switch_case_list_type; |
2846
|
363 tree_decl_elt *tree_decl_elt_type; |
|
364 tree_decl_init_list *tree_decl_init_list_type; |
|
365 tree_decl_command *tree_decl_command_type; |
578
|
366 tree_statement *tree_statement_type; |
|
367 tree_statement_list *tree_statement_list_type; |
1
|
368 tree_plot_command *tree_plot_command_type; |
578
|
369 subplot *subplot_type; |
|
370 subplot_list *subplot_list_type; |
|
371 plot_limits *plot_limits_type; |
|
372 plot_range *plot_range_type; |
|
373 subplot_using *subplot_using_type; |
|
374 subplot_style *subplot_style_type; |
3165
|
375 subplot_axes *subplot_axes_type; |
2891
|
376 octave_user_function *octave_user_function_type; |
1
|
377 } |
|
378 |
143
|
379 // Tokens with line and column information. |
|
380 %token <tok_val> '=' ':' '-' '+' '*' '/' |
4018
|
381 %token <tok_val> ADD_EQ SUB_EQ MUL_EQ DIV_EQ LEFTDIV_EQ POW_EQ |
|
382 %token <tok_val> EMUL_EQ EDIV_EQ ELEFTDIV_EQ EPOW_EQ AND_EQ OR_EQ |
2899
|
383 %token <tok_val> LSHIFT_EQ RSHIFT_EQ LSHIFT RSHIFT |
428
|
384 %token <tok_val> EXPR_AND_AND EXPR_OR_OR |
143
|
385 %token <tok_val> EXPR_AND EXPR_OR EXPR_NOT |
|
386 %token <tok_val> EXPR_LT EXPR_LE EXPR_EQ EXPR_NE EXPR_GE EXPR_GT |
1276
|
387 %token <tok_val> LEFTDIV EMUL EDIV ELEFTDIV EPLUS EMINUS |
|
388 %token <tok_val> QUOTE TRANSPOSE |
143
|
389 %token <tok_val> PLUS_PLUS MINUS_MINUS POW EPOW |
|
390 %token <tok_val> NUM IMAG_NUM |
2970
|
391 %token <tok_val> STRUCT_ELT |
2883
|
392 %token <tok_val> NAME |
143
|
393 %token <tok_val> END |
|
394 %token <tok_val> PLOT |
3165
|
395 %token <tok_val> TEXT STYLE AXES_TAG |
3484
|
396 %token <tok_val> FOR WHILE DO UNTIL |
1491
|
397 %token <tok_val> IF ELSEIF ELSE |
2764
|
398 %token <tok_val> SWITCH CASE OTHERWISE |
1491
|
399 %token <tok_val> BREAK CONTINUE FUNC_RET |
924
|
400 %token <tok_val> UNWIND CLEANUP |
1489
|
401 %token <tok_val> TRY CATCH |
2846
|
402 %token <tok_val> GLOBAL STATIC |
1
|
403 |
143
|
404 // Other tokens. |
2970
|
405 %token END_OF_INPUT LEXICAL_ERROR |
3973
|
406 %token FCN VARARGIN VARARGOUT ALL_VA_ARGS |
3165
|
407 %token USING TITLE WITH AXES COLON OPEN_BRACE CLOSE_BRACE CLEAR |
1
|
408 |
143
|
409 // Nonterminals we construct. |
3665
|
410 %type <comment_type> stash_comment function_beg |
2525
|
411 %type <sep_type> sep_no_nl opt_sep_no_nl sep opt_sep |
578
|
412 %type <tree_type> input |
2960
|
413 %type <tree_constant_type> constant magic_colon |
3351
|
414 %type <tree_matrix_type> matrix_rows matrix_rows1 |
|
415 %type <tree_cell_type> cell_rows cell_rows1 |
|
416 %type <tree_expression_type> title matrix cell |
2970
|
417 %type <tree_expression_type> primary_expr postfix_expr prefix_expr binary_expr |
|
418 %type <tree_expression_type> simple_expr colon_expr assign_expr expression |
1
|
419 %type <tree_identifier_type> identifier |
2891
|
420 %type <octave_user_function_type> function1 function2 function3 |
2970
|
421 %type <tree_index_expression_type> word_list_cmd |
|
422 %type <tree_colon_expression_type> colon_expr1 |
3351
|
423 %type <tree_argument_list_type> arg_list word_list assign_lhs |
|
424 %type <tree_argument_list_type> cell_or_matrix_row |
723
|
425 %type <tree_parameter_list_type> param_list param_list1 |
|
426 %type <tree_parameter_list_type> return_list return_list1 |
2970
|
427 %type <tree_command_type> command select_command loop_command |
|
428 %type <tree_command_type> jump_command except_command function |
578
|
429 %type <tree_if_command_type> if_command |
|
430 %type <tree_if_clause_type> elseif_clause else_clause |
|
431 %type <tree_if_command_list_type> if_cmd_list1 if_cmd_list |
2764
|
432 %type <tree_switch_command_type> switch_command |
|
433 %type <tree_switch_case_type> switch_case default_case |
|
434 %type <tree_switch_case_list_type> case_list1 case_list |
2846
|
435 %type <tree_decl_elt_type> decl2 |
|
436 %type <tree_decl_init_list_type> decl1 |
|
437 %type <tree_decl_command_type> declaration |
578
|
438 %type <tree_statement_type> statement |
627
|
439 %type <tree_statement_list_type> simple_list simple_list1 list list1 |
2891
|
440 %type <tree_statement_list_type> opt_list input1 function4 |
1
|
441 %type <tree_plot_command_type> plot_command |
578
|
442 %type <subplot_type> plot_command2 plot_options |
|
443 %type <subplot_list_type> plot_command1 |
|
444 %type <plot_limits_type> ranges |
|
445 %type <plot_range_type> ranges1 |
|
446 %type <subplot_using_type> using using1 |
|
447 %type <subplot_style_type> style |
3165
|
448 %type <subplot_axes_type> axes |
1
|
449 |
143
|
450 // Precedence and associativity. |
1
|
451 %left ';' ',' '\n' |
4018
|
452 %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
|
453 %left EXPR_OR_OR |
|
454 %left EXPR_AND_AND |
|
455 %left EXPR_OR |
|
456 %left EXPR_AND |
1
|
457 %left EXPR_LT EXPR_LE EXPR_EQ EXPR_NE EXPR_GE EXPR_GT |
2899
|
458 %left LSHIFT RSHIFT |
1
|
459 %left ':' |
1276
|
460 %left '-' '+' EPLUS EMINUS |
1
|
461 %left '*' '/' LEFTDIV EMUL EDIV ELEFTDIV |
|
462 %left QUOTE TRANSPOSE |
|
463 %left UNARY PLUS_PLUS MINUS_MINUS EXPR_NOT |
|
464 %right POW EPOW |
3351
|
465 %left '(' '.' '{' |
1
|
466 |
143
|
467 // Where to start. |
1
|
468 %start input |
|
469 |
|
470 %% |
|
471 |
2970
|
472 // ============================== |
|
473 // Statements and statement lists |
|
474 // ============================== |
|
475 |
627
|
476 input : input1 |
1
|
477 { |
627
|
478 global_command = $1; |
1
|
479 promptflag = 1; |
|
480 YYACCEPT; |
|
481 } |
627
|
482 | simple_list parse_error |
1091
|
483 { ABORT_PARSE; } |
627
|
484 | parse_error |
1091
|
485 { ABORT_PARSE; } |
627
|
486 ; |
|
487 |
|
488 input1 : '\n' |
|
489 { $$ = 0; } |
3883
|
490 | END_OF_INPUT |
|
491 { |
|
492 parser_end_of_input = 1; |
|
493 $$ = 0; |
|
494 } |
327
|
495 | simple_list |
627
|
496 { $$ = $1; } |
1
|
497 | simple_list '\n' |
627
|
498 { $$ = $1; } |
1
|
499 | simple_list END_OF_INPUT |
627
|
500 { $$ = $1; } |
|
501 ; |
|
502 |
2525
|
503 simple_list : simple_list1 opt_sep_no_nl |
1
|
504 { |
2525
|
505 set_stmt_print_flag ($1, $2, false); |
1511
|
506 $$ = $1; |
1
|
507 } |
|
508 ; |
|
509 |
578
|
510 simple_list1 : statement |
|
511 { $$ = new tree_statement_list ($1); } |
2525
|
512 | simple_list1 sep_no_nl statement |
1
|
513 { |
2525
|
514 set_stmt_print_flag ($1, $2, false); |
578
|
515 $1->append ($3); |
1511
|
516 $$ = $1; |
1
|
517 } |
|
518 ; |
|
519 |
|
520 opt_list : // empty |
578
|
521 { $$ = new tree_statement_list (); } |
1
|
522 | list |
|
523 { $$ = $1; } |
496
|
524 ; |
1
|
525 |
2525
|
526 list : list1 opt_sep |
1511
|
527 { |
2525
|
528 set_stmt_print_flag ($1, $2, true); |
1829
|
529 $$ = $1; |
1
|
530 } |
|
531 ; |
|
532 |
578
|
533 list1 : statement |
440
|
534 { |
2857
|
535 lexer_flags.beginning_of_function = false; |
578
|
536 $$ = new tree_statement_list ($1); |
440
|
537 } |
2525
|
538 | list1 sep statement |
1511
|
539 { |
2525
|
540 set_stmt_print_flag ($1, $2, true); |
578
|
541 $1->append ($3); |
1511
|
542 $$ = $1; |
1
|
543 } |
|
544 ; |
|
545 |
2970
|
546 statement : expression |
3665
|
547 { |
|
548 octave_comment_list *comment |
|
549 = octave_comment_buffer::get_comment (); |
|
550 |
|
551 $$ = new tree_statement ($1, comment); |
|
552 } |
2970
|
553 | command |
3665
|
554 { |
|
555 octave_comment_list *comment |
|
556 = octave_comment_buffer::get_comment (); |
|
557 |
|
558 $$ = new tree_statement ($1, comment); |
|
559 } |
883
|
560 | PLOT CLEAR |
|
561 { |
|
562 symbol_record *sr = lookup_by_name ("clearplot", 0); |
|
563 tree_identifier *id = new tree_identifier (sr); |
3665
|
564 |
|
565 octave_comment_list *comment |
|
566 = octave_comment_buffer::get_comment (); |
|
567 |
|
568 $$ = new tree_statement (id, comment); |
883
|
569 } |
1
|
570 ; |
|
571 |
2970
|
572 // =========== |
|
573 // Expressions |
|
574 // =========== |
|
575 |
|
576 identifier : NAME |
|
577 { |
|
578 $$ = new tree_identifier |
|
579 ($1->sym_rec (), $1->line (), $1->column ()); |
|
580 } |
|
581 ; |
|
582 |
|
583 constant : NUM |
|
584 { $$ = make_constant (NUM, $1); } |
|
585 | IMAG_NUM |
|
586 { $$ = make_constant (IMAG_NUM, $1); } |
|
587 | TEXT |
|
588 { $$ = make_constant (TEXT, $1); } |
|
589 ; |
|
590 |
3189
|
591 in_matrix_or_assign_lhs |
|
592 : // empty |
|
593 { lexer_flags.looking_at_matrix_or_assign_lhs = true; } |
|
594 ; |
|
595 |
2970
|
596 matrix : '[' ']' |
|
597 { $$ = new tree_constant (octave_value (Matrix ())); } |
|
598 | '[' ';' ']' |
|
599 { $$ = new tree_constant (octave_value (Matrix ())); } |
3351
|
600 | '[' in_matrix_or_assign_lhs matrix_rows ']' |
3203
|
601 { |
|
602 $$ = finish_matrix ($3); |
|
603 lexer_flags.looking_at_matrix_or_assign_lhs = false; |
|
604 } |
2970
|
605 ; |
|
606 |
3351
|
607 matrix_rows : matrix_rows1 |
2970
|
608 { $$ = $1; } |
3351
|
609 | matrix_rows1 ';' // Ignore trailing semicolon. |
2970
|
610 { $$ = $1; } |
|
611 ; |
|
612 |
3351
|
613 matrix_rows1 : cell_or_matrix_row |
2970
|
614 { $$ = new tree_matrix ($1); } |
3351
|
615 | matrix_rows1 ';' cell_or_matrix_row |
2970
|
616 { |
|
617 $1->append ($3); |
|
618 $$ = $1; |
|
619 } |
|
620 ; |
|
621 |
3351
|
622 cell : '{' '}' |
3928
|
623 { $$ = new tree_constant (octave_value (Cell ())); } |
3351
|
624 | '{' ';' '}' |
3928
|
625 { $$ = new tree_constant (octave_value (Cell ())); } |
3351
|
626 | '{' cell_rows '}' |
|
627 { $$ = finish_cell ($2); } |
|
628 ; |
|
629 |
|
630 cell_rows : cell_rows1 |
|
631 { $$ = $1; } |
|
632 | cell_rows1 ';' // Ignore trailing semicolon. |
|
633 { $$ = $1; } |
|
634 ; |
|
635 |
|
636 cell_rows1 : cell_or_matrix_row |
|
637 { $$ = new tree_cell ($1); } |
|
638 | cell_rows1 ';' cell_or_matrix_row |
|
639 { |
|
640 $1->append ($3); |
|
641 $$ = $1; |
|
642 } |
|
643 ; |
|
644 |
|
645 cell_or_matrix_row |
|
646 : arg_list |
2970
|
647 { $$ = $1; } |
|
648 | arg_list ',' // Ignore trailing comma. |
|
649 { $$ = $1; } |
|
650 ; |
|
651 |
|
652 primary_expr : identifier |
|
653 { $$ = $1; } |
|
654 | constant |
|
655 { $$ = $1; } |
|
656 | matrix |
|
657 { $$ = $1; } |
3351
|
658 | cell |
|
659 { $$ = $1; } |
2970
|
660 | '(' expression ')' |
|
661 { $$ = $2->mark_in_parens (); } |
|
662 ; |
|
663 |
|
664 magic_colon : ':' |
|
665 { |
|
666 octave_value tmp (octave_value::magic_colon_t); |
|
667 $$ = new tree_constant (tmp); |
|
668 } |
|
669 ; |
|
670 |
|
671 arg_list : expression |
|
672 { $$ = new tree_argument_list ($1); } |
|
673 | magic_colon |
|
674 { $$ = new tree_argument_list ($1); } |
|
675 | ALL_VA_ARGS |
|
676 { |
|
677 octave_value tmp (octave_value::all_va_args_t); |
|
678 tree_constant *all_va_args = new tree_constant (tmp); |
|
679 $$ = new tree_argument_list (all_va_args); |
|
680 } |
|
681 | arg_list ',' magic_colon |
|
682 { |
|
683 $1->append ($3); |
|
684 $$ = $1; |
|
685 } |
|
686 | arg_list ',' expression |
|
687 { |
|
688 $1->append ($3); |
|
689 $$ = $1; |
|
690 } |
|
691 | arg_list ',' ALL_VA_ARGS |
|
692 { |
|
693 octave_value tmp (octave_value::all_va_args_t); |
|
694 tree_constant *all_va_args = new tree_constant (tmp); |
|
695 $1->append (all_va_args); |
|
696 $$ = $1; |
|
697 } |
|
698 ; |
|
699 |
|
700 parsing_indir : // empty |
|
701 { lexer_flags.looking_at_indirect_ref = true; } |
|
702 ; |
|
703 |
|
704 postfix_expr : primary_expr |
|
705 { $$ = $1; } |
|
706 | postfix_expr '(' ')' |
3933
|
707 { $$ = make_index_expression ($1, 0, '('); } |
2970
|
708 | postfix_expr '(' arg_list ')' |
3933
|
709 { $$ = make_index_expression ($1, $3, '('); } |
3929
|
710 | postfix_expr '{' '}' |
3933
|
711 { $$ = make_index_expression ($1, 0, '{'); } |
3351
|
712 | postfix_expr '{' arg_list '}' |
3933
|
713 { $$ = make_index_expression ($1, $3, '{'); } |
2970
|
714 | postfix_expr PLUS_PLUS |
|
715 { $$ = make_postfix_op (PLUS_PLUS, $1, $2); } |
|
716 | postfix_expr MINUS_MINUS |
|
717 { $$ = make_postfix_op (MINUS_MINUS, $1, $2); } |
|
718 | postfix_expr QUOTE |
|
719 { $$ = make_postfix_op (QUOTE, $1, $2); } |
|
720 | postfix_expr TRANSPOSE |
|
721 { $$ = make_postfix_op (TRANSPOSE, $1, $2); } |
|
722 | postfix_expr '.' parsing_indir STRUCT_ELT |
|
723 { $$ = make_indirect_ref ($1, $4->text ()); } |
|
724 ; |
|
725 |
|
726 prefix_expr : postfix_expr |
|
727 { $$ = $1; } |
3178
|
728 | binary_expr |
|
729 { $$ = $1; } |
2970
|
730 | PLUS_PLUS prefix_expr %prec UNARY |
|
731 { $$ = make_prefix_op (PLUS_PLUS, $2, $1); } |
|
732 | MINUS_MINUS prefix_expr %prec UNARY |
|
733 { $$ = make_prefix_op (MINUS_MINUS, $2, $1); } |
|
734 | EXPR_NOT prefix_expr %prec UNARY |
|
735 { $$ = make_prefix_op (EXPR_NOT, $2, $1); } |
|
736 | '+' prefix_expr %prec UNARY |
|
737 { $$ = $2; } |
|
738 | '-' prefix_expr %prec UNARY |
|
739 { $$ = make_prefix_op ('-', $2, $1); } |
|
740 ; |
|
741 |
3178
|
742 binary_expr : prefix_expr POW prefix_expr |
2970
|
743 { $$ = make_binary_op (POW, $1, $2, $3); } |
3178
|
744 | prefix_expr EPOW prefix_expr |
2970
|
745 { $$ = make_binary_op (EPOW, $1, $2, $3); } |
3178
|
746 | prefix_expr '+' prefix_expr |
2970
|
747 { $$ = make_binary_op ('+', $1, $2, $3); } |
3178
|
748 | prefix_expr '-' prefix_expr |
2970
|
749 { $$ = make_binary_op ('-', $1, $2, $3); } |
3178
|
750 | prefix_expr '*' prefix_expr |
2970
|
751 { $$ = make_binary_op ('*', $1, $2, $3); } |
3178
|
752 | prefix_expr '/' prefix_expr |
2970
|
753 { $$ = make_binary_op ('/', $1, $2, $3); } |
3178
|
754 | prefix_expr EPLUS prefix_expr |
2970
|
755 { $$ = make_binary_op ('+', $1, $2, $3); } |
3178
|
756 | prefix_expr EMINUS prefix_expr |
2970
|
757 { $$ = make_binary_op ('-', $1, $2, $3); } |
3178
|
758 | prefix_expr EMUL prefix_expr |
2970
|
759 { $$ = make_binary_op (EMUL, $1, $2, $3); } |
3178
|
760 | prefix_expr EDIV prefix_expr |
2970
|
761 { $$ = make_binary_op (EDIV, $1, $2, $3); } |
3178
|
762 | prefix_expr LEFTDIV prefix_expr |
2970
|
763 { $$ = make_binary_op (LEFTDIV, $1, $2, $3); } |
3178
|
764 | prefix_expr ELEFTDIV prefix_expr |
2970
|
765 { $$ = make_binary_op (ELEFTDIV, $1, $2, $3); } |
|
766 ; |
|
767 |
|
768 colon_expr : colon_expr1 |
|
769 { $$ = finish_colon_expression ($1); } |
|
770 ; |
|
771 |
3178
|
772 colon_expr1 : prefix_expr |
2970
|
773 { $$ = new tree_colon_expression ($1); } |
3178
|
774 | colon_expr1 ':' prefix_expr |
2970
|
775 { |
|
776 if (! ($$ = $1->append ($3))) |
|
777 ABORT_PARSE; |
|
778 } |
|
779 ; |
|
780 |
|
781 simple_expr : colon_expr |
|
782 { $$ = $1; } |
|
783 | simple_expr LSHIFT simple_expr |
|
784 { $$ = make_binary_op (LSHIFT, $1, $2, $3); } |
|
785 | simple_expr RSHIFT simple_expr |
|
786 { $$ = make_binary_op (RSHIFT, $1, $2, $3); } |
|
787 | simple_expr EXPR_LT simple_expr |
|
788 { $$ = make_binary_op (EXPR_LT, $1, $2, $3); } |
|
789 | simple_expr EXPR_LE simple_expr |
|
790 { $$ = make_binary_op (EXPR_LE, $1, $2, $3); } |
|
791 | simple_expr EXPR_EQ simple_expr |
|
792 { $$ = make_binary_op (EXPR_EQ, $1, $2, $3); } |
|
793 | simple_expr EXPR_GE simple_expr |
|
794 { $$ = make_binary_op (EXPR_GE, $1, $2, $3); } |
|
795 | simple_expr EXPR_GT simple_expr |
|
796 { $$ = make_binary_op (EXPR_GT, $1, $2, $3); } |
|
797 | simple_expr EXPR_NE simple_expr |
|
798 { $$ = make_binary_op (EXPR_NE, $1, $2, $3); } |
|
799 | simple_expr EXPR_AND simple_expr |
|
800 { $$ = make_binary_op (EXPR_AND, $1, $2, $3); } |
|
801 | simple_expr EXPR_OR simple_expr |
|
802 { $$ = make_binary_op (EXPR_OR, $1, $2, $3); } |
|
803 | simple_expr EXPR_AND_AND simple_expr |
|
804 { $$ = make_boolean_op (EXPR_AND_AND, $1, $2, $3); } |
|
805 | simple_expr EXPR_OR_OR simple_expr |
|
806 { $$ = make_boolean_op (EXPR_OR_OR, $1, $2, $3); } |
|
807 ; |
|
808 |
|
809 // Arrange for the lexer to return CLOSE_BRACE for `]' by looking ahead |
|
810 // one token for an assignment op. |
|
811 |
|
812 assign_lhs : simple_expr |
|
813 { $$ = new tree_argument_list ($1); } |
3189
|
814 | '[' in_matrix_or_assign_lhs arg_list CLOSE_BRACE |
|
815 { |
|
816 $$ = $3; |
|
817 lexer_flags.looking_at_matrix_or_assign_lhs = false; |
|
818 } |
2970
|
819 ; |
|
820 |
|
821 assign_expr : assign_lhs '=' expression |
|
822 { $$ = make_assign_op ('=', $1, $2, $3); } |
|
823 | assign_lhs ADD_EQ expression |
|
824 { $$ = make_assign_op (ADD_EQ, $1, $2, $3); } |
|
825 | assign_lhs SUB_EQ expression |
|
826 { $$ = make_assign_op (SUB_EQ, $1, $2, $3); } |
|
827 | assign_lhs MUL_EQ expression |
|
828 { $$ = make_assign_op (MUL_EQ, $1, $2, $3); } |
|
829 | assign_lhs DIV_EQ expression |
|
830 { $$ = make_assign_op (DIV_EQ, $1, $2, $3); } |
3204
|
831 | assign_lhs LEFTDIV_EQ expression |
|
832 { $$ = make_assign_op (LEFTDIV_EQ, $1, $2, $3); } |
4018
|
833 | assign_lhs POW_EQ expression |
|
834 { $$ = make_assign_op (POW_EQ, $1, $2, $3); } |
2970
|
835 | assign_lhs LSHIFT_EQ expression |
|
836 { $$ = make_assign_op (LSHIFT_EQ, $1, $2, $3); } |
|
837 | assign_lhs RSHIFT_EQ expression |
|
838 { $$ = make_assign_op (RSHIFT_EQ, $1, $2, $3); } |
|
839 | assign_lhs EMUL_EQ expression |
|
840 { $$ = make_assign_op (EMUL_EQ, $1, $2, $3); } |
|
841 | assign_lhs EDIV_EQ expression |
|
842 { $$ = make_assign_op (EDIV_EQ, $1, $2, $3); } |
3204
|
843 | assign_lhs ELEFTDIV_EQ expression |
|
844 { $$ = make_assign_op (ELEFTDIV_EQ, $1, $2, $3); } |
4018
|
845 | assign_lhs EPOW_EQ expression |
|
846 { $$ = make_assign_op (EPOW_EQ, $1, $2, $3); } |
2970
|
847 | assign_lhs AND_EQ expression |
|
848 { $$ = make_assign_op (AND_EQ, $1, $2, $3); } |
|
849 | assign_lhs OR_EQ expression |
|
850 { $$ = make_assign_op (OR_EQ, $1, $2, $3); } |
|
851 ; |
|
852 |
|
853 word_list_cmd : identifier word_list |
3933
|
854 { $$ = make_index_expression ($1, $2, '('); } |
2970
|
855 ; |
|
856 |
|
857 word_list : TEXT |
|
858 { |
|
859 tree_constant *tmp = make_constant (TEXT, $1); |
|
860 $$ = new tree_argument_list (tmp); |
|
861 } |
|
862 | word_list TEXT |
|
863 { |
|
864 tree_constant *tmp = make_constant (TEXT, $2); |
|
865 $1->append (tmp); |
|
866 $$ = $1; |
|
867 } |
|
868 ; |
|
869 |
|
870 expression : simple_expr |
|
871 { $$ = $1; } |
|
872 | word_list_cmd |
|
873 { $$ = $1; } |
|
874 | assign_expr |
|
875 { $$ = $1; } |
|
876 ; |
|
877 |
|
878 // ================================================ |
|
879 // Commands, declarations, and function definitions |
|
880 // ================================================ |
|
881 |
|
882 command : declaration |
|
883 { $$ = $1; } |
|
884 | select_command |
|
885 { $$ = $1; } |
|
886 | loop_command |
|
887 { $$ = $1; } |
|
888 | jump_command |
|
889 { $$ = $1; } |
|
890 | except_command |
|
891 { $$ = $1; } |
|
892 | function |
|
893 { $$ = $1; } |
|
894 | plot_command |
|
895 { $$ = $1; } |
|
896 ; |
|
897 |
|
898 // ===================== |
|
899 // Declaration statemnts |
|
900 // ===================== |
|
901 |
|
902 declaration : GLOBAL decl1 |
|
903 { $$ = make_decl_command (GLOBAL, $1, $2); } |
|
904 | STATIC decl1 |
|
905 { $$ = make_decl_command (STATIC, $1, $2); } |
|
906 ; |
|
907 |
|
908 decl1 : decl2 |
|
909 { $$ = new tree_decl_init_list ($1); } |
|
910 | decl1 decl2 |
|
911 { |
|
912 $1->append ($2); |
|
913 $$ = $1; |
|
914 } |
|
915 ; |
|
916 |
|
917 decl2 : identifier |
|
918 { $$ = new tree_decl_elt ($1); } |
|
919 | identifier '=' expression |
|
920 { $$ = new tree_decl_elt ($1, $3); } |
|
921 ; |
|
922 |
|
923 // ==================== |
|
924 // Selection statements |
|
925 // ==================== |
|
926 |
|
927 select_command : if_command |
|
928 { $$ = $1; } |
|
929 | switch_command |
|
930 { $$ = $1; } |
|
931 ; |
|
932 |
|
933 // ============ |
|
934 // If statement |
|
935 // ============ |
|
936 |
3665
|
937 if_command : IF stash_comment if_cmd_list END |
2970
|
938 { |
3665
|
939 if (! ($$ = finish_if_command ($1, $3, $4, $2))) |
2970
|
940 ABORT_PARSE; |
|
941 } |
|
942 ; |
|
943 |
|
944 if_cmd_list : if_cmd_list1 |
|
945 { $$ = $1; } |
|
946 | if_cmd_list1 else_clause |
|
947 { |
|
948 $1->append ($2); |
|
949 $$ = $1; |
|
950 } |
|
951 ; |
|
952 |
|
953 if_cmd_list1 : expression opt_sep opt_list |
|
954 { $$ = start_if_command ($1, $3); } |
|
955 | if_cmd_list1 elseif_clause |
|
956 { |
|
957 $1->append ($2); |
|
958 $$ = $1; |
|
959 } |
|
960 ; |
|
961 |
3665
|
962 elseif_clause : ELSEIF stash_comment opt_sep expression opt_sep opt_list |
|
963 { $$ = make_elseif_clause ($4, $6, $2); } |
2970
|
964 ; |
|
965 |
3665
|
966 else_clause : ELSE stash_comment opt_sep opt_list |
|
967 { |
|
968 $$ = new tree_if_clause ($4, $2); |
|
969 } |
2970
|
970 ; |
|
971 |
|
972 // ================ |
|
973 // Switch statement |
|
974 // ================ |
|
975 |
3665
|
976 switch_command : SWITCH stash_comment expression opt_sep case_list END |
2970
|
977 { |
3665
|
978 if (! ($$ = finish_switch_command ($1, $3, $5, $6, $2))) |
2970
|
979 ABORT_PARSE; |
|
980 } |
|
981 ; |
|
982 |
4044
|
983 case_list : // empty |
|
984 { $$ = new tree_switch_case_list (); } |
|
985 | case_list1 |
2970
|
986 { $$ = $1; } |
|
987 | case_list1 default_case |
|
988 { |
|
989 $1->append ($2); |
|
990 $$ = $1; |
|
991 } |
|
992 ; |
|
993 |
|
994 case_list1 : switch_case |
|
995 { $$ = new tree_switch_case_list ($1); } |
|
996 | case_list1 switch_case |
|
997 { |
|
998 $1->append ($2); |
|
999 $$ = $1; |
|
1000 } |
|
1001 ; |
|
1002 |
4025
|
1003 switch_case : CASE stash_comment opt_sep expression opt_sep opt_list |
3665
|
1004 { $$ = make_switch_case ($4, $6, $2); } |
2970
|
1005 ; |
|
1006 |
3665
|
1007 default_case : OTHERWISE stash_comment opt_sep opt_list |
|
1008 { |
|
1009 $$ = new tree_switch_case ($4, $2); |
|
1010 } |
2970
|
1011 ; |
|
1012 |
|
1013 // ======= |
|
1014 // Looping |
|
1015 // ======= |
|
1016 |
3665
|
1017 loop_command : WHILE stash_comment expression opt_sep opt_list END |
2970
|
1018 { |
3665
|
1019 if (! ($$ = make_while_command ($1, $3, $5, $6, $2))) |
2970
|
1020 ABORT_PARSE; |
|
1021 } |
3665
|
1022 | DO stash_comment opt_sep opt_list UNTIL expression |
3484
|
1023 { |
3665
|
1024 if (! ($$ = make_do_until_command ($1, $4, $6, $2))) |
3484
|
1025 ABORT_PARSE; |
|
1026 } |
3665
|
1027 | FOR stash_comment assign_lhs '=' expression opt_sep opt_list END |
2970
|
1028 { |
3665
|
1029 if (! ($$ = make_for_command ($1, $3, $5, $7, $8, $2))) |
2970
|
1030 ABORT_PARSE; |
|
1031 } |
|
1032 ; |
|
1033 |
|
1034 // ======= |
|
1035 // Jumping |
|
1036 // ======= |
|
1037 |
|
1038 jump_command : BREAK |
|
1039 { |
|
1040 if (! ($$ = make_break_command ($1))) |
|
1041 ABORT_PARSE; |
|
1042 } |
|
1043 | CONTINUE |
|
1044 { |
|
1045 if (! ($$ = make_continue_command ($1))) |
|
1046 ABORT_PARSE; |
|
1047 } |
|
1048 | FUNC_RET |
|
1049 { |
|
1050 if (! ($$ = make_return_command ($1))) |
|
1051 ABORT_PARSE; |
|
1052 } |
|
1053 ; |
|
1054 |
|
1055 // ========== |
|
1056 // Exceptions |
|
1057 // ========== |
|
1058 |
3665
|
1059 except_command : UNWIND stash_comment opt_sep opt_list CLEANUP |
|
1060 stash_comment opt_sep opt_list END |
2970
|
1061 { |
3665
|
1062 if (! ($$ = make_unwind_command ($1, $4, $8, $9, $2, $6))) |
2970
|
1063 ABORT_PARSE; |
|
1064 } |
3665
|
1065 | TRY stash_comment opt_sep opt_list CATCH |
|
1066 stash_comment opt_sep opt_list END |
2970
|
1067 { |
3665
|
1068 if (! ($$ = make_try_command ($1, $4, $8, $9, $2, $6))) |
2970
|
1069 ABORT_PARSE; |
|
1070 } |
|
1071 ; |
|
1072 |
|
1073 // =========================================== |
|
1074 // Some `subroutines' for function definitions |
|
1075 // =========================================== |
|
1076 |
3903
|
1077 save_symtab : // empty |
|
1078 { |
|
1079 if (symtab_context) |
|
1080 panic_impossible (); |
|
1081 |
|
1082 symtab_context = curr_sym_tab; |
|
1083 } |
|
1084 ; |
|
1085 |
4009
|
1086 function_symtab : // empty |
|
1087 { curr_sym_tab = fbi_sym_tab; } |
2970
|
1088 ; |
|
1089 |
|
1090 local_symtab : // empty |
|
1091 { curr_sym_tab = tmp_local_sym_tab; } |
|
1092 ; |
|
1093 |
|
1094 in_return_list : // empty |
|
1095 { lexer_flags.looking_at_return_list = true; } |
|
1096 ; |
|
1097 |
|
1098 parsed_fcn_name : // empty |
|
1099 { lexer_flags.parsed_function_name = true; } |
|
1100 ; |
|
1101 |
|
1102 // =========================== |
|
1103 // List of function parameters |
|
1104 // =========================== |
|
1105 |
|
1106 param_list_beg : '(' |
|
1107 { lexer_flags.looking_at_parameter_list = true; } |
|
1108 ; |
|
1109 |
|
1110 param_list_end : ')' |
|
1111 { lexer_flags.looking_at_parameter_list = false; } |
|
1112 ; |
|
1113 |
|
1114 param_list : param_list_beg param_list_end |
|
1115 { |
|
1116 lexer_flags.quote_is_transpose = false; |
|
1117 $$ = 0; |
|
1118 } |
3973
|
1119 | param_list_beg VARARGIN param_list_end |
2970
|
1120 { |
|
1121 lexer_flags.quote_is_transpose = false; |
|
1122 tree_parameter_list *tmp = new tree_parameter_list (); |
|
1123 tmp->mark_varargs_only (); |
|
1124 $$ = tmp; |
|
1125 } |
|
1126 | param_list1 param_list_end |
|
1127 { |
|
1128 lexer_flags.quote_is_transpose = false; |
|
1129 $1->mark_as_formal_parameters (); |
|
1130 $$ = $1; |
|
1131 } |
3973
|
1132 | param_list1 ',' VARARGIN param_list_end |
2970
|
1133 { |
|
1134 lexer_flags.quote_is_transpose = false; |
|
1135 $1->mark_as_formal_parameters (); |
|
1136 $1->mark_varargs (); |
|
1137 $$ = $1; |
|
1138 } |
|
1139 ; |
|
1140 |
|
1141 param_list1 : param_list_beg identifier |
|
1142 { $$ = new tree_parameter_list ($2); } |
|
1143 | param_list1 ',' identifier |
|
1144 { |
|
1145 $1->append ($3); |
|
1146 $$ = $1; |
|
1147 } |
|
1148 | param_list_beg error |
|
1149 { |
|
1150 yyerror ("invalid parameter list"); |
|
1151 $$ = 0; |
|
1152 ABORT_PARSE; |
|
1153 } |
|
1154 | param_list1 ',' error |
|
1155 { |
|
1156 yyerror ("invalid parameter list"); |
|
1157 $$ = 0; |
|
1158 ABORT_PARSE; |
|
1159 } |
|
1160 ; |
|
1161 |
|
1162 // =================================== |
|
1163 // List of function return value names |
|
1164 // =================================== |
|
1165 |
3189
|
1166 return_list_beg : '[' in_return_list local_symtab |
2970
|
1167 ; |
|
1168 |
|
1169 return_list : return_list_beg return_list_end |
|
1170 { |
|
1171 lexer_flags.looking_at_return_list = false; |
|
1172 $$ = new tree_parameter_list (); |
|
1173 } |
3973
|
1174 | return_list_beg VARARGOUT return_list_end |
2970
|
1175 { |
|
1176 lexer_flags.looking_at_return_list = false; |
|
1177 tree_parameter_list *tmp = new tree_parameter_list (); |
|
1178 tmp->mark_varargs_only (); |
|
1179 $$ = tmp; |
|
1180 } |
|
1181 | return_list_beg return_list1 return_list_end |
|
1182 { |
|
1183 lexer_flags.looking_at_return_list = false; |
|
1184 $$ = $2; |
|
1185 } |
3973
|
1186 | return_list_beg return_list1 ',' VARARGOUT return_list_end |
2970
|
1187 { |
|
1188 lexer_flags.looking_at_return_list = false; |
|
1189 $2->mark_varargs (); |
|
1190 $$ = $2; |
|
1191 } |
|
1192 ; |
|
1193 |
|
1194 return_list1 : identifier |
|
1195 { $$ = new tree_parameter_list ($1); } |
|
1196 | return_list1 ',' identifier |
|
1197 { |
|
1198 $1->append ($3); |
|
1199 $$ = $1; |
|
1200 } |
|
1201 ; |
|
1202 |
4009
|
1203 return_list_end : function_symtab ']' |
2970
|
1204 ; |
|
1205 |
|
1206 // =================== |
|
1207 // Function definition |
|
1208 // =================== |
|
1209 |
4009
|
1210 function_beg : save_symtab FCN stash_comment function_symtab |
3903
|
1211 { $$ = $3; } |
2970
|
1212 ; |
|
1213 |
|
1214 function : function_beg function2 |
|
1215 { |
3665
|
1216 $2->stash_leading_comment ($1); |
2970
|
1217 recover_from_parsing_function (); |
|
1218 $$ = 0; |
|
1219 } |
|
1220 | function_beg identifier function1 |
|
1221 { |
3665
|
1222 finish_function ($2, $3, $1); |
2970
|
1223 recover_from_parsing_function (); |
|
1224 $$ = 0; |
|
1225 } |
|
1226 | function_beg return_list function1 |
|
1227 { |
3665
|
1228 finish_function ($2, $3, $1); |
2970
|
1229 recover_from_parsing_function (); |
|
1230 $$ = 0; |
|
1231 } |
|
1232 ; |
|
1233 |
4009
|
1234 function1 : function_symtab '=' function2 |
2970
|
1235 { $$ = $3; } |
|
1236 ; |
|
1237 |
|
1238 function2 : identifier local_symtab parsed_fcn_name function3 |
|
1239 { |
|
1240 if (! ($$ = frob_function ($1, $4))) |
|
1241 ABORT_PARSE; |
|
1242 } |
|
1243 ; |
|
1244 |
|
1245 function3 : param_list function4 |
|
1246 { $$ = start_function ($1, $2); } |
|
1247 | function4 |
|
1248 { $$ = start_function (0, $1); } |
|
1249 ; |
|
1250 |
|
1251 function4 : opt_sep opt_list function_end |
|
1252 { $$ = $2; } |
|
1253 ; |
|
1254 |
|
1255 function_end : END |
|
1256 { |
|
1257 if (end_token_ok ($1, token::function_end)) |
|
1258 { |
|
1259 if (reading_fcn_file) |
|
1260 check_for_garbage_after_fcn_def (); |
|
1261 } |
|
1262 else |
|
1263 ABORT_PARSE; |
|
1264 } |
|
1265 | END_OF_INPUT |
|
1266 { |
3939
|
1267 if (! (reading_fcn_file || reading_script_file |
|
1268 || get_input_from_eval_string)) |
2970
|
1269 YYABORT; |
|
1270 } |
|
1271 ; |
|
1272 |
|
1273 // ======== |
|
1274 // Plotting |
|
1275 // ======== |
|
1276 |
3180
|
1277 plot_command : PLOT |
|
1278 { |
|
1279 if (! ($$ = make_plot_command ($1, 0, 0))) |
|
1280 ABORT_PARSE; |
|
1281 } |
|
1282 | PLOT ranges |
|
1283 { |
|
1284 if (! ($$ = make_plot_command ($1, $2, 0))) |
|
1285 ABORT_PARSE; |
|
1286 } |
|
1287 | PLOT plot_command1 |
1
|
1288 { |
1623
|
1289 if (! ($$ = make_plot_command ($1, 0, $2))) |
|
1290 ABORT_PARSE; |
1
|
1291 } |
|
1292 | PLOT ranges plot_command1 |
|
1293 { |
1623
|
1294 if (! ($$ = make_plot_command ($1, $2, $3))) |
|
1295 ABORT_PARSE; |
1
|
1296 } |
|
1297 ; |
|
1298 |
|
1299 ranges : ranges1 |
578
|
1300 { $$ = new plot_limits ($1); } |
1
|
1301 | ranges1 ranges1 |
578
|
1302 { $$ = new plot_limits ($1, $2); } |
1
|
1303 | ranges1 ranges1 ranges1 |
578
|
1304 { $$ = new plot_limits ($1, $2, $3); } |
1
|
1305 ; |
|
1306 |
|
1307 ranges1 : OPEN_BRACE expression COLON expression CLOSE_BRACE |
578
|
1308 { $$ = new plot_range ($2, $4); } |
1
|
1309 | OPEN_BRACE COLON expression CLOSE_BRACE |
578
|
1310 { $$ = new plot_range (0, $3); } |
1
|
1311 | OPEN_BRACE expression COLON CLOSE_BRACE |
578
|
1312 { $$ = new plot_range ($2, 0); } |
1
|
1313 | OPEN_BRACE COLON CLOSE_BRACE |
578
|
1314 { $$ = new plot_range (); } |
1
|
1315 | OPEN_BRACE CLOSE_BRACE |
578
|
1316 { $$ = new plot_range (); } |
1
|
1317 ; |
|
1318 |
3180
|
1319 plot_command1 : plot_command2 |
578
|
1320 { $$ = new subplot_list ($1); } |
1
|
1321 | plot_command1 ',' plot_command2 |
1829
|
1322 { |
|
1323 $1->append ($3); |
|
1324 $$ = $1; |
|
1325 } |
1
|
1326 ; |
|
1327 |
|
1328 plot_command2 : expression |
578
|
1329 { $$ = new subplot ($1); } |
1
|
1330 | expression plot_options |
3165
|
1331 { $$ = $2->add_data ($1); } |
1
|
1332 ; |
|
1333 |
|
1334 plot_options : using |
3165
|
1335 { |
|
1336 subplot *tmp = new subplot (); |
|
1337 $$ = tmp->add_clause ($1); |
|
1338 } |
1
|
1339 | title |
3165
|
1340 { |
|
1341 subplot *tmp = new subplot (); |
|
1342 $$ = tmp->add_clause ($1); |
|
1343 } |
1
|
1344 | style |
3165
|
1345 { |
|
1346 subplot *tmp = new subplot (); |
|
1347 $$ = tmp->add_clause ($1); |
|
1348 } |
|
1349 | axes |
|
1350 { |
|
1351 subplot *tmp = new subplot (); |
|
1352 $$ = tmp->add_clause ($1); |
|
1353 } |
|
1354 | plot_options using |
|
1355 { |
|
1356 if (! ($$ = $1->add_clause ($2))) |
|
1357 { |
|
1358 yyerror ("only one using option may be specified"); |
|
1359 ABORT_PARSE; |
|
1360 } |
|
1361 } |
|
1362 | plot_options title |
|
1363 { |
|
1364 if (! ($$ = $1->add_clause ($2))) |
|
1365 { |
|
1366 yyerror ("only one title option my be specified"); |
|
1367 ABORT_PARSE; |
|
1368 } |
|
1369 } |
|
1370 | plot_options style |
|
1371 { |
|
1372 if (! ($$ = $1->add_clause ($2))) |
|
1373 { |
|
1374 yyerror ("only one style option my be specified"); |
|
1375 ABORT_PARSE; |
|
1376 } |
|
1377 } |
|
1378 | plot_options axes |
|
1379 { |
|
1380 if (! ($$ = $1->add_clause ($2))) |
|
1381 { |
|
1382 yyerror ("only one axes option may be specified"); |
|
1383 ABORT_PARSE; |
|
1384 } |
|
1385 } |
|
1386 ; |
|
1387 |
|
1388 axes : AXES AXES_TAG |
|
1389 { |
|
1390 lexer_flags.in_plot_axes = false; |
|
1391 $$ = new subplot_axes ($2->text ()); |
|
1392 } |
1
|
1393 ; |
|
1394 |
|
1395 using : using1 |
|
1396 { |
2857
|
1397 lexer_flags.in_plot_using = false; |
1
|
1398 $$ = $1; |
|
1399 } |
|
1400 | using1 expression |
|
1401 { |
2857
|
1402 lexer_flags.in_plot_using = false; |
1
|
1403 $$ = $1->set_format ($2); |
|
1404 } |
|
1405 ; |
|
1406 |
|
1407 using1 : USING expression |
|
1408 { |
578
|
1409 subplot_using *tmp = new subplot_using (); |
1
|
1410 $$ = tmp->add_qualifier ($2); |
|
1411 } |
|
1412 | using1 COLON expression |
|
1413 { $$ = $1->add_qualifier ($3); } |
|
1414 ; |
|
1415 |
|
1416 title : TITLE expression |
|
1417 { $$ = $2; } |
|
1418 ; |
|
1419 |
|
1420 style : WITH STYLE |
1755
|
1421 { $$ = new subplot_style ($2->text ()); } |
1
|
1422 | WITH STYLE expression |
1755
|
1423 { $$ = new subplot_style ($2->text (), $3); } |
2542
|
1424 | WITH STYLE expression expression |
|
1425 { $$ = new subplot_style ($2->text (), $3, $4); } |
1
|
1426 ; |
|
1427 |
2970
|
1428 // ============= |
|
1429 // Miscellaneous |
|
1430 // ============= |
|
1431 |
3665
|
1432 stash_comment : // empty |
|
1433 { $$ = octave_comment_buffer::get_comment (); } |
|
1434 ; |
|
1435 |
2970
|
1436 parse_error : LEXICAL_ERROR |
|
1437 { yyerror ("parse error"); } |
|
1438 | error |
1
|
1439 ; |
|
1440 |
2525
|
1441 sep_no_nl : ',' |
|
1442 { $$ = ','; } |
|
1443 | ';' |
|
1444 { $$ = ';'; } |
|
1445 | sep_no_nl ',' |
|
1446 { $$ = $1; } |
|
1447 | sep_no_nl ';' |
|
1448 { $$ = $1; } |
|
1449 ; |
|
1450 |
|
1451 opt_sep_no_nl : // empty |
|
1452 { $$ = 0; } |
|
1453 | sep_no_nl |
|
1454 { $$ = $1; } |
|
1455 ; |
|
1456 |
|
1457 sep : ',' |
|
1458 { $$ = ','; } |
|
1459 | ';' |
|
1460 { $$ = ';'; } |
|
1461 | '\n' |
|
1462 { $$ = '\n'; } |
|
1463 | sep ',' |
|
1464 { $$ = $1; } |
|
1465 | sep ';' |
|
1466 { $$ = $1; } |
|
1467 | sep '\n' |
|
1468 { $$ = $1; } |
|
1469 ; |
|
1470 |
|
1471 opt_sep : // empty |
|
1472 { $$ = 0; } |
|
1473 | sep |
|
1474 { $$ = $1; } |
|
1475 ; |
|
1476 |
1
|
1477 %% |
|
1478 |
666
|
1479 // Generic error messages. |
|
1480 |
1
|
1481 static void |
2805
|
1482 yyerror (const char *s) |
1
|
1483 { |
143
|
1484 int err_col = current_input_column - 1; |
1
|
1485 |
3523
|
1486 std::ostrstream output_buf; |
1
|
1487 |
1005
|
1488 if (reading_fcn_file || reading_script_file) |
|
1489 output_buf << "parse error near line " << input_line_number |
1607
|
1490 << " of file " << curr_fcn_file_full_name; |
1005
|
1491 else |
|
1492 output_buf << "parse error:"; |
581
|
1493 |
1005
|
1494 if (s && strcmp (s, "parse error") != 0) |
|
1495 output_buf << "\n\n " << s; |
|
1496 |
|
1497 output_buf << "\n\n"; |
1
|
1498 |
1755
|
1499 if (! current_input_line.empty ()) |
1
|
1500 { |
1755
|
1501 size_t len = current_input_line.length (); |
1060
|
1502 |
1755
|
1503 if (current_input_line[len-1] == '\n') |
|
1504 current_input_line.resize (len-1); |
1005
|
1505 |
335
|
1506 // Print the line, maybe with a pointer near the error token. |
1005
|
1507 |
1755
|
1508 output_buf << ">>> " << current_input_line << "\n"; |
1060
|
1509 |
|
1510 if (err_col == 0) |
|
1511 err_col = len; |
|
1512 |
|
1513 for (int i = 0; i < err_col + 3; i++) |
|
1514 output_buf << " "; |
|
1515 |
|
1516 output_buf << "^"; |
1
|
1517 } |
1005
|
1518 |
3538
|
1519 output_buf << "\n" << std::ends; |
581
|
1520 |
1005
|
1521 char *msg = output_buf.str (); |
|
1522 |
1090
|
1523 parse_error ("%s", msg); |
1005
|
1524 |
|
1525 delete [] msg; |
1
|
1526 } |
|
1527 |
666
|
1528 // Error mesages for mismatched end tokens. |
|
1529 |
496
|
1530 static void |
2805
|
1531 end_error (const char *type, token::end_tok_type ettype, int l, int c) |
496
|
1532 { |
2805
|
1533 static const char *fmt |
|
1534 = "`%s' command matched by `%s' near line %d column %d"; |
496
|
1535 |
|
1536 switch (ettype) |
|
1537 { |
|
1538 case token::simple_end: |
|
1539 error (fmt, type, "end", l, c); |
|
1540 break; |
777
|
1541 |
496
|
1542 case token::for_end: |
|
1543 error (fmt, type, "endfor", l, c); |
|
1544 break; |
777
|
1545 |
496
|
1546 case token::function_end: |
|
1547 error (fmt, type, "endfunction", l, c); |
|
1548 break; |
777
|
1549 |
496
|
1550 case token::if_end: |
|
1551 error (fmt, type, "endif", l, c); |
|
1552 break; |
777
|
1553 |
3233
|
1554 case token::switch_end: |
|
1555 error (fmt, type, "endswitch", l, c); |
|
1556 break; |
|
1557 |
496
|
1558 case token::while_end: |
|
1559 error (fmt, type, "endwhile", l, c); |
|
1560 break; |
777
|
1561 |
1371
|
1562 case token::unwind_protect_end: |
|
1563 error (fmt, type, "end_unwind_protect", l, c); |
|
1564 break; |
|
1565 |
496
|
1566 default: |
|
1567 panic_impossible (); |
|
1568 break; |
|
1569 } |
|
1570 } |
|
1571 |
666
|
1572 // Check to see that end tokens are properly matched. |
|
1573 |
2857
|
1574 static bool |
|
1575 end_token_ok (token *tok, token::end_tok_type expected) |
143
|
1576 { |
2857
|
1577 bool retval = true; |
|
1578 |
143
|
1579 token::end_tok_type ettype = tok->ettype (); |
2857
|
1580 |
143
|
1581 if (ettype != expected && ettype != token::simple_end) |
|
1582 { |
2857
|
1583 retval = false; |
|
1584 |
143
|
1585 yyerror ("parse error"); |
|
1586 |
|
1587 int l = tok->line (); |
|
1588 int c = tok->column (); |
|
1589 |
|
1590 switch (expected) |
|
1591 { |
|
1592 case token::for_end: |
|
1593 end_error ("for", ettype, l, c); |
|
1594 break; |
777
|
1595 |
143
|
1596 case token::function_end: |
|
1597 end_error ("function", ettype, l, c); |
|
1598 break; |
777
|
1599 |
143
|
1600 case token::if_end: |
|
1601 end_error ("if", ettype, l, c); |
|
1602 break; |
777
|
1603 |
1489
|
1604 case token::try_catch_end: |
|
1605 end_error ("try", ettype, l, c); |
|
1606 break; |
|
1607 |
2764
|
1608 case token::switch_end: |
|
1609 end_error ("switch", ettype, l, c); |
|
1610 break; |
|
1611 |
1489
|
1612 case token::unwind_protect_end: |
|
1613 end_error ("unwind_protect", ettype, l, c); |
|
1614 break; |
|
1615 |
143
|
1616 case token::while_end: |
|
1617 end_error ("while", ettype, l, c); |
|
1618 break; |
777
|
1619 |
143
|
1620 default: |
|
1621 panic_impossible (); |
|
1622 break; |
|
1623 } |
|
1624 } |
2857
|
1625 |
|
1626 return retval; |
143
|
1627 } |
|
1628 |
666
|
1629 // Maybe print a warning if an assignment expression is used as the |
|
1630 // test in a logical expression. |
|
1631 |
496
|
1632 static void |
|
1633 maybe_warn_assign_as_truth_value (tree_expression *expr) |
1
|
1634 { |
2166
|
1635 if (Vwarn_assign_as_truth_value |
1
|
1636 && expr->is_assignment_expression () |
2961
|
1637 && expr->paren_count () < 2) |
1
|
1638 { |
|
1639 warning ("suggest parenthesis around assignment used as truth value"); |
|
1640 } |
|
1641 } |
578
|
1642 |
2764
|
1643 // Maybe print a warning about switch labels that aren't constants. |
|
1644 |
|
1645 static void |
|
1646 maybe_warn_variable_switch_label (tree_expression *expr) |
|
1647 { |
|
1648 if (Vwarn_variable_switch_label && ! expr->is_constant ()) |
|
1649 { |
|
1650 warning ("variable switch label"); |
|
1651 } |
|
1652 } |
|
1653 |
1623
|
1654 // Create a plot command. |
|
1655 |
|
1656 static tree_plot_command * |
|
1657 make_plot_command (token *tok, plot_limits *range, subplot_list *list) |
|
1658 { |
|
1659 if (range) |
|
1660 { |
|
1661 if (tok->pttype () == token::replot) |
|
1662 { |
|
1663 yyerror ("cannot specify new ranges with replot"); |
|
1664 return 0; |
|
1665 } |
|
1666 } |
|
1667 else if (! list && tok->pttype () != token::replot) |
|
1668 { |
|
1669 yyerror ("must have something to plot"); |
|
1670 return 0; |
|
1671 } |
|
1672 |
2857
|
1673 lexer_flags.plotting = false; |
|
1674 lexer_flags.past_plot_range = false; |
|
1675 lexer_flags.in_plot_range = false; |
|
1676 lexer_flags.in_plot_using = false; |
|
1677 lexer_flags.in_plot_style = false; |
1623
|
1678 |
|
1679 return new tree_plot_command (list, range, tok->pttype ()); |
|
1680 } |
|
1681 |
2533
|
1682 static tree_expression * |
|
1683 fold (tree_binary_expression *e) |
|
1684 { |
3110
|
1685 tree_expression *retval = e; |
|
1686 |
3292
|
1687 unwind_protect::begin_frame ("fold_binary_expression"); |
3110
|
1688 |
|
1689 unwind_protect_int (error_state); |
|
1690 |
3815
|
1691 unwind_protect_bool (discard_error_messages); |
|
1692 discard_error_messages = true; |
2533
|
1693 |
|
1694 tree_expression *op1 = e->lhs (); |
|
1695 tree_expression *op2 = e->rhs (); |
|
1696 |
|
1697 if (op1->is_constant () && op2->is_constant ()) |
|
1698 { |
2970
|
1699 octave_value tmp = e->rvalue (); |
2533
|
1700 |
3489
|
1701 if (! (error_state || warning_state)) |
2533
|
1702 { |
|
1703 tree_constant *tc_retval = new tree_constant (tmp); |
|
1704 |
3523
|
1705 std::ostrstream buf; |
2533
|
1706 |
|
1707 tree_print_code tpc (buf); |
|
1708 |
|
1709 e->accept (tpc); |
|
1710 |
3538
|
1711 buf << std::ends; |
2533
|
1712 |
|
1713 char *s = buf.str (); |
|
1714 |
|
1715 tc_retval->stash_original_text (s); |
|
1716 |
|
1717 delete [] s; |
|
1718 |
|
1719 delete e; |
|
1720 |
|
1721 retval = tc_retval; |
|
1722 } |
|
1723 } |
3110
|
1724 |
3292
|
1725 unwind_protect::run_frame ("fold_binary_expression"); |
|
1726 |
|
1727 return retval; |
|
1728 } |
|
1729 |
|
1730 static tree_expression * |
|
1731 fold (tree_unary_expression *e) |
|
1732 { |
|
1733 tree_expression *retval = e; |
|
1734 |
|
1735 unwind_protect::begin_frame ("fold_unary_expression"); |
|
1736 |
|
1737 unwind_protect_int (error_state); |
|
1738 |
3815
|
1739 unwind_protect_bool (discard_error_messages); |
|
1740 discard_error_messages = true; |
3292
|
1741 |
|
1742 tree_expression *op = e->operand (); |
|
1743 |
|
1744 if (op->is_constant ()) |
|
1745 { |
|
1746 octave_value tmp = e->rvalue (); |
|
1747 |
3489
|
1748 if (! (error_state || warning_state)) |
3292
|
1749 { |
|
1750 tree_constant *tc_retval = new tree_constant (tmp); |
|
1751 |
3523
|
1752 std::ostrstream buf; |
3292
|
1753 |
|
1754 tree_print_code tpc (buf); |
|
1755 |
|
1756 e->accept (tpc); |
|
1757 |
3538
|
1758 buf << std::ends; |
3292
|
1759 |
|
1760 char *s = buf.str (); |
|
1761 |
|
1762 tc_retval->stash_original_text (s); |
|
1763 |
|
1764 delete [] s; |
|
1765 |
|
1766 delete e; |
|
1767 |
|
1768 retval = tc_retval; |
|
1769 } |
|
1770 } |
|
1771 |
|
1772 unwind_protect::run_frame ("fold_unary_expression"); |
2533
|
1773 |
|
1774 return retval; |
|
1775 } |
|
1776 |
1623
|
1777 // Finish building a range. |
|
1778 |
|
1779 static tree_expression * |
|
1780 finish_colon_expression (tree_colon_expression *e) |
|
1781 { |
3110
|
1782 tree_expression *retval = e; |
|
1783 |
|
1784 unwind_protect::begin_frame ("finish_colon_expression"); |
|
1785 |
|
1786 unwind_protect_int (error_state); |
|
1787 |
3815
|
1788 unwind_protect_bool (discard_error_messages); |
|
1789 discard_error_messages = true; |
1623
|
1790 |
2533
|
1791 tree_expression *base = e->base (); |
|
1792 tree_expression *limit = e->limit (); |
|
1793 tree_expression *incr = e->increment (); |
|
1794 |
2970
|
1795 if (base) |
1623
|
1796 { |
2970
|
1797 if (limit) |
2533
|
1798 { |
2970
|
1799 if (base->is_constant () && limit->is_constant () |
|
1800 && (! incr || (incr && incr->is_constant ()))) |
|
1801 { |
|
1802 octave_value tmp = e->rvalue (); |
|
1803 |
3489
|
1804 if (! (error_state || warning_state)) |
2970
|
1805 { |
|
1806 tree_constant *tc_retval = new tree_constant (tmp); |
|
1807 |
3523
|
1808 std::ostrstream buf; |
2970
|
1809 |
|
1810 tree_print_code tpc (buf); |
|
1811 |
|
1812 e->accept (tpc); |
|
1813 |
3538
|
1814 buf << std::ends; |
2970
|
1815 |
|
1816 char *s = buf.str (); |
|
1817 |
|
1818 tc_retval->stash_original_text (s); |
|
1819 |
|
1820 delete [] s; |
|
1821 |
|
1822 delete e; |
|
1823 |
|
1824 retval = tc_retval; |
|
1825 } |
|
1826 } |
2533
|
1827 } |
|
1828 else |
2970
|
1829 { |
2990
|
1830 e->preserve_base (); |
|
1831 delete e; |
2970
|
1832 |
|
1833 // XXX FIXME XXX -- need to attempt constant folding here |
|
1834 // too (we need a generic way to do that). |
|
1835 retval = base; |
|
1836 } |
1623
|
1837 } |
|
1838 |
3110
|
1839 unwind_protect::run_frame ("finish_colon_expression"); |
|
1840 |
1623
|
1841 return retval; |
|
1842 } |
|
1843 |
1607
|
1844 // Make a constant. |
|
1845 |
2375
|
1846 static tree_constant * |
1607
|
1847 make_constant (int op, token *tok_val) |
|
1848 { |
|
1849 int l = tok_val->line (); |
|
1850 int c = tok_val->column (); |
|
1851 |
3216
|
1852 tree_constant *retval = 0; |
1607
|
1853 |
|
1854 switch (op) |
|
1855 { |
|
1856 case NUM: |
2533
|
1857 { |
2883
|
1858 octave_value tmp (tok_val->number ()); |
|
1859 retval = new tree_constant (tmp, l, c); |
2533
|
1860 retval->stash_original_text (tok_val->text_rep ()); |
|
1861 } |
1607
|
1862 break; |
|
1863 |
|
1864 case IMAG_NUM: |
|
1865 { |
2883
|
1866 octave_value tmp (Complex (0.0, tok_val->number ())); |
|
1867 retval = new tree_constant (tmp, l, c); |
1607
|
1868 retval->stash_original_text (tok_val->text_rep ()); |
|
1869 } |
|
1870 break; |
|
1871 |
|
1872 case TEXT: |
2883
|
1873 { |
|
1874 octave_value tmp (tok_val->text ()); |
|
1875 retval = new tree_constant (tmp, l, c); |
|
1876 } |
1607
|
1877 break; |
|
1878 |
|
1879 default: |
|
1880 panic_impossible (); |
|
1881 break; |
|
1882 } |
|
1883 |
|
1884 return retval; |
|
1885 } |
|
1886 |
666
|
1887 // Build a binary expression. |
|
1888 |
578
|
1889 static tree_expression * |
|
1890 make_binary_op (int op, tree_expression *op1, token *tok_val, |
|
1891 tree_expression *op2) |
|
1892 { |
2883
|
1893 octave_value::binary_op t = octave_value::unknown_binary_op; |
1623
|
1894 |
578
|
1895 switch (op) |
|
1896 { |
|
1897 case POW: |
3525
|
1898 t = octave_value::op_pow; |
578
|
1899 break; |
777
|
1900 |
578
|
1901 case EPOW: |
3525
|
1902 t = octave_value::op_el_pow; |
578
|
1903 break; |
777
|
1904 |
578
|
1905 case '+': |
3525
|
1906 t = octave_value::op_add; |
578
|
1907 break; |
777
|
1908 |
578
|
1909 case '-': |
3525
|
1910 t = octave_value::op_sub; |
578
|
1911 break; |
777
|
1912 |
578
|
1913 case '*': |
3525
|
1914 t = octave_value::op_mul; |
578
|
1915 break; |
777
|
1916 |
578
|
1917 case '/': |
3525
|
1918 t = octave_value::op_div; |
578
|
1919 break; |
777
|
1920 |
578
|
1921 case EMUL: |
3525
|
1922 t = octave_value::op_el_mul; |
578
|
1923 break; |
777
|
1924 |
578
|
1925 case EDIV: |
3525
|
1926 t = octave_value::op_el_div; |
578
|
1927 break; |
777
|
1928 |
578
|
1929 case LEFTDIV: |
3525
|
1930 t = octave_value::op_ldiv; |
578
|
1931 break; |
777
|
1932 |
578
|
1933 case ELEFTDIV: |
3525
|
1934 t = octave_value::op_el_ldiv; |
578
|
1935 break; |
777
|
1936 |
2899
|
1937 case LSHIFT: |
3525
|
1938 t = octave_value::op_lshift; |
2899
|
1939 break; |
|
1940 |
|
1941 case RSHIFT: |
3525
|
1942 t = octave_value::op_rshift; |
2899
|
1943 break; |
|
1944 |
578
|
1945 case EXPR_LT: |
3525
|
1946 t = octave_value::op_lt; |
578
|
1947 break; |
777
|
1948 |
578
|
1949 case EXPR_LE: |
3525
|
1950 t = octave_value::op_le; |
578
|
1951 break; |
777
|
1952 |
578
|
1953 case EXPR_EQ: |
3525
|
1954 t = octave_value::op_eq; |
578
|
1955 break; |
777
|
1956 |
578
|
1957 case EXPR_GE: |
3525
|
1958 t = octave_value::op_ge; |
578
|
1959 break; |
777
|
1960 |
578
|
1961 case EXPR_GT: |
3525
|
1962 t = octave_value::op_gt; |
578
|
1963 break; |
777
|
1964 |
578
|
1965 case EXPR_NE: |
3525
|
1966 t = octave_value::op_ne; |
578
|
1967 break; |
777
|
1968 |
578
|
1969 case EXPR_AND: |
3525
|
1970 t = octave_value::op_el_and; |
578
|
1971 break; |
777
|
1972 |
578
|
1973 case EXPR_OR: |
3525
|
1974 t = octave_value::op_el_or; |
4023
|
1975 if (Vwarn_precedence_change |
4024
|
1976 && op2->paren_count () == 0 && op2->is_binary_expression ()) |
4023
|
1977 { |
|
1978 tree_binary_expression *e |
|
1979 = dynamic_cast<tree_binary_expression *> (op2); |
|
1980 |
|
1981 if (e->op_type () == octave_value::op_el_and) |
|
1982 warning ("meaning may have changed due to change in precedence for & and | operators"); |
|
1983 } |
578
|
1984 break; |
777
|
1985 |
578
|
1986 default: |
|
1987 panic_impossible (); |
|
1988 break; |
|
1989 } |
|
1990 |
|
1991 int l = tok_val->line (); |
|
1992 int c = tok_val->column (); |
|
1993 |
2533
|
1994 tree_binary_expression *e |
|
1995 = new tree_binary_expression (op1, op2, l, c, t); |
1623
|
1996 |
2533
|
1997 return fold (e); |
578
|
1998 } |
|
1999 |
2375
|
2000 // Build a boolean expression. |
666
|
2001 |
578
|
2002 static tree_expression * |
2375
|
2003 make_boolean_op (int op, tree_expression *op1, token *tok_val, |
|
2004 tree_expression *op2) |
578
|
2005 { |
2375
|
2006 tree_boolean_expression::type t; |
|
2007 |
578
|
2008 switch (op) |
|
2009 { |
2375
|
2010 case EXPR_AND_AND: |
2805
|
2011 t = tree_boolean_expression::bool_and; |
578
|
2012 break; |
777
|
2013 |
2375
|
2014 case EXPR_OR_OR: |
2805
|
2015 t = tree_boolean_expression::bool_or; |
4023
|
2016 if (Vwarn_precedence_change |
4024
|
2017 && op2->paren_count () == 0 && op2->is_boolean_expression ()) |
4023
|
2018 { |
|
2019 tree_boolean_expression *e |
|
2020 = dynamic_cast<tree_boolean_expression *> (op2); |
|
2021 |
|
2022 if (e->op_type () == tree_boolean_expression::bool_and) |
|
2023 warning ("meaning may have changed due to change in precedence for && and || operators"); |
|
2024 } |
578
|
2025 break; |
777
|
2026 |
578
|
2027 default: |
|
2028 panic_impossible (); |
|
2029 break; |
|
2030 } |
|
2031 |
|
2032 int l = tok_val->line (); |
|
2033 int c = tok_val->column (); |
|
2034 |
2533
|
2035 tree_boolean_expression *e |
|
2036 = new tree_boolean_expression (op1, op2, l, c, t); |
2375
|
2037 |
2533
|
2038 return fold (e); |
578
|
2039 } |
|
2040 |
2375
|
2041 // Build a prefix expression. |
666
|
2042 |
578
|
2043 static tree_expression * |
2960
|
2044 make_prefix_op (int op, tree_expression *op1, token *tok_val) |
578
|
2045 { |
3203
|
2046 octave_value::unary_op t = octave_value::unknown_unary_op; |
2375
|
2047 |
578
|
2048 switch (op) |
|
2049 { |
2960
|
2050 case EXPR_NOT: |
3525
|
2051 t = octave_value::op_not; |
2960
|
2052 break; |
|
2053 |
|
2054 case '-': |
3525
|
2055 t = octave_value::op_uminus; |
2960
|
2056 break; |
|
2057 |
578
|
2058 case PLUS_PLUS: |
3525
|
2059 t = octave_value::op_incr; |
578
|
2060 break; |
777
|
2061 |
578
|
2062 case MINUS_MINUS: |
3525
|
2063 t = octave_value::op_decr; |
578
|
2064 break; |
777
|
2065 |
578
|
2066 default: |
|
2067 panic_impossible (); |
|
2068 break; |
|
2069 } |
|
2070 |
|
2071 int l = tok_val->line (); |
|
2072 int c = tok_val->column (); |
|
2073 |
3292
|
2074 tree_prefix_expression *e |
|
2075 = new tree_prefix_expression (op1, l, c, t); |
|
2076 |
|
2077 return fold (e); |
578
|
2078 } |
|
2079 |
2375
|
2080 // Build a postfix expression. |
666
|
2081 |
578
|
2082 static tree_expression * |
2960
|
2083 make_postfix_op (int op, tree_expression *op1, token *tok_val) |
578
|
2084 { |
3203
|
2085 octave_value::unary_op t = octave_value::unknown_unary_op; |
1623
|
2086 |
578
|
2087 switch (op) |
|
2088 { |
2960
|
2089 case QUOTE: |
3525
|
2090 t = octave_value::op_hermitian; |
2960
|
2091 break; |
|
2092 |
|
2093 case TRANSPOSE: |
3525
|
2094 t = octave_value::op_transpose; |
2960
|
2095 break; |
|
2096 |
2375
|
2097 case PLUS_PLUS: |
3525
|
2098 t = octave_value::op_incr; |
578
|
2099 break; |
777
|
2100 |
2375
|
2101 case MINUS_MINUS: |
3525
|
2102 t = octave_value::op_decr; |
578
|
2103 break; |
777
|
2104 |
578
|
2105 default: |
|
2106 panic_impossible (); |
|
2107 break; |
|
2108 } |
|
2109 |
|
2110 int l = tok_val->line (); |
|
2111 int c = tok_val->column (); |
|
2112 |
3292
|
2113 tree_postfix_expression *e |
|
2114 = new tree_postfix_expression (op1, l, c, t); |
|
2115 |
|
2116 return fold (e); |
1623
|
2117 } |
|
2118 |
|
2119 // Build an unwind-protect command. |
|
2120 |
|
2121 static tree_command * |
|
2122 make_unwind_command (token *unwind_tok, tree_statement_list *body, |
3665
|
2123 tree_statement_list *cleanup, token *end_tok, |
|
2124 octave_comment_list *lc, octave_comment_list *mc) |
1623
|
2125 { |
|
2126 tree_command *retval = 0; |
|
2127 |
2857
|
2128 if (end_token_ok (end_tok, token::unwind_protect_end)) |
1623
|
2129 { |
3665
|
2130 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2131 |
1623
|
2132 int l = unwind_tok->line (); |
|
2133 int c = unwind_tok->column (); |
|
2134 |
3665
|
2135 retval = new tree_unwind_protect_command (body, cleanup, |
|
2136 lc, mc, tc, l, c); |
1623
|
2137 } |
|
2138 |
|
2139 return retval; |
|
2140 } |
|
2141 |
|
2142 // Build a try-catch command. |
|
2143 |
|
2144 static tree_command * |
|
2145 make_try_command (token *try_tok, tree_statement_list *body, |
3665
|
2146 tree_statement_list *cleanup, token *end_tok, |
|
2147 octave_comment_list *lc, octave_comment_list *mc) |
1623
|
2148 { |
|
2149 tree_command *retval = 0; |
|
2150 |
2857
|
2151 if (end_token_ok (end_tok, token::try_catch_end)) |
1623
|
2152 { |
3665
|
2153 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2154 |
1623
|
2155 int l = try_tok->line (); |
|
2156 int c = try_tok->column (); |
|
2157 |
3665
|
2158 retval = new tree_try_catch_command (body, cleanup, |
|
2159 lc, mc, tc, l, c); |
1623
|
2160 } |
|
2161 |
|
2162 return retval; |
|
2163 } |
|
2164 |
|
2165 // Build a while command. |
|
2166 |
|
2167 static tree_command * |
|
2168 make_while_command (token *while_tok, tree_expression *expr, |
3665
|
2169 tree_statement_list *body, token *end_tok, |
|
2170 octave_comment_list *lc) |
1623
|
2171 { |
|
2172 tree_command *retval = 0; |
|
2173 |
|
2174 maybe_warn_assign_as_truth_value (expr); |
|
2175 |
2857
|
2176 if (end_token_ok (end_tok, token::while_end)) |
1623
|
2177 { |
3665
|
2178 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2179 |
1826
|
2180 lexer_flags.looping--; |
1623
|
2181 |
|
2182 int l = while_tok->line (); |
|
2183 int c = while_tok->column (); |
|
2184 |
3665
|
2185 retval = new tree_while_command (expr, body, lc, tc, l, c); |
1623
|
2186 } |
|
2187 |
|
2188 return retval; |
|
2189 } |
|
2190 |
3484
|
2191 // Build a do-until command. |
|
2192 |
|
2193 static tree_command * |
|
2194 make_do_until_command (token *do_tok, tree_statement_list *body, |
3665
|
2195 tree_expression *expr, octave_comment_list *lc) |
3484
|
2196 { |
|
2197 tree_command *retval = 0; |
|
2198 |
|
2199 maybe_warn_assign_as_truth_value (expr); |
|
2200 |
3665
|
2201 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2202 |
3484
|
2203 lexer_flags.looping--; |
|
2204 |
|
2205 int l = do_tok->line (); |
|
2206 int c = do_tok->column (); |
|
2207 |
3665
|
2208 retval = new tree_do_until_command (expr, body, lc, tc, l, c); |
3484
|
2209 |
|
2210 return retval; |
|
2211 } |
|
2212 |
1623
|
2213 // Build a for command. |
|
2214 |
|
2215 static tree_command * |
2970
|
2216 make_for_command (token *for_tok, tree_argument_list *lhs, |
1623
|
2217 tree_expression *expr, tree_statement_list *body, |
3665
|
2218 token *end_tok, octave_comment_list *lc) |
1623
|
2219 { |
|
2220 tree_command *retval = 0; |
|
2221 |
2857
|
2222 if (end_token_ok (end_tok, token::for_end)) |
1623
|
2223 { |
3665
|
2224 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2225 |
1826
|
2226 lexer_flags.looping--; |
1623
|
2227 |
|
2228 int l = for_tok->line (); |
|
2229 int c = for_tok->column (); |
|
2230 |
2970
|
2231 if (lhs->length () == 1) |
|
2232 { |
|
2233 tree_expression *tmp = lhs->remove_front (); |
|
2234 |
3665
|
2235 retval = new tree_simple_for_command (tmp, expr, body, |
|
2236 lc, tc, l, c); |
2970
|
2237 |
|
2238 delete lhs; |
|
2239 } |
|
2240 else |
3665
|
2241 retval = new tree_complex_for_command (lhs, expr, body, |
|
2242 lc, tc, l, c); |
1623
|
2243 } |
|
2244 |
|
2245 return retval; |
|
2246 } |
|
2247 |
|
2248 // Build a break command. |
|
2249 |
|
2250 static tree_command * |
|
2251 make_break_command (token *break_tok) |
|
2252 { |
|
2253 tree_command *retval = 0; |
|
2254 |
2620
|
2255 int l = break_tok->line (); |
|
2256 int c = break_tok->column (); |
|
2257 |
3489
|
2258 if (lexer_flags.looping || lexer_flags.defining_func |
3877
|
2259 || reading_script_file || evaluating_function_body |
|
2260 || evaluating_looping_command) |
2620
|
2261 retval = new tree_break_command (l, c); |
1623
|
2262 else |
2620
|
2263 retval = new tree_no_op_command ("break", l, c); |
1623
|
2264 |
|
2265 return retval; |
|
2266 } |
|
2267 |
|
2268 // Build a continue command. |
|
2269 |
|
2270 static tree_command * |
|
2271 make_continue_command (token *continue_tok) |
|
2272 { |
|
2273 tree_command *retval = 0; |
|
2274 |
2620
|
2275 int l = continue_tok->line (); |
|
2276 int c = continue_tok->column (); |
|
2277 |
3877
|
2278 if (lexer_flags.looping || evaluating_looping_command) |
2620
|
2279 retval = new tree_continue_command (l, c); |
1623
|
2280 else |
2620
|
2281 retval = new tree_no_op_command ("continue", l, c); |
1623
|
2282 |
|
2283 return retval; |
|
2284 } |
|
2285 |
|
2286 // Build a return command. |
|
2287 |
|
2288 static tree_command * |
|
2289 make_return_command (token *return_tok) |
|
2290 { |
|
2291 tree_command *retval = 0; |
|
2292 |
2620
|
2293 int l = return_tok->line (); |
|
2294 int c = return_tok->column (); |
|
2295 |
3489
|
2296 if (lexer_flags.defining_func || reading_script_file |
|
2297 || evaluating_function_body) |
2620
|
2298 retval = new tree_return_command (l, c); |
1623
|
2299 else |
2620
|
2300 retval = new tree_no_op_command ("return", l, c); |
1623
|
2301 |
|
2302 return retval; |
|
2303 } |
|
2304 |
|
2305 // Start an if command. |
|
2306 |
|
2307 static tree_if_command_list * |
|
2308 start_if_command (tree_expression *expr, tree_statement_list *list) |
|
2309 { |
|
2310 maybe_warn_assign_as_truth_value (expr); |
|
2311 |
|
2312 tree_if_clause *t = new tree_if_clause (expr, list); |
|
2313 |
|
2314 return new tree_if_command_list (t); |
|
2315 } |
|
2316 |
|
2317 // Finish an if command. |
|
2318 |
|
2319 static tree_if_command * |
|
2320 finish_if_command (token *if_tok, tree_if_command_list *list, |
3665
|
2321 token *end_tok, octave_comment_list *lc) |
1623
|
2322 { |
|
2323 tree_if_command *retval = 0; |
|
2324 |
2857
|
2325 if (end_token_ok (end_tok, token::if_end)) |
1623
|
2326 { |
3665
|
2327 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2328 |
1623
|
2329 int l = if_tok->line (); |
|
2330 int c = if_tok->column (); |
|
2331 |
3665
|
2332 retval = new tree_if_command (list, lc, tc, l, c); |
1623
|
2333 } |
|
2334 |
|
2335 return retval; |
|
2336 } |
|
2337 |
|
2338 // Build an elseif clause. |
|
2339 |
|
2340 static tree_if_clause * |
3665
|
2341 make_elseif_clause (tree_expression *expr, tree_statement_list *list, |
|
2342 octave_comment_list *lc) |
1623
|
2343 { |
|
2344 maybe_warn_assign_as_truth_value (expr); |
|
2345 |
3665
|
2346 return new tree_if_clause (expr, list, lc); |
1623
|
2347 } |
|
2348 |
2764
|
2349 // Finish a switch command. |
|
2350 |
|
2351 static tree_switch_command * |
|
2352 finish_switch_command (token *switch_tok, tree_expression *expr, |
3665
|
2353 tree_switch_case_list *list, token *end_tok, |
|
2354 octave_comment_list *lc) |
2764
|
2355 { |
|
2356 tree_switch_command *retval = 0; |
|
2357 |
2857
|
2358 if (end_token_ok (end_tok, token::switch_end)) |
2764
|
2359 { |
3665
|
2360 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2361 |
2764
|
2362 int l = switch_tok->line (); |
|
2363 int c = switch_tok->column (); |
|
2364 |
3665
|
2365 retval = new tree_switch_command (expr, list, lc, tc, l, c); |
2764
|
2366 } |
|
2367 |
|
2368 return retval; |
|
2369 } |
|
2370 |
|
2371 // Build a switch case. |
|
2372 |
|
2373 static tree_switch_case * |
3665
|
2374 make_switch_case (tree_expression *expr, tree_statement_list *list, |
|
2375 octave_comment_list *lc) |
2764
|
2376 { |
|
2377 maybe_warn_variable_switch_label (expr); |
|
2378 |
3665
|
2379 return new tree_switch_case (expr, list, lc); |
2764
|
2380 } |
|
2381 |
1623
|
2382 // Build an assignment to a variable. |
|
2383 |
|
2384 static tree_expression * |
2970
|
2385 make_assign_op (int op, tree_argument_list *lhs, token *eq_tok, |
|
2386 tree_expression *rhs) |
1623
|
2387 { |
2970
|
2388 tree_expression *retval = 0; |
|
2389 |
2883
|
2390 octave_value::assign_op t = octave_value::unknown_assign_op; |
|
2391 |
|
2392 switch (op) |
|
2393 { |
|
2394 case '=': |
3525
|
2395 t = octave_value::op_asn_eq; |
2883
|
2396 break; |
|
2397 |
|
2398 case ADD_EQ: |
3525
|
2399 t = octave_value::op_add_eq; |
2883
|
2400 break; |
|
2401 |
|
2402 case SUB_EQ: |
3525
|
2403 t = octave_value::op_sub_eq; |
2883
|
2404 break; |
|
2405 |
|
2406 case MUL_EQ: |
3525
|
2407 t = octave_value::op_mul_eq; |
2883
|
2408 break; |
|
2409 |
|
2410 case DIV_EQ: |
3525
|
2411 t = octave_value::op_div_eq; |
2883
|
2412 break; |
|
2413 |
3204
|
2414 case LEFTDIV_EQ: |
3525
|
2415 t = octave_value::op_ldiv_eq; |
3204
|
2416 break; |
|
2417 |
4018
|
2418 case POW_EQ: |
|
2419 t = octave_value::op_pow_eq; |
|
2420 break; |
|
2421 |
2899
|
2422 case LSHIFT_EQ: |
3525
|
2423 t = octave_value::op_lshift_eq; |
2899
|
2424 break; |
|
2425 |
|
2426 case RSHIFT_EQ: |
3525
|
2427 t = octave_value::op_rshift_eq; |
2899
|
2428 break; |
|
2429 |
2883
|
2430 case EMUL_EQ: |
3525
|
2431 t = octave_value::op_el_mul_eq; |
2883
|
2432 break; |
|
2433 |
|
2434 case EDIV_EQ: |
3525
|
2435 t = octave_value::op_el_div_eq; |
2883
|
2436 break; |
|
2437 |
3204
|
2438 case ELEFTDIV_EQ: |
3525
|
2439 t = octave_value::op_el_ldiv_eq; |
3204
|
2440 break; |
|
2441 |
4018
|
2442 case EPOW_EQ: |
|
2443 t = octave_value::op_el_pow_eq; |
|
2444 break; |
|
2445 |
2883
|
2446 case AND_EQ: |
3525
|
2447 t = octave_value::op_el_and_eq; |
2883
|
2448 break; |
|
2449 |
|
2450 case OR_EQ: |
3525
|
2451 t = octave_value::op_el_or_eq; |
2883
|
2452 break; |
|
2453 |
|
2454 default: |
|
2455 panic_impossible (); |
|
2456 break; |
|
2457 } |
|
2458 |
1623
|
2459 int l = eq_tok->line (); |
|
2460 int c = eq_tok->column (); |
|
2461 |
2970
|
2462 if (lhs->length () == 1) |
666
|
2463 { |
2970
|
2464 tree_expression *tmp = lhs->remove_front (); |
|
2465 |
|
2466 retval = new tree_simple_assignment (tmp, rhs, false, l, c, t); |
|
2467 |
|
2468 delete lhs; |
666
|
2469 } |
|
2470 else |
3208
|
2471 return new tree_multi_assignment (lhs, rhs, false, l, c, t); |
666
|
2472 |
|
2473 return retval; |
|
2474 } |
751
|
2475 |
1623
|
2476 // Begin defining a function. |
|
2477 |
2891
|
2478 static octave_user_function * |
|
2479 start_function (tree_parameter_list *param_list, tree_statement_list *body) |
1623
|
2480 { |
|
2481 body->mark_as_function_body (); |
|
2482 |
2891
|
2483 // We'll fill in the return list later. |
|
2484 |
|
2485 octave_user_function *fcn |
|
2486 = new octave_user_function (param_list, 0, body, curr_sym_tab); |
1623
|
2487 |
3665
|
2488 if (fcn) |
|
2489 { |
|
2490 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2491 |
|
2492 fcn->stash_trailing_comment (tc); |
|
2493 } |
|
2494 |
1623
|
2495 return fcn; |
|
2496 } |
|
2497 |
|
2498 // Do most of the work for defining a function. |
|
2499 |
2891
|
2500 static octave_user_function * |
|
2501 frob_function (tree_identifier *id, octave_user_function *fcn) |
1623
|
2502 { |
3523
|
2503 std::string id_name = id->name (); |
1623
|
2504 |
|
2505 // If input is coming from a file, issue a warning if the name of |
|
2506 // the file does not match the name of the function stated in the |
|
2507 // file. Matlab doesn't provide a diagnostic (it ignores the stated |
|
2508 // name). |
|
2509 |
|
2510 fcn->stash_function_name (id_name); |
|
2511 |
|
2512 if (reading_fcn_file) |
|
2513 { |
1755
|
2514 if (curr_fcn_file_name != id_name) |
1623
|
2515 { |
2166
|
2516 if (Vwarn_function_name_clash) |
1623
|
2517 warning ("function name `%s' does not agree with function\ |
1755
|
2518 file name `%s'", id_name.c_str (), curr_fcn_file_full_name.c_str ()); |
1623
|
2519 |
4009
|
2520 fbi_sym_tab->rename (id_name, curr_fcn_file_name); |
1623
|
2521 |
|
2522 if (error_state) |
|
2523 return 0; |
|
2524 |
|
2525 id_name = id->name (); |
|
2526 } |
|
2527 |
3712
|
2528 octave_time now; |
3162
|
2529 |
1623
|
2530 fcn->stash_function_name (id_name); |
|
2531 fcn->stash_fcn_file_name (); |
3162
|
2532 fcn->stash_fcn_file_time (now); |
1623
|
2533 fcn->mark_as_system_fcn_file (); |
3162
|
2534 |
|
2535 if (Vwarn_future_time_stamp) |
|
2536 { |
3523
|
2537 std::string nm = fcn->fcn_file_name (); |
3162
|
2538 |
|
2539 file_stat fs (nm); |
|
2540 |
|
2541 if (fs && fs.is_newer (now)) |
|
2542 warning ("time stamp for `%s' is in the future", nm.c_str ()); |
|
2543 } |
1623
|
2544 } |
|
2545 else if (! (input_from_tmp_history_file || input_from_startup_file) |
|
2546 && reading_script_file |
1755
|
2547 && curr_fcn_file_name == id_name) |
1623
|
2548 { |
|
2549 warning ("function `%s' defined within script file `%s'", |
1755
|
2550 id_name.c_str (), curr_fcn_file_full_name.c_str ()); |
1623
|
2551 } |
|
2552 |
|
2553 top_level_sym_tab->clear (id_name); |
|
2554 |
4009
|
2555 symbol_record *sr = fbi_sym_tab->lookup (id_name); |
2791
|
2556 |
|
2557 if (sr) |
|
2558 fcn->stash_symtab_ptr (sr); |
|
2559 else |
|
2560 panic_impossible (); |
|
2561 |
3016
|
2562 id->define (fcn, symbol_record::USER_FUNCTION); |
1755
|
2563 |
1623
|
2564 id->document (help_buf); |
|
2565 |
3844
|
2566 help_buf.resize (0); |
|
2567 |
1623
|
2568 return fcn; |
|
2569 } |
|
2570 |
|
2571 // Finish defining a function. |
|
2572 |
2891
|
2573 static octave_user_function * |
3665
|
2574 finish_function (tree_identifier *id, octave_user_function *fcn, |
|
2575 octave_comment_list *lc) |
1623
|
2576 { |
2883
|
2577 tree_parameter_list *tpl = new tree_parameter_list (id); |
1623
|
2578 |
|
2579 tpl->mark_as_formal_parameters (); |
|
2580 |
3665
|
2581 fcn->stash_leading_comment (lc); |
|
2582 |
1623
|
2583 return fcn->define_ret_list (tpl); |
|
2584 } |
|
2585 |
|
2586 // Finish defining a function a different way. |
|
2587 |
2891
|
2588 static octave_user_function * |
3665
|
2589 finish_function (tree_parameter_list *ret_list, |
|
2590 octave_user_function *fcn, octave_comment_list *lc) |
1623
|
2591 { |
|
2592 ret_list->mark_as_formal_parameters (); |
|
2593 |
3665
|
2594 fcn->stash_leading_comment (lc); |
|
2595 |
1623
|
2596 return fcn->define_ret_list (ret_list); |
|
2597 } |
|
2598 |
2883
|
2599 static void |
|
2600 recover_from_parsing_function (void) |
|
2601 { |
3903
|
2602 if (! symtab_context) |
|
2603 panic_impossible (); |
|
2604 |
|
2605 curr_sym_tab = symtab_context; |
|
2606 symtab_context = 0; |
2883
|
2607 |
|
2608 lexer_flags.defining_func = false; |
|
2609 lexer_flags.beginning_of_function = false; |
|
2610 lexer_flags.parsed_function_name = false; |
|
2611 lexer_flags.looking_at_return_list = false; |
|
2612 lexer_flags.looking_at_parameter_list = false; |
|
2613 } |
|
2614 |
2846
|
2615 // Make an index expression. |
|
2616 |
751
|
2617 static tree_index_expression * |
3929
|
2618 make_index_expression (tree_expression *expr, tree_argument_list *args, |
3933
|
2619 char type) |
751
|
2620 { |
|
2621 tree_index_expression *retval = 0; |
|
2622 |
2970
|
2623 int l = expr->line (); |
|
2624 int c = expr->column (); |
|
2625 |
|
2626 expr->mark_postfix_indexed (); |
|
2627 |
3933
|
2628 if (expr->is_index_expression ()) |
|
2629 { |
|
2630 tree_index_expression *tmp = static_cast<tree_index_expression *> (expr); |
|
2631 |
|
2632 tmp->append (args, type); |
|
2633 |
|
2634 retval = tmp; |
|
2635 } |
|
2636 else |
|
2637 retval = new tree_index_expression (expr, args, l, c, type); |
2970
|
2638 |
|
2639 return retval; |
|
2640 } |
|
2641 |
|
2642 // Make an indirect reference expression. |
|
2643 |
3930
|
2644 static tree_index_expression * |
3523
|
2645 make_indirect_ref (tree_expression *expr, const std::string& elt) |
2970
|
2646 { |
3930
|
2647 tree_index_expression *retval = 0; |
2970
|
2648 |
|
2649 int l = expr->line (); |
|
2650 int c = expr->column (); |
|
2651 |
3933
|
2652 if (expr->is_index_expression ()) |
|
2653 { |
|
2654 tree_index_expression *tmp = static_cast<tree_index_expression *> (expr); |
|
2655 |
|
2656 tmp->append (elt); |
|
2657 |
|
2658 retval = tmp; |
|
2659 } |
|
2660 else |
|
2661 retval = new tree_index_expression (expr, elt, l, c); |
2970
|
2662 |
|
2663 lexer_flags.looking_at_indirect_ref = false; |
751
|
2664 |
|
2665 return retval; |
|
2666 } |
1511
|
2667 |
2846
|
2668 // Make a declaration command. |
|
2669 |
|
2670 static tree_decl_command * |
|
2671 make_decl_command (int tok, token *tok_val, tree_decl_init_list *lst) |
|
2672 { |
|
2673 tree_decl_command *retval = 0; |
|
2674 |
|
2675 int l = tok_val->line (); |
|
2676 int c = tok_val->column (); |
|
2677 |
|
2678 switch (tok) |
|
2679 { |
|
2680 case GLOBAL: |
|
2681 retval = new tree_global_command (lst, l, c); |
|
2682 break; |
|
2683 |
|
2684 case STATIC: |
|
2685 if (lexer_flags.defining_func) |
|
2686 retval = new tree_static_command (lst, l, c); |
|
2687 else |
|
2688 { |
|
2689 if (reading_script_file) |
|
2690 warning ("ignoring static declaration near line %d of file `%s'", |
|
2691 l, curr_fcn_file_full_name.c_str ()); |
|
2692 else |
|
2693 warning ("ignoring static declaration near line %d", l); |
|
2694 } |
|
2695 break; |
|
2696 |
|
2697 default: |
|
2698 panic_impossible (); |
|
2699 break; |
|
2700 } |
|
2701 |
|
2702 return retval; |
|
2703 } |
|
2704 |
1623
|
2705 // Finish building a matrix list. |
|
2706 |
|
2707 static tree_expression * |
1829
|
2708 finish_matrix (tree_matrix *m) |
1623
|
2709 { |
3110
|
2710 tree_expression *retval = m; |
|
2711 |
|
2712 unwind_protect::begin_frame ("finish_matrix"); |
|
2713 |
|
2714 unwind_protect_int (error_state); |
|
2715 |
3815
|
2716 unwind_protect_bool (discard_error_messages); |
|
2717 discard_error_messages = true; |
1623
|
2718 |
2533
|
2719 if (m->all_elements_are_constant ()) |
1829
|
2720 { |
2970
|
2721 octave_value tmp = m->rvalue (); |
1623
|
2722 |
3489
|
2723 if (! (error_state || warning_state)) |
2533
|
2724 { |
|
2725 tree_constant *tc_retval = new tree_constant (tmp); |
|
2726 |
3523
|
2727 std::ostrstream buf; |
2533
|
2728 |
|
2729 tree_print_code tpc (buf); |
|
2730 |
|
2731 m->accept (tpc); |
|
2732 |
3538
|
2733 buf << std::ends; |
1623
|
2734 |
2533
|
2735 char *s = buf.str (); |
|
2736 |
|
2737 tc_retval->stash_original_text (s); |
|
2738 |
|
2739 delete [] s; |
|
2740 |
|
2741 delete m; |
|
2742 |
|
2743 retval = tc_retval; |
|
2744 } |
1623
|
2745 } |
3110
|
2746 |
|
2747 unwind_protect::run_frame ("finish_matrix"); |
1623
|
2748 |
|
2749 return retval; |
|
2750 } |
|
2751 |
3351
|
2752 // Finish building a cell list. |
|
2753 |
|
2754 static tree_expression * |
|
2755 finish_cell (tree_cell *c) |
|
2756 { |
|
2757 // For now, this doesn't do anything. |
|
2758 |
|
2759 return c; |
|
2760 } |
|
2761 |
1511
|
2762 static void |
|
2763 maybe_warn_missing_semi (tree_statement_list *t) |
|
2764 { |
2166
|
2765 if (lexer_flags.defining_func && Vwarn_missing_semicolon) |
1511
|
2766 { |
|
2767 tree_statement *tmp = t->rear(); |
1607
|
2768 |
1511
|
2769 if (tmp->is_expression ()) |
1607
|
2770 warning ("missing semicolon near line %d, column %d in file `%s'", |
1755
|
2771 tmp->line (), tmp->column (), |
|
2772 curr_fcn_file_full_name.c_str ()); |
1511
|
2773 } |
|
2774 } |
1994
|
2775 |
2525
|
2776 static void |
|
2777 set_stmt_print_flag (tree_statement_list *list, char sep, |
|
2778 bool warn_missing_semi) |
|
2779 { |
|
2780 switch (sep) |
|
2781 { |
|
2782 case ';': |
|
2783 { |
|
2784 tree_statement *tmp = list->rear (); |
|
2785 tmp->set_print_flag (0); |
|
2786 } |
|
2787 break; |
|
2788 |
|
2789 case 0: |
|
2790 case ',': |
|
2791 case '\n': |
|
2792 if (warn_missing_semi) |
|
2793 maybe_warn_missing_semi (list); |
|
2794 break; |
|
2795 |
|
2796 default: |
|
2797 warning ("unrecognized separator type!"); |
|
2798 break; |
|
2799 } |
|
2800 } |
|
2801 |
3021
|
2802 void |
|
2803 parse_and_execute (FILE *f) |
|
2804 { |
|
2805 unwind_protect::begin_frame ("parse_and_execute"); |
3880
|
2806 |
3021
|
2807 YY_BUFFER_STATE old_buf = current_buffer (); |
|
2808 YY_BUFFER_STATE new_buf = create_buffer (f); |
|
2809 |
|
2810 unwind_protect::add (restore_input_buffer, old_buf); |
|
2811 unwind_protect::add (delete_input_buffer, new_buf); |
|
2812 |
|
2813 switch_to_buffer (new_buf); |
|
2814 |
|
2815 unwind_protect_bool (line_editing); |
|
2816 unwind_protect_bool (input_from_command_line_file); |
3880
|
2817 unwind_protect_bool (get_input_from_eval_string); |
3883
|
2818 unwind_protect_bool (parser_end_of_input); |
3021
|
2819 |
|
2820 line_editing = false; |
|
2821 input_from_command_line_file = false; |
3880
|
2822 get_input_from_eval_string = false; |
3883
|
2823 parser_end_of_input = false; |
3021
|
2824 |
|
2825 unwind_protect_ptr (curr_sym_tab); |
|
2826 |
|
2827 int retval; |
|
2828 do |
|
2829 { |
|
2830 reset_parser (); |
|
2831 |
|
2832 retval = yyparse (); |
|
2833 |
3883
|
2834 if (retval == 0) |
|
2835 { |
|
2836 if (global_command) |
3021
|
2837 { |
3883
|
2838 global_command->eval (); |
|
2839 |
|
2840 delete global_command; |
|
2841 |
|
2842 global_command = 0; |
|
2843 |
|
2844 bool quit = (tree_return_command::returning |
|
2845 || tree_break_command::breaking); |
|
2846 |
|
2847 if (tree_return_command::returning) |
|
2848 tree_return_command::returning = 0; |
|
2849 |
|
2850 if (tree_break_command::breaking) |
|
2851 tree_break_command::breaking--; |
|
2852 |
|
2853 if (error_state) |
|
2854 { |
|
2855 error ("near line %d of file `%s'", input_line_number, |
|
2856 curr_fcn_file_full_name.c_str ()); |
|
2857 |
|
2858 break; |
|
2859 } |
|
2860 |
|
2861 if (quit) |
|
2862 break; |
3021
|
2863 } |
3883
|
2864 else if (parser_end_of_input) |
3021
|
2865 break; |
3883
|
2866 } |
3021
|
2867 } |
|
2868 while (retval == 0); |
|
2869 |
|
2870 unwind_protect::run_frame ("parse_and_execute"); |
|
2871 } |
|
2872 |
|
2873 static void |
|
2874 safe_fclose (void *f) |
|
2875 { |
3765
|
2876 // XXX FIXME XXX -- comments at the end of an input file are |
|
2877 // discarded (otherwise, they would be appended to the next |
|
2878 // statement, possibly from the command line or another file, which |
|
2879 // can be quite confusing). |
|
2880 |
|
2881 octave_comment_buffer::get_comment (); |
|
2882 |
3021
|
2883 if (f) |
|
2884 fclose (static_cast<FILE *> (f)); |
|
2885 } |
|
2886 |
|
2887 void |
3523
|
2888 parse_and_execute (const std::string& s, bool verbose, const char *warn_for) |
3021
|
2889 { |
|
2890 unwind_protect::begin_frame ("parse_and_execute_2"); |
|
2891 |
|
2892 unwind_protect_bool (reading_script_file); |
|
2893 unwind_protect_str (curr_fcn_file_full_name); |
|
2894 |
|
2895 reading_script_file = true; |
|
2896 curr_fcn_file_full_name = s; |
|
2897 |
|
2898 FILE *f = get_input_from_file (s, 0); |
|
2899 |
|
2900 if (f) |
|
2901 { |
|
2902 unwind_protect::add (safe_fclose, f); |
|
2903 |
|
2904 unwind_protect_int (input_line_number); |
|
2905 unwind_protect_int (current_input_column); |
|
2906 |
|
2907 input_line_number = 0; |
|
2908 current_input_column = 1; |
|
2909 |
|
2910 if (verbose) |
|
2911 { |
3531
|
2912 std::cout << "reading commands from " << s << " ... "; |
3021
|
2913 reading_startup_message_printed = true; |
3531
|
2914 std::cout.flush (); |
3021
|
2915 } |
|
2916 |
|
2917 parse_and_execute (f); |
|
2918 |
|
2919 if (verbose) |
3538
|
2920 std::cout << "done." << std::endl; |
3021
|
2921 } |
|
2922 else if (warn_for) |
|
2923 error ("%s: unable to open file `%s'", warn_for, s.c_str ()); |
|
2924 |
|
2925 unwind_protect::run_frame ("parse_and_execute_2"); |
|
2926 } |
|
2927 |
|
2928 static bool |
3523
|
2929 looks_like_octave_copyright (const std::string& s) |
3021
|
2930 { |
|
2931 bool retval = false; |
|
2932 |
3523
|
2933 std::string t = s.substr (0, 14); |
3427
|
2934 |
|
2935 if (t == "Copyright (C) ") |
3021
|
2936 { |
|
2937 size_t pos = s.find ('\n'); |
|
2938 |
|
2939 if (pos != NPOS) |
|
2940 { |
|
2941 pos = s.find ('\n', pos + 1); |
|
2942 |
|
2943 if (pos != NPOS) |
|
2944 { |
|
2945 pos++; |
|
2946 |
3427
|
2947 t = s.substr (pos, 28); |
|
2948 |
|
2949 if (t == "This file is part of Octave." |
|
2950 || t == "This program is free softwar") |
3021
|
2951 retval = true; |
|
2952 } |
|
2953 } |
|
2954 } |
|
2955 |
|
2956 return retval; |
|
2957 } |
|
2958 |
|
2959 // Eat whitespace and comments from FFILE, returning the text of the |
|
2960 // comments read if it doesn't look like a copyright notice. If |
|
2961 // IN_PARTS, consider each block of comments separately; otherwise, |
|
2962 // grab them all at once. If UPDATE_POS is TRUE, line and column |
3665
|
2963 // number information is updated. If SAVE_COPYRIGHT is TRUE, then |
|
2964 // comments that are recognized as a copyright notice are saved in the |
|
2965 // comment buffer. |
3021
|
2966 |
|
2967 // XXX FIXME XXX -- grab_help_text() in lex.l duplicates some of this |
|
2968 // code! |
|
2969 |
3536
|
2970 static std::string |
3665
|
2971 gobble_leading_white_space (FILE *ffile, bool in_parts, |
|
2972 bool update_pos, bool save_copyright) |
3021
|
2973 { |
3523
|
2974 std::string help_txt; |
3021
|
2975 |
3802
|
2976 // TRUE means we have already seen the first block of comments. |
3021
|
2977 bool first_comments_seen = false; |
3802
|
2978 |
|
2979 // TRUE means we are at the beginning of a comment block. |
3021
|
2980 bool begin_comment = false; |
3802
|
2981 |
|
2982 // TRUE means we have already cached the help text. |
3021
|
2983 bool have_help_text = false; |
3802
|
2984 |
|
2985 // TRUE means we are currently reading a comment block. |
3021
|
2986 bool in_comment = false; |
3802
|
2987 |
|
2988 // TRUE means we should discard the first space from the input |
|
2989 // (used to strip leading spaces from the help text). |
3427
|
2990 bool discard_space = true; |
3802
|
2991 |
3021
|
2992 int c; |
|
2993 |
|
2994 while ((c = getc (ffile)) != EOF) |
|
2995 { |
|
2996 if (update_pos) |
|
2997 current_input_column++; |
|
2998 |
|
2999 if (begin_comment) |
|
3000 { |
|
3001 if (c == '%' || c == '#') |
|
3002 continue; |
3427
|
3003 else if (discard_space && c == ' ') |
|
3004 { |
|
3005 discard_space = false; |
|
3006 continue; |
|
3007 } |
3021
|
3008 else |
|
3009 begin_comment = false; |
|
3010 } |
|
3011 |
|
3012 if (in_comment) |
|
3013 { |
|
3014 if (! have_help_text) |
|
3015 { |
|
3016 first_comments_seen = true; |
|
3017 help_txt += (char) c; |
|
3018 } |
|
3019 |
|
3020 if (c == '\n') |
|
3021 { |
|
3022 if (update_pos) |
|
3023 { |
|
3024 input_line_number++; |
|
3025 current_input_column = 0; |
|
3026 } |
3427
|
3027 |
3021
|
3028 in_comment = false; |
3427
|
3029 discard_space = true; |
3021
|
3030 |
|
3031 if (in_parts) |
|
3032 { |
|
3033 if ((c = getc (ffile)) != EOF) |
|
3034 { |
|
3035 if (update_pos) |
|
3036 current_input_column--; |
|
3037 ungetc (c, ffile); |
|
3038 if (c == '\n') |
|
3039 break; |
|
3040 } |
|
3041 else |
|
3042 break; |
|
3043 } |
|
3044 } |
|
3045 } |
|
3046 else |
|
3047 { |
|
3048 switch (c) |
|
3049 { |
|
3050 case ' ': |
|
3051 case '\t': |
|
3052 if (first_comments_seen) |
|
3053 have_help_text = true; |
|
3054 break; |
|
3055 |
3802
|
3056 case '%': |
|
3057 case '#': |
|
3058 begin_comment = true; |
|
3059 in_comment = true; |
|
3060 break; |
|
3061 |
3021
|
3062 case '\n': |
|
3063 if (first_comments_seen) |
|
3064 have_help_text = true; |
|
3065 if (update_pos) |
|
3066 { |
|
3067 input_line_number++; |
|
3068 current_input_column = 0; |
|
3069 } |
|
3070 continue; |
|
3071 |
3802
|
3072 case '\r': |
|
3073 c = getc (ffile); |
|
3074 if (update_pos) |
|
3075 current_input_column++; |
|
3076 if (c == EOF) |
|
3077 goto done; |
|
3078 else if (c == '\n') |
|
3079 { |
|
3080 if (first_comments_seen) |
|
3081 have_help_text = true; |
|
3082 if (update_pos) |
|
3083 { |
|
3084 input_line_number++; |
|
3085 current_input_column = 0; |
|
3086 } |
|
3087 continue; |
|
3088 } |
|
3089 |
|
3090 // Fall through... |
3021
|
3091 |
|
3092 default: |
|
3093 if (update_pos) |
|
3094 current_input_column--; |
|
3095 ungetc (c, ffile); |
|
3096 goto done; |
|
3097 } |
|
3098 } |
|
3099 } |
|
3100 |
|
3101 done: |
|
3102 |
|
3103 if (! help_txt.empty ()) |
|
3104 { |
3665
|
3105 if (looks_like_octave_copyright (help_txt)) |
|
3106 { |
|
3107 if (save_copyright) |
|
3108 octave_comment_buffer::append (help_txt); |
|
3109 |
|
3110 help_txt.resize (0); |
|
3111 } |
3021
|
3112 |
|
3113 if (in_parts && help_txt.empty ()) |
3665
|
3114 help_txt = gobble_leading_white_space (ffile, in_parts, |
|
3115 update_pos, false); |
3021
|
3116 } |
|
3117 |
|
3118 return help_txt; |
|
3119 } |
|
3120 |
3536
|
3121 std::string |
3523
|
3122 get_help_from_file (const std::string& path) |
3021
|
3123 { |
3523
|
3124 std::string retval; |
3021
|
3125 |
|
3126 if (! path.empty ()) |
|
3127 { |
|
3128 FILE *fptr = fopen (path.c_str (), "r"); |
|
3129 |
|
3130 if (fptr) |
|
3131 { |
|
3132 unwind_protect::add (safe_fclose, (void *) fptr); |
|
3133 |
3665
|
3134 retval = gobble_leading_white_space (fptr, true, true, false); |
3021
|
3135 |
|
3136 unwind_protect::run (); |
|
3137 } |
|
3138 } |
|
3139 |
|
3140 return retval; |
|
3141 } |
|
3142 |
|
3143 static int |
|
3144 is_function_file (FILE *ffile) |
|
3145 { |
|
3146 int status = 0; |
|
3147 |
|
3148 long pos = ftell (ffile); |
|
3149 |
3665
|
3150 gobble_leading_white_space (ffile, false, false, false); |
3021
|
3151 |
|
3152 char buf [10]; |
|
3153 fgets (buf, 10, ffile); |
|
3154 int len = strlen (buf); |
|
3155 if (len > 8 && strncmp (buf, "function", 8) == 0 |
|
3156 && ! (isalnum (buf[8]) || buf[8] == '_')) |
|
3157 status = 1; |
|
3158 |
|
3159 fseek (ffile, pos, SEEK_SET); |
|
3160 |
|
3161 return status; |
|
3162 } |
|
3163 |
|
3164 static void |
|
3165 restore_command_history (void *) |
|
3166 { |
|
3167 command_history::ignore_entries (! Vsaving_history); |
|
3168 } |
|
3169 |
|
3170 static void |
|
3171 restore_input_stream (void *f) |
|
3172 { |
|
3173 command_editor::set_input_stream (static_cast<FILE *> (f)); |
|
3174 } |
|
3175 |
3180
|
3176 static void |
|
3177 clear_current_script_file_name (void *) |
|
3178 { |
|
3179 bind_builtin_variable ("current_script_file_name", octave_value ()); |
|
3180 } |
|
3181 |
3021
|
3182 static bool |
3523
|
3183 parse_fcn_file (const std::string& ff, bool exec_script, bool force_script = false) |
3021
|
3184 { |
|
3185 unwind_protect::begin_frame ("parse_fcn_file"); |
|
3186 |
|
3187 int script_file_executed = false; |
|
3188 |
|
3189 // Open function file and parse. |
|
3190 |
|
3191 bool old_reading_fcn_file_state = reading_fcn_file; |
|
3192 |
|
3193 FILE *in_stream = command_editor::get_input_stream (); |
|
3194 |
|
3195 unwind_protect::add (restore_input_stream, in_stream); |
|
3196 |
|
3197 unwind_protect_ptr (ff_instream); |
|
3198 |
|
3199 unwind_protect_int (input_line_number); |
|
3200 unwind_protect_int (current_input_column); |
|
3201 unwind_protect_bool (reading_fcn_file); |
|
3202 unwind_protect_bool (line_editing); |
|
3203 |
|
3204 input_line_number = 0; |
|
3205 current_input_column = 1; |
|
3206 reading_fcn_file = true; |
|
3207 line_editing = false; |
|
3208 |
|
3209 FILE *ffile = get_input_from_file (ff, 0); |
|
3210 |
|
3211 unwind_protect::add (safe_fclose, ffile); |
|
3212 |
|
3213 if (ffile) |
|
3214 { |
|
3215 // Check to see if this file defines a function or is just a |
|
3216 // list of commands. |
|
3217 |
3165
|
3218 if (! force_script && is_function_file (ffile)) |
3021
|
3219 { |
|
3220 // XXX FIXME XXX -- we shouldn't need both the |
|
3221 // command_history object and the |
|
3222 // Vsaving_history variable... |
|
3223 command_history::ignore_entries (); |
|
3224 |
|
3225 unwind_protect::add (restore_command_history, 0); |
|
3226 |
|
3227 unwind_protect_int (Vecho_executing_commands); |
|
3228 unwind_protect_bool (Vsaving_history); |
|
3229 unwind_protect_bool (reading_fcn_file); |
|
3230 unwind_protect_bool (input_from_command_line_file); |
3877
|
3231 unwind_protect_bool (get_input_from_eval_string); |
3883
|
3232 unwind_protect_bool (parser_end_of_input); |
3021
|
3233 |
|
3234 Vecho_executing_commands = ECHO_OFF; |
|
3235 Vsaving_history = false; |
|
3236 reading_fcn_file = true; |
|
3237 input_from_command_line_file = false; |
3877
|
3238 get_input_from_eval_string = false; |
3883
|
3239 parser_end_of_input = false; |
3021
|
3240 |
|
3241 YY_BUFFER_STATE old_buf = current_buffer (); |
|
3242 YY_BUFFER_STATE new_buf = create_buffer (ffile); |
|
3243 |
|
3244 unwind_protect::add (restore_input_buffer, (void *) old_buf); |
|
3245 unwind_protect::add (delete_input_buffer, (void *) new_buf); |
|
3246 |
|
3247 switch_to_buffer (new_buf); |
|
3248 |
|
3249 unwind_protect_ptr (curr_sym_tab); |
|
3250 |
|
3251 reset_parser (); |
|
3252 |
3665
|
3253 help_buf = gobble_leading_white_space (ffile, true, true, true); |
|
3254 |
|
3255 octave_comment_buffer::append (help_buf); |
3021
|
3256 |
|
3257 // XXX FIXME XXX -- this should not be necessary. |
3665
|
3258 gobble_leading_white_space (ffile, false, true, false); |
3021
|
3259 |
|
3260 int status = yyparse (); |
|
3261 |
|
3262 if (status != 0) |
|
3263 { |
|
3264 error ("parse error while reading function file %s", |
|
3265 ff.c_str ()); |
4009
|
3266 fbi_sym_tab->clear (curr_fcn_file_name); |
3021
|
3267 } |
|
3268 } |
|
3269 else if (exec_script) |
|
3270 { |
|
3271 // The value of `reading_fcn_file' will be restored to the |
|
3272 // proper value when we unwind from this frame. |
|
3273 reading_fcn_file = old_reading_fcn_file_state; |
|
3274 |
|
3275 // XXX FIXME XXX -- we shouldn't need both the |
|
3276 // command_history object and the |
|
3277 // Vsaving_history variable... |
|
3278 command_history::ignore_entries (); |
|
3279 |
|
3280 unwind_protect::add (restore_command_history, 0); |
|
3281 |
|
3282 unwind_protect_bool (Vsaving_history); |
|
3283 unwind_protect_bool (reading_script_file); |
|
3284 |
|
3285 Vsaving_history = false; |
|
3286 reading_script_file = true; |
|
3287 |
3180
|
3288 unwind_protect::add (clear_current_script_file_name, 0); |
|
3289 |
|
3290 bind_builtin_variable ("current_script_file_name", ff); |
|
3291 |
3021
|
3292 parse_and_execute (ffile); |
|
3293 |
|
3294 script_file_executed = true; |
|
3295 } |
|
3296 } |
3880
|
3297 else |
|
3298 error ("no such file, `%s'", ff.c_str ()); |
3021
|
3299 |
|
3300 unwind_protect::run_frame ("parse_fcn_file"); |
|
3301 |
|
3302 return script_file_executed; |
|
3303 } |
|
3304 |
|
3305 bool |
|
3306 load_fcn_from_file (symbol_record *sym_rec, bool exec_script) |
|
3307 { |
|
3308 bool script_file_executed = false; |
|
3309 |
3523
|
3310 std::string nm = sym_rec->name (); |
3021
|
3311 |
3325
|
3312 if (octave_dynamic_loader::load (nm)) |
3021
|
3313 { |
|
3314 force_link_to_function (nm); |
|
3315 } |
|
3316 else |
|
3317 { |
3523
|
3318 std::string ff = fcn_file_in_path (nm); |
3021
|
3319 |
|
3320 // These are needed by yyparse. |
|
3321 |
|
3322 unwind_protect::begin_frame ("load_fcn_from_file"); |
|
3323 |
|
3324 unwind_protect_str (curr_fcn_file_name); |
|
3325 unwind_protect_str (curr_fcn_file_full_name); |
|
3326 |
|
3327 curr_fcn_file_name = nm; |
|
3328 curr_fcn_file_full_name = ff; |
|
3329 |
|
3330 if (ff.length () > 0) |
3165
|
3331 script_file_executed = parse_fcn_file (ff, exec_script); |
3021
|
3332 |
|
3333 if (! (error_state || script_file_executed)) |
|
3334 force_link_to_function (nm); |
|
3335 |
|
3336 unwind_protect::run_frame ("load_fcn_from_file"); |
|
3337 } |
|
3338 |
|
3339 return script_file_executed; |
|
3340 } |
|
3341 |
|
3342 DEFUN (source, args, , |
3371
|
3343 "-*- texinfo -*-\n\ |
|
3344 @deftypefn {Built-in Function} {} source (@var{file})\n\ |
|
3345 Parse and execute the contents of @var{file}. This is equivalent to\n\ |
|
3346 executing commands from a script file, but without requiring the file to\n\ |
|
3347 be named @file{@var{file}.m}.\n\ |
|
3348 @end deftypefn") |
3021
|
3349 { |
|
3350 octave_value_list retval; |
|
3351 |
|
3352 int nargin = args.length (); |
|
3353 |
|
3354 if (nargin == 1) |
|
3355 { |
3523
|
3356 std::string file_name = args(0).string_value (); |
3021
|
3357 |
|
3358 if (! error_state) |
|
3359 { |
3523
|
3360 std::string file_full_name = file_ops::tilde_expand (file_name); |
3388
|
3361 |
|
3362 unwind_protect::begin_frame ("Fsource"); |
|
3363 |
|
3364 unwind_protect_str (curr_fcn_file_name); |
|
3365 unwind_protect_str (curr_fcn_file_full_name); |
|
3366 |
|
3367 curr_fcn_file_name = file_name; |
|
3368 curr_fcn_file_full_name = file_full_name; |
|
3369 |
|
3370 parse_fcn_file (file_full_name, true, true); |
3021
|
3371 |
|
3372 if (error_state) |
3388
|
3373 error ("source: error sourcing file `%s'", |
|
3374 file_full_name.c_str ()); |
|
3375 |
|
3376 unwind_protect::run_frame ("Fsource"); |
3021
|
3377 } |
|
3378 else |
|
3379 error ("source: expecting file name as argument"); |
|
3380 } |
|
3381 else |
|
3382 print_usage ("source"); |
|
3383 |
|
3384 return retval; |
|
3385 } |
|
3386 |
3726
|
3387 // Evaluate an Octave function (built-in or interpreted) and return |
3844
|
3388 // the list of result values. NAME is the name of the function to |
|
3389 // call. ARGS are the arguments to the function. NARGOUT is the |
|
3390 // number of output arguments expected. |
3726
|
3391 |
3021
|
3392 octave_value_list |
3523
|
3393 feval (const std::string& name, const octave_value_list& args, int nargout) |
3156
|
3394 { |
|
3395 octave_value_list retval; |
|
3396 |
|
3397 octave_function *fcn = is_valid_function (name, "feval", 1); |
|
3398 |
|
3399 if (fcn) |
3544
|
3400 retval = fcn->do_multi_index_op (nargout, args); |
3156
|
3401 |
|
3402 return retval; |
|
3403 } |
|
3404 |
3726
|
3405 // Evaluate an Octave function (built-in or interpreted) and return |
|
3406 // the list of result values. The first element of ARGS should be a |
|
3407 // string containing the name of the function to call, then the rest |
|
3408 // are the actual arguments to the function. NARGOUT is the number of |
|
3409 // output arguments expected. |
|
3410 |
3156
|
3411 octave_value_list |
3021
|
3412 feval (const octave_value_list& args, int nargout) |
|
3413 { |
|
3414 octave_value_list retval; |
|
3415 |
3156
|
3416 if (args.length () > 0) |
3021
|
3417 { |
3523
|
3418 std::string name = args(0).string_value (); |
3156
|
3419 |
|
3420 if (! error_state) |
3021
|
3421 { |
3156
|
3422 int tmp_nargin = args.length () - 1; |
|
3423 |
|
3424 octave_value_list tmp_args (tmp_nargin, octave_value ()); |
|
3425 |
|
3426 for (int i = 0; i < tmp_nargin; i++) |
3178
|
3427 tmp_args(i) = args(i+1); |
|
3428 |
|
3429 string_vector arg_names = args.name_tags (); |
|
3430 |
|
3431 if (! arg_names.empty ()) |
3156
|
3432 { |
3726
|
3433 // tmp_nargin and arg_names.length () - 1 may differ if |
|
3434 // we are passed all_va_args. |
|
3435 |
|
3436 int n = arg_names.length () - 1; |
|
3437 |
|
3438 int len = n > tmp_nargin ? tmp_nargin : n; |
|
3439 |
|
3440 string_vector tmp_arg_names (len); |
|
3441 |
|
3442 for (int i = 0; i < len; i++) |
3178
|
3443 tmp_arg_names(i) = arg_names(i+1); |
|
3444 |
|
3445 tmp_args.stash_name_tags (tmp_arg_names); |
3156
|
3446 } |
|
3447 |
|
3448 retval = feval (name, tmp_args, nargout); |
3021
|
3449 } |
|
3450 } |
|
3451 |
|
3452 return retval; |
|
3453 } |
|
3454 |
|
3455 DEFUN (feval, args, nargout, |
3371
|
3456 "-*- texinfo -*-\n\ |
|
3457 @deftypefn {Built-in Function} {} feval (@var{name}, @dots{})\n\ |
|
3458 Evaluate the function named @var{name}. Any arguments after the first\n\ |
|
3459 are passed on to the named function. For example,\n\ |
|
3460 \n\ |
|
3461 @example\n\ |
|
3462 feval (\"acos\", -1)\n\ |
|
3463 @result{} 3.1416\n\ |
|
3464 @end example\n\ |
3021
|
3465 \n\ |
3371
|
3466 @noindent\n\ |
|
3467 calls the function @code{acos} with the argument @samp{-1}.\n\ |
|
3468 \n\ |
|
3469 The function @code{feval} is necessary in order to be able to write\n\ |
|
3470 functions that call user-supplied functions, because Octave does not\n\ |
|
3471 have a way to declare a pointer to a function (like C) or to declare a\n\ |
|
3472 special kind of variable that can be used to hold the name of a function\n\ |
|
3473 (like @code{EXTERNAL} in Fortran). Instead, you must refer to functions\n\ |
|
3474 by name, and use @code{feval} to call them.\n\ |
|
3475 @end deftypefn") |
3021
|
3476 { |
|
3477 octave_value_list retval; |
|
3478 |
|
3479 int nargin = args.length (); |
|
3480 |
|
3481 if (nargin > 0) |
|
3482 retval = feval (args, nargout); |
|
3483 else |
|
3484 print_usage ("feval"); |
|
3485 |
|
3486 return retval; |
|
3487 } |
|
3488 |
3099
|
3489 octave_value_list |
3523
|
3490 eval_string (const std::string& s, bool silent, int& parse_status, int nargout) |
3021
|
3491 { |
3877
|
3492 octave_value_list retval; |
|
3493 |
3021
|
3494 unwind_protect::begin_frame ("eval_string"); |
|
3495 |
|
3496 unwind_protect_bool (get_input_from_eval_string); |
3877
|
3497 unwind_protect_bool (input_from_eval_string_pending); |
3021
|
3498 unwind_protect_bool (input_from_command_line_file); |
3883
|
3499 unwind_protect_bool (parser_end_of_input); |
3021
|
3500 unwind_protect_str (current_eval_string); |
|
3501 |
|
3502 get_input_from_eval_string = true; |
3877
|
3503 input_from_eval_string_pending = true; |
3021
|
3504 input_from_command_line_file = false; |
3883
|
3505 parser_end_of_input = false; |
3021
|
3506 current_eval_string = s; |
|
3507 |
3877
|
3508 unwind_protect_ptr (global_command); |
|
3509 |
3021
|
3510 YY_BUFFER_STATE old_buf = current_buffer (); |
|
3511 YY_BUFFER_STATE new_buf = create_buffer (0); |
|
3512 |
|
3513 unwind_protect::add (restore_input_buffer, old_buf); |
|
3514 unwind_protect::add (delete_input_buffer, new_buf); |
|
3515 |
|
3516 switch_to_buffer (new_buf); |
|
3517 |
|
3518 unwind_protect_ptr (curr_sym_tab); |
|
3519 |
3877
|
3520 do |
|
3521 { |
|
3522 parse_status = yyparse (); |
|
3523 |
|
3524 tree_statement_list *command = global_command; |
|
3525 |
3883
|
3526 if (parse_status == 0) |
3877
|
3527 { |
3883
|
3528 if (command) |
|
3529 { |
|
3530 retval = command->eval (silent, nargout); |
|
3531 |
|
3532 delete command; |
|
3533 |
|
3534 command = 0; |
|
3535 |
|
3536 if (error_state |
|
3537 || tree_return_command::returning |
|
3538 || tree_break_command::breaking |
|
3539 || tree_continue_command::continuing) |
|
3540 break; |
|
3541 } |
|
3542 else if (parser_end_of_input) |
3877
|
3543 break; |
3883
|
3544 } |
3877
|
3545 } |
|
3546 while (parse_status == 0); |
3021
|
3547 |
|
3548 unwind_protect::run_frame ("eval_string"); |
|
3549 |
|
3550 return retval; |
|
3551 } |
|
3552 |
|
3553 octave_value |
3523
|
3554 eval_string (const std::string& s, bool silent, int& parse_status) |
3021
|
3555 { |
|
3556 octave_value retval; |
|
3557 |
|
3558 octave_value_list tmp = eval_string (s, silent, parse_status, 1); |
|
3559 |
|
3560 if (! tmp.empty ()) |
|
3561 retval = tmp(0); |
|
3562 |
|
3563 return retval; |
|
3564 } |
|
3565 |
|
3566 static octave_value_list |
|
3567 eval_string (const octave_value& arg, bool silent, int& parse_status, |
|
3568 int nargout) |
|
3569 { |
3523
|
3570 std::string s = arg.string_value (); |
3021
|
3571 |
|
3572 if (error_state) |
|
3573 { |
3523
|
3574 error ("eval: expecting std::string argument"); |
3021
|
3575 return -1.0; |
|
3576 } |
|
3577 |
|
3578 return eval_string (s, silent, parse_status, nargout); |
|
3579 } |
|
3580 |
|
3581 DEFUN (eval, args, nargout, |
3371
|
3582 "-*- texinfo -*-\n\ |
|
3583 @deftypefn {Built-in Function} {} eval (@var{try}, @var{catch})\n\ |
|
3584 Parse the string @var{try} and evaluate it as if it were an Octave\n\ |
|
3585 program, returning the last value computed. If that fails, evaluate\n\ |
|
3586 the string @var{catch}. The string @var{try} is evaluated in the\n\ |
|
3587 current context, so any results remain available after @code{eval}\n\ |
|
3588 returns. For example,\n\ |
|
3589 \n\ |
|
3590 @example\n\ |
|
3591 @group\n\ |
|
3592 eval (\"a = 13\")\n\ |
|
3593 @print{} a = 13\n\ |
|
3594 @result{} 13\n\ |
|
3595 @end group\n\ |
|
3596 @end example\n\ |
3021
|
3597 \n\ |
3371
|
3598 In this case, the value of the evaluated expression is printed and it is\n\ |
|
3599 also returned returned from @code{eval}. Just as with any other\n\ |
|
3600 expression, you can turn printing off by ending the expression in a\n\ |
|
3601 semicolon. For example,\n\ |
|
3602 \n\ |
|
3603 @example\n\ |
|
3604 eval (\"a = 13;\")\n\ |
|
3605 @result{} 13\n\ |
|
3606 @end example\n\ |
|
3607 \n\ |
|
3608 In this example, the variable @code{a} has been given the value 13, but\n\ |
|
3609 the value of the expression is not printed. You can also turn off\n\ |
|
3610 automatic printing for all expressions executed by @code{eval} using the\n\ |
|
3611 variable @code{default_eval_print_flag}.\n\ |
|
3612 @end deftypefn") |
3021
|
3613 { |
|
3614 octave_value_list retval; |
|
3615 |
|
3616 int nargin = args.length (); |
|
3617 |
|
3618 if (nargin > 0) |
|
3619 { |
|
3620 unwind_protect::begin_frame ("Feval"); |
|
3621 |
|
3622 if (nargin > 1) |
|
3623 { |
|
3624 unwind_protect_bool (buffer_error_messages); |
|
3625 buffer_error_messages = true; |
|
3626 } |
|
3627 |
|
3628 int parse_status = 0; |
|
3629 |
|
3630 retval = eval_string (args(0), ! Vdefault_eval_print_flag, |
|
3631 parse_status, nargout); |
|
3632 |
|
3633 if (nargin > 1 && (parse_status != 0 || error_state)) |
|
3634 { |
|
3635 error_state = 0; |
|
3636 |
|
3637 // Set up for letting the user print any messages from |
|
3638 // errors that occurred in the first part of this eval(). |
|
3639 |
|
3640 buffer_error_messages = false; |
3815
|
3641 |
3021
|
3642 bind_global_error_variable (); |
3815
|
3643 |
3021
|
3644 unwind_protect::add (clear_global_error_variable, 0); |
|
3645 |
|
3646 eval_string (args(1), 0, parse_status, nargout); |
|
3647 |
|
3648 retval = octave_value_list (); |
|
3649 } |
|
3650 |
|
3651 unwind_protect::run_frame ("Feval"); |
|
3652 } |
|
3653 else |
|
3654 print_usage ("eval"); |
|
3655 |
|
3656 return retval; |
|
3657 } |
|
3658 |
|
3659 static int |
|
3660 default_eval_print_flag (void) |
|
3661 { |
|
3662 Vdefault_eval_print_flag = check_preference ("default_eval_print_flag"); |
|
3663 |
|
3664 return 0; |
|
3665 } |
|
3666 |
2166
|
3667 static int |
|
3668 warn_assign_as_truth_value (void) |
|
3669 { |
|
3670 Vwarn_assign_as_truth_value |
|
3671 = check_preference ("warn_assign_as_truth_value"); |
|
3672 |
|
3673 return 0; |
|
3674 } |
|
3675 |
|
3676 static int |
|
3677 warn_function_name_clash (void) |
|
3678 { |
|
3679 Vwarn_function_name_clash = check_preference ("warn_function_name_clash"); |
|
3680 |
|
3681 return 0; |
|
3682 } |
|
3683 |
|
3684 static int |
3162
|
3685 warn_future_time_stamp (void) |
|
3686 { |
|
3687 Vwarn_future_time_stamp = check_preference ("warn_future_time_stamp"); |
|
3688 |
|
3689 return 0; |
|
3690 } |
|
3691 |
|
3692 static int |
2166
|
3693 warn_missing_semicolon (void) |
|
3694 { |
|
3695 Vwarn_missing_semicolon = check_preference ("warn_missing_semicolon"); |
|
3696 |
|
3697 return 0; |
|
3698 } |
|
3699 |
2764
|
3700 static int |
4023
|
3701 warn_precedence_change (void) |
|
3702 { |
|
3703 Vwarn_precedence_change = check_preference ("warn_precedence_change"); |
|
3704 |
|
3705 return 0; |
|
3706 } |
|
3707 |
|
3708 static int |
2764
|
3709 warn_variable_switch_label (void) |
|
3710 { |
|
3711 Vwarn_variable_switch_label |
|
3712 = check_preference ("warn_variable_switch_label"); |
|
3713 |
|
3714 return 0; |
|
3715 } |
|
3716 |
2166
|
3717 void |
|
3718 symbols_of_parse (void) |
|
3719 { |
3258
|
3720 DEFVAR (default_eval_print_flag, 1.0, default_eval_print_flag, |
3371
|
3721 "-*- texinfo -*-\n\ |
|
3722 @defvr {Built-in Variable} default_eval_print_flag\n\ |
|
3723 If the value of this variable is nonzero, Octave prints the results of\n\ |
|
3724 commands executed by @code{eval} that do not end with semicolons. If it\n\ |
|
3725 is zero, automatic printing is suppressed. The default value is 1.\n\ |
|
3726 @end defvr"); |
3021
|
3727 |
3258
|
3728 DEFVAR (warn_assign_as_truth_value, 1.0, warn_assign_as_truth_value, |
3366
|
3729 "-*- texinfo -*-\n\ |
|
3730 @defvr {Built-in Variable} warn_assign_as_truth_value\n\ |
|
3731 If the value of @code{warn_assign_as_truth_value} is nonzero, a\n\ |
|
3732 warning is issued for statements like\n\ |
|
3733 \n\ |
|
3734 @example\n\ |
|
3735 if (s = t)\n\ |
|
3736 ...\n\ |
|
3737 @end example\n\ |
|
3738 \n\ |
|
3739 @noindent\n\ |
|
3740 since such statements are not common, and it is likely that the intent\n\ |
|
3741 was to write\n\ |
|
3742 \n\ |
|
3743 @example\n\ |
|
3744 if (s == t)\n\ |
|
3745 ...\n\ |
|
3746 @end example\n\ |
|
3747 \n\ |
|
3748 @noindent\n\ |
|
3749 instead.\n\ |
|
3750 \n\ |
|
3751 There are times when it is useful to write code that contains\n\ |
|
3752 assignments within the condition of a @code{while} or @code{if}\n\ |
|
3753 statement. For example, statements like\n\ |
|
3754 \n\ |
|
3755 @example\n\ |
|
3756 while (c = getc())\n\ |
|
3757 ...\n\ |
|
3758 @end example\n\ |
|
3759 \n\ |
|
3760 @noindent\n\ |
|
3761 are common in C programming.\n\ |
|
3762 \n\ |
|
3763 It is possible to avoid all warnings about such statements by setting\n\ |
|
3764 @code{warn_assign_as_truth_value} to 0, but that may also\n\ |
|
3765 let real errors like\n\ |
|
3766 \n\ |
|
3767 @example\n\ |
|
3768 if (x = 1) # intended to test (x == 1)!\n\ |
|
3769 ...\n\ |
|
3770 @end example\n\ |
|
3771 \n\ |
|
3772 @noindent\n\ |
|
3773 slip by.\n\ |
|
3774 \n\ |
|
3775 In such cases, it is possible suppress errors for specific statements by\n\ |
|
3776 writing them with an extra set of parentheses. For example, writing the\n\ |
|
3777 previous example as\n\ |
|
3778 \n\ |
|
3779 @example\n\ |
|
3780 while ((c = getc()))\n\ |
|
3781 ...\n\ |
|
3782 @end example\n\ |
|
3783 \n\ |
|
3784 @noindent\n\ |
|
3785 will prevent the warning from being printed for this statement, while\n\ |
|
3786 allowing Octave to warn about other assignments used in conditional\n\ |
|
3787 contexts.\n\ |
|
3788 \n\ |
|
3789 The default value of @code{warn_assign_as_truth_value} is 1.\n\ |
|
3790 @end defvr"); |
2166
|
3791 |
3258
|
3792 DEFVAR (warn_function_name_clash, 1.0, warn_function_name_clash, |
3371
|
3793 "-*- texinfo -*-\n\ |
|
3794 @defvr {Built-in Variable} warn_function_name_clash\n\ |
|
3795 If the value of @code{warn_function_name_clash} is nonzero, a warning is\n\ |
|
3796 issued when Octave finds that the name of a function defined in a\n\ |
|
3797 function file differs from the name of the file. (If the names\n\ |
|
3798 disagree, the name declared inside the file is ignored.) If the value\n\ |
|
3799 is 0, the warning is omitted. The default value is 1.\n\ |
|
3800 @end defvr"); |
2166
|
3801 |
3258
|
3802 DEFVAR (warn_future_time_stamp, 1.0, warn_future_time_stamp, |
3446
|
3803 "-*- texinfo -*-\n\ |
|
3804 @defvr {Built-in Variable} warn_future_time_stamp\n\ |
|
3805 If the value of this variable is nonzero, Octave will print a warning\n\ |
|
3806 if it finds a function file with a time stamp that is in the future.\n\ |
|
3807 @end defvr"); |
3162
|
3808 |
3258
|
3809 DEFVAR (warn_missing_semicolon, 0.0, warn_missing_semicolon, |
3371
|
3810 "-*- texinfo -*-\n\ |
|
3811 @defvr {Built-in Variable} warn_missing_semicolon\n\ |
|
3812 If the value of this variable is nonzero, Octave will warn when\n\ |
|
3813 statements in function definitions don't end in semicolons. The default\n\ |
|
3814 value is 0.\n\ |
|
3815 @end defvr"); |
2764
|
3816 |
4023
|
3817 DEFVAR (warn_precedence_change, 1.0, warn_precedence_change, |
|
3818 "-*- texinfo -*-\n\ |
|
3819 @defvr {Built-in Variable} warn_precedence_change\n\ |
|
3820 If the value of this variable is nonzero, Octave will warn about\n\ |
|
3821 possible changes in the meaning of some code due to changes in\n\ |
|
3822 precedence for some operators. Precedence changes have typically\n\ |
|
3823 been made for Matlab compatibility. The default value is 1.\n\ |
|
3824 @end defvr"); |
|
3825 |
3258
|
3826 DEFVAR (warn_variable_switch_label, 0.0, warn_variable_switch_label, |
3366
|
3827 "-*- texinfo -*-\n\ |
|
3828 @defvr {Built-in Variable} warn_variable_switch_label\n\ |
|
3829 If the value of this variable is nonzero, Octave will print a warning if\n\ |
|
3830 a switch label is not a constant or constant expression\n\ |
|
3831 @end defvr"); |
3347
|
3832 |
2166
|
3833 } |
|
3834 |
1994
|
3835 /* |
|
3836 ;;; Local Variables: *** |
|
3837 ;;; mode: text *** |
|
3838 ;;; End: *** |
|
3839 */ |