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