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