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