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 |
|
983 case_list : case_list1 |
|
984 { $$ = $1; } |
|
985 | case_list1 default_case |
|
986 { |
|
987 $1->append ($2); |
|
988 $$ = $1; |
|
989 } |
|
990 ; |
|
991 |
|
992 case_list1 : switch_case |
|
993 { $$ = new tree_switch_case_list ($1); } |
|
994 | case_list1 switch_case |
|
995 { |
|
996 $1->append ($2); |
|
997 $$ = $1; |
|
998 } |
|
999 ; |
|
1000 |
4025
|
1001 switch_case : CASE stash_comment opt_sep expression opt_sep opt_list |
3665
|
1002 { $$ = make_switch_case ($4, $6, $2); } |
2970
|
1003 ; |
|
1004 |
3665
|
1005 default_case : OTHERWISE stash_comment opt_sep opt_list |
|
1006 { |
|
1007 $$ = new tree_switch_case ($4, $2); |
|
1008 } |
2970
|
1009 ; |
|
1010 |
|
1011 // ======= |
|
1012 // Looping |
|
1013 // ======= |
|
1014 |
3665
|
1015 loop_command : WHILE stash_comment expression opt_sep opt_list END |
2970
|
1016 { |
3665
|
1017 if (! ($$ = make_while_command ($1, $3, $5, $6, $2))) |
2970
|
1018 ABORT_PARSE; |
|
1019 } |
3665
|
1020 | DO stash_comment opt_sep opt_list UNTIL expression |
3484
|
1021 { |
3665
|
1022 if (! ($$ = make_do_until_command ($1, $4, $6, $2))) |
3484
|
1023 ABORT_PARSE; |
|
1024 } |
3665
|
1025 | FOR stash_comment assign_lhs '=' expression opt_sep opt_list END |
2970
|
1026 { |
3665
|
1027 if (! ($$ = make_for_command ($1, $3, $5, $7, $8, $2))) |
2970
|
1028 ABORT_PARSE; |
|
1029 } |
|
1030 ; |
|
1031 |
|
1032 // ======= |
|
1033 // Jumping |
|
1034 // ======= |
|
1035 |
|
1036 jump_command : BREAK |
|
1037 { |
|
1038 if (! ($$ = make_break_command ($1))) |
|
1039 ABORT_PARSE; |
|
1040 } |
|
1041 | CONTINUE |
|
1042 { |
|
1043 if (! ($$ = make_continue_command ($1))) |
|
1044 ABORT_PARSE; |
|
1045 } |
|
1046 | FUNC_RET |
|
1047 { |
|
1048 if (! ($$ = make_return_command ($1))) |
|
1049 ABORT_PARSE; |
|
1050 } |
|
1051 ; |
|
1052 |
|
1053 // ========== |
|
1054 // Exceptions |
|
1055 // ========== |
|
1056 |
3665
|
1057 except_command : UNWIND stash_comment opt_sep opt_list CLEANUP |
|
1058 stash_comment opt_sep opt_list END |
2970
|
1059 { |
3665
|
1060 if (! ($$ = make_unwind_command ($1, $4, $8, $9, $2, $6))) |
2970
|
1061 ABORT_PARSE; |
|
1062 } |
3665
|
1063 | TRY stash_comment opt_sep opt_list CATCH |
|
1064 stash_comment opt_sep opt_list END |
2970
|
1065 { |
3665
|
1066 if (! ($$ = make_try_command ($1, $4, $8, $9, $2, $6))) |
2970
|
1067 ABORT_PARSE; |
|
1068 } |
|
1069 ; |
|
1070 |
|
1071 // =========================================== |
|
1072 // Some `subroutines' for function definitions |
|
1073 // =========================================== |
|
1074 |
3903
|
1075 save_symtab : // empty |
|
1076 { |
|
1077 if (symtab_context) |
|
1078 panic_impossible (); |
|
1079 |
|
1080 symtab_context = curr_sym_tab; |
|
1081 } |
|
1082 ; |
|
1083 |
4009
|
1084 function_symtab : // empty |
|
1085 { curr_sym_tab = fbi_sym_tab; } |
2970
|
1086 ; |
|
1087 |
|
1088 local_symtab : // empty |
|
1089 { curr_sym_tab = tmp_local_sym_tab; } |
|
1090 ; |
|
1091 |
|
1092 in_return_list : // empty |
|
1093 { lexer_flags.looking_at_return_list = true; } |
|
1094 ; |
|
1095 |
|
1096 parsed_fcn_name : // empty |
|
1097 { lexer_flags.parsed_function_name = true; } |
|
1098 ; |
|
1099 |
|
1100 // =========================== |
|
1101 // List of function parameters |
|
1102 // =========================== |
|
1103 |
|
1104 param_list_beg : '(' |
|
1105 { lexer_flags.looking_at_parameter_list = true; } |
|
1106 ; |
|
1107 |
|
1108 param_list_end : ')' |
|
1109 { lexer_flags.looking_at_parameter_list = false; } |
|
1110 ; |
|
1111 |
|
1112 param_list : param_list_beg param_list_end |
|
1113 { |
|
1114 lexer_flags.quote_is_transpose = false; |
|
1115 $$ = 0; |
|
1116 } |
3973
|
1117 | param_list_beg VARARGIN param_list_end |
2970
|
1118 { |
|
1119 lexer_flags.quote_is_transpose = false; |
|
1120 tree_parameter_list *tmp = new tree_parameter_list (); |
|
1121 tmp->mark_varargs_only (); |
|
1122 $$ = tmp; |
|
1123 } |
|
1124 | param_list1 param_list_end |
|
1125 { |
|
1126 lexer_flags.quote_is_transpose = false; |
|
1127 $1->mark_as_formal_parameters (); |
|
1128 $$ = $1; |
|
1129 } |
3973
|
1130 | param_list1 ',' VARARGIN param_list_end |
2970
|
1131 { |
|
1132 lexer_flags.quote_is_transpose = false; |
|
1133 $1->mark_as_formal_parameters (); |
|
1134 $1->mark_varargs (); |
|
1135 $$ = $1; |
|
1136 } |
|
1137 ; |
|
1138 |
|
1139 param_list1 : param_list_beg identifier |
|
1140 { $$ = new tree_parameter_list ($2); } |
|
1141 | param_list1 ',' identifier |
|
1142 { |
|
1143 $1->append ($3); |
|
1144 $$ = $1; |
|
1145 } |
|
1146 | param_list_beg error |
|
1147 { |
|
1148 yyerror ("invalid parameter list"); |
|
1149 $$ = 0; |
|
1150 ABORT_PARSE; |
|
1151 } |
|
1152 | param_list1 ',' error |
|
1153 { |
|
1154 yyerror ("invalid parameter list"); |
|
1155 $$ = 0; |
|
1156 ABORT_PARSE; |
|
1157 } |
|
1158 ; |
|
1159 |
|
1160 // =================================== |
|
1161 // List of function return value names |
|
1162 // =================================== |
|
1163 |
3189
|
1164 return_list_beg : '[' in_return_list local_symtab |
2970
|
1165 ; |
|
1166 |
|
1167 return_list : return_list_beg return_list_end |
|
1168 { |
|
1169 lexer_flags.looking_at_return_list = false; |
|
1170 $$ = new tree_parameter_list (); |
|
1171 } |
3973
|
1172 | return_list_beg VARARGOUT return_list_end |
2970
|
1173 { |
|
1174 lexer_flags.looking_at_return_list = false; |
|
1175 tree_parameter_list *tmp = new tree_parameter_list (); |
|
1176 tmp->mark_varargs_only (); |
|
1177 $$ = tmp; |
|
1178 } |
|
1179 | return_list_beg return_list1 return_list_end |
|
1180 { |
|
1181 lexer_flags.looking_at_return_list = false; |
|
1182 $$ = $2; |
|
1183 } |
3973
|
1184 | return_list_beg return_list1 ',' VARARGOUT return_list_end |
2970
|
1185 { |
|
1186 lexer_flags.looking_at_return_list = false; |
|
1187 $2->mark_varargs (); |
|
1188 $$ = $2; |
|
1189 } |
|
1190 ; |
|
1191 |
|
1192 return_list1 : identifier |
|
1193 { $$ = new tree_parameter_list ($1); } |
|
1194 | return_list1 ',' identifier |
|
1195 { |
|
1196 $1->append ($3); |
|
1197 $$ = $1; |
|
1198 } |
|
1199 ; |
|
1200 |
4009
|
1201 return_list_end : function_symtab ']' |
2970
|
1202 ; |
|
1203 |
|
1204 // =================== |
|
1205 // Function definition |
|
1206 // =================== |
|
1207 |
4009
|
1208 function_beg : save_symtab FCN stash_comment function_symtab |
3903
|
1209 { $$ = $3; } |
2970
|
1210 ; |
|
1211 |
|
1212 function : function_beg function2 |
|
1213 { |
3665
|
1214 $2->stash_leading_comment ($1); |
2970
|
1215 recover_from_parsing_function (); |
|
1216 $$ = 0; |
|
1217 } |
|
1218 | function_beg identifier function1 |
|
1219 { |
3665
|
1220 finish_function ($2, $3, $1); |
2970
|
1221 recover_from_parsing_function (); |
|
1222 $$ = 0; |
|
1223 } |
|
1224 | function_beg return_list function1 |
|
1225 { |
3665
|
1226 finish_function ($2, $3, $1); |
2970
|
1227 recover_from_parsing_function (); |
|
1228 $$ = 0; |
|
1229 } |
|
1230 ; |
|
1231 |
4009
|
1232 function1 : function_symtab '=' function2 |
2970
|
1233 { $$ = $3; } |
|
1234 ; |
|
1235 |
|
1236 function2 : identifier local_symtab parsed_fcn_name function3 |
|
1237 { |
|
1238 if (! ($$ = frob_function ($1, $4))) |
|
1239 ABORT_PARSE; |
|
1240 } |
|
1241 ; |
|
1242 |
|
1243 function3 : param_list function4 |
|
1244 { $$ = start_function ($1, $2); } |
|
1245 | function4 |
|
1246 { $$ = start_function (0, $1); } |
|
1247 ; |
|
1248 |
|
1249 function4 : opt_sep opt_list function_end |
|
1250 { $$ = $2; } |
|
1251 ; |
|
1252 |
|
1253 function_end : END |
|
1254 { |
|
1255 if (end_token_ok ($1, token::function_end)) |
|
1256 { |
|
1257 if (reading_fcn_file) |
|
1258 check_for_garbage_after_fcn_def (); |
|
1259 } |
|
1260 else |
|
1261 ABORT_PARSE; |
|
1262 } |
|
1263 | END_OF_INPUT |
|
1264 { |
3939
|
1265 if (! (reading_fcn_file || reading_script_file |
|
1266 || get_input_from_eval_string)) |
2970
|
1267 YYABORT; |
|
1268 } |
|
1269 ; |
|
1270 |
|
1271 // ======== |
|
1272 // Plotting |
|
1273 // ======== |
|
1274 |
3180
|
1275 plot_command : PLOT |
|
1276 { |
|
1277 if (! ($$ = make_plot_command ($1, 0, 0))) |
|
1278 ABORT_PARSE; |
|
1279 } |
|
1280 | PLOT ranges |
|
1281 { |
|
1282 if (! ($$ = make_plot_command ($1, $2, 0))) |
|
1283 ABORT_PARSE; |
|
1284 } |
|
1285 | PLOT plot_command1 |
1
|
1286 { |
1623
|
1287 if (! ($$ = make_plot_command ($1, 0, $2))) |
|
1288 ABORT_PARSE; |
1
|
1289 } |
|
1290 | PLOT ranges plot_command1 |
|
1291 { |
1623
|
1292 if (! ($$ = make_plot_command ($1, $2, $3))) |
|
1293 ABORT_PARSE; |
1
|
1294 } |
|
1295 ; |
|
1296 |
|
1297 ranges : ranges1 |
578
|
1298 { $$ = new plot_limits ($1); } |
1
|
1299 | ranges1 ranges1 |
578
|
1300 { $$ = new plot_limits ($1, $2); } |
1
|
1301 | ranges1 ranges1 ranges1 |
578
|
1302 { $$ = new plot_limits ($1, $2, $3); } |
1
|
1303 ; |
|
1304 |
|
1305 ranges1 : OPEN_BRACE expression COLON expression CLOSE_BRACE |
578
|
1306 { $$ = new plot_range ($2, $4); } |
1
|
1307 | OPEN_BRACE COLON expression CLOSE_BRACE |
578
|
1308 { $$ = new plot_range (0, $3); } |
1
|
1309 | OPEN_BRACE expression COLON CLOSE_BRACE |
578
|
1310 { $$ = new plot_range ($2, 0); } |
1
|
1311 | OPEN_BRACE COLON CLOSE_BRACE |
578
|
1312 { $$ = new plot_range (); } |
1
|
1313 | OPEN_BRACE CLOSE_BRACE |
578
|
1314 { $$ = new plot_range (); } |
1
|
1315 ; |
|
1316 |
3180
|
1317 plot_command1 : plot_command2 |
578
|
1318 { $$ = new subplot_list ($1); } |
1
|
1319 | plot_command1 ',' plot_command2 |
1829
|
1320 { |
|
1321 $1->append ($3); |
|
1322 $$ = $1; |
|
1323 } |
1
|
1324 ; |
|
1325 |
|
1326 plot_command2 : expression |
578
|
1327 { $$ = new subplot ($1); } |
1
|
1328 | expression plot_options |
3165
|
1329 { $$ = $2->add_data ($1); } |
1
|
1330 ; |
|
1331 |
|
1332 plot_options : using |
3165
|
1333 { |
|
1334 subplot *tmp = new subplot (); |
|
1335 $$ = tmp->add_clause ($1); |
|
1336 } |
1
|
1337 | title |
3165
|
1338 { |
|
1339 subplot *tmp = new subplot (); |
|
1340 $$ = tmp->add_clause ($1); |
|
1341 } |
1
|
1342 | style |
3165
|
1343 { |
|
1344 subplot *tmp = new subplot (); |
|
1345 $$ = tmp->add_clause ($1); |
|
1346 } |
|
1347 | axes |
|
1348 { |
|
1349 subplot *tmp = new subplot (); |
|
1350 $$ = tmp->add_clause ($1); |
|
1351 } |
|
1352 | plot_options using |
|
1353 { |
|
1354 if (! ($$ = $1->add_clause ($2))) |
|
1355 { |
|
1356 yyerror ("only one using option may be specified"); |
|
1357 ABORT_PARSE; |
|
1358 } |
|
1359 } |
|
1360 | plot_options title |
|
1361 { |
|
1362 if (! ($$ = $1->add_clause ($2))) |
|
1363 { |
|
1364 yyerror ("only one title option my be specified"); |
|
1365 ABORT_PARSE; |
|
1366 } |
|
1367 } |
|
1368 | plot_options style |
|
1369 { |
|
1370 if (! ($$ = $1->add_clause ($2))) |
|
1371 { |
|
1372 yyerror ("only one style option my be specified"); |
|
1373 ABORT_PARSE; |
|
1374 } |
|
1375 } |
|
1376 | plot_options axes |
|
1377 { |
|
1378 if (! ($$ = $1->add_clause ($2))) |
|
1379 { |
|
1380 yyerror ("only one axes option may be specified"); |
|
1381 ABORT_PARSE; |
|
1382 } |
|
1383 } |
|
1384 ; |
|
1385 |
|
1386 axes : AXES AXES_TAG |
|
1387 { |
|
1388 lexer_flags.in_plot_axes = false; |
|
1389 $$ = new subplot_axes ($2->text ()); |
|
1390 } |
1
|
1391 ; |
|
1392 |
|
1393 using : using1 |
|
1394 { |
2857
|
1395 lexer_flags.in_plot_using = false; |
1
|
1396 $$ = $1; |
|
1397 } |
|
1398 | using1 expression |
|
1399 { |
2857
|
1400 lexer_flags.in_plot_using = false; |
1
|
1401 $$ = $1->set_format ($2); |
|
1402 } |
|
1403 ; |
|
1404 |
|
1405 using1 : USING expression |
|
1406 { |
578
|
1407 subplot_using *tmp = new subplot_using (); |
1
|
1408 $$ = tmp->add_qualifier ($2); |
|
1409 } |
|
1410 | using1 COLON expression |
|
1411 { $$ = $1->add_qualifier ($3); } |
|
1412 ; |
|
1413 |
|
1414 title : TITLE expression |
|
1415 { $$ = $2; } |
|
1416 ; |
|
1417 |
|
1418 style : WITH STYLE |
1755
|
1419 { $$ = new subplot_style ($2->text ()); } |
1
|
1420 | WITH STYLE expression |
1755
|
1421 { $$ = new subplot_style ($2->text (), $3); } |
2542
|
1422 | WITH STYLE expression expression |
|
1423 { $$ = new subplot_style ($2->text (), $3, $4); } |
1
|
1424 ; |
|
1425 |
2970
|
1426 // ============= |
|
1427 // Miscellaneous |
|
1428 // ============= |
|
1429 |
3665
|
1430 stash_comment : // empty |
|
1431 { $$ = octave_comment_buffer::get_comment (); } |
|
1432 ; |
|
1433 |
2970
|
1434 parse_error : LEXICAL_ERROR |
|
1435 { yyerror ("parse error"); } |
|
1436 | error |
1
|
1437 ; |
|
1438 |
2525
|
1439 sep_no_nl : ',' |
|
1440 { $$ = ','; } |
|
1441 | ';' |
|
1442 { $$ = ';'; } |
|
1443 | sep_no_nl ',' |
|
1444 { $$ = $1; } |
|
1445 | sep_no_nl ';' |
|
1446 { $$ = $1; } |
|
1447 ; |
|
1448 |
|
1449 opt_sep_no_nl : // empty |
|
1450 { $$ = 0; } |
|
1451 | sep_no_nl |
|
1452 { $$ = $1; } |
|
1453 ; |
|
1454 |
|
1455 sep : ',' |
|
1456 { $$ = ','; } |
|
1457 | ';' |
|
1458 { $$ = ';'; } |
|
1459 | '\n' |
|
1460 { $$ = '\n'; } |
|
1461 | sep ',' |
|
1462 { $$ = $1; } |
|
1463 | sep ';' |
|
1464 { $$ = $1; } |
|
1465 | sep '\n' |
|
1466 { $$ = $1; } |
|
1467 ; |
|
1468 |
|
1469 opt_sep : // empty |
|
1470 { $$ = 0; } |
|
1471 | sep |
|
1472 { $$ = $1; } |
|
1473 ; |
|
1474 |
1
|
1475 %% |
|
1476 |
666
|
1477 // Generic error messages. |
|
1478 |
1
|
1479 static void |
2805
|
1480 yyerror (const char *s) |
1
|
1481 { |
143
|
1482 int err_col = current_input_column - 1; |
1
|
1483 |
3523
|
1484 std::ostrstream output_buf; |
1
|
1485 |
1005
|
1486 if (reading_fcn_file || reading_script_file) |
|
1487 output_buf << "parse error near line " << input_line_number |
1607
|
1488 << " of file " << curr_fcn_file_full_name; |
1005
|
1489 else |
|
1490 output_buf << "parse error:"; |
581
|
1491 |
1005
|
1492 if (s && strcmp (s, "parse error") != 0) |
|
1493 output_buf << "\n\n " << s; |
|
1494 |
|
1495 output_buf << "\n\n"; |
1
|
1496 |
1755
|
1497 if (! current_input_line.empty ()) |
1
|
1498 { |
1755
|
1499 size_t len = current_input_line.length (); |
1060
|
1500 |
1755
|
1501 if (current_input_line[len-1] == '\n') |
|
1502 current_input_line.resize (len-1); |
1005
|
1503 |
335
|
1504 // Print the line, maybe with a pointer near the error token. |
1005
|
1505 |
1755
|
1506 output_buf << ">>> " << current_input_line << "\n"; |
1060
|
1507 |
|
1508 if (err_col == 0) |
|
1509 err_col = len; |
|
1510 |
|
1511 for (int i = 0; i < err_col + 3; i++) |
|
1512 output_buf << " "; |
|
1513 |
|
1514 output_buf << "^"; |
1
|
1515 } |
1005
|
1516 |
3538
|
1517 output_buf << "\n" << std::ends; |
581
|
1518 |
1005
|
1519 char *msg = output_buf.str (); |
|
1520 |
1090
|
1521 parse_error ("%s", msg); |
1005
|
1522 |
|
1523 delete [] msg; |
1
|
1524 } |
|
1525 |
666
|
1526 // Error mesages for mismatched end tokens. |
|
1527 |
496
|
1528 static void |
2805
|
1529 end_error (const char *type, token::end_tok_type ettype, int l, int c) |
496
|
1530 { |
2805
|
1531 static const char *fmt |
|
1532 = "`%s' command matched by `%s' near line %d column %d"; |
496
|
1533 |
|
1534 switch (ettype) |
|
1535 { |
|
1536 case token::simple_end: |
|
1537 error (fmt, type, "end", l, c); |
|
1538 break; |
777
|
1539 |
496
|
1540 case token::for_end: |
|
1541 error (fmt, type, "endfor", l, c); |
|
1542 break; |
777
|
1543 |
496
|
1544 case token::function_end: |
|
1545 error (fmt, type, "endfunction", l, c); |
|
1546 break; |
777
|
1547 |
496
|
1548 case token::if_end: |
|
1549 error (fmt, type, "endif", l, c); |
|
1550 break; |
777
|
1551 |
3233
|
1552 case token::switch_end: |
|
1553 error (fmt, type, "endswitch", l, c); |
|
1554 break; |
|
1555 |
496
|
1556 case token::while_end: |
|
1557 error (fmt, type, "endwhile", l, c); |
|
1558 break; |
777
|
1559 |
1371
|
1560 case token::unwind_protect_end: |
|
1561 error (fmt, type, "end_unwind_protect", l, c); |
|
1562 break; |
|
1563 |
496
|
1564 default: |
|
1565 panic_impossible (); |
|
1566 break; |
|
1567 } |
|
1568 } |
|
1569 |
666
|
1570 // Check to see that end tokens are properly matched. |
|
1571 |
2857
|
1572 static bool |
|
1573 end_token_ok (token *tok, token::end_tok_type expected) |
143
|
1574 { |
2857
|
1575 bool retval = true; |
|
1576 |
143
|
1577 token::end_tok_type ettype = tok->ettype (); |
2857
|
1578 |
143
|
1579 if (ettype != expected && ettype != token::simple_end) |
|
1580 { |
2857
|
1581 retval = false; |
|
1582 |
143
|
1583 yyerror ("parse error"); |
|
1584 |
|
1585 int l = tok->line (); |
|
1586 int c = tok->column (); |
|
1587 |
|
1588 switch (expected) |
|
1589 { |
|
1590 case token::for_end: |
|
1591 end_error ("for", ettype, l, c); |
|
1592 break; |
777
|
1593 |
143
|
1594 case token::function_end: |
|
1595 end_error ("function", ettype, l, c); |
|
1596 break; |
777
|
1597 |
143
|
1598 case token::if_end: |
|
1599 end_error ("if", ettype, l, c); |
|
1600 break; |
777
|
1601 |
1489
|
1602 case token::try_catch_end: |
|
1603 end_error ("try", ettype, l, c); |
|
1604 break; |
|
1605 |
2764
|
1606 case token::switch_end: |
|
1607 end_error ("switch", ettype, l, c); |
|
1608 break; |
|
1609 |
1489
|
1610 case token::unwind_protect_end: |
|
1611 end_error ("unwind_protect", ettype, l, c); |
|
1612 break; |
|
1613 |
143
|
1614 case token::while_end: |
|
1615 end_error ("while", ettype, l, c); |
|
1616 break; |
777
|
1617 |
143
|
1618 default: |
|
1619 panic_impossible (); |
|
1620 break; |
|
1621 } |
|
1622 } |
2857
|
1623 |
|
1624 return retval; |
143
|
1625 } |
|
1626 |
666
|
1627 // Maybe print a warning if an assignment expression is used as the |
|
1628 // test in a logical expression. |
|
1629 |
496
|
1630 static void |
|
1631 maybe_warn_assign_as_truth_value (tree_expression *expr) |
1
|
1632 { |
2166
|
1633 if (Vwarn_assign_as_truth_value |
1
|
1634 && expr->is_assignment_expression () |
2961
|
1635 && expr->paren_count () < 2) |
1
|
1636 { |
|
1637 warning ("suggest parenthesis around assignment used as truth value"); |
|
1638 } |
|
1639 } |
578
|
1640 |
2764
|
1641 // Maybe print a warning about switch labels that aren't constants. |
|
1642 |
|
1643 static void |
|
1644 maybe_warn_variable_switch_label (tree_expression *expr) |
|
1645 { |
|
1646 if (Vwarn_variable_switch_label && ! expr->is_constant ()) |
|
1647 { |
|
1648 warning ("variable switch label"); |
|
1649 } |
|
1650 } |
|
1651 |
1623
|
1652 // Create a plot command. |
|
1653 |
|
1654 static tree_plot_command * |
|
1655 make_plot_command (token *tok, plot_limits *range, subplot_list *list) |
|
1656 { |
|
1657 if (range) |
|
1658 { |
|
1659 if (tok->pttype () == token::replot) |
|
1660 { |
|
1661 yyerror ("cannot specify new ranges with replot"); |
|
1662 return 0; |
|
1663 } |
|
1664 } |
|
1665 else if (! list && tok->pttype () != token::replot) |
|
1666 { |
|
1667 yyerror ("must have something to plot"); |
|
1668 return 0; |
|
1669 } |
|
1670 |
2857
|
1671 lexer_flags.plotting = false; |
|
1672 lexer_flags.past_plot_range = false; |
|
1673 lexer_flags.in_plot_range = false; |
|
1674 lexer_flags.in_plot_using = false; |
|
1675 lexer_flags.in_plot_style = false; |
1623
|
1676 |
|
1677 return new tree_plot_command (list, range, tok->pttype ()); |
|
1678 } |
|
1679 |
2533
|
1680 static tree_expression * |
|
1681 fold (tree_binary_expression *e) |
|
1682 { |
3110
|
1683 tree_expression *retval = e; |
|
1684 |
3292
|
1685 unwind_protect::begin_frame ("fold_binary_expression"); |
3110
|
1686 |
|
1687 unwind_protect_int (error_state); |
|
1688 |
3815
|
1689 unwind_protect_bool (discard_error_messages); |
|
1690 discard_error_messages = true; |
2533
|
1691 |
|
1692 tree_expression *op1 = e->lhs (); |
|
1693 tree_expression *op2 = e->rhs (); |
|
1694 |
|
1695 if (op1->is_constant () && op2->is_constant ()) |
|
1696 { |
2970
|
1697 octave_value tmp = e->rvalue (); |
2533
|
1698 |
3489
|
1699 if (! (error_state || warning_state)) |
2533
|
1700 { |
|
1701 tree_constant *tc_retval = new tree_constant (tmp); |
|
1702 |
3523
|
1703 std::ostrstream buf; |
2533
|
1704 |
|
1705 tree_print_code tpc (buf); |
|
1706 |
|
1707 e->accept (tpc); |
|
1708 |
3538
|
1709 buf << std::ends; |
2533
|
1710 |
|
1711 char *s = buf.str (); |
|
1712 |
|
1713 tc_retval->stash_original_text (s); |
|
1714 |
|
1715 delete [] s; |
|
1716 |
|
1717 delete e; |
|
1718 |
|
1719 retval = tc_retval; |
|
1720 } |
|
1721 } |
3110
|
1722 |
3292
|
1723 unwind_protect::run_frame ("fold_binary_expression"); |
|
1724 |
|
1725 return retval; |
|
1726 } |
|
1727 |
|
1728 static tree_expression * |
|
1729 fold (tree_unary_expression *e) |
|
1730 { |
|
1731 tree_expression *retval = e; |
|
1732 |
|
1733 unwind_protect::begin_frame ("fold_unary_expression"); |
|
1734 |
|
1735 unwind_protect_int (error_state); |
|
1736 |
3815
|
1737 unwind_protect_bool (discard_error_messages); |
|
1738 discard_error_messages = true; |
3292
|
1739 |
|
1740 tree_expression *op = e->operand (); |
|
1741 |
|
1742 if (op->is_constant ()) |
|
1743 { |
|
1744 octave_value tmp = e->rvalue (); |
|
1745 |
3489
|
1746 if (! (error_state || warning_state)) |
3292
|
1747 { |
|
1748 tree_constant *tc_retval = new tree_constant (tmp); |
|
1749 |
3523
|
1750 std::ostrstream buf; |
3292
|
1751 |
|
1752 tree_print_code tpc (buf); |
|
1753 |
|
1754 e->accept (tpc); |
|
1755 |
3538
|
1756 buf << std::ends; |
3292
|
1757 |
|
1758 char *s = buf.str (); |
|
1759 |
|
1760 tc_retval->stash_original_text (s); |
|
1761 |
|
1762 delete [] s; |
|
1763 |
|
1764 delete e; |
|
1765 |
|
1766 retval = tc_retval; |
|
1767 } |
|
1768 } |
|
1769 |
|
1770 unwind_protect::run_frame ("fold_unary_expression"); |
2533
|
1771 |
|
1772 return retval; |
|
1773 } |
|
1774 |
1623
|
1775 // Finish building a range. |
|
1776 |
|
1777 static tree_expression * |
|
1778 finish_colon_expression (tree_colon_expression *e) |
|
1779 { |
3110
|
1780 tree_expression *retval = e; |
|
1781 |
|
1782 unwind_protect::begin_frame ("finish_colon_expression"); |
|
1783 |
|
1784 unwind_protect_int (error_state); |
|
1785 |
3815
|
1786 unwind_protect_bool (discard_error_messages); |
|
1787 discard_error_messages = true; |
1623
|
1788 |
2533
|
1789 tree_expression *base = e->base (); |
|
1790 tree_expression *limit = e->limit (); |
|
1791 tree_expression *incr = e->increment (); |
|
1792 |
2970
|
1793 if (base) |
1623
|
1794 { |
2970
|
1795 if (limit) |
2533
|
1796 { |
2970
|
1797 if (base->is_constant () && limit->is_constant () |
|
1798 && (! incr || (incr && incr->is_constant ()))) |
|
1799 { |
|
1800 octave_value tmp = e->rvalue (); |
|
1801 |
3489
|
1802 if (! (error_state || warning_state)) |
2970
|
1803 { |
|
1804 tree_constant *tc_retval = new tree_constant (tmp); |
|
1805 |
3523
|
1806 std::ostrstream buf; |
2970
|
1807 |
|
1808 tree_print_code tpc (buf); |
|
1809 |
|
1810 e->accept (tpc); |
|
1811 |
3538
|
1812 buf << std::ends; |
2970
|
1813 |
|
1814 char *s = buf.str (); |
|
1815 |
|
1816 tc_retval->stash_original_text (s); |
|
1817 |
|
1818 delete [] s; |
|
1819 |
|
1820 delete e; |
|
1821 |
|
1822 retval = tc_retval; |
|
1823 } |
|
1824 } |
2533
|
1825 } |
|
1826 else |
2970
|
1827 { |
2990
|
1828 e->preserve_base (); |
|
1829 delete e; |
2970
|
1830 |
|
1831 // XXX FIXME XXX -- need to attempt constant folding here |
|
1832 // too (we need a generic way to do that). |
|
1833 retval = base; |
|
1834 } |
1623
|
1835 } |
|
1836 |
3110
|
1837 unwind_protect::run_frame ("finish_colon_expression"); |
|
1838 |
1623
|
1839 return retval; |
|
1840 } |
|
1841 |
1607
|
1842 // Make a constant. |
|
1843 |
2375
|
1844 static tree_constant * |
1607
|
1845 make_constant (int op, token *tok_val) |
|
1846 { |
|
1847 int l = tok_val->line (); |
|
1848 int c = tok_val->column (); |
|
1849 |
3216
|
1850 tree_constant *retval = 0; |
1607
|
1851 |
|
1852 switch (op) |
|
1853 { |
|
1854 case NUM: |
2533
|
1855 { |
2883
|
1856 octave_value tmp (tok_val->number ()); |
|
1857 retval = new tree_constant (tmp, l, c); |
2533
|
1858 retval->stash_original_text (tok_val->text_rep ()); |
|
1859 } |
1607
|
1860 break; |
|
1861 |
|
1862 case IMAG_NUM: |
|
1863 { |
2883
|
1864 octave_value tmp (Complex (0.0, tok_val->number ())); |
|
1865 retval = new tree_constant (tmp, l, c); |
1607
|
1866 retval->stash_original_text (tok_val->text_rep ()); |
|
1867 } |
|
1868 break; |
|
1869 |
|
1870 case TEXT: |
2883
|
1871 { |
|
1872 octave_value tmp (tok_val->text ()); |
|
1873 retval = new tree_constant (tmp, l, c); |
|
1874 } |
1607
|
1875 break; |
|
1876 |
|
1877 default: |
|
1878 panic_impossible (); |
|
1879 break; |
|
1880 } |
|
1881 |
|
1882 return retval; |
|
1883 } |
|
1884 |
666
|
1885 // Build a binary expression. |
|
1886 |
578
|
1887 static tree_expression * |
|
1888 make_binary_op (int op, tree_expression *op1, token *tok_val, |
|
1889 tree_expression *op2) |
|
1890 { |
2883
|
1891 octave_value::binary_op t = octave_value::unknown_binary_op; |
1623
|
1892 |
578
|
1893 switch (op) |
|
1894 { |
|
1895 case POW: |
3525
|
1896 t = octave_value::op_pow; |
578
|
1897 break; |
777
|
1898 |
578
|
1899 case EPOW: |
3525
|
1900 t = octave_value::op_el_pow; |
578
|
1901 break; |
777
|
1902 |
578
|
1903 case '+': |
3525
|
1904 t = octave_value::op_add; |
578
|
1905 break; |
777
|
1906 |
578
|
1907 case '-': |
3525
|
1908 t = octave_value::op_sub; |
578
|
1909 break; |
777
|
1910 |
578
|
1911 case '*': |
3525
|
1912 t = octave_value::op_mul; |
578
|
1913 break; |
777
|
1914 |
578
|
1915 case '/': |
3525
|
1916 t = octave_value::op_div; |
578
|
1917 break; |
777
|
1918 |
578
|
1919 case EMUL: |
3525
|
1920 t = octave_value::op_el_mul; |
578
|
1921 break; |
777
|
1922 |
578
|
1923 case EDIV: |
3525
|
1924 t = octave_value::op_el_div; |
578
|
1925 break; |
777
|
1926 |
578
|
1927 case LEFTDIV: |
3525
|
1928 t = octave_value::op_ldiv; |
578
|
1929 break; |
777
|
1930 |
578
|
1931 case ELEFTDIV: |
3525
|
1932 t = octave_value::op_el_ldiv; |
578
|
1933 break; |
777
|
1934 |
2899
|
1935 case LSHIFT: |
3525
|
1936 t = octave_value::op_lshift; |
2899
|
1937 break; |
|
1938 |
|
1939 case RSHIFT: |
3525
|
1940 t = octave_value::op_rshift; |
2899
|
1941 break; |
|
1942 |
578
|
1943 case EXPR_LT: |
3525
|
1944 t = octave_value::op_lt; |
578
|
1945 break; |
777
|
1946 |
578
|
1947 case EXPR_LE: |
3525
|
1948 t = octave_value::op_le; |
578
|
1949 break; |
777
|
1950 |
578
|
1951 case EXPR_EQ: |
3525
|
1952 t = octave_value::op_eq; |
578
|
1953 break; |
777
|
1954 |
578
|
1955 case EXPR_GE: |
3525
|
1956 t = octave_value::op_ge; |
578
|
1957 break; |
777
|
1958 |
578
|
1959 case EXPR_GT: |
3525
|
1960 t = octave_value::op_gt; |
578
|
1961 break; |
777
|
1962 |
578
|
1963 case EXPR_NE: |
3525
|
1964 t = octave_value::op_ne; |
578
|
1965 break; |
777
|
1966 |
578
|
1967 case EXPR_AND: |
3525
|
1968 t = octave_value::op_el_and; |
578
|
1969 break; |
777
|
1970 |
578
|
1971 case EXPR_OR: |
3525
|
1972 t = octave_value::op_el_or; |
4023
|
1973 if (Vwarn_precedence_change |
4024
|
1974 && op2->paren_count () == 0 && op2->is_binary_expression ()) |
4023
|
1975 { |
|
1976 tree_binary_expression *e |
|
1977 = dynamic_cast<tree_binary_expression *> (op2); |
|
1978 |
|
1979 if (e->op_type () == octave_value::op_el_and) |
|
1980 warning ("meaning may have changed due to change in precedence for & and | operators"); |
|
1981 } |
578
|
1982 break; |
777
|
1983 |
578
|
1984 default: |
|
1985 panic_impossible (); |
|
1986 break; |
|
1987 } |
|
1988 |
|
1989 int l = tok_val->line (); |
|
1990 int c = tok_val->column (); |
|
1991 |
2533
|
1992 tree_binary_expression *e |
|
1993 = new tree_binary_expression (op1, op2, l, c, t); |
1623
|
1994 |
2533
|
1995 return fold (e); |
578
|
1996 } |
|
1997 |
2375
|
1998 // Build a boolean expression. |
666
|
1999 |
578
|
2000 static tree_expression * |
2375
|
2001 make_boolean_op (int op, tree_expression *op1, token *tok_val, |
|
2002 tree_expression *op2) |
578
|
2003 { |
2375
|
2004 tree_boolean_expression::type t; |
|
2005 |
578
|
2006 switch (op) |
|
2007 { |
2375
|
2008 case EXPR_AND_AND: |
2805
|
2009 t = tree_boolean_expression::bool_and; |
578
|
2010 break; |
777
|
2011 |
2375
|
2012 case EXPR_OR_OR: |
2805
|
2013 t = tree_boolean_expression::bool_or; |
4023
|
2014 if (Vwarn_precedence_change |
4024
|
2015 && op2->paren_count () == 0 && op2->is_boolean_expression ()) |
4023
|
2016 { |
|
2017 tree_boolean_expression *e |
|
2018 = dynamic_cast<tree_boolean_expression *> (op2); |
|
2019 |
|
2020 if (e->op_type () == tree_boolean_expression::bool_and) |
|
2021 warning ("meaning may have changed due to change in precedence for && and || operators"); |
|
2022 } |
578
|
2023 break; |
777
|
2024 |
578
|
2025 default: |
|
2026 panic_impossible (); |
|
2027 break; |
|
2028 } |
|
2029 |
|
2030 int l = tok_val->line (); |
|
2031 int c = tok_val->column (); |
|
2032 |
2533
|
2033 tree_boolean_expression *e |
|
2034 = new tree_boolean_expression (op1, op2, l, c, t); |
2375
|
2035 |
2533
|
2036 return fold (e); |
578
|
2037 } |
|
2038 |
2375
|
2039 // Build a prefix expression. |
666
|
2040 |
578
|
2041 static tree_expression * |
2960
|
2042 make_prefix_op (int op, tree_expression *op1, token *tok_val) |
578
|
2043 { |
3203
|
2044 octave_value::unary_op t = octave_value::unknown_unary_op; |
2375
|
2045 |
578
|
2046 switch (op) |
|
2047 { |
2960
|
2048 case EXPR_NOT: |
3525
|
2049 t = octave_value::op_not; |
2960
|
2050 break; |
|
2051 |
|
2052 case '-': |
3525
|
2053 t = octave_value::op_uminus; |
2960
|
2054 break; |
|
2055 |
578
|
2056 case PLUS_PLUS: |
3525
|
2057 t = octave_value::op_incr; |
578
|
2058 break; |
777
|
2059 |
578
|
2060 case MINUS_MINUS: |
3525
|
2061 t = octave_value::op_decr; |
578
|
2062 break; |
777
|
2063 |
578
|
2064 default: |
|
2065 panic_impossible (); |
|
2066 break; |
|
2067 } |
|
2068 |
|
2069 int l = tok_val->line (); |
|
2070 int c = tok_val->column (); |
|
2071 |
3292
|
2072 tree_prefix_expression *e |
|
2073 = new tree_prefix_expression (op1, l, c, t); |
|
2074 |
|
2075 return fold (e); |
578
|
2076 } |
|
2077 |
2375
|
2078 // Build a postfix expression. |
666
|
2079 |
578
|
2080 static tree_expression * |
2960
|
2081 make_postfix_op (int op, tree_expression *op1, token *tok_val) |
578
|
2082 { |
3203
|
2083 octave_value::unary_op t = octave_value::unknown_unary_op; |
1623
|
2084 |
578
|
2085 switch (op) |
|
2086 { |
2960
|
2087 case QUOTE: |
3525
|
2088 t = octave_value::op_hermitian; |
2960
|
2089 break; |
|
2090 |
|
2091 case TRANSPOSE: |
3525
|
2092 t = octave_value::op_transpose; |
2960
|
2093 break; |
|
2094 |
2375
|
2095 case PLUS_PLUS: |
3525
|
2096 t = octave_value::op_incr; |
578
|
2097 break; |
777
|
2098 |
2375
|
2099 case MINUS_MINUS: |
3525
|
2100 t = octave_value::op_decr; |
578
|
2101 break; |
777
|
2102 |
578
|
2103 default: |
|
2104 panic_impossible (); |
|
2105 break; |
|
2106 } |
|
2107 |
|
2108 int l = tok_val->line (); |
|
2109 int c = tok_val->column (); |
|
2110 |
3292
|
2111 tree_postfix_expression *e |
|
2112 = new tree_postfix_expression (op1, l, c, t); |
|
2113 |
|
2114 return fold (e); |
1623
|
2115 } |
|
2116 |
|
2117 // Build an unwind-protect command. |
|
2118 |
|
2119 static tree_command * |
|
2120 make_unwind_command (token *unwind_tok, tree_statement_list *body, |
3665
|
2121 tree_statement_list *cleanup, token *end_tok, |
|
2122 octave_comment_list *lc, octave_comment_list *mc) |
1623
|
2123 { |
|
2124 tree_command *retval = 0; |
|
2125 |
2857
|
2126 if (end_token_ok (end_tok, token::unwind_protect_end)) |
1623
|
2127 { |
3665
|
2128 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2129 |
1623
|
2130 int l = unwind_tok->line (); |
|
2131 int c = unwind_tok->column (); |
|
2132 |
3665
|
2133 retval = new tree_unwind_protect_command (body, cleanup, |
|
2134 lc, mc, tc, l, c); |
1623
|
2135 } |
|
2136 |
|
2137 return retval; |
|
2138 } |
|
2139 |
|
2140 // Build a try-catch command. |
|
2141 |
|
2142 static tree_command * |
|
2143 make_try_command (token *try_tok, tree_statement_list *body, |
3665
|
2144 tree_statement_list *cleanup, token *end_tok, |
|
2145 octave_comment_list *lc, octave_comment_list *mc) |
1623
|
2146 { |
|
2147 tree_command *retval = 0; |
|
2148 |
2857
|
2149 if (end_token_ok (end_tok, token::try_catch_end)) |
1623
|
2150 { |
3665
|
2151 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2152 |
1623
|
2153 int l = try_tok->line (); |
|
2154 int c = try_tok->column (); |
|
2155 |
3665
|
2156 retval = new tree_try_catch_command (body, cleanup, |
|
2157 lc, mc, tc, l, c); |
1623
|
2158 } |
|
2159 |
|
2160 return retval; |
|
2161 } |
|
2162 |
|
2163 // Build a while command. |
|
2164 |
|
2165 static tree_command * |
|
2166 make_while_command (token *while_tok, tree_expression *expr, |
3665
|
2167 tree_statement_list *body, token *end_tok, |
|
2168 octave_comment_list *lc) |
1623
|
2169 { |
|
2170 tree_command *retval = 0; |
|
2171 |
|
2172 maybe_warn_assign_as_truth_value (expr); |
|
2173 |
2857
|
2174 if (end_token_ok (end_tok, token::while_end)) |
1623
|
2175 { |
3665
|
2176 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2177 |
1826
|
2178 lexer_flags.looping--; |
1623
|
2179 |
|
2180 int l = while_tok->line (); |
|
2181 int c = while_tok->column (); |
|
2182 |
3665
|
2183 retval = new tree_while_command (expr, body, lc, tc, l, c); |
1623
|
2184 } |
|
2185 |
|
2186 return retval; |
|
2187 } |
|
2188 |
3484
|
2189 // Build a do-until command. |
|
2190 |
|
2191 static tree_command * |
|
2192 make_do_until_command (token *do_tok, tree_statement_list *body, |
3665
|
2193 tree_expression *expr, octave_comment_list *lc) |
3484
|
2194 { |
|
2195 tree_command *retval = 0; |
|
2196 |
|
2197 maybe_warn_assign_as_truth_value (expr); |
|
2198 |
3665
|
2199 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2200 |
3484
|
2201 lexer_flags.looping--; |
|
2202 |
|
2203 int l = do_tok->line (); |
|
2204 int c = do_tok->column (); |
|
2205 |
3665
|
2206 retval = new tree_do_until_command (expr, body, lc, tc, l, c); |
3484
|
2207 |
|
2208 return retval; |
|
2209 } |
|
2210 |
1623
|
2211 // Build a for command. |
|
2212 |
|
2213 static tree_command * |
2970
|
2214 make_for_command (token *for_tok, tree_argument_list *lhs, |
1623
|
2215 tree_expression *expr, tree_statement_list *body, |
3665
|
2216 token *end_tok, octave_comment_list *lc) |
1623
|
2217 { |
|
2218 tree_command *retval = 0; |
|
2219 |
2857
|
2220 if (end_token_ok (end_tok, token::for_end)) |
1623
|
2221 { |
3665
|
2222 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2223 |
1826
|
2224 lexer_flags.looping--; |
1623
|
2225 |
|
2226 int l = for_tok->line (); |
|
2227 int c = for_tok->column (); |
|
2228 |
2970
|
2229 if (lhs->length () == 1) |
|
2230 { |
|
2231 tree_expression *tmp = lhs->remove_front (); |
|
2232 |
3665
|
2233 retval = new tree_simple_for_command (tmp, expr, body, |
|
2234 lc, tc, l, c); |
2970
|
2235 |
|
2236 delete lhs; |
|
2237 } |
|
2238 else |
3665
|
2239 retval = new tree_complex_for_command (lhs, expr, body, |
|
2240 lc, tc, l, c); |
1623
|
2241 } |
|
2242 |
|
2243 return retval; |
|
2244 } |
|
2245 |
|
2246 // Build a break command. |
|
2247 |
|
2248 static tree_command * |
|
2249 make_break_command (token *break_tok) |
|
2250 { |
|
2251 tree_command *retval = 0; |
|
2252 |
2620
|
2253 int l = break_tok->line (); |
|
2254 int c = break_tok->column (); |
|
2255 |
3489
|
2256 if (lexer_flags.looping || lexer_flags.defining_func |
3877
|
2257 || reading_script_file || evaluating_function_body |
|
2258 || evaluating_looping_command) |
2620
|
2259 retval = new tree_break_command (l, c); |
1623
|
2260 else |
2620
|
2261 retval = new tree_no_op_command ("break", l, c); |
1623
|
2262 |
|
2263 return retval; |
|
2264 } |
|
2265 |
|
2266 // Build a continue command. |
|
2267 |
|
2268 static tree_command * |
|
2269 make_continue_command (token *continue_tok) |
|
2270 { |
|
2271 tree_command *retval = 0; |
|
2272 |
2620
|
2273 int l = continue_tok->line (); |
|
2274 int c = continue_tok->column (); |
|
2275 |
3877
|
2276 if (lexer_flags.looping || evaluating_looping_command) |
2620
|
2277 retval = new tree_continue_command (l, c); |
1623
|
2278 else |
2620
|
2279 retval = new tree_no_op_command ("continue", l, c); |
1623
|
2280 |
|
2281 return retval; |
|
2282 } |
|
2283 |
|
2284 // Build a return command. |
|
2285 |
|
2286 static tree_command * |
|
2287 make_return_command (token *return_tok) |
|
2288 { |
|
2289 tree_command *retval = 0; |
|
2290 |
2620
|
2291 int l = return_tok->line (); |
|
2292 int c = return_tok->column (); |
|
2293 |
3489
|
2294 if (lexer_flags.defining_func || reading_script_file |
|
2295 || evaluating_function_body) |
2620
|
2296 retval = new tree_return_command (l, c); |
1623
|
2297 else |
2620
|
2298 retval = new tree_no_op_command ("return", l, c); |
1623
|
2299 |
|
2300 return retval; |
|
2301 } |
|
2302 |
|
2303 // Start an if command. |
|
2304 |
|
2305 static tree_if_command_list * |
|
2306 start_if_command (tree_expression *expr, tree_statement_list *list) |
|
2307 { |
|
2308 maybe_warn_assign_as_truth_value (expr); |
|
2309 |
|
2310 tree_if_clause *t = new tree_if_clause (expr, list); |
|
2311 |
|
2312 return new tree_if_command_list (t); |
|
2313 } |
|
2314 |
|
2315 // Finish an if command. |
|
2316 |
|
2317 static tree_if_command * |
|
2318 finish_if_command (token *if_tok, tree_if_command_list *list, |
3665
|
2319 token *end_tok, octave_comment_list *lc) |
1623
|
2320 { |
|
2321 tree_if_command *retval = 0; |
|
2322 |
2857
|
2323 if (end_token_ok (end_tok, token::if_end)) |
1623
|
2324 { |
3665
|
2325 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2326 |
1623
|
2327 int l = if_tok->line (); |
|
2328 int c = if_tok->column (); |
|
2329 |
3665
|
2330 retval = new tree_if_command (list, lc, tc, l, c); |
1623
|
2331 } |
|
2332 |
|
2333 return retval; |
|
2334 } |
|
2335 |
|
2336 // Build an elseif clause. |
|
2337 |
|
2338 static tree_if_clause * |
3665
|
2339 make_elseif_clause (tree_expression *expr, tree_statement_list *list, |
|
2340 octave_comment_list *lc) |
1623
|
2341 { |
|
2342 maybe_warn_assign_as_truth_value (expr); |
|
2343 |
3665
|
2344 return new tree_if_clause (expr, list, lc); |
1623
|
2345 } |
|
2346 |
2764
|
2347 // Finish a switch command. |
|
2348 |
|
2349 static tree_switch_command * |
|
2350 finish_switch_command (token *switch_tok, tree_expression *expr, |
3665
|
2351 tree_switch_case_list *list, token *end_tok, |
|
2352 octave_comment_list *lc) |
2764
|
2353 { |
|
2354 tree_switch_command *retval = 0; |
|
2355 |
2857
|
2356 if (end_token_ok (end_tok, token::switch_end)) |
2764
|
2357 { |
3665
|
2358 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2359 |
2764
|
2360 int l = switch_tok->line (); |
|
2361 int c = switch_tok->column (); |
|
2362 |
3665
|
2363 retval = new tree_switch_command (expr, list, lc, tc, l, c); |
2764
|
2364 } |
|
2365 |
|
2366 return retval; |
|
2367 } |
|
2368 |
|
2369 // Build a switch case. |
|
2370 |
|
2371 static tree_switch_case * |
3665
|
2372 make_switch_case (tree_expression *expr, tree_statement_list *list, |
|
2373 octave_comment_list *lc) |
2764
|
2374 { |
|
2375 maybe_warn_variable_switch_label (expr); |
|
2376 |
3665
|
2377 return new tree_switch_case (expr, list, lc); |
2764
|
2378 } |
|
2379 |
1623
|
2380 // Build an assignment to a variable. |
|
2381 |
|
2382 static tree_expression * |
2970
|
2383 make_assign_op (int op, tree_argument_list *lhs, token *eq_tok, |
|
2384 tree_expression *rhs) |
1623
|
2385 { |
2970
|
2386 tree_expression *retval = 0; |
|
2387 |
2883
|
2388 octave_value::assign_op t = octave_value::unknown_assign_op; |
|
2389 |
|
2390 switch (op) |
|
2391 { |
|
2392 case '=': |
3525
|
2393 t = octave_value::op_asn_eq; |
2883
|
2394 break; |
|
2395 |
|
2396 case ADD_EQ: |
3525
|
2397 t = octave_value::op_add_eq; |
2883
|
2398 break; |
|
2399 |
|
2400 case SUB_EQ: |
3525
|
2401 t = octave_value::op_sub_eq; |
2883
|
2402 break; |
|
2403 |
|
2404 case MUL_EQ: |
3525
|
2405 t = octave_value::op_mul_eq; |
2883
|
2406 break; |
|
2407 |
|
2408 case DIV_EQ: |
3525
|
2409 t = octave_value::op_div_eq; |
2883
|
2410 break; |
|
2411 |
3204
|
2412 case LEFTDIV_EQ: |
3525
|
2413 t = octave_value::op_ldiv_eq; |
3204
|
2414 break; |
|
2415 |
4018
|
2416 case POW_EQ: |
|
2417 t = octave_value::op_pow_eq; |
|
2418 break; |
|
2419 |
2899
|
2420 case LSHIFT_EQ: |
3525
|
2421 t = octave_value::op_lshift_eq; |
2899
|
2422 break; |
|
2423 |
|
2424 case RSHIFT_EQ: |
3525
|
2425 t = octave_value::op_rshift_eq; |
2899
|
2426 break; |
|
2427 |
2883
|
2428 case EMUL_EQ: |
3525
|
2429 t = octave_value::op_el_mul_eq; |
2883
|
2430 break; |
|
2431 |
|
2432 case EDIV_EQ: |
3525
|
2433 t = octave_value::op_el_div_eq; |
2883
|
2434 break; |
|
2435 |
3204
|
2436 case ELEFTDIV_EQ: |
3525
|
2437 t = octave_value::op_el_ldiv_eq; |
3204
|
2438 break; |
|
2439 |
4018
|
2440 case EPOW_EQ: |
|
2441 t = octave_value::op_el_pow_eq; |
|
2442 break; |
|
2443 |
2883
|
2444 case AND_EQ: |
3525
|
2445 t = octave_value::op_el_and_eq; |
2883
|
2446 break; |
|
2447 |
|
2448 case OR_EQ: |
3525
|
2449 t = octave_value::op_el_or_eq; |
2883
|
2450 break; |
|
2451 |
|
2452 default: |
|
2453 panic_impossible (); |
|
2454 break; |
|
2455 } |
|
2456 |
1623
|
2457 int l = eq_tok->line (); |
|
2458 int c = eq_tok->column (); |
|
2459 |
2970
|
2460 if (lhs->length () == 1) |
666
|
2461 { |
2970
|
2462 tree_expression *tmp = lhs->remove_front (); |
|
2463 |
|
2464 retval = new tree_simple_assignment (tmp, rhs, false, l, c, t); |
|
2465 |
|
2466 delete lhs; |
666
|
2467 } |
|
2468 else |
3208
|
2469 return new tree_multi_assignment (lhs, rhs, false, l, c, t); |
666
|
2470 |
|
2471 return retval; |
|
2472 } |
751
|
2473 |
1623
|
2474 // Begin defining a function. |
|
2475 |
2891
|
2476 static octave_user_function * |
|
2477 start_function (tree_parameter_list *param_list, tree_statement_list *body) |
1623
|
2478 { |
|
2479 body->mark_as_function_body (); |
|
2480 |
2891
|
2481 // We'll fill in the return list later. |
|
2482 |
|
2483 octave_user_function *fcn |
|
2484 = new octave_user_function (param_list, 0, body, curr_sym_tab); |
1623
|
2485 |
3665
|
2486 if (fcn) |
|
2487 { |
|
2488 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
|
2489 |
|
2490 fcn->stash_trailing_comment (tc); |
|
2491 } |
|
2492 |
1623
|
2493 return fcn; |
|
2494 } |
|
2495 |
|
2496 // Do most of the work for defining a function. |
|
2497 |
2891
|
2498 static octave_user_function * |
|
2499 frob_function (tree_identifier *id, octave_user_function *fcn) |
1623
|
2500 { |
3523
|
2501 std::string id_name = id->name (); |
1623
|
2502 |
|
2503 // If input is coming from a file, issue a warning if the name of |
|
2504 // the file does not match the name of the function stated in the |
|
2505 // file. Matlab doesn't provide a diagnostic (it ignores the stated |
|
2506 // name). |
|
2507 |
|
2508 fcn->stash_function_name (id_name); |
|
2509 |
|
2510 if (reading_fcn_file) |
|
2511 { |
1755
|
2512 if (curr_fcn_file_name != id_name) |
1623
|
2513 { |
2166
|
2514 if (Vwarn_function_name_clash) |
1623
|
2515 warning ("function name `%s' does not agree with function\ |
1755
|
2516 file name `%s'", id_name.c_str (), curr_fcn_file_full_name.c_str ()); |
1623
|
2517 |
4009
|
2518 fbi_sym_tab->rename (id_name, curr_fcn_file_name); |
1623
|
2519 |
|
2520 if (error_state) |
|
2521 return 0; |
|
2522 |
|
2523 id_name = id->name (); |
|
2524 } |
|
2525 |
3712
|
2526 octave_time now; |
3162
|
2527 |
1623
|
2528 fcn->stash_function_name (id_name); |
|
2529 fcn->stash_fcn_file_name (); |
3162
|
2530 fcn->stash_fcn_file_time (now); |
1623
|
2531 fcn->mark_as_system_fcn_file (); |
3162
|
2532 |
|
2533 if (Vwarn_future_time_stamp) |
|
2534 { |
3523
|
2535 std::string nm = fcn->fcn_file_name (); |
3162
|
2536 |
|
2537 file_stat fs (nm); |
|
2538 |
|
2539 if (fs && fs.is_newer (now)) |
|
2540 warning ("time stamp for `%s' is in the future", nm.c_str ()); |
|
2541 } |
1623
|
2542 } |
|
2543 else if (! (input_from_tmp_history_file || input_from_startup_file) |
|
2544 && reading_script_file |
1755
|
2545 && curr_fcn_file_name == id_name) |
1623
|
2546 { |
|
2547 warning ("function `%s' defined within script file `%s'", |
1755
|
2548 id_name.c_str (), curr_fcn_file_full_name.c_str ()); |
1623
|
2549 } |
|
2550 |
|
2551 top_level_sym_tab->clear (id_name); |
|
2552 |
4009
|
2553 symbol_record *sr = fbi_sym_tab->lookup (id_name); |
2791
|
2554 |
|
2555 if (sr) |
|
2556 fcn->stash_symtab_ptr (sr); |
|
2557 else |
|
2558 panic_impossible (); |
|
2559 |
3016
|
2560 id->define (fcn, symbol_record::USER_FUNCTION); |
1755
|
2561 |
1623
|
2562 id->document (help_buf); |
|
2563 |
3844
|
2564 help_buf.resize (0); |
|
2565 |
1623
|
2566 return fcn; |
|
2567 } |
|
2568 |
|
2569 // Finish defining a function. |
|
2570 |
2891
|
2571 static octave_user_function * |
3665
|
2572 finish_function (tree_identifier *id, octave_user_function *fcn, |
|
2573 octave_comment_list *lc) |
1623
|
2574 { |
2883
|
2575 tree_parameter_list *tpl = new tree_parameter_list (id); |
1623
|
2576 |
|
2577 tpl->mark_as_formal_parameters (); |
|
2578 |
3665
|
2579 fcn->stash_leading_comment (lc); |
|
2580 |
1623
|
2581 return fcn->define_ret_list (tpl); |
|
2582 } |
|
2583 |
|
2584 // Finish defining a function a different way. |
|
2585 |
2891
|
2586 static octave_user_function * |
3665
|
2587 finish_function (tree_parameter_list *ret_list, |
|
2588 octave_user_function *fcn, octave_comment_list *lc) |
1623
|
2589 { |
|
2590 ret_list->mark_as_formal_parameters (); |
|
2591 |
3665
|
2592 fcn->stash_leading_comment (lc); |
|
2593 |
1623
|
2594 return fcn->define_ret_list (ret_list); |
|
2595 } |
|
2596 |
2883
|
2597 static void |
|
2598 recover_from_parsing_function (void) |
|
2599 { |
3903
|
2600 if (! symtab_context) |
|
2601 panic_impossible (); |
|
2602 |
|
2603 curr_sym_tab = symtab_context; |
|
2604 symtab_context = 0; |
2883
|
2605 |
|
2606 lexer_flags.defining_func = false; |
|
2607 lexer_flags.beginning_of_function = false; |
|
2608 lexer_flags.parsed_function_name = false; |
|
2609 lexer_flags.looking_at_return_list = false; |
|
2610 lexer_flags.looking_at_parameter_list = false; |
|
2611 } |
|
2612 |
2846
|
2613 // Make an index expression. |
|
2614 |
751
|
2615 static tree_index_expression * |
3929
|
2616 make_index_expression (tree_expression *expr, tree_argument_list *args, |
3933
|
2617 char type) |
751
|
2618 { |
|
2619 tree_index_expression *retval = 0; |
|
2620 |
2970
|
2621 int l = expr->line (); |
|
2622 int c = expr->column (); |
|
2623 |
|
2624 expr->mark_postfix_indexed (); |
|
2625 |
3933
|
2626 if (expr->is_index_expression ()) |
|
2627 { |
|
2628 tree_index_expression *tmp = static_cast<tree_index_expression *> (expr); |
|
2629 |
|
2630 tmp->append (args, type); |
|
2631 |
|
2632 retval = tmp; |
|
2633 } |
|
2634 else |
|
2635 retval = new tree_index_expression (expr, args, l, c, type); |
2970
|
2636 |
|
2637 return retval; |
|
2638 } |
|
2639 |
|
2640 // Make an indirect reference expression. |
|
2641 |
3930
|
2642 static tree_index_expression * |
3523
|
2643 make_indirect_ref (tree_expression *expr, const std::string& elt) |
2970
|
2644 { |
3930
|
2645 tree_index_expression *retval = 0; |
2970
|
2646 |
|
2647 int l = expr->line (); |
|
2648 int c = expr->column (); |
|
2649 |
3933
|
2650 if (expr->is_index_expression ()) |
|
2651 { |
|
2652 tree_index_expression *tmp = static_cast<tree_index_expression *> (expr); |
|
2653 |
|
2654 tmp->append (elt); |
|
2655 |
|
2656 retval = tmp; |
|
2657 } |
|
2658 else |
|
2659 retval = new tree_index_expression (expr, elt, l, c); |
2970
|
2660 |
|
2661 lexer_flags.looking_at_indirect_ref = false; |
751
|
2662 |
|
2663 return retval; |
|
2664 } |
1511
|
2665 |
2846
|
2666 // Make a declaration command. |
|
2667 |
|
2668 static tree_decl_command * |
|
2669 make_decl_command (int tok, token *tok_val, tree_decl_init_list *lst) |
|
2670 { |
|
2671 tree_decl_command *retval = 0; |
|
2672 |
|
2673 int l = tok_val->line (); |
|
2674 int c = tok_val->column (); |
|
2675 |
|
2676 switch (tok) |
|
2677 { |
|
2678 case GLOBAL: |
|
2679 retval = new tree_global_command (lst, l, c); |
|
2680 break; |
|
2681 |
|
2682 case STATIC: |
|
2683 if (lexer_flags.defining_func) |
|
2684 retval = new tree_static_command (lst, l, c); |
|
2685 else |
|
2686 { |
|
2687 if (reading_script_file) |
|
2688 warning ("ignoring static declaration near line %d of file `%s'", |
|
2689 l, curr_fcn_file_full_name.c_str ()); |
|
2690 else |
|
2691 warning ("ignoring static declaration near line %d", l); |
|
2692 } |
|
2693 break; |
|
2694 |
|
2695 default: |
|
2696 panic_impossible (); |
|
2697 break; |
|
2698 } |
|
2699 |
|
2700 return retval; |
|
2701 } |
|
2702 |
1623
|
2703 // Finish building a matrix list. |
|
2704 |
|
2705 static tree_expression * |
1829
|
2706 finish_matrix (tree_matrix *m) |
1623
|
2707 { |
3110
|
2708 tree_expression *retval = m; |
|
2709 |
|
2710 unwind_protect::begin_frame ("finish_matrix"); |
|
2711 |
|
2712 unwind_protect_int (error_state); |
|
2713 |
3815
|
2714 unwind_protect_bool (discard_error_messages); |
|
2715 discard_error_messages = true; |
1623
|
2716 |
2533
|
2717 if (m->all_elements_are_constant ()) |
1829
|
2718 { |
2970
|
2719 octave_value tmp = m->rvalue (); |
1623
|
2720 |
3489
|
2721 if (! (error_state || warning_state)) |
2533
|
2722 { |
|
2723 tree_constant *tc_retval = new tree_constant (tmp); |
|
2724 |
3523
|
2725 std::ostrstream buf; |
2533
|
2726 |
|
2727 tree_print_code tpc (buf); |
|
2728 |
|
2729 m->accept (tpc); |
|
2730 |
3538
|
2731 buf << std::ends; |
1623
|
2732 |
2533
|
2733 char *s = buf.str (); |
|
2734 |
|
2735 tc_retval->stash_original_text (s); |
|
2736 |
|
2737 delete [] s; |
|
2738 |
|
2739 delete m; |
|
2740 |
|
2741 retval = tc_retval; |
|
2742 } |
1623
|
2743 } |
3110
|
2744 |
|
2745 unwind_protect::run_frame ("finish_matrix"); |
1623
|
2746 |
|
2747 return retval; |
|
2748 } |
|
2749 |
3351
|
2750 // Finish building a cell list. |
|
2751 |
|
2752 static tree_expression * |
|
2753 finish_cell (tree_cell *c) |
|
2754 { |
|
2755 // For now, this doesn't do anything. |
|
2756 |
|
2757 return c; |
|
2758 } |
|
2759 |
1511
|
2760 static void |
|
2761 maybe_warn_missing_semi (tree_statement_list *t) |
|
2762 { |
2166
|
2763 if (lexer_flags.defining_func && Vwarn_missing_semicolon) |
1511
|
2764 { |
|
2765 tree_statement *tmp = t->rear(); |
1607
|
2766 |
1511
|
2767 if (tmp->is_expression ()) |
1607
|
2768 warning ("missing semicolon near line %d, column %d in file `%s'", |
1755
|
2769 tmp->line (), tmp->column (), |
|
2770 curr_fcn_file_full_name.c_str ()); |
1511
|
2771 } |
|
2772 } |
1994
|
2773 |
2525
|
2774 static void |
|
2775 set_stmt_print_flag (tree_statement_list *list, char sep, |
|
2776 bool warn_missing_semi) |
|
2777 { |
|
2778 switch (sep) |
|
2779 { |
|
2780 case ';': |
|
2781 { |
|
2782 tree_statement *tmp = list->rear (); |
|
2783 tmp->set_print_flag (0); |
|
2784 } |
|
2785 break; |
|
2786 |
|
2787 case 0: |
|
2788 case ',': |
|
2789 case '\n': |
|
2790 if (warn_missing_semi) |
|
2791 maybe_warn_missing_semi (list); |
|
2792 break; |
|
2793 |
|
2794 default: |
|
2795 warning ("unrecognized separator type!"); |
|
2796 break; |
|
2797 } |
|
2798 } |
|
2799 |
3021
|
2800 void |
|
2801 parse_and_execute (FILE *f) |
|
2802 { |
|
2803 unwind_protect::begin_frame ("parse_and_execute"); |
3880
|
2804 |
3021
|
2805 YY_BUFFER_STATE old_buf = current_buffer (); |
|
2806 YY_BUFFER_STATE new_buf = create_buffer (f); |
|
2807 |
|
2808 unwind_protect::add (restore_input_buffer, old_buf); |
|
2809 unwind_protect::add (delete_input_buffer, new_buf); |
|
2810 |
|
2811 switch_to_buffer (new_buf); |
|
2812 |
|
2813 unwind_protect_bool (line_editing); |
|
2814 unwind_protect_bool (input_from_command_line_file); |
3880
|
2815 unwind_protect_bool (get_input_from_eval_string); |
3883
|
2816 unwind_protect_bool (parser_end_of_input); |
3021
|
2817 |
|
2818 line_editing = false; |
|
2819 input_from_command_line_file = false; |
3880
|
2820 get_input_from_eval_string = false; |
3883
|
2821 parser_end_of_input = false; |
3021
|
2822 |
|
2823 unwind_protect_ptr (curr_sym_tab); |
|
2824 |
|
2825 int retval; |
|
2826 do |
|
2827 { |
|
2828 reset_parser (); |
|
2829 |
|
2830 retval = yyparse (); |
|
2831 |
3883
|
2832 if (retval == 0) |
|
2833 { |
|
2834 if (global_command) |
3021
|
2835 { |
3883
|
2836 global_command->eval (); |
|
2837 |
|
2838 delete global_command; |
|
2839 |
|
2840 global_command = 0; |
|
2841 |
|
2842 bool quit = (tree_return_command::returning |
|
2843 || tree_break_command::breaking); |
|
2844 |
|
2845 if (tree_return_command::returning) |
|
2846 tree_return_command::returning = 0; |
|
2847 |
|
2848 if (tree_break_command::breaking) |
|
2849 tree_break_command::breaking--; |
|
2850 |
|
2851 if (error_state) |
|
2852 { |
|
2853 error ("near line %d of file `%s'", input_line_number, |
|
2854 curr_fcn_file_full_name.c_str ()); |
|
2855 |
|
2856 break; |
|
2857 } |
|
2858 |
|
2859 if (quit) |
|
2860 break; |
3021
|
2861 } |
3883
|
2862 else if (parser_end_of_input) |
3021
|
2863 break; |
3883
|
2864 } |
3021
|
2865 } |
|
2866 while (retval == 0); |
|
2867 |
|
2868 unwind_protect::run_frame ("parse_and_execute"); |
|
2869 } |
|
2870 |
|
2871 static void |
|
2872 safe_fclose (void *f) |
|
2873 { |
3765
|
2874 // XXX FIXME XXX -- comments at the end of an input file are |
|
2875 // discarded (otherwise, they would be appended to the next |
|
2876 // statement, possibly from the command line or another file, which |
|
2877 // can be quite confusing). |
|
2878 |
|
2879 octave_comment_buffer::get_comment (); |
|
2880 |
3021
|
2881 if (f) |
|
2882 fclose (static_cast<FILE *> (f)); |
|
2883 } |
|
2884 |
|
2885 void |
3523
|
2886 parse_and_execute (const std::string& s, bool verbose, const char *warn_for) |
3021
|
2887 { |
|
2888 unwind_protect::begin_frame ("parse_and_execute_2"); |
|
2889 |
|
2890 unwind_protect_bool (reading_script_file); |
|
2891 unwind_protect_str (curr_fcn_file_full_name); |
|
2892 |
|
2893 reading_script_file = true; |
|
2894 curr_fcn_file_full_name = s; |
|
2895 |
|
2896 FILE *f = get_input_from_file (s, 0); |
|
2897 |
|
2898 if (f) |
|
2899 { |
|
2900 unwind_protect::add (safe_fclose, f); |
|
2901 |
|
2902 unwind_protect_int (input_line_number); |
|
2903 unwind_protect_int (current_input_column); |
|
2904 |
|
2905 input_line_number = 0; |
|
2906 current_input_column = 1; |
|
2907 |
|
2908 if (verbose) |
|
2909 { |
3531
|
2910 std::cout << "reading commands from " << s << " ... "; |
3021
|
2911 reading_startup_message_printed = true; |
3531
|
2912 std::cout.flush (); |
3021
|
2913 } |
|
2914 |
|
2915 parse_and_execute (f); |
|
2916 |
|
2917 if (verbose) |
3538
|
2918 std::cout << "done." << std::endl; |
3021
|
2919 } |
|
2920 else if (warn_for) |
|
2921 error ("%s: unable to open file `%s'", warn_for, s.c_str ()); |
|
2922 |
|
2923 unwind_protect::run_frame ("parse_and_execute_2"); |
|
2924 } |
|
2925 |
|
2926 static bool |
3523
|
2927 looks_like_octave_copyright (const std::string& s) |
3021
|
2928 { |
|
2929 bool retval = false; |
|
2930 |
3523
|
2931 std::string t = s.substr (0, 14); |
3427
|
2932 |
|
2933 if (t == "Copyright (C) ") |
3021
|
2934 { |
|
2935 size_t pos = s.find ('\n'); |
|
2936 |
|
2937 if (pos != NPOS) |
|
2938 { |
|
2939 pos = s.find ('\n', pos + 1); |
|
2940 |
|
2941 if (pos != NPOS) |
|
2942 { |
|
2943 pos++; |
|
2944 |
3427
|
2945 t = s.substr (pos, 28); |
|
2946 |
|
2947 if (t == "This file is part of Octave." |
|
2948 || t == "This program is free softwar") |
3021
|
2949 retval = true; |
|
2950 } |
|
2951 } |
|
2952 } |
|
2953 |
|
2954 return retval; |
|
2955 } |
|
2956 |
|
2957 // Eat whitespace and comments from FFILE, returning the text of the |
|
2958 // comments read if it doesn't look like a copyright notice. If |
|
2959 // IN_PARTS, consider each block of comments separately; otherwise, |
|
2960 // grab them all at once. If UPDATE_POS is TRUE, line and column |
3665
|
2961 // number information is updated. If SAVE_COPYRIGHT is TRUE, then |
|
2962 // comments that are recognized as a copyright notice are saved in the |
|
2963 // comment buffer. |
3021
|
2964 |
|
2965 // XXX FIXME XXX -- grab_help_text() in lex.l duplicates some of this |
|
2966 // code! |
|
2967 |
3536
|
2968 static std::string |
3665
|
2969 gobble_leading_white_space (FILE *ffile, bool in_parts, |
|
2970 bool update_pos, bool save_copyright) |
3021
|
2971 { |
3523
|
2972 std::string help_txt; |
3021
|
2973 |
3802
|
2974 // TRUE means we have already seen the first block of comments. |
3021
|
2975 bool first_comments_seen = false; |
3802
|
2976 |
|
2977 // TRUE means we are at the beginning of a comment block. |
3021
|
2978 bool begin_comment = false; |
3802
|
2979 |
|
2980 // TRUE means we have already cached the help text. |
3021
|
2981 bool have_help_text = false; |
3802
|
2982 |
|
2983 // TRUE means we are currently reading a comment block. |
3021
|
2984 bool in_comment = false; |
3802
|
2985 |
|
2986 // TRUE means we should discard the first space from the input |
|
2987 // (used to strip leading spaces from the help text). |
3427
|
2988 bool discard_space = true; |
3802
|
2989 |
3021
|
2990 int c; |
|
2991 |
|
2992 while ((c = getc (ffile)) != EOF) |
|
2993 { |
|
2994 if (update_pos) |
|
2995 current_input_column++; |
|
2996 |
|
2997 if (begin_comment) |
|
2998 { |
|
2999 if (c == '%' || c == '#') |
|
3000 continue; |
3427
|
3001 else if (discard_space && c == ' ') |
|
3002 { |
|
3003 discard_space = false; |
|
3004 continue; |
|
3005 } |
3021
|
3006 else |
|
3007 begin_comment = false; |
|
3008 } |
|
3009 |
|
3010 if (in_comment) |
|
3011 { |
|
3012 if (! have_help_text) |
|
3013 { |
|
3014 first_comments_seen = true; |
|
3015 help_txt += (char) c; |
|
3016 } |
|
3017 |
|
3018 if (c == '\n') |
|
3019 { |
|
3020 if (update_pos) |
|
3021 { |
|
3022 input_line_number++; |
|
3023 current_input_column = 0; |
|
3024 } |
3427
|
3025 |
3021
|
3026 in_comment = false; |
3427
|
3027 discard_space = true; |
3021
|
3028 |
|
3029 if (in_parts) |
|
3030 { |
|
3031 if ((c = getc (ffile)) != EOF) |
|
3032 { |
|
3033 if (update_pos) |
|
3034 current_input_column--; |
|
3035 ungetc (c, ffile); |
|
3036 if (c == '\n') |
|
3037 break; |
|
3038 } |
|
3039 else |
|
3040 break; |
|
3041 } |
|
3042 } |
|
3043 } |
|
3044 else |
|
3045 { |
|
3046 switch (c) |
|
3047 { |
|
3048 case ' ': |
|
3049 case '\t': |
|
3050 if (first_comments_seen) |
|
3051 have_help_text = true; |
|
3052 break; |
|
3053 |
3802
|
3054 case '%': |
|
3055 case '#': |
|
3056 begin_comment = true; |
|
3057 in_comment = true; |
|
3058 break; |
|
3059 |
3021
|
3060 case '\n': |
|
3061 if (first_comments_seen) |
|
3062 have_help_text = true; |
|
3063 if (update_pos) |
|
3064 { |
|
3065 input_line_number++; |
|
3066 current_input_column = 0; |
|
3067 } |
|
3068 continue; |
|
3069 |
3802
|
3070 case '\r': |
|
3071 c = getc (ffile); |
|
3072 if (update_pos) |
|
3073 current_input_column++; |
|
3074 if (c == EOF) |
|
3075 goto done; |
|
3076 else if (c == '\n') |
|
3077 { |
|
3078 if (first_comments_seen) |
|
3079 have_help_text = true; |
|
3080 if (update_pos) |
|
3081 { |
|
3082 input_line_number++; |
|
3083 current_input_column = 0; |
|
3084 } |
|
3085 continue; |
|
3086 } |
|
3087 |
|
3088 // Fall through... |
3021
|
3089 |
|
3090 default: |
|
3091 if (update_pos) |
|
3092 current_input_column--; |
|
3093 ungetc (c, ffile); |
|
3094 goto done; |
|
3095 } |
|
3096 } |
|
3097 } |
|
3098 |
|
3099 done: |
|
3100 |
|
3101 if (! help_txt.empty ()) |
|
3102 { |
3665
|
3103 if (looks_like_octave_copyright (help_txt)) |
|
3104 { |
|
3105 if (save_copyright) |
|
3106 octave_comment_buffer::append (help_txt); |
|
3107 |
|
3108 help_txt.resize (0); |
|
3109 } |
3021
|
3110 |
|
3111 if (in_parts && help_txt.empty ()) |
3665
|
3112 help_txt = gobble_leading_white_space (ffile, in_parts, |
|
3113 update_pos, false); |
3021
|
3114 } |
|
3115 |
|
3116 return help_txt; |
|
3117 } |
|
3118 |
3536
|
3119 std::string |
3523
|
3120 get_help_from_file (const std::string& path) |
3021
|
3121 { |
3523
|
3122 std::string retval; |
3021
|
3123 |
|
3124 if (! path.empty ()) |
|
3125 { |
|
3126 FILE *fptr = fopen (path.c_str (), "r"); |
|
3127 |
|
3128 if (fptr) |
|
3129 { |
|
3130 unwind_protect::add (safe_fclose, (void *) fptr); |
|
3131 |
3665
|
3132 retval = gobble_leading_white_space (fptr, true, true, false); |
3021
|
3133 |
|
3134 unwind_protect::run (); |
|
3135 } |
|
3136 } |
|
3137 |
|
3138 return retval; |
|
3139 } |
|
3140 |
|
3141 static int |
|
3142 is_function_file (FILE *ffile) |
|
3143 { |
|
3144 int status = 0; |
|
3145 |
|
3146 long pos = ftell (ffile); |
|
3147 |
3665
|
3148 gobble_leading_white_space (ffile, false, false, false); |
3021
|
3149 |
|
3150 char buf [10]; |
|
3151 fgets (buf, 10, ffile); |
|
3152 int len = strlen (buf); |
|
3153 if (len > 8 && strncmp (buf, "function", 8) == 0 |
|
3154 && ! (isalnum (buf[8]) || buf[8] == '_')) |
|
3155 status = 1; |
|
3156 |
|
3157 fseek (ffile, pos, SEEK_SET); |
|
3158 |
|
3159 return status; |
|
3160 } |
|
3161 |
|
3162 static void |
|
3163 restore_command_history (void *) |
|
3164 { |
|
3165 command_history::ignore_entries (! Vsaving_history); |
|
3166 } |
|
3167 |
|
3168 static void |
|
3169 restore_input_stream (void *f) |
|
3170 { |
|
3171 command_editor::set_input_stream (static_cast<FILE *> (f)); |
|
3172 } |
|
3173 |
3180
|
3174 static void |
|
3175 clear_current_script_file_name (void *) |
|
3176 { |
|
3177 bind_builtin_variable ("current_script_file_name", octave_value ()); |
|
3178 } |
|
3179 |
3021
|
3180 static bool |
3523
|
3181 parse_fcn_file (const std::string& ff, bool exec_script, bool force_script = false) |
3021
|
3182 { |
|
3183 unwind_protect::begin_frame ("parse_fcn_file"); |
|
3184 |
|
3185 int script_file_executed = false; |
|
3186 |
|
3187 // Open function file and parse. |
|
3188 |
|
3189 bool old_reading_fcn_file_state = reading_fcn_file; |
|
3190 |
|
3191 FILE *in_stream = command_editor::get_input_stream (); |
|
3192 |
|
3193 unwind_protect::add (restore_input_stream, in_stream); |
|
3194 |
|
3195 unwind_protect_ptr (ff_instream); |
|
3196 |
|
3197 unwind_protect_int (input_line_number); |
|
3198 unwind_protect_int (current_input_column); |
|
3199 unwind_protect_bool (reading_fcn_file); |
|
3200 unwind_protect_bool (line_editing); |
|
3201 |
|
3202 input_line_number = 0; |
|
3203 current_input_column = 1; |
|
3204 reading_fcn_file = true; |
|
3205 line_editing = false; |
|
3206 |
|
3207 FILE *ffile = get_input_from_file (ff, 0); |
|
3208 |
|
3209 unwind_protect::add (safe_fclose, ffile); |
|
3210 |
|
3211 if (ffile) |
|
3212 { |
|
3213 // Check to see if this file defines a function or is just a |
|
3214 // list of commands. |
|
3215 |
3165
|
3216 if (! force_script && is_function_file (ffile)) |
3021
|
3217 { |
|
3218 // XXX FIXME XXX -- we shouldn't need both the |
|
3219 // command_history object and the |
|
3220 // Vsaving_history variable... |
|
3221 command_history::ignore_entries (); |
|
3222 |
|
3223 unwind_protect::add (restore_command_history, 0); |
|
3224 |
|
3225 unwind_protect_int (Vecho_executing_commands); |
|
3226 unwind_protect_bool (Vsaving_history); |
|
3227 unwind_protect_bool (reading_fcn_file); |
|
3228 unwind_protect_bool (input_from_command_line_file); |
3877
|
3229 unwind_protect_bool (get_input_from_eval_string); |
3883
|
3230 unwind_protect_bool (parser_end_of_input); |
3021
|
3231 |
|
3232 Vecho_executing_commands = ECHO_OFF; |
|
3233 Vsaving_history = false; |
|
3234 reading_fcn_file = true; |
|
3235 input_from_command_line_file = false; |
3877
|
3236 get_input_from_eval_string = false; |
3883
|
3237 parser_end_of_input = false; |
3021
|
3238 |
|
3239 YY_BUFFER_STATE old_buf = current_buffer (); |
|
3240 YY_BUFFER_STATE new_buf = create_buffer (ffile); |
|
3241 |
|
3242 unwind_protect::add (restore_input_buffer, (void *) old_buf); |
|
3243 unwind_protect::add (delete_input_buffer, (void *) new_buf); |
|
3244 |
|
3245 switch_to_buffer (new_buf); |
|
3246 |
|
3247 unwind_protect_ptr (curr_sym_tab); |
|
3248 |
|
3249 reset_parser (); |
|
3250 |
3665
|
3251 help_buf = gobble_leading_white_space (ffile, true, true, true); |
|
3252 |
|
3253 octave_comment_buffer::append (help_buf); |
3021
|
3254 |
|
3255 // XXX FIXME XXX -- this should not be necessary. |
3665
|
3256 gobble_leading_white_space (ffile, false, true, false); |
3021
|
3257 |
|
3258 int status = yyparse (); |
|
3259 |
|
3260 if (status != 0) |
|
3261 { |
|
3262 error ("parse error while reading function file %s", |
|
3263 ff.c_str ()); |
4009
|
3264 fbi_sym_tab->clear (curr_fcn_file_name); |
3021
|
3265 } |
|
3266 } |
|
3267 else if (exec_script) |
|
3268 { |
|
3269 // The value of `reading_fcn_file' will be restored to the |
|
3270 // proper value when we unwind from this frame. |
|
3271 reading_fcn_file = old_reading_fcn_file_state; |
|
3272 |
|
3273 // XXX FIXME XXX -- we shouldn't need both the |
|
3274 // command_history object and the |
|
3275 // Vsaving_history variable... |
|
3276 command_history::ignore_entries (); |
|
3277 |
|
3278 unwind_protect::add (restore_command_history, 0); |
|
3279 |
|
3280 unwind_protect_bool (Vsaving_history); |
|
3281 unwind_protect_bool (reading_script_file); |
|
3282 |
|
3283 Vsaving_history = false; |
|
3284 reading_script_file = true; |
|
3285 |
3180
|
3286 unwind_protect::add (clear_current_script_file_name, 0); |
|
3287 |
|
3288 bind_builtin_variable ("current_script_file_name", ff); |
|
3289 |
3021
|
3290 parse_and_execute (ffile); |
|
3291 |
|
3292 script_file_executed = true; |
|
3293 } |
|
3294 } |
3880
|
3295 else |
|
3296 error ("no such file, `%s'", ff.c_str ()); |
3021
|
3297 |
|
3298 unwind_protect::run_frame ("parse_fcn_file"); |
|
3299 |
|
3300 return script_file_executed; |
|
3301 } |
|
3302 |
|
3303 bool |
|
3304 load_fcn_from_file (symbol_record *sym_rec, bool exec_script) |
|
3305 { |
|
3306 bool script_file_executed = false; |
|
3307 |
3523
|
3308 std::string nm = sym_rec->name (); |
3021
|
3309 |
3325
|
3310 if (octave_dynamic_loader::load (nm)) |
3021
|
3311 { |
|
3312 force_link_to_function (nm); |
|
3313 } |
|
3314 else |
|
3315 { |
3523
|
3316 std::string ff = fcn_file_in_path (nm); |
3021
|
3317 |
|
3318 // These are needed by yyparse. |
|
3319 |
|
3320 unwind_protect::begin_frame ("load_fcn_from_file"); |
|
3321 |
|
3322 unwind_protect_str (curr_fcn_file_name); |
|
3323 unwind_protect_str (curr_fcn_file_full_name); |
|
3324 |
|
3325 curr_fcn_file_name = nm; |
|
3326 curr_fcn_file_full_name = ff; |
|
3327 |
|
3328 if (ff.length () > 0) |
3165
|
3329 script_file_executed = parse_fcn_file (ff, exec_script); |
3021
|
3330 |
|
3331 if (! (error_state || script_file_executed)) |
|
3332 force_link_to_function (nm); |
|
3333 |
|
3334 unwind_protect::run_frame ("load_fcn_from_file"); |
|
3335 } |
|
3336 |
|
3337 return script_file_executed; |
|
3338 } |
|
3339 |
|
3340 DEFUN (source, args, , |
3371
|
3341 "-*- texinfo -*-\n\ |
|
3342 @deftypefn {Built-in Function} {} source (@var{file})\n\ |
|
3343 Parse and execute the contents of @var{file}. This is equivalent to\n\ |
|
3344 executing commands from a script file, but without requiring the file to\n\ |
|
3345 be named @file{@var{file}.m}.\n\ |
|
3346 @end deftypefn") |
3021
|
3347 { |
|
3348 octave_value_list retval; |
|
3349 |
|
3350 int nargin = args.length (); |
|
3351 |
|
3352 if (nargin == 1) |
|
3353 { |
3523
|
3354 std::string file_name = args(0).string_value (); |
3021
|
3355 |
|
3356 if (! error_state) |
|
3357 { |
3523
|
3358 std::string file_full_name = file_ops::tilde_expand (file_name); |
3388
|
3359 |
|
3360 unwind_protect::begin_frame ("Fsource"); |
|
3361 |
|
3362 unwind_protect_str (curr_fcn_file_name); |
|
3363 unwind_protect_str (curr_fcn_file_full_name); |
|
3364 |
|
3365 curr_fcn_file_name = file_name; |
|
3366 curr_fcn_file_full_name = file_full_name; |
|
3367 |
|
3368 parse_fcn_file (file_full_name, true, true); |
3021
|
3369 |
|
3370 if (error_state) |
3388
|
3371 error ("source: error sourcing file `%s'", |
|
3372 file_full_name.c_str ()); |
|
3373 |
|
3374 unwind_protect::run_frame ("Fsource"); |
3021
|
3375 } |
|
3376 else |
|
3377 error ("source: expecting file name as argument"); |
|
3378 } |
|
3379 else |
|
3380 print_usage ("source"); |
|
3381 |
|
3382 return retval; |
|
3383 } |
|
3384 |
3726
|
3385 // Evaluate an Octave function (built-in or interpreted) and return |
3844
|
3386 // the list of result values. NAME is the name of the function to |
|
3387 // call. ARGS are the arguments to the function. NARGOUT is the |
|
3388 // number of output arguments expected. |
3726
|
3389 |
3021
|
3390 octave_value_list |
3523
|
3391 feval (const std::string& name, const octave_value_list& args, int nargout) |
3156
|
3392 { |
|
3393 octave_value_list retval; |
|
3394 |
|
3395 octave_function *fcn = is_valid_function (name, "feval", 1); |
|
3396 |
|
3397 if (fcn) |
3544
|
3398 retval = fcn->do_multi_index_op (nargout, args); |
3156
|
3399 |
|
3400 return retval; |
|
3401 } |
|
3402 |
3726
|
3403 // Evaluate an Octave function (built-in or interpreted) and return |
|
3404 // the list of result values. The first element of ARGS should be a |
|
3405 // string containing the name of the function to call, then the rest |
|
3406 // are the actual arguments to the function. NARGOUT is the number of |
|
3407 // output arguments expected. |
|
3408 |
3156
|
3409 octave_value_list |
3021
|
3410 feval (const octave_value_list& args, int nargout) |
|
3411 { |
|
3412 octave_value_list retval; |
|
3413 |
3156
|
3414 if (args.length () > 0) |
3021
|
3415 { |
3523
|
3416 std::string name = args(0).string_value (); |
3156
|
3417 |
|
3418 if (! error_state) |
3021
|
3419 { |
3156
|
3420 int tmp_nargin = args.length () - 1; |
|
3421 |
|
3422 octave_value_list tmp_args (tmp_nargin, octave_value ()); |
|
3423 |
|
3424 for (int i = 0; i < tmp_nargin; i++) |
3178
|
3425 tmp_args(i) = args(i+1); |
|
3426 |
|
3427 string_vector arg_names = args.name_tags (); |
|
3428 |
|
3429 if (! arg_names.empty ()) |
3156
|
3430 { |
3726
|
3431 // tmp_nargin and arg_names.length () - 1 may differ if |
|
3432 // we are passed all_va_args. |
|
3433 |
|
3434 int n = arg_names.length () - 1; |
|
3435 |
|
3436 int len = n > tmp_nargin ? tmp_nargin : n; |
|
3437 |
|
3438 string_vector tmp_arg_names (len); |
|
3439 |
|
3440 for (int i = 0; i < len; i++) |
3178
|
3441 tmp_arg_names(i) = arg_names(i+1); |
|
3442 |
|
3443 tmp_args.stash_name_tags (tmp_arg_names); |
3156
|
3444 } |
|
3445 |
|
3446 retval = feval (name, tmp_args, nargout); |
3021
|
3447 } |
|
3448 } |
|
3449 |
|
3450 return retval; |
|
3451 } |
|
3452 |
|
3453 DEFUN (feval, args, nargout, |
3371
|
3454 "-*- texinfo -*-\n\ |
|
3455 @deftypefn {Built-in Function} {} feval (@var{name}, @dots{})\n\ |
|
3456 Evaluate the function named @var{name}. Any arguments after the first\n\ |
|
3457 are passed on to the named function. For example,\n\ |
|
3458 \n\ |
|
3459 @example\n\ |
|
3460 feval (\"acos\", -1)\n\ |
|
3461 @result{} 3.1416\n\ |
|
3462 @end example\n\ |
3021
|
3463 \n\ |
3371
|
3464 @noindent\n\ |
|
3465 calls the function @code{acos} with the argument @samp{-1}.\n\ |
|
3466 \n\ |
|
3467 The function @code{feval} is necessary in order to be able to write\n\ |
|
3468 functions that call user-supplied functions, because Octave does not\n\ |
|
3469 have a way to declare a pointer to a function (like C) or to declare a\n\ |
|
3470 special kind of variable that can be used to hold the name of a function\n\ |
|
3471 (like @code{EXTERNAL} in Fortran). Instead, you must refer to functions\n\ |
|
3472 by name, and use @code{feval} to call them.\n\ |
|
3473 @end deftypefn") |
3021
|
3474 { |
|
3475 octave_value_list retval; |
|
3476 |
|
3477 int nargin = args.length (); |
|
3478 |
|
3479 if (nargin > 0) |
|
3480 retval = feval (args, nargout); |
|
3481 else |
|
3482 print_usage ("feval"); |
|
3483 |
|
3484 return retval; |
|
3485 } |
|
3486 |
3099
|
3487 octave_value_list |
3523
|
3488 eval_string (const std::string& s, bool silent, int& parse_status, int nargout) |
3021
|
3489 { |
3877
|
3490 octave_value_list retval; |
|
3491 |
3021
|
3492 unwind_protect::begin_frame ("eval_string"); |
|
3493 |
|
3494 unwind_protect_bool (get_input_from_eval_string); |
3877
|
3495 unwind_protect_bool (input_from_eval_string_pending); |
3021
|
3496 unwind_protect_bool (input_from_command_line_file); |
3883
|
3497 unwind_protect_bool (parser_end_of_input); |
3021
|
3498 unwind_protect_str (current_eval_string); |
|
3499 |
|
3500 get_input_from_eval_string = true; |
3877
|
3501 input_from_eval_string_pending = true; |
3021
|
3502 input_from_command_line_file = false; |
3883
|
3503 parser_end_of_input = false; |
3021
|
3504 current_eval_string = s; |
|
3505 |
3877
|
3506 unwind_protect_ptr (global_command); |
|
3507 |
3021
|
3508 YY_BUFFER_STATE old_buf = current_buffer (); |
|
3509 YY_BUFFER_STATE new_buf = create_buffer (0); |
|
3510 |
|
3511 unwind_protect::add (restore_input_buffer, old_buf); |
|
3512 unwind_protect::add (delete_input_buffer, new_buf); |
|
3513 |
|
3514 switch_to_buffer (new_buf); |
|
3515 |
|
3516 unwind_protect_ptr (curr_sym_tab); |
|
3517 |
3877
|
3518 do |
|
3519 { |
|
3520 parse_status = yyparse (); |
|
3521 |
|
3522 tree_statement_list *command = global_command; |
|
3523 |
3883
|
3524 if (parse_status == 0) |
3877
|
3525 { |
3883
|
3526 if (command) |
|
3527 { |
|
3528 retval = command->eval (silent, nargout); |
|
3529 |
|
3530 delete command; |
|
3531 |
|
3532 command = 0; |
|
3533 |
|
3534 if (error_state |
|
3535 || tree_return_command::returning |
|
3536 || tree_break_command::breaking |
|
3537 || tree_continue_command::continuing) |
|
3538 break; |
|
3539 } |
|
3540 else if (parser_end_of_input) |
3877
|
3541 break; |
3883
|
3542 } |
3877
|
3543 } |
|
3544 while (parse_status == 0); |
3021
|
3545 |
|
3546 unwind_protect::run_frame ("eval_string"); |
|
3547 |
|
3548 return retval; |
|
3549 } |
|
3550 |
|
3551 octave_value |
3523
|
3552 eval_string (const std::string& s, bool silent, int& parse_status) |
3021
|
3553 { |
|
3554 octave_value retval; |
|
3555 |
|
3556 octave_value_list tmp = eval_string (s, silent, parse_status, 1); |
|
3557 |
|
3558 if (! tmp.empty ()) |
|
3559 retval = tmp(0); |
|
3560 |
|
3561 return retval; |
|
3562 } |
|
3563 |
|
3564 static octave_value_list |
|
3565 eval_string (const octave_value& arg, bool silent, int& parse_status, |
|
3566 int nargout) |
|
3567 { |
3523
|
3568 std::string s = arg.string_value (); |
3021
|
3569 |
|
3570 if (error_state) |
|
3571 { |
3523
|
3572 error ("eval: expecting std::string argument"); |
3021
|
3573 return -1.0; |
|
3574 } |
|
3575 |
|
3576 return eval_string (s, silent, parse_status, nargout); |
|
3577 } |
|
3578 |
|
3579 DEFUN (eval, args, nargout, |
3371
|
3580 "-*- texinfo -*-\n\ |
|
3581 @deftypefn {Built-in Function} {} eval (@var{try}, @var{catch})\n\ |
|
3582 Parse the string @var{try} and evaluate it as if it were an Octave\n\ |
|
3583 program, returning the last value computed. If that fails, evaluate\n\ |
|
3584 the string @var{catch}. The string @var{try} is evaluated in the\n\ |
|
3585 current context, so any results remain available after @code{eval}\n\ |
|
3586 returns. For example,\n\ |
|
3587 \n\ |
|
3588 @example\n\ |
|
3589 @group\n\ |
|
3590 eval (\"a = 13\")\n\ |
|
3591 @print{} a = 13\n\ |
|
3592 @result{} 13\n\ |
|
3593 @end group\n\ |
|
3594 @end example\n\ |
3021
|
3595 \n\ |
3371
|
3596 In this case, the value of the evaluated expression is printed and it is\n\ |
|
3597 also returned returned from @code{eval}. Just as with any other\n\ |
|
3598 expression, you can turn printing off by ending the expression in a\n\ |
|
3599 semicolon. For example,\n\ |
|
3600 \n\ |
|
3601 @example\n\ |
|
3602 eval (\"a = 13;\")\n\ |
|
3603 @result{} 13\n\ |
|
3604 @end example\n\ |
|
3605 \n\ |
|
3606 In this example, the variable @code{a} has been given the value 13, but\n\ |
|
3607 the value of the expression is not printed. You can also turn off\n\ |
|
3608 automatic printing for all expressions executed by @code{eval} using the\n\ |
|
3609 variable @code{default_eval_print_flag}.\n\ |
|
3610 @end deftypefn") |
3021
|
3611 { |
|
3612 octave_value_list retval; |
|
3613 |
|
3614 int nargin = args.length (); |
|
3615 |
|
3616 if (nargin > 0) |
|
3617 { |
|
3618 unwind_protect::begin_frame ("Feval"); |
|
3619 |
|
3620 if (nargin > 1) |
|
3621 { |
|
3622 unwind_protect_bool (buffer_error_messages); |
|
3623 buffer_error_messages = true; |
|
3624 } |
|
3625 |
|
3626 int parse_status = 0; |
|
3627 |
|
3628 retval = eval_string (args(0), ! Vdefault_eval_print_flag, |
|
3629 parse_status, nargout); |
|
3630 |
|
3631 if (nargin > 1 && (parse_status != 0 || error_state)) |
|
3632 { |
|
3633 error_state = 0; |
|
3634 |
|
3635 // Set up for letting the user print any messages from |
|
3636 // errors that occurred in the first part of this eval(). |
|
3637 |
|
3638 buffer_error_messages = false; |
3815
|
3639 |
3021
|
3640 bind_global_error_variable (); |
3815
|
3641 |
3021
|
3642 unwind_protect::add (clear_global_error_variable, 0); |
|
3643 |
|
3644 eval_string (args(1), 0, parse_status, nargout); |
|
3645 |
|
3646 retval = octave_value_list (); |
|
3647 } |
|
3648 |
|
3649 unwind_protect::run_frame ("Feval"); |
|
3650 } |
|
3651 else |
|
3652 print_usage ("eval"); |
|
3653 |
|
3654 return retval; |
|
3655 } |
|
3656 |
|
3657 static int |
|
3658 default_eval_print_flag (void) |
|
3659 { |
|
3660 Vdefault_eval_print_flag = check_preference ("default_eval_print_flag"); |
|
3661 |
|
3662 return 0; |
|
3663 } |
|
3664 |
2166
|
3665 static int |
|
3666 warn_assign_as_truth_value (void) |
|
3667 { |
|
3668 Vwarn_assign_as_truth_value |
|
3669 = check_preference ("warn_assign_as_truth_value"); |
|
3670 |
|
3671 return 0; |
|
3672 } |
|
3673 |
|
3674 static int |
|
3675 warn_function_name_clash (void) |
|
3676 { |
|
3677 Vwarn_function_name_clash = check_preference ("warn_function_name_clash"); |
|
3678 |
|
3679 return 0; |
|
3680 } |
|
3681 |
|
3682 static int |
3162
|
3683 warn_future_time_stamp (void) |
|
3684 { |
|
3685 Vwarn_future_time_stamp = check_preference ("warn_future_time_stamp"); |
|
3686 |
|
3687 return 0; |
|
3688 } |
|
3689 |
|
3690 static int |
2166
|
3691 warn_missing_semicolon (void) |
|
3692 { |
|
3693 Vwarn_missing_semicolon = check_preference ("warn_missing_semicolon"); |
|
3694 |
|
3695 return 0; |
|
3696 } |
|
3697 |
2764
|
3698 static int |
4023
|
3699 warn_precedence_change (void) |
|
3700 { |
|
3701 Vwarn_precedence_change = check_preference ("warn_precedence_change"); |
|
3702 |
|
3703 return 0; |
|
3704 } |
|
3705 |
|
3706 static int |
2764
|
3707 warn_variable_switch_label (void) |
|
3708 { |
|
3709 Vwarn_variable_switch_label |
|
3710 = check_preference ("warn_variable_switch_label"); |
|
3711 |
|
3712 return 0; |
|
3713 } |
|
3714 |
2166
|
3715 void |
|
3716 symbols_of_parse (void) |
|
3717 { |
3258
|
3718 DEFVAR (default_eval_print_flag, 1.0, default_eval_print_flag, |
3371
|
3719 "-*- texinfo -*-\n\ |
|
3720 @defvr {Built-in Variable} default_eval_print_flag\n\ |
|
3721 If the value of this variable is nonzero, Octave prints the results of\n\ |
|
3722 commands executed by @code{eval} that do not end with semicolons. If it\n\ |
|
3723 is zero, automatic printing is suppressed. The default value is 1.\n\ |
|
3724 @end defvr"); |
3021
|
3725 |
3258
|
3726 DEFVAR (warn_assign_as_truth_value, 1.0, warn_assign_as_truth_value, |
3366
|
3727 "-*- texinfo -*-\n\ |
|
3728 @defvr {Built-in Variable} warn_assign_as_truth_value\n\ |
|
3729 If the value of @code{warn_assign_as_truth_value} is nonzero, a\n\ |
|
3730 warning is issued for statements like\n\ |
|
3731 \n\ |
|
3732 @example\n\ |
|
3733 if (s = t)\n\ |
|
3734 ...\n\ |
|
3735 @end example\n\ |
|
3736 \n\ |
|
3737 @noindent\n\ |
|
3738 since such statements are not common, and it is likely that the intent\n\ |
|
3739 was to write\n\ |
|
3740 \n\ |
|
3741 @example\n\ |
|
3742 if (s == t)\n\ |
|
3743 ...\n\ |
|
3744 @end example\n\ |
|
3745 \n\ |
|
3746 @noindent\n\ |
|
3747 instead.\n\ |
|
3748 \n\ |
|
3749 There are times when it is useful to write code that contains\n\ |
|
3750 assignments within the condition of a @code{while} or @code{if}\n\ |
|
3751 statement. For example, statements like\n\ |
|
3752 \n\ |
|
3753 @example\n\ |
|
3754 while (c = getc())\n\ |
|
3755 ...\n\ |
|
3756 @end example\n\ |
|
3757 \n\ |
|
3758 @noindent\n\ |
|
3759 are common in C programming.\n\ |
|
3760 \n\ |
|
3761 It is possible to avoid all warnings about such statements by setting\n\ |
|
3762 @code{warn_assign_as_truth_value} to 0, but that may also\n\ |
|
3763 let real errors like\n\ |
|
3764 \n\ |
|
3765 @example\n\ |
|
3766 if (x = 1) # intended to test (x == 1)!\n\ |
|
3767 ...\n\ |
|
3768 @end example\n\ |
|
3769 \n\ |
|
3770 @noindent\n\ |
|
3771 slip by.\n\ |
|
3772 \n\ |
|
3773 In such cases, it is possible suppress errors for specific statements by\n\ |
|
3774 writing them with an extra set of parentheses. For example, writing the\n\ |
|
3775 previous example as\n\ |
|
3776 \n\ |
|
3777 @example\n\ |
|
3778 while ((c = getc()))\n\ |
|
3779 ...\n\ |
|
3780 @end example\n\ |
|
3781 \n\ |
|
3782 @noindent\n\ |
|
3783 will prevent the warning from being printed for this statement, while\n\ |
|
3784 allowing Octave to warn about other assignments used in conditional\n\ |
|
3785 contexts.\n\ |
|
3786 \n\ |
|
3787 The default value of @code{warn_assign_as_truth_value} is 1.\n\ |
|
3788 @end defvr"); |
2166
|
3789 |
3258
|
3790 DEFVAR (warn_function_name_clash, 1.0, warn_function_name_clash, |
3371
|
3791 "-*- texinfo -*-\n\ |
|
3792 @defvr {Built-in Variable} warn_function_name_clash\n\ |
|
3793 If the value of @code{warn_function_name_clash} is nonzero, a warning is\n\ |
|
3794 issued when Octave finds that the name of a function defined in a\n\ |
|
3795 function file differs from the name of the file. (If the names\n\ |
|
3796 disagree, the name declared inside the file is ignored.) If the value\n\ |
|
3797 is 0, the warning is omitted. The default value is 1.\n\ |
|
3798 @end defvr"); |
2166
|
3799 |
3258
|
3800 DEFVAR (warn_future_time_stamp, 1.0, warn_future_time_stamp, |
3446
|
3801 "-*- texinfo -*-\n\ |
|
3802 @defvr {Built-in Variable} warn_future_time_stamp\n\ |
|
3803 If the value of this variable is nonzero, Octave will print a warning\n\ |
|
3804 if it finds a function file with a time stamp that is in the future.\n\ |
|
3805 @end defvr"); |
3162
|
3806 |
3258
|
3807 DEFVAR (warn_missing_semicolon, 0.0, warn_missing_semicolon, |
3371
|
3808 "-*- texinfo -*-\n\ |
|
3809 @defvr {Built-in Variable} warn_missing_semicolon\n\ |
|
3810 If the value of this variable is nonzero, Octave will warn when\n\ |
|
3811 statements in function definitions don't end in semicolons. The default\n\ |
|
3812 value is 0.\n\ |
|
3813 @end defvr"); |
2764
|
3814 |
4023
|
3815 DEFVAR (warn_precedence_change, 1.0, warn_precedence_change, |
|
3816 "-*- texinfo -*-\n\ |
|
3817 @defvr {Built-in Variable} warn_precedence_change\n\ |
|
3818 If the value of this variable is nonzero, Octave will warn about\n\ |
|
3819 possible changes in the meaning of some code due to changes in\n\ |
|
3820 precedence for some operators. Precedence changes have typically\n\ |
|
3821 been made for Matlab compatibility. The default value is 1.\n\ |
|
3822 @end defvr"); |
|
3823 |
3258
|
3824 DEFVAR (warn_variable_switch_label, 0.0, warn_variable_switch_label, |
3366
|
3825 "-*- texinfo -*-\n\ |
|
3826 @defvr {Built-in Variable} warn_variable_switch_label\n\ |
|
3827 If the value of this variable is nonzero, Octave will print a warning if\n\ |
|
3828 a switch label is not a constant or constant expression\n\ |
|
3829 @end defvr"); |
3347
|
3830 |
2166
|
3831 } |
|
3832 |
1994
|
3833 /* |
|
3834 ;;; Local Variables: *** |
|
3835 ;;; mode: text *** |
|
3836 ;;; End: *** |
|
3837 */ |