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