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