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