Mercurial > octave
annotate src/parse.y @ 7690:97e535ec65db
make_constant: stash original text for strings
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 03 Apr 2008 14:09:49 -0400 |
parents | ae90e05ad299 |
children | 27a5f578750c |
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 | |
7634
ae90e05ad299
fix parameter list initializer bug
John W. Eaton <jwe@octave.org>
parents:
7587
diff
changeset
|
932 decl_param_init : // empty |
ae90e05ad299
fix parameter list initializer bug
John W. Eaton <jwe@octave.org>
parents:
7587
diff
changeset
|
933 { lexer_flags.looking_at_initializer_expression = true; } |
ae90e05ad299
fix parameter list initializer bug
John W. Eaton <jwe@octave.org>
parents:
7587
diff
changeset
|
934 |
2970 | 935 decl2 : identifier |
936 { $$ = new tree_decl_elt ($1); } | |
7634
ae90e05ad299
fix parameter list initializer bug
John W. Eaton <jwe@octave.org>
parents:
7587
diff
changeset
|
937 | identifier '=' decl_param_init expression |
ae90e05ad299
fix parameter list initializer bug
John W. Eaton <jwe@octave.org>
parents:
7587
diff
changeset
|
938 { |
ae90e05ad299
fix parameter list initializer bug
John W. Eaton <jwe@octave.org>
parents:
7587
diff
changeset
|
939 lexer_flags.looking_at_initializer_expression = false; |
ae90e05ad299
fix parameter list initializer bug
John W. Eaton <jwe@octave.org>
parents:
7587
diff
changeset
|
940 $$ = new tree_decl_elt ($1, $4); |
ae90e05ad299
fix parameter list initializer bug
John W. Eaton <jwe@octave.org>
parents:
7587
diff
changeset
|
941 } |
2970 | 942 ; |
943 | |
944 // ==================== | |
945 // Selection statements | |
946 // ==================== | |
947 | |
948 select_command : if_command | |
949 { $$ = $1; } | |
950 | switch_command | |
951 { $$ = $1; } | |
952 ; | |
953 | |
954 // ============ | |
955 // If statement | |
956 // ============ | |
957 | |
3665 | 958 if_command : IF stash_comment if_cmd_list END |
2970 | 959 { |
3665 | 960 if (! ($$ = finish_if_command ($1, $3, $4, $2))) |
2970 | 961 ABORT_PARSE; |
962 } | |
963 ; | |
964 | |
965 if_cmd_list : if_cmd_list1 | |
966 { $$ = $1; } | |
967 | if_cmd_list1 else_clause | |
968 { | |
969 $1->append ($2); | |
970 $$ = $1; | |
971 } | |
972 ; | |
973 | |
974 if_cmd_list1 : expression opt_sep opt_list | |
975 { $$ = start_if_command ($1, $3); } | |
976 | if_cmd_list1 elseif_clause | |
977 { | |
978 $1->append ($2); | |
979 $$ = $1; | |
980 } | |
981 ; | |
982 | |
3665 | 983 elseif_clause : ELSEIF stash_comment opt_sep expression opt_sep opt_list |
984 { $$ = make_elseif_clause ($4, $6, $2); } | |
2970 | 985 ; |
986 | |
3665 | 987 else_clause : ELSE stash_comment opt_sep opt_list |
988 { | |
989 $$ = new tree_if_clause ($4, $2); | |
990 } | |
2970 | 991 ; |
992 | |
993 // ================ | |
994 // Switch statement | |
995 // ================ | |
996 | |
3665 | 997 switch_command : SWITCH stash_comment expression opt_sep case_list END |
2970 | 998 { |
3665 | 999 if (! ($$ = finish_switch_command ($1, $3, $5, $6, $2))) |
2970 | 1000 ABORT_PARSE; |
1001 } | |
1002 ; | |
1003 | |
4044 | 1004 case_list : // empty |
1005 { $$ = new tree_switch_case_list (); } | |
1006 | case_list1 | |
2970 | 1007 { $$ = $1; } |
1008 | case_list1 default_case | |
1009 { | |
1010 $1->append ($2); | |
1011 $$ = $1; | |
1012 } | |
1013 ; | |
1014 | |
1015 case_list1 : switch_case | |
1016 { $$ = new tree_switch_case_list ($1); } | |
1017 | case_list1 switch_case | |
1018 { | |
1019 $1->append ($2); | |
1020 $$ = $1; | |
1021 } | |
1022 ; | |
1023 | |
4025 | 1024 switch_case : CASE stash_comment opt_sep expression opt_sep opt_list |
3665 | 1025 { $$ = make_switch_case ($4, $6, $2); } |
2970 | 1026 ; |
1027 | |
3665 | 1028 default_case : OTHERWISE stash_comment opt_sep opt_list |
1029 { | |
1030 $$ = new tree_switch_case ($4, $2); | |
1031 } | |
2970 | 1032 ; |
1033 | |
1034 // ======= | |
1035 // Looping | |
1036 // ======= | |
1037 | |
3665 | 1038 loop_command : WHILE stash_comment expression opt_sep opt_list END |
2970 | 1039 { |
3665 | 1040 if (! ($$ = make_while_command ($1, $3, $5, $6, $2))) |
2970 | 1041 ABORT_PARSE; |
1042 } | |
3665 | 1043 | DO stash_comment opt_sep opt_list UNTIL expression |
3484 | 1044 { |
3665 | 1045 if (! ($$ = make_do_until_command ($1, $4, $6, $2))) |
3484 | 1046 ABORT_PARSE; |
1047 } | |
3665 | 1048 | FOR stash_comment assign_lhs '=' expression opt_sep opt_list END |
2970 | 1049 { |
3665 | 1050 if (! ($$ = make_for_command ($1, $3, $5, $7, $8, $2))) |
2970 | 1051 ABORT_PARSE; |
1052 } | |
5127 | 1053 | FOR stash_comment '(' assign_lhs '=' expression ')' opt_sep opt_list END |
1054 { | |
1055 if (! ($$ = make_for_command ($1, $4, $6, $9, $10, $2))) | |
1056 ABORT_PARSE; | |
1057 } | |
2970 | 1058 ; |
1059 | |
1060 // ======= | |
1061 // Jumping | |
1062 // ======= | |
1063 | |
4207 | 1064 jump_command : BREAK |
2970 | 1065 { |
4207 | 1066 if (! ($$ = make_break_command ($1))) |
2970 | 1067 ABORT_PARSE; |
1068 } | |
1069 | CONTINUE | |
1070 { | |
4207 | 1071 if (! ($$ = make_continue_command ($1))) |
2970 | 1072 ABORT_PARSE; |
1073 } | |
1074 | FUNC_RET | |
1075 { | |
4207 | 1076 if (! ($$ = make_return_command ($1))) |
2970 | 1077 ABORT_PARSE; |
1078 } | |
1079 ; | |
1080 | |
1081 // ========== | |
1082 // Exceptions | |
1083 // ========== | |
1084 | |
3665 | 1085 except_command : UNWIND stash_comment opt_sep opt_list CLEANUP |
1086 stash_comment opt_sep opt_list END | |
2970 | 1087 { |
3665 | 1088 if (! ($$ = make_unwind_command ($1, $4, $8, $9, $2, $6))) |
2970 | 1089 ABORT_PARSE; |
1090 } | |
3665 | 1091 | TRY stash_comment opt_sep opt_list CATCH |
1092 stash_comment opt_sep opt_list END | |
2970 | 1093 { |
3665 | 1094 if (! ($$ = make_try_command ($1, $4, $8, $9, $2, $6))) |
2970 | 1095 ABORT_PARSE; |
1096 } | |
5344 | 1097 | TRY stash_comment opt_sep opt_list END |
1098 { | |
1099 if (! ($$ = make_try_command ($1, $4, 0, $5, $2, 0))) | |
1100 ABORT_PARSE; | |
1101 } | |
2970 | 1102 ; |
1103 | |
1104 // =========================================== | |
1105 // Some `subroutines' for function definitions | |
1106 // =========================================== | |
1107 | |
7336 | 1108 push_fcn_symtab : // empty |
1109 { | |
1110 symtab_context.push (symbol_table::current_scope ()); | |
1111 symbol_table::set_scope (symbol_table::alloc_scope ()); | |
1112 | |
1113 if (! lexer_flags.parsing_nested_function) | |
1114 symbol_table::set_parent_scope (symbol_table::current_scope ()); | |
1115 } | |
2970 | 1116 ; |
1117 | |
1118 // =========================== | |
1119 // List of function parameters | |
1120 // =========================== | |
1121 | |
1122 param_list_beg : '(' | |
4935 | 1123 { |
1124 lexer_flags.looking_at_parameter_list = true; | |
1125 | |
1126 if (lexer_flags.looking_at_function_handle) | |
1127 { | |
7336 | 1128 symtab_context.push (symbol_table::current_scope ()); |
1129 symbol_table::set_scope (symbol_table::alloc_scope ()); | |
4935 | 1130 lexer_flags.looking_at_function_handle--; |
1131 } | |
1132 } | |
2970 | 1133 ; |
1134 | |
1135 param_list_end : ')' | |
1136 { lexer_flags.looking_at_parameter_list = false; } | |
1137 ; | |
1138 | |
4935 | 1139 param_list : param_list_beg param_list1 param_list_end |
2970 | 1140 { |
1141 lexer_flags.quote_is_transpose = false; | |
4935 | 1142 $$ = $2; |
2970 | 1143 } |
1144 | param_list_beg error | |
1145 { | |
1146 yyerror ("invalid parameter list"); | |
1147 $$ = 0; | |
1148 ABORT_PARSE; | |
1149 } | |
4935 | 1150 ; |
1151 | |
1152 param_list1 : // empty | |
1153 { $$ = 0; } | |
1154 | param_list2 | |
1155 { | |
1156 $1->mark_as_formal_parameters (); | |
7587
1f662945c2be
handle varargin and varargout without keywords
John W. Eaton <jwe@octave.org>
parents:
7562
diff
changeset
|
1157 if ($1->validate (tree_parameter_list::in)) |
1f662945c2be
handle varargin and varargout without keywords
John W. Eaton <jwe@octave.org>
parents:
7562
diff
changeset
|
1158 $$ = $1; |
1f662945c2be
handle varargin and varargout without keywords
John W. Eaton <jwe@octave.org>
parents:
7562
diff
changeset
|
1159 else |
1f662945c2be
handle varargin and varargout without keywords
John W. Eaton <jwe@octave.org>
parents:
7562
diff
changeset
|
1160 ABORT_PARSE; |
4935 | 1161 } |
1162 ; | |
1163 | |
6215 | 1164 param_list2 : decl2 |
4935 | 1165 { $$ = new tree_parameter_list ($1); } |
6215 | 1166 | param_list2 ',' decl2 |
4935 | 1167 { |
1168 $1->append ($3); | |
1169 $$ = $1; | |
2970 | 1170 } |
1171 ; | |
1172 | |
1173 // =================================== | |
1174 // List of function return value names | |
1175 // =================================== | |
1176 | |
7336 | 1177 return_list : '[' ']' |
2970 | 1178 { |
1179 lexer_flags.looking_at_return_list = false; | |
1180 $$ = new tree_parameter_list (); | |
1181 } | |
7336 | 1182 | return_list1 |
1183 { | |
1184 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
|
1185 if ($1->validate (tree_parameter_list::out)) |
1f662945c2be
handle varargin and varargout without keywords
John W. Eaton <jwe@octave.org>
parents:
7562
diff
changeset
|
1186 $$ = $1; |
1f662945c2be
handle varargin and varargout without keywords
John W. Eaton <jwe@octave.org>
parents:
7562
diff
changeset
|
1187 else |
1f662945c2be
handle varargin and varargout without keywords
John W. Eaton <jwe@octave.org>
parents:
7562
diff
changeset
|
1188 ABORT_PARSE; |
7336 | 1189 } |
1190 | '[' return_list1 ']' | |
2970 | 1191 { |
1192 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
|
1193 if ($2->validate (tree_parameter_list::out)) |
1f662945c2be
handle varargin and varargout without keywords
John W. Eaton <jwe@octave.org>
parents:
7562
diff
changeset
|
1194 $$ = $2; |
1f662945c2be
handle varargin and varargout without keywords
John W. Eaton <jwe@octave.org>
parents:
7562
diff
changeset
|
1195 else |
1f662945c2be
handle varargin and varargout without keywords
John W. Eaton <jwe@octave.org>
parents:
7562
diff
changeset
|
1196 ABORT_PARSE; |
2970 | 1197 } |
1198 ; | |
1199 | |
1200 return_list1 : identifier | |
6215 | 1201 { $$ = new tree_parameter_list (new tree_decl_elt ($1)); } |
2970 | 1202 | return_list1 ',' identifier |
1203 { | |
6215 | 1204 $1->append (new tree_decl_elt ($3)); |
2970 | 1205 $$ = $1; |
1206 } | |
1207 ; | |
1208 | |
1209 // =================== | |
1210 // Function definition | |
1211 // =================== | |
1212 | |
7336 | 1213 function_beg : push_fcn_symtab FCN stash_comment |
1214 { $$ = $3; } | |
2970 | 1215 ; |
1216 | |
7336 | 1217 function : function_beg function1 |
2970 | 1218 { |
3665 | 1219 $2->stash_leading_comment ($1); |
2970 | 1220 recover_from_parsing_function (); |
1221 $$ = 0; | |
1222 } | |
7336 | 1223 | function_beg return_list '=' function1 |
2970 | 1224 { |
7336 | 1225 finish_function ($2, $4, $1); |
2970 | 1226 recover_from_parsing_function (); |
1227 $$ = 0; | |
1228 } | |
1229 ; | |
1230 | |
7336 | 1231 fcn_name : identifier |
2970 | 1232 { |
4238 | 1233 std::string id_name = $1->name (); |
1234 | |
1235 if (reading_fcn_file | |
1236 && ! lexer_flags.parsing_nested_function) | |
1237 parent_function_name = (curr_fcn_file_name == id_name) | |
1238 ? id_name : curr_fcn_file_name; | |
1239 | |
1240 lexer_flags.parsed_function_name = true; | |
1241 | |
1242 $$ = $1; | |
1243 } | |
1244 ; | |
1245 | |
7336 | 1246 function1 : fcn_name function2 |
4238 | 1247 { |
4872 | 1248 std::string fname = $1->name (); |
1249 | |
1250 delete $1; | |
1251 | |
1252 if (! ($$ = frob_function (fname, $2))) | |
2970 | 1253 ABORT_PARSE; |
1254 } | |
1255 ; | |
1256 | |
7336 | 1257 function2 : param_list opt_sep opt_list function_end |
1258 { $$ = start_function ($1, $3); } | |
1259 | opt_sep opt_list function_end | |
1260 { $$ = start_function (0, $2); } | |
2970 | 1261 ; |
1262 | |
1263 function_end : END | |
1264 { | |
4238 | 1265 if (! end_token_ok ($1, token::function_end)) |
2970 | 1266 ABORT_PARSE; |
1267 } | |
1268 | END_OF_INPUT | |
1269 { | |
4240 | 1270 if (lexer_flags.parsing_nested_function) |
1271 lexer_flags.parsing_nested_function = -1; | |
4238 | 1272 |
3939 | 1273 if (! (reading_fcn_file || reading_script_file |
1274 || get_input_from_eval_string)) | |
2970 | 1275 YYABORT; |
1276 } | |
1277 ; | |
1278 | |
1279 // ============= | |
1280 // Miscellaneous | |
1281 // ============= | |
1282 | |
3665 | 1283 stash_comment : // empty |
1284 { $$ = octave_comment_buffer::get_comment (); } | |
1285 ; | |
1286 | |
2970 | 1287 parse_error : LEXICAL_ERROR |
1288 { yyerror ("parse error"); } | |
1289 | error | |
1 | 1290 ; |
1291 | |
2525 | 1292 sep_no_nl : ',' |
1293 { $$ = ','; } | |
1294 | ';' | |
1295 { $$ = ';'; } | |
1296 | sep_no_nl ',' | |
1297 { $$ = $1; } | |
1298 | sep_no_nl ';' | |
1299 { $$ = $1; } | |
1300 ; | |
1301 | |
1302 opt_sep_no_nl : // empty | |
1303 { $$ = 0; } | |
1304 | sep_no_nl | |
1305 { $$ = $1; } | |
1306 ; | |
1307 | |
1308 sep : ',' | |
1309 { $$ = ','; } | |
1310 | ';' | |
1311 { $$ = ';'; } | |
1312 | '\n' | |
1313 { $$ = '\n'; } | |
1314 | sep ',' | |
1315 { $$ = $1; } | |
1316 | sep ';' | |
1317 { $$ = $1; } | |
1318 | sep '\n' | |
1319 { $$ = $1; } | |
1320 ; | |
1321 | |
1322 opt_sep : // empty | |
1323 { $$ = 0; } | |
1324 | sep | |
1325 { $$ = $1; } | |
1326 ; | |
1327 | |
1 | 1328 %% |
1329 | |
666 | 1330 // Generic error messages. |
1331 | |
1 | 1332 static void |
2805 | 1333 yyerror (const char *s) |
1 | 1334 { |
143 | 1335 int err_col = current_input_column - 1; |
1 | 1336 |
5765 | 1337 std::ostringstream output_buf; |
1 | 1338 |
1005 | 1339 if (reading_fcn_file || reading_script_file) |
1340 output_buf << "parse error near line " << input_line_number | |
1607 | 1341 << " of file " << curr_fcn_file_full_name; |
1005 | 1342 else |
1343 output_buf << "parse error:"; | |
581 | 1344 |
1005 | 1345 if (s && strcmp (s, "parse error") != 0) |
1346 output_buf << "\n\n " << s; | |
1347 | |
1348 output_buf << "\n\n"; | |
1 | 1349 |
1755 | 1350 if (! current_input_line.empty ()) |
1 | 1351 { |
1755 | 1352 size_t len = current_input_line.length (); |
1060 | 1353 |
1755 | 1354 if (current_input_line[len-1] == '\n') |
1355 current_input_line.resize (len-1); | |
1005 | 1356 |
4051 | 1357 // Print the line, maybe with a pointer near the error token. |
1005 | 1358 |
1755 | 1359 output_buf << ">>> " << current_input_line << "\n"; |
1060 | 1360 |
1361 if (err_col == 0) | |
1362 err_col = len; | |
1363 | |
1364 for (int i = 0; i < err_col + 3; i++) | |
1365 output_buf << " "; | |
1366 | |
1367 output_buf << "^"; | |
1 | 1368 } |
1005 | 1369 |
5765 | 1370 output_buf << "\n"; |
1371 | |
1372 std::string msg = output_buf.str (); | |
1373 | |
1374 parse_error ("%s", msg.c_str ()); | |
1 | 1375 } |
1376 | |
666 | 1377 // Error mesages for mismatched end tokens. |
1378 | |
496 | 1379 static void |
2805 | 1380 end_error (const char *type, token::end_tok_type ettype, int l, int c) |
496 | 1381 { |
2805 | 1382 static const char *fmt |
1383 = "`%s' command matched by `%s' near line %d column %d"; | |
496 | 1384 |
1385 switch (ettype) | |
1386 { | |
1387 case token::simple_end: | |
1388 error (fmt, type, "end", l, c); | |
1389 break; | |
777 | 1390 |
496 | 1391 case token::for_end: |
1392 error (fmt, type, "endfor", l, c); | |
1393 break; | |
777 | 1394 |
496 | 1395 case token::function_end: |
1396 error (fmt, type, "endfunction", l, c); | |
1397 break; | |
777 | 1398 |
496 | 1399 case token::if_end: |
1400 error (fmt, type, "endif", l, c); | |
1401 break; | |
777 | 1402 |
3233 | 1403 case token::switch_end: |
1404 error (fmt, type, "endswitch", l, c); | |
1405 break; | |
1406 | |
496 | 1407 case token::while_end: |
1408 error (fmt, type, "endwhile", l, c); | |
1409 break; | |
777 | 1410 |
5400 | 1411 case token::try_catch_end: |
1412 error (fmt, type, "end_try_catch", l, c); | |
1413 break; | |
1414 | |
1371 | 1415 case token::unwind_protect_end: |
1416 error (fmt, type, "end_unwind_protect", l, c); | |
1417 break; | |
1418 | |
496 | 1419 default: |
1420 panic_impossible (); | |
1421 break; | |
1422 } | |
1423 } | |
1424 | |
666 | 1425 // Check to see that end tokens are properly matched. |
1426 | |
2857 | 1427 static bool |
1428 end_token_ok (token *tok, token::end_tok_type expected) | |
143 | 1429 { |
2857 | 1430 bool retval = true; |
1431 | |
143 | 1432 token::end_tok_type ettype = tok->ettype (); |
2857 | 1433 |
143 | 1434 if (ettype != expected && ettype != token::simple_end) |
1435 { | |
2857 | 1436 retval = false; |
1437 | |
143 | 1438 yyerror ("parse error"); |
1439 | |
1440 int l = tok->line (); | |
1441 int c = tok->column (); | |
1442 | |
1443 switch (expected) | |
1444 { | |
1445 case token::for_end: | |
1446 end_error ("for", ettype, l, c); | |
1447 break; | |
777 | 1448 |
143 | 1449 case token::function_end: |
1450 end_error ("function", ettype, l, c); | |
1451 break; | |
777 | 1452 |
143 | 1453 case token::if_end: |
1454 end_error ("if", ettype, l, c); | |
1455 break; | |
777 | 1456 |
1489 | 1457 case token::try_catch_end: |
1458 end_error ("try", ettype, l, c); | |
1459 break; | |
1460 | |
2764 | 1461 case token::switch_end: |
1462 end_error ("switch", ettype, l, c); | |
1463 break; | |
1464 | |
1489 | 1465 case token::unwind_protect_end: |
1466 end_error ("unwind_protect", ettype, l, c); | |
1467 break; | |
1468 | |
143 | 1469 case token::while_end: |
1470 end_error ("while", ettype, l, c); | |
1471 break; | |
777 | 1472 |
143 | 1473 default: |
1474 panic_impossible (); | |
1475 break; | |
1476 } | |
1477 } | |
2857 | 1478 |
1479 return retval; | |
143 | 1480 } |
1481 | |
666 | 1482 // Maybe print a warning if an assignment expression is used as the |
1483 // test in a logical expression. | |
1484 | |
496 | 1485 static void |
1486 maybe_warn_assign_as_truth_value (tree_expression *expr) | |
1 | 1487 { |
5781 | 1488 if (expr->is_assignment_expression () |
2961 | 1489 && expr->paren_count () < 2) |
1 | 1490 { |
5781 | 1491 warning_with_id |
1492 ("Octave:assign-as-truth-value", | |
1493 "suggest parenthesis around assignment used as truth value"); | |
1 | 1494 } |
1495 } | |
578 | 1496 |
2764 | 1497 // Maybe print a warning about switch labels that aren't constants. |
1498 | |
1499 static void | |
1500 maybe_warn_variable_switch_label (tree_expression *expr) | |
1501 { | |
5781 | 1502 if (! expr->is_constant ()) |
5878 | 1503 warning_with_id ("Octave:variable-switch-label", |
5781 | 1504 "variable switch label"); |
2764 | 1505 } |
1506 | |
2533 | 1507 static tree_expression * |
1508 fold (tree_binary_expression *e) | |
1509 { | |
3110 | 1510 tree_expression *retval = e; |
1511 | |
3292 | 1512 unwind_protect::begin_frame ("fold_binary_expression"); |
3110 | 1513 |
1514 unwind_protect_int (error_state); | |
4452 | 1515 unwind_protect_int (warning_state); |
3110 | 1516 |
3815 | 1517 unwind_protect_bool (discard_error_messages); |
4452 | 1518 unwind_protect_bool (discard_warning_messages); |
1519 | |
3815 | 1520 discard_error_messages = true; |
4452 | 1521 discard_warning_messages = true; |
2533 | 1522 |
1523 tree_expression *op1 = e->lhs (); | |
1524 tree_expression *op2 = e->rhs (); | |
1525 | |
5161 | 1526 octave_value::binary_op op_type = e->op_type (); |
1527 | |
1528 if (op1->is_constant () && op2->is_constant () | |
5781 | 1529 && (! ((warning_enabled ("Octave:associativity-change") |
5161 | 1530 && (op_type == POW || op_type == EPOW)) |
5781 | 1531 || (warning_enabled ("Octave:precedence-change") |
5161 | 1532 && (op_type == EXPR_OR || op_type == EXPR_OR_OR))))) |
2533 | 1533 { |
2970 | 1534 octave_value tmp = e->rvalue (); |
2533 | 1535 |
3489 | 1536 if (! (error_state || warning_state)) |
2533 | 1537 { |
1538 tree_constant *tc_retval = new tree_constant (tmp); | |
1539 | |
5765 | 1540 std::ostringstream buf; |
2533 | 1541 |
1542 tree_print_code tpc (buf); | |
1543 | |
1544 e->accept (tpc); | |
1545 | |
5765 | 1546 tc_retval->stash_original_text (buf.str ()); |
2533 | 1547 |
1548 delete e; | |
1549 | |
1550 retval = tc_retval; | |
1551 } | |
1552 } | |
3110 | 1553 |
3292 | 1554 unwind_protect::run_frame ("fold_binary_expression"); |
1555 | |
1556 return retval; | |
1557 } | |
1558 | |
1559 static tree_expression * | |
1560 fold (tree_unary_expression *e) | |
1561 { | |
1562 tree_expression *retval = e; | |
1563 | |
1564 unwind_protect::begin_frame ("fold_unary_expression"); | |
1565 | |
1566 unwind_protect_int (error_state); | |
4452 | 1567 unwind_protect_int (warning_state); |
3292 | 1568 |
3815 | 1569 unwind_protect_bool (discard_error_messages); |
4452 | 1570 unwind_protect_bool (discard_warning_messages); |
1571 | |
3815 | 1572 discard_error_messages = true; |
4452 | 1573 discard_warning_messages = true; |
3292 | 1574 |
1575 tree_expression *op = e->operand (); | |
1576 | |
1577 if (op->is_constant ()) | |
1578 { | |
1579 octave_value tmp = e->rvalue (); | |
1580 | |
3489 | 1581 if (! (error_state || warning_state)) |
3292 | 1582 { |
1583 tree_constant *tc_retval = new tree_constant (tmp); | |
1584 | |
5765 | 1585 std::ostringstream buf; |
3292 | 1586 |
1587 tree_print_code tpc (buf); | |
1588 | |
1589 e->accept (tpc); | |
1590 | |
5765 | 1591 tc_retval->stash_original_text (buf.str ()); |
3292 | 1592 |
1593 delete e; | |
1594 | |
1595 retval = tc_retval; | |
1596 } | |
1597 } | |
1598 | |
1599 unwind_protect::run_frame ("fold_unary_expression"); | |
2533 | 1600 |
1601 return retval; | |
1602 } | |
1603 | |
1623 | 1604 // Finish building a range. |
1605 | |
1606 static tree_expression * | |
1607 finish_colon_expression (tree_colon_expression *e) | |
1608 { | |
3110 | 1609 tree_expression *retval = e; |
1610 | |
1611 unwind_protect::begin_frame ("finish_colon_expression"); | |
1612 | |
1613 unwind_protect_int (error_state); | |
4452 | 1614 unwind_protect_int (warning_state); |
3110 | 1615 |
3815 | 1616 unwind_protect_bool (discard_error_messages); |
4452 | 1617 unwind_protect_bool (discard_warning_messages); |
1618 | |
3815 | 1619 discard_error_messages = true; |
4452 | 1620 discard_warning_messages = true; |
1623 | 1621 |
2533 | 1622 tree_expression *base = e->base (); |
1623 tree_expression *limit = e->limit (); | |
1624 tree_expression *incr = e->increment (); | |
1625 | |
2970 | 1626 if (base) |
1623 | 1627 { |
2970 | 1628 if (limit) |
2533 | 1629 { |
2970 | 1630 if (base->is_constant () && limit->is_constant () |
1631 && (! incr || (incr && incr->is_constant ()))) | |
1632 { | |
1633 octave_value tmp = e->rvalue (); | |
1634 | |
3489 | 1635 if (! (error_state || warning_state)) |
2970 | 1636 { |
1637 tree_constant *tc_retval = new tree_constant (tmp); | |
1638 | |
5765 | 1639 std::ostringstream buf; |
2970 | 1640 |
1641 tree_print_code tpc (buf); | |
1642 | |
1643 e->accept (tpc); | |
1644 | |
5765 | 1645 tc_retval->stash_original_text (buf.str ()); |
2970 | 1646 |
1647 delete e; | |
1648 | |
1649 retval = tc_retval; | |
1650 } | |
1651 } | |
2533 | 1652 } |
1653 else | |
2970 | 1654 { |
2990 | 1655 e->preserve_base (); |
1656 delete e; | |
2970 | 1657 |
5775 | 1658 // FIXME -- need to attempt constant folding here |
2970 | 1659 // too (we need a generic way to do that). |
1660 retval = base; | |
1661 } | |
1623 | 1662 } |
1663 | |
3110 | 1664 unwind_protect::run_frame ("finish_colon_expression"); |
1665 | |
1623 | 1666 return retval; |
1667 } | |
1668 | |
1607 | 1669 // Make a constant. |
1670 | |
2375 | 1671 static tree_constant * |
1607 | 1672 make_constant (int op, token *tok_val) |
1673 { | |
1674 int l = tok_val->line (); | |
1675 int c = tok_val->column (); | |
1676 | |
3216 | 1677 tree_constant *retval = 0; |
1607 | 1678 |
1679 switch (op) | |
1680 { | |
1681 case NUM: | |
2533 | 1682 { |
2883 | 1683 octave_value tmp (tok_val->number ()); |
1684 retval = new tree_constant (tmp, l, c); | |
2533 | 1685 retval->stash_original_text (tok_val->text_rep ()); |
1686 } | |
1607 | 1687 break; |
1688 | |
1689 case IMAG_NUM: | |
1690 { | |
2883 | 1691 octave_value tmp (Complex (0.0, tok_val->number ())); |
1692 retval = new tree_constant (tmp, l, c); | |
1607 | 1693 retval->stash_original_text (tok_val->text_rep ()); |
1694 } | |
1695 break; | |
1696 | |
5279 | 1697 case DQ_STRING: |
1698 case SQ_STRING: | |
2883 | 1699 { |
7690
97e535ec65db
make_constant: stash original text for strings
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1700 char delim = op == DQ_STRING ? '"' : '\''; |
97e535ec65db
make_constant: stash original text for strings
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1701 octave_value tmp (tok_val->text (), delim); |
2883 | 1702 retval = new tree_constant (tmp, l, c); |
7690
97e535ec65db
make_constant: stash original text for strings
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1703 // FIXME -- maybe this should also be handled by |
97e535ec65db
make_constant: stash original text for strings
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1704 // tok_val->text_rep () for character strings? |
97e535ec65db
make_constant: stash original text for strings
John W. Eaton <jwe@octave.org>
parents:
7634
diff
changeset
|
1705 retval->stash_original_text (delim + tok_val->text () + delim); |
2883 | 1706 } |
1607 | 1707 break; |
1708 | |
1709 default: | |
1710 panic_impossible (); | |
1711 break; | |
1712 } | |
1713 | |
1714 return retval; | |
1715 } | |
1716 | |
4342 | 1717 // Make a function handle. |
1718 | |
1719 static tree_fcn_handle * | |
1720 make_fcn_handle (token *tok_val) | |
1721 { | |
1722 int l = tok_val->line (); | |
1723 int c = tok_val->column (); | |
1724 | |
1725 tree_fcn_handle *retval = new tree_fcn_handle (tok_val->text (), l, c); | |
1726 | |
1727 return retval; | |
1728 } | |
1729 | |
4935 | 1730 // Make an anonymous function handle. |
1731 | |
5861 | 1732 static tree_anon_fcn_handle * |
4935 | 1733 make_anon_fcn_handle (tree_parameter_list *param_list, tree_statement *stmt) |
1734 { | |
5775 | 1735 // FIXME -- need to get these from the location of the @ symbol. |
4935 | 1736 |
1737 int l = -1; | |
1738 int c = -1; | |
1739 | |
1740 tree_parameter_list *ret_list = 0; | |
1741 | |
7336 | 1742 symbol_table::scope_id fcn_scope = symbol_table::current_scope (); |
5861 | 1743 |
1744 if (symtab_context.empty ()) | |
1745 panic_impossible (); | |
1746 | |
7336 | 1747 symbol_table::set_scope (symtab_context.top ()); |
5861 | 1748 |
1749 symtab_context.pop (); | |
1750 | |
7351 | 1751 stmt->set_print_flag (false); |
4935 | 1752 |
1753 tree_statement_list *body = new tree_statement_list (stmt); | |
1754 | |
1755 body->mark_as_function_body (); | |
1756 | |
5861 | 1757 tree_anon_fcn_handle *retval |
7336 | 1758 = new tree_anon_fcn_handle (param_list, ret_list, body, fcn_scope, l, c); |
4935 | 1759 |
1760 return retval; | |
1761 } | |
1762 | |
5161 | 1763 static void |
1764 maybe_warn_associativity_change (tree_expression *op) | |
1765 { | |
5781 | 1766 if (op->paren_count () == 0 && op->is_binary_expression ()) |
5161 | 1767 { |
1768 tree_binary_expression *e | |
1769 = dynamic_cast<tree_binary_expression *> (op); | |
1770 | |
1771 octave_value::binary_op op_type = e->op_type (); | |
1772 | |
1773 if (op_type == octave_value::op_pow | |
1774 || op_type == octave_value::op_el_pow) | |
1775 { | |
1776 std::string op_str = octave_value::binary_op_as_string (op_type); | |
1777 | |
5781 | 1778 warning_with_id |
1779 ("Octave:associativity-change", | |
1780 "meaning may have changed due to change in associativity for %s operator", op_str.c_str ()); | |
5161 | 1781 } |
1782 } | |
1783 } | |
1784 | |
666 | 1785 // Build a binary expression. |
1786 | |
578 | 1787 static tree_expression * |
1788 make_binary_op (int op, tree_expression *op1, token *tok_val, | |
1789 tree_expression *op2) | |
1790 { | |
2883 | 1791 octave_value::binary_op t = octave_value::unknown_binary_op; |
1623 | 1792 |
578 | 1793 switch (op) |
1794 { | |
1795 case POW: | |
3525 | 1796 t = octave_value::op_pow; |
5161 | 1797 maybe_warn_associativity_change (op1); |
578 | 1798 break; |
777 | 1799 |
578 | 1800 case EPOW: |
3525 | 1801 t = octave_value::op_el_pow; |
5161 | 1802 maybe_warn_associativity_change (op1); |
578 | 1803 break; |
777 | 1804 |
578 | 1805 case '+': |
3525 | 1806 t = octave_value::op_add; |
578 | 1807 break; |
777 | 1808 |
578 | 1809 case '-': |
3525 | 1810 t = octave_value::op_sub; |
578 | 1811 break; |
777 | 1812 |
578 | 1813 case '*': |
3525 | 1814 t = octave_value::op_mul; |
578 | 1815 break; |
777 | 1816 |
578 | 1817 case '/': |
3525 | 1818 t = octave_value::op_div; |
578 | 1819 break; |
777 | 1820 |
578 | 1821 case EMUL: |
3525 | 1822 t = octave_value::op_el_mul; |
578 | 1823 break; |
777 | 1824 |
578 | 1825 case EDIV: |
3525 | 1826 t = octave_value::op_el_div; |
578 | 1827 break; |
777 | 1828 |
578 | 1829 case LEFTDIV: |
3525 | 1830 t = octave_value::op_ldiv; |
578 | 1831 break; |
777 | 1832 |
578 | 1833 case ELEFTDIV: |
3525 | 1834 t = octave_value::op_el_ldiv; |
578 | 1835 break; |
777 | 1836 |
2899 | 1837 case LSHIFT: |
3525 | 1838 t = octave_value::op_lshift; |
2899 | 1839 break; |
1840 | |
1841 case RSHIFT: | |
3525 | 1842 t = octave_value::op_rshift; |
2899 | 1843 break; |
1844 | |
578 | 1845 case EXPR_LT: |
3525 | 1846 t = octave_value::op_lt; |
578 | 1847 break; |
777 | 1848 |
578 | 1849 case EXPR_LE: |
3525 | 1850 t = octave_value::op_le; |
578 | 1851 break; |
777 | 1852 |
578 | 1853 case EXPR_EQ: |
3525 | 1854 t = octave_value::op_eq; |
578 | 1855 break; |
777 | 1856 |
578 | 1857 case EXPR_GE: |
3525 | 1858 t = octave_value::op_ge; |
578 | 1859 break; |
777 | 1860 |
578 | 1861 case EXPR_GT: |
3525 | 1862 t = octave_value::op_gt; |
578 | 1863 break; |
777 | 1864 |
578 | 1865 case EXPR_NE: |
3525 | 1866 t = octave_value::op_ne; |
578 | 1867 break; |
777 | 1868 |
578 | 1869 case EXPR_AND: |
3525 | 1870 t = octave_value::op_el_and; |
578 | 1871 break; |
777 | 1872 |
578 | 1873 case EXPR_OR: |
3525 | 1874 t = octave_value::op_el_or; |
5781 | 1875 if (op2->paren_count () == 0 && op2->is_binary_expression ()) |
4023 | 1876 { |
1877 tree_binary_expression *e | |
1878 = dynamic_cast<tree_binary_expression *> (op2); | |
1879 | |
1880 if (e->op_type () == octave_value::op_el_and) | |
5781 | 1881 warning_with_id |
1882 ("Octave:precedence-change", | |
1883 "meaning may have changed due to change in precedence for & and | operators"); | |
4023 | 1884 } |
578 | 1885 break; |
777 | 1886 |
578 | 1887 default: |
1888 panic_impossible (); | |
1889 break; | |
1890 } | |
1891 | |
1892 int l = tok_val->line (); | |
1893 int c = tok_val->column (); | |
1894 | |
2533 | 1895 tree_binary_expression *e |
1896 = new tree_binary_expression (op1, op2, l, c, t); | |
1623 | 1897 |
2533 | 1898 return fold (e); |
578 | 1899 } |
1900 | |
2375 | 1901 // Build a boolean expression. |
666 | 1902 |
578 | 1903 static tree_expression * |
2375 | 1904 make_boolean_op (int op, tree_expression *op1, token *tok_val, |
1905 tree_expression *op2) | |
578 | 1906 { |
2375 | 1907 tree_boolean_expression::type t; |
1908 | |
578 | 1909 switch (op) |
1910 { | |
2375 | 1911 case EXPR_AND_AND: |
2805 | 1912 t = tree_boolean_expression::bool_and; |
578 | 1913 break; |
777 | 1914 |
2375 | 1915 case EXPR_OR_OR: |
2805 | 1916 t = tree_boolean_expression::bool_or; |
5781 | 1917 if (op2->paren_count () == 0 && op2->is_boolean_expression ()) |
4023 | 1918 { |
1919 tree_boolean_expression *e | |
1920 = dynamic_cast<tree_boolean_expression *> (op2); | |
1921 | |
1922 if (e->op_type () == tree_boolean_expression::bool_and) | |
5781 | 1923 warning_with_id |
1924 ("Octave:precedence-change", | |
1925 "meaning may have changed due to change in precedence for && and || operators"); | |
4023 | 1926 } |
578 | 1927 break; |
777 | 1928 |
578 | 1929 default: |
1930 panic_impossible (); | |
1931 break; | |
1932 } | |
1933 | |
1934 int l = tok_val->line (); | |
1935 int c = tok_val->column (); | |
1936 | |
2533 | 1937 tree_boolean_expression *e |
1938 = new tree_boolean_expression (op1, op2, l, c, t); | |
2375 | 1939 |
2533 | 1940 return fold (e); |
578 | 1941 } |
1942 | |
2375 | 1943 // Build a prefix expression. |
666 | 1944 |
578 | 1945 static tree_expression * |
2960 | 1946 make_prefix_op (int op, tree_expression *op1, token *tok_val) |
578 | 1947 { |
3203 | 1948 octave_value::unary_op t = octave_value::unknown_unary_op; |
2375 | 1949 |
578 | 1950 switch (op) |
1951 { | |
2960 | 1952 case EXPR_NOT: |
3525 | 1953 t = octave_value::op_not; |
2960 | 1954 break; |
1955 | |
4965 | 1956 case '+': |
1957 t = octave_value::op_uplus; | |
1958 break; | |
1959 | |
2960 | 1960 case '-': |
3525 | 1961 t = octave_value::op_uminus; |
2960 | 1962 break; |
1963 | |
578 | 1964 case PLUS_PLUS: |
3525 | 1965 t = octave_value::op_incr; |
578 | 1966 break; |
777 | 1967 |
578 | 1968 case MINUS_MINUS: |
3525 | 1969 t = octave_value::op_decr; |
578 | 1970 break; |
777 | 1971 |
578 | 1972 default: |
1973 panic_impossible (); | |
1974 break; | |
1975 } | |
1976 | |
1977 int l = tok_val->line (); | |
1978 int c = tok_val->column (); | |
1979 | |
3292 | 1980 tree_prefix_expression *e |
1981 = new tree_prefix_expression (op1, l, c, t); | |
1982 | |
1983 return fold (e); | |
578 | 1984 } |
1985 | |
2375 | 1986 // Build a postfix expression. |
666 | 1987 |
578 | 1988 static tree_expression * |
2960 | 1989 make_postfix_op (int op, tree_expression *op1, token *tok_val) |
578 | 1990 { |
3203 | 1991 octave_value::unary_op t = octave_value::unknown_unary_op; |
1623 | 1992 |
578 | 1993 switch (op) |
1994 { | |
2960 | 1995 case QUOTE: |
3525 | 1996 t = octave_value::op_hermitian; |
2960 | 1997 break; |
1998 | |
1999 case TRANSPOSE: | |
3525 | 2000 t = octave_value::op_transpose; |
2960 | 2001 break; |
2002 | |
2375 | 2003 case PLUS_PLUS: |
3525 | 2004 t = octave_value::op_incr; |
578 | 2005 break; |
777 | 2006 |
2375 | 2007 case MINUS_MINUS: |
3525 | 2008 t = octave_value::op_decr; |
578 | 2009 break; |
777 | 2010 |
578 | 2011 default: |
2012 panic_impossible (); | |
2013 break; | |
2014 } | |
2015 | |
2016 int l = tok_val->line (); | |
2017 int c = tok_val->column (); | |
2018 | |
3292 | 2019 tree_postfix_expression *e |
2020 = new tree_postfix_expression (op1, l, c, t); | |
2021 | |
2022 return fold (e); | |
1623 | 2023 } |
2024 | |
2025 // Build an unwind-protect command. | |
2026 | |
2027 static tree_command * | |
2028 make_unwind_command (token *unwind_tok, tree_statement_list *body, | |
3665 | 2029 tree_statement_list *cleanup, token *end_tok, |
2030 octave_comment_list *lc, octave_comment_list *mc) | |
1623 | 2031 { |
2032 tree_command *retval = 0; | |
2033 | |
2857 | 2034 if (end_token_ok (end_tok, token::unwind_protect_end)) |
1623 | 2035 { |
3665 | 2036 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
2037 | |
1623 | 2038 int l = unwind_tok->line (); |
2039 int c = unwind_tok->column (); | |
2040 | |
3665 | 2041 retval = new tree_unwind_protect_command (body, cleanup, |
2042 lc, mc, tc, l, c); | |
1623 | 2043 } |
2044 | |
2045 return retval; | |
2046 } | |
2047 | |
2048 // Build a try-catch command. | |
2049 | |
2050 static tree_command * | |
2051 make_try_command (token *try_tok, tree_statement_list *body, | |
3665 | 2052 tree_statement_list *cleanup, token *end_tok, |
2053 octave_comment_list *lc, octave_comment_list *mc) | |
1623 | 2054 { |
2055 tree_command *retval = 0; | |
2056 | |
2857 | 2057 if (end_token_ok (end_tok, token::try_catch_end)) |
1623 | 2058 { |
3665 | 2059 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
2060 | |
1623 | 2061 int l = try_tok->line (); |
2062 int c = try_tok->column (); | |
2063 | |
3665 | 2064 retval = new tree_try_catch_command (body, cleanup, |
2065 lc, mc, tc, l, c); | |
1623 | 2066 } |
2067 | |
2068 return retval; | |
2069 } | |
2070 | |
2071 // Build a while command. | |
2072 | |
2073 static tree_command * | |
2074 make_while_command (token *while_tok, tree_expression *expr, | |
3665 | 2075 tree_statement_list *body, token *end_tok, |
2076 octave_comment_list *lc) | |
1623 | 2077 { |
2078 tree_command *retval = 0; | |
2079 | |
2080 maybe_warn_assign_as_truth_value (expr); | |
2081 | |
2857 | 2082 if (end_token_ok (end_tok, token::while_end)) |
1623 | 2083 { |
3665 | 2084 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
2085 | |
1826 | 2086 lexer_flags.looping--; |
1623 | 2087 |
2088 int l = while_tok->line (); | |
2089 int c = while_tok->column (); | |
2090 | |
3665 | 2091 retval = new tree_while_command (expr, body, lc, tc, l, c); |
1623 | 2092 } |
2093 | |
2094 return retval; | |
2095 } | |
2096 | |
3484 | 2097 // Build a do-until command. |
2098 | |
2099 static tree_command * | |
2100 make_do_until_command (token *do_tok, tree_statement_list *body, | |
3665 | 2101 tree_expression *expr, octave_comment_list *lc) |
3484 | 2102 { |
2103 tree_command *retval = 0; | |
2104 | |
2105 maybe_warn_assign_as_truth_value (expr); | |
2106 | |
3665 | 2107 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
2108 | |
3484 | 2109 lexer_flags.looping--; |
2110 | |
2111 int l = do_tok->line (); | |
2112 int c = do_tok->column (); | |
2113 | |
3665 | 2114 retval = new tree_do_until_command (expr, body, lc, tc, l, c); |
3484 | 2115 |
2116 return retval; | |
2117 } | |
2118 | |
1623 | 2119 // Build a for command. |
2120 | |
2121 static tree_command * | |
2970 | 2122 make_for_command (token *for_tok, tree_argument_list *lhs, |
1623 | 2123 tree_expression *expr, tree_statement_list *body, |
3665 | 2124 token *end_tok, octave_comment_list *lc) |
1623 | 2125 { |
2126 tree_command *retval = 0; | |
2127 | |
2857 | 2128 if (end_token_ok (end_tok, token::for_end)) |
1623 | 2129 { |
3665 | 2130 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
2131 | |
1826 | 2132 lexer_flags.looping--; |
1623 | 2133 |
2134 int l = for_tok->line (); | |
2135 int c = for_tok->column (); | |
2136 | |
2970 | 2137 if (lhs->length () == 1) |
2138 { | |
2139 tree_expression *tmp = lhs->remove_front (); | |
2140 | |
3665 | 2141 retval = new tree_simple_for_command (tmp, expr, body, |
2142 lc, tc, l, c); | |
2970 | 2143 |
2144 delete lhs; | |
2145 } | |
2146 else | |
3665 | 2147 retval = new tree_complex_for_command (lhs, expr, body, |
2148 lc, tc, l, c); | |
1623 | 2149 } |
2150 | |
2151 return retval; | |
2152 } | |
2153 | |
4207 | 2154 // Build a break command. |
2155 | |
2156 static tree_command * | |
2157 make_break_command (token *break_tok) | |
1623 | 2158 { |
4207 | 2159 tree_command *retval = 0; |
1623 | 2160 |
2620 | 2161 int l = break_tok->line (); |
2162 int c = break_tok->column (); | |
2163 | |
3489 | 2164 if (lexer_flags.looping || lexer_flags.defining_func |
3877 | 2165 || reading_script_file || evaluating_function_body |
2166 || evaluating_looping_command) | |
4207 | 2167 retval = new tree_break_command (l, c); |
1623 | 2168 else |
4207 | 2169 retval = new tree_no_op_command ("break", l, c); |
1623 | 2170 |
2171 return retval; | |
2172 } | |
2173 | |
4207 | 2174 // Build a continue command. |
2175 | |
2176 static tree_command * | |
2177 make_continue_command (token *continue_tok) | |
1623 | 2178 { |
4207 | 2179 tree_command *retval = 0; |
1623 | 2180 |
2620 | 2181 int l = continue_tok->line (); |
2182 int c = continue_tok->column (); | |
2183 | |
3877 | 2184 if (lexer_flags.looping || evaluating_looping_command) |
4207 | 2185 retval = new tree_continue_command (l, c); |
1623 | 2186 else |
4207 | 2187 retval = new tree_no_op_command ("continue", l, c); |
1623 | 2188 |
2189 return retval; | |
2190 } | |
2191 | |
4207 | 2192 // Build a return command. |
2193 | |
2194 static tree_command * | |
2195 make_return_command (token *return_tok) | |
1623 | 2196 { |
4207 | 2197 tree_command *retval = 0; |
1623 | 2198 |
2620 | 2199 int l = return_tok->line (); |
2200 int c = return_tok->column (); | |
2201 | |
3489 | 2202 if (lexer_flags.defining_func || reading_script_file |
2203 || evaluating_function_body) | |
4207 | 2204 retval = new tree_return_command (l, c); |
1623 | 2205 else |
4207 | 2206 retval = new tree_no_op_command ("return", l, c); |
1623 | 2207 |
2208 return retval; | |
2209 } | |
2210 | |
2211 // Start an if command. | |
2212 | |
2213 static tree_if_command_list * | |
2214 start_if_command (tree_expression *expr, tree_statement_list *list) | |
2215 { | |
2216 maybe_warn_assign_as_truth_value (expr); | |
2217 | |
2218 tree_if_clause *t = new tree_if_clause (expr, list); | |
2219 | |
2220 return new tree_if_command_list (t); | |
2221 } | |
2222 | |
2223 // Finish an if command. | |
2224 | |
2225 static tree_if_command * | |
2226 finish_if_command (token *if_tok, tree_if_command_list *list, | |
3665 | 2227 token *end_tok, octave_comment_list *lc) |
1623 | 2228 { |
2229 tree_if_command *retval = 0; | |
2230 | |
2857 | 2231 if (end_token_ok (end_tok, token::if_end)) |
1623 | 2232 { |
3665 | 2233 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
2234 | |
1623 | 2235 int l = if_tok->line (); |
2236 int c = if_tok->column (); | |
2237 | |
3665 | 2238 retval = new tree_if_command (list, lc, tc, l, c); |
1623 | 2239 } |
2240 | |
2241 return retval; | |
2242 } | |
2243 | |
2244 // Build an elseif clause. | |
2245 | |
2246 static tree_if_clause * | |
3665 | 2247 make_elseif_clause (tree_expression *expr, tree_statement_list *list, |
2248 octave_comment_list *lc) | |
1623 | 2249 { |
2250 maybe_warn_assign_as_truth_value (expr); | |
2251 | |
3665 | 2252 return new tree_if_clause (expr, list, lc); |
1623 | 2253 } |
2254 | |
2764 | 2255 // Finish a switch command. |
2256 | |
2257 static tree_switch_command * | |
2258 finish_switch_command (token *switch_tok, tree_expression *expr, | |
3665 | 2259 tree_switch_case_list *list, token *end_tok, |
2260 octave_comment_list *lc) | |
2764 | 2261 { |
2262 tree_switch_command *retval = 0; | |
2263 | |
2857 | 2264 if (end_token_ok (end_tok, token::switch_end)) |
2764 | 2265 { |
3665 | 2266 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
2267 | |
2764 | 2268 int l = switch_tok->line (); |
2269 int c = switch_tok->column (); | |
2270 | |
3665 | 2271 retval = new tree_switch_command (expr, list, lc, tc, l, c); |
2764 | 2272 } |
2273 | |
2274 return retval; | |
2275 } | |
2276 | |
2277 // Build a switch case. | |
2278 | |
2279 static tree_switch_case * | |
3665 | 2280 make_switch_case (tree_expression *expr, tree_statement_list *list, |
2281 octave_comment_list *lc) | |
2764 | 2282 { |
2283 maybe_warn_variable_switch_label (expr); | |
2284 | |
3665 | 2285 return new tree_switch_case (expr, list, lc); |
2764 | 2286 } |
2287 | |
1623 | 2288 // Build an assignment to a variable. |
2289 | |
2290 static tree_expression * | |
2970 | 2291 make_assign_op (int op, tree_argument_list *lhs, token *eq_tok, |
2292 tree_expression *rhs) | |
1623 | 2293 { |
2970 | 2294 tree_expression *retval = 0; |
2295 | |
2883 | 2296 octave_value::assign_op t = octave_value::unknown_assign_op; |
2297 | |
2298 switch (op) | |
2299 { | |
2300 case '=': | |
3525 | 2301 t = octave_value::op_asn_eq; |
2883 | 2302 break; |
2303 | |
2304 case ADD_EQ: | |
3525 | 2305 t = octave_value::op_add_eq; |
2883 | 2306 break; |
2307 | |
2308 case SUB_EQ: | |
3525 | 2309 t = octave_value::op_sub_eq; |
2883 | 2310 break; |
2311 | |
2312 case MUL_EQ: | |
3525 | 2313 t = octave_value::op_mul_eq; |
2883 | 2314 break; |
2315 | |
2316 case DIV_EQ: | |
3525 | 2317 t = octave_value::op_div_eq; |
2883 | 2318 break; |
2319 | |
3204 | 2320 case LEFTDIV_EQ: |
3525 | 2321 t = octave_value::op_ldiv_eq; |
3204 | 2322 break; |
2323 | |
4018 | 2324 case POW_EQ: |
2325 t = octave_value::op_pow_eq; | |
2326 break; | |
2327 | |
2899 | 2328 case LSHIFT_EQ: |
3525 | 2329 t = octave_value::op_lshift_eq; |
2899 | 2330 break; |
2331 | |
2332 case RSHIFT_EQ: | |
3525 | 2333 t = octave_value::op_rshift_eq; |
2899 | 2334 break; |
2335 | |
2883 | 2336 case EMUL_EQ: |
3525 | 2337 t = octave_value::op_el_mul_eq; |
2883 | 2338 break; |
2339 | |
2340 case EDIV_EQ: | |
3525 | 2341 t = octave_value::op_el_div_eq; |
2883 | 2342 break; |
2343 | |
3204 | 2344 case ELEFTDIV_EQ: |
3525 | 2345 t = octave_value::op_el_ldiv_eq; |
3204 | 2346 break; |
2347 | |
4018 | 2348 case EPOW_EQ: |
2349 t = octave_value::op_el_pow_eq; | |
2350 break; | |
2351 | |
2883 | 2352 case AND_EQ: |
3525 | 2353 t = octave_value::op_el_and_eq; |
2883 | 2354 break; |
2355 | |
2356 case OR_EQ: | |
3525 | 2357 t = octave_value::op_el_or_eq; |
2883 | 2358 break; |
2359 | |
2360 default: | |
2361 panic_impossible (); | |
2362 break; | |
2363 } | |
2364 | |
1623 | 2365 int l = eq_tok->line (); |
2366 int c = eq_tok->column (); | |
2367 | |
5841 | 2368 if (lhs->is_simple_assign_lhs ()) |
666 | 2369 { |
2970 | 2370 tree_expression *tmp = lhs->remove_front (); |
2371 | |
2372 retval = new tree_simple_assignment (tmp, rhs, false, l, c, t); | |
2373 | |
2374 delete lhs; | |
666 | 2375 } |
2376 else | |
3208 | 2377 return new tree_multi_assignment (lhs, rhs, false, l, c, t); |
666 | 2378 |
2379 return retval; | |
2380 } | |
751 | 2381 |
1623 | 2382 // Begin defining a function. |
2383 | |
2891 | 2384 static octave_user_function * |
2385 start_function (tree_parameter_list *param_list, tree_statement_list *body) | |
1623 | 2386 { |
2387 body->mark_as_function_body (); | |
2388 | |
2891 | 2389 // We'll fill in the return list later. |
2390 | |
2391 octave_user_function *fcn | |
7336 | 2392 = new octave_user_function (symbol_table::current_scope (), |
2393 param_list, 0, body); | |
2394 | |
1623 | 2395 |
3665 | 2396 if (fcn) |
2397 { | |
2398 octave_comment_list *tc = octave_comment_buffer::get_comment (); | |
2399 | |
2400 fcn->stash_trailing_comment (tc); | |
2401 } | |
2402 | |
1623 | 2403 return fcn; |
2404 } | |
2405 | |
2406 // Do most of the work for defining a function. | |
2407 | |
2891 | 2408 static octave_user_function * |
4872 | 2409 frob_function (const std::string& fname, octave_user_function *fcn) |
1623 | 2410 { |
4872 | 2411 std::string id_name = fname; |
1623 | 2412 |
2413 // If input is coming from a file, issue a warning if the name of | |
2414 // the file does not match the name of the function stated in the | |
2415 // file. Matlab doesn't provide a diagnostic (it ignores the stated | |
2416 // name). | |
2417 | |
5484 | 2418 if (reading_fcn_file || autoloading) |
1623 | 2419 { |
7336 | 2420 if (! (autoloading |
2421 || lexer_flags.parsing_nested_function | |
2422 || lexer_flags.parsing_class_method)) | |
1623 | 2423 { |
7336 | 2424 // FIXME -- should curr_fcn_file_name already be |
2425 // preprocessed when we get here? It seems to only be a | |
2426 // problem with relative file names. | |
2427 | |
2428 std::string nm = curr_fcn_file_name; | |
2429 | |
2430 size_t pos = nm.find_last_of (file_ops::dir_sep_chars); | |
2431 | |
2432 if (pos != NPOS) | |
2433 nm = curr_fcn_file_name.substr (pos+1); | |
2434 | |
2435 if (nm != id_name) | |
2436 { | |
2437 warning_with_id | |
2438 ("Octave:function-name-clash", | |
2439 "function name `%s' does not agree with function file name `%s'", | |
2440 id_name.c_str (), curr_fcn_file_full_name.c_str ()); | |
2441 | |
2442 id_name = nm; | |
2443 } | |
1623 | 2444 } |
2445 | |
3712 | 2446 octave_time now; |
3162 | 2447 |
4343 | 2448 fcn->stash_fcn_file_name (curr_fcn_file_full_name); |
3162 | 2449 fcn->stash_fcn_file_time (now); |
1623 | 2450 fcn->mark_as_system_fcn_file (); |
3162 | 2451 |
6323 | 2452 if (fcn_file_from_relative_lookup) |
2453 fcn->mark_relative (); | |
2454 | |
2455 if (lexer_flags.parsing_nested_function) | |
2456 fcn->stash_parent_fcn_name (parent_function_name); | |
2457 | |
7336 | 2458 if (lexer_flags.parsing_class_method) |
2459 { | |
2460 if (current_class_name == id_name) | |
2461 fcn->mark_as_class_constructor (); | |
2462 else | |
2463 fcn->mark_as_class_method (); | |
2464 | |
2465 fcn->stash_dispatch_class (current_class_name); | |
2466 } | |
2467 | |
5781 | 2468 std::string nm = fcn->fcn_file_name (); |
2469 | |
2470 file_stat fs (nm); | |
2471 | |
2472 if (fs && fs.is_newer (now)) | |
2473 warning_with_id ("Octave:future-time-stamp", | |
2474 "time stamp for `%s' is in the future", nm.c_str ()); | |
1623 | 2475 } |
2476 else if (! (input_from_tmp_history_file || input_from_startup_file) | |
2477 && reading_script_file | |
1755 | 2478 && curr_fcn_file_name == id_name) |
1623 | 2479 { |
2480 warning ("function `%s' defined within script file `%s'", | |
1755 | 2481 id_name.c_str (), curr_fcn_file_full_name.c_str ()); |
1623 | 2482 } |
2483 | |
4872 | 2484 fcn->stash_function_name (id_name); |
2485 | |
7336 | 2486 if (! help_buf.empty ()) |
2487 { | |
2488 fcn->document (help_buf.top ()); | |
2489 | |
2490 help_buf.pop (); | |
2491 } | |
2492 | |
2493 if (lexer_flags.parsing_nested_function) | |
5609 | 2494 { |
7336 | 2495 std::string nm = fcn->name (); |
2496 | |
2497 fcn->mark_as_nested_function (); | |
2498 | |
2499 symbol_table::install_subfunction (nm, octave_value (fcn)); | |
2500 | |
2501 if (lexer_flags.parsing_nested_function < 0) | |
2502 { | |
2503 lexer_flags.parsing_nested_function = 0; | |
2504 symbol_table::reset_parent_scope (); | |
5397 | 2505 } |
4238 | 2506 } |
7336 | 2507 else if (! reading_fcn_file) |
4426 | 2508 { |
7336 | 2509 std::string nm = fcn->name (); |
2510 | |
2511 symbol_table::install_cmdline_function (nm, octave_value (fcn)); | |
2512 | |
2513 // Make sure that any variable with the same name as the new | |
2514 // function is cleared. | |
2515 | |
2516 symbol_table::varref (nm) = octave_value (); | |
4426 | 2517 } |
7336 | 2518 else |
2519 curr_fcn_ptr = fcn; | |
4426 | 2520 |
1623 | 2521 return fcn; |
2522 } | |
2523 | |
2524 // Finish defining a function. | |
2525 | |
7336 | 2526 static void |
3665 | 2527 finish_function (tree_parameter_list *ret_list, |
2528 octave_user_function *fcn, octave_comment_list *lc) | |
1623 | 2529 { |
2530 ret_list->mark_as_formal_parameters (); | |
2531 | |
3665 | 2532 fcn->stash_leading_comment (lc); |
2533 | |
7336 | 2534 fcn->define_ret_list (ret_list); |
1623 | 2535 } |
2536 | |
2883 | 2537 static void |
2538 recover_from_parsing_function (void) | |
2539 { | |
4238 | 2540 if (symtab_context.empty ()) |
3903 | 2541 panic_impossible (); |
2542 | |
7336 | 2543 symbol_table::set_scope (symtab_context.top ()); |
4238 | 2544 symtab_context.pop (); |
2883 | 2545 |
2546 lexer_flags.defining_func = false; | |
2547 lexer_flags.beginning_of_function = false; | |
2548 lexer_flags.parsed_function_name = false; | |
2549 lexer_flags.looking_at_return_list = false; | |
2550 lexer_flags.looking_at_parameter_list = false; | |
2551 } | |
2552 | |
2846 | 2553 // Make an index expression. |
2554 | |
751 | 2555 static tree_index_expression * |
3929 | 2556 make_index_expression (tree_expression *expr, tree_argument_list *args, |
3933 | 2557 char type) |
751 | 2558 { |
2559 tree_index_expression *retval = 0; | |
2560 | |
2970 | 2561 int l = expr->line (); |
2562 int c = expr->column (); | |
2563 | |
2564 expr->mark_postfix_indexed (); | |
2565 | |
3933 | 2566 if (expr->is_index_expression ()) |
2567 { | |
2568 tree_index_expression *tmp = static_cast<tree_index_expression *> (expr); | |
2569 | |
2570 tmp->append (args, type); | |
2571 | |
2572 retval = tmp; | |
2573 } | |
2574 else | |
2575 retval = new tree_index_expression (expr, args, l, c, type); | |
2970 | 2576 |
2577 return retval; | |
2578 } | |
2579 | |
2580 // Make an indirect reference expression. | |
2581 | |
3930 | 2582 static tree_index_expression * |
3523 | 2583 make_indirect_ref (tree_expression *expr, const std::string& elt) |
2970 | 2584 { |
3930 | 2585 tree_index_expression *retval = 0; |
2970 | 2586 |
2587 int l = expr->line (); | |
2588 int c = expr->column (); | |
2589 | |
3933 | 2590 if (expr->is_index_expression ()) |
2591 { | |
2592 tree_index_expression *tmp = static_cast<tree_index_expression *> (expr); | |
2593 | |
2594 tmp->append (elt); | |
2595 | |
2596 retval = tmp; | |
2597 } | |
2598 else | |
2599 retval = new tree_index_expression (expr, elt, l, c); | |
2970 | 2600 |
2601 lexer_flags.looking_at_indirect_ref = false; | |
751 | 2602 |
2603 return retval; | |
2604 } | |
1511 | 2605 |
4131 | 2606 // Make an indirect reference expression with dynamic field name. |
2607 | |
2608 static tree_index_expression * | |
2609 make_indirect_ref (tree_expression *expr, tree_expression *elt) | |
2610 { | |
2611 tree_index_expression *retval = 0; | |
2612 | |
2613 int l = expr->line (); | |
2614 int c = expr->column (); | |
2615 | |
2616 if (expr->is_index_expression ()) | |
2617 { | |
2618 tree_index_expression *tmp = static_cast<tree_index_expression *> (expr); | |
2619 | |
2620 tmp->append (elt); | |
2621 | |
2622 retval = tmp; | |
2623 } | |
2624 else | |
2625 retval = new tree_index_expression (expr, elt, l, c); | |
2626 | |
2627 lexer_flags.looking_at_indirect_ref = false; | |
2628 | |
2629 return retval; | |
2630 } | |
2631 | |
2846 | 2632 // Make a declaration command. |
2633 | |
2634 static tree_decl_command * | |
2635 make_decl_command (int tok, token *tok_val, tree_decl_init_list *lst) | |
2636 { | |
2637 tree_decl_command *retval = 0; | |
2638 | |
2639 int l = tok_val->line (); | |
2640 int c = tok_val->column (); | |
2641 | |
2642 switch (tok) | |
2643 { | |
2644 case GLOBAL: | |
2645 retval = new tree_global_command (lst, l, c); | |
2646 break; | |
2647 | |
2648 case STATIC: | |
2649 if (lexer_flags.defining_func) | |
2650 retval = new tree_static_command (lst, l, c); | |
2651 else | |
2652 { | |
2653 if (reading_script_file) | |
4844 | 2654 warning ("ignoring persistent declaration near line %d of file `%s'", |
2846 | 2655 l, curr_fcn_file_full_name.c_str ()); |
2656 else | |
4844 | 2657 warning ("ignoring persistent declaration near line %d", l); |
2846 | 2658 } |
2659 break; | |
2660 | |
2661 default: | |
2662 panic_impossible (); | |
2663 break; | |
2664 } | |
2665 | |
2666 return retval; | |
2667 } | |
2668 | |
1623 | 2669 // Finish building a matrix list. |
2670 | |
2671 static tree_expression * | |
1829 | 2672 finish_matrix (tree_matrix *m) |
1623 | 2673 { |
3110 | 2674 tree_expression *retval = m; |
2675 | |
2676 unwind_protect::begin_frame ("finish_matrix"); | |
2677 | |
2678 unwind_protect_int (error_state); | |
4452 | 2679 unwind_protect_int (warning_state); |
3110 | 2680 |
3815 | 2681 unwind_protect_bool (discard_error_messages); |
4452 | 2682 unwind_protect_bool (discard_warning_messages); |
2683 | |
3815 | 2684 discard_error_messages = true; |
4452 | 2685 discard_warning_messages = true; |
1623 | 2686 |
2533 | 2687 if (m->all_elements_are_constant ()) |
1829 | 2688 { |
2970 | 2689 octave_value tmp = m->rvalue (); |
1623 | 2690 |
3489 | 2691 if (! (error_state || warning_state)) |
2533 | 2692 { |
2693 tree_constant *tc_retval = new tree_constant (tmp); | |
2694 | |
5765 | 2695 std::ostringstream buf; |
2533 | 2696 |
2697 tree_print_code tpc (buf); | |
2698 | |
2699 m->accept (tpc); | |
2700 | |
5765 | 2701 tc_retval->stash_original_text (buf.str ()); |
2533 | 2702 |
2703 delete m; | |
2704 | |
2705 retval = tc_retval; | |
2706 } | |
1623 | 2707 } |
3110 | 2708 |
2709 unwind_protect::run_frame ("finish_matrix"); | |
1623 | 2710 |
2711 return retval; | |
2712 } | |
2713 | |
3351 | 2714 // Finish building a cell list. |
2715 | |
2716 static tree_expression * | |
2717 finish_cell (tree_cell *c) | |
2718 { | |
5875 | 2719 return finish_matrix (c); |
3351 | 2720 } |
2721 | |
1511 | 2722 static void |
2723 maybe_warn_missing_semi (tree_statement_list *t) | |
2724 { | |
5781 | 2725 if (lexer_flags.defining_func) |
1511 | 2726 { |
4219 | 2727 tree_statement *tmp = t->back(); |
1607 | 2728 |
1511 | 2729 if (tmp->is_expression ()) |
5781 | 2730 warning_with_id |
2731 ("Octave:missing-semicolon", | |
2732 "missing semicolon near line %d, column %d in file `%s'", | |
2733 tmp->line (), tmp->column (), curr_fcn_file_full_name.c_str ()); | |
1511 | 2734 } |
2735 } | |
1994 | 2736 |
2525 | 2737 static void |
2738 set_stmt_print_flag (tree_statement_list *list, char sep, | |
2739 bool warn_missing_semi) | |
2740 { | |
2741 switch (sep) | |
2742 { | |
2743 case ';': | |
2744 { | |
4219 | 2745 tree_statement *tmp = list->back (); |
2525 | 2746 tmp->set_print_flag (0); |
2747 } | |
2748 break; | |
2749 | |
2750 case 0: | |
2751 case ',': | |
2752 case '\n': | |
2753 if (warn_missing_semi) | |
2754 maybe_warn_missing_semi (list); | |
2755 break; | |
2756 | |
2757 default: | |
2758 warning ("unrecognized separator type!"); | |
2759 break; | |
2760 } | |
2761 } | |
2762 | |
3021 | 2763 void |
2764 parse_and_execute (FILE *f) | |
2765 { | |
2766 unwind_protect::begin_frame ("parse_and_execute"); | |
3880 | 2767 |
5309 | 2768 unwind_protect_ptr (global_command); |
2769 | |
3021 | 2770 YY_BUFFER_STATE old_buf = current_buffer (); |
2771 YY_BUFFER_STATE new_buf = create_buffer (f); | |
2772 | |
2773 unwind_protect::add (restore_input_buffer, old_buf); | |
2774 unwind_protect::add (delete_input_buffer, new_buf); | |
2775 | |
2776 switch_to_buffer (new_buf); | |
2777 | |
2778 unwind_protect_bool (line_editing); | |
3880 | 2779 unwind_protect_bool (get_input_from_eval_string); |
3883 | 2780 unwind_protect_bool (parser_end_of_input); |
3021 | 2781 |
2782 line_editing = false; | |
3880 | 2783 get_input_from_eval_string = false; |
3883 | 2784 parser_end_of_input = false; |
3021 | 2785 |
2786 int retval; | |
2787 do | |
2788 { | |
2789 reset_parser (); | |
2790 | |
2791 retval = yyparse (); | |
2792 | |
3883 | 2793 if (retval == 0) |
2794 { | |
2795 if (global_command) | |
3021 | 2796 { |
3883 | 2797 global_command->eval (); |
2798 | |
2799 delete global_command; | |
2800 | |
2801 global_command = 0; | |
2802 | |
4171 | 2803 OCTAVE_QUIT; |
2804 | |
4207 | 2805 bool quit = (tree_return_command::returning |
2806 || tree_break_command::breaking); | |
2807 | |
2808 if (tree_return_command::returning) | |
2809 tree_return_command::returning = 0; | |
2810 | |
2811 if (tree_break_command::breaking) | |
2812 tree_break_command::breaking--; | |
3883 | 2813 |
2814 if (error_state) | |
2815 { | |
2816 error ("near line %d of file `%s'", input_line_number, | |
2817 curr_fcn_file_full_name.c_str ()); | |
2818 | |
2819 break; | |
2820 } | |
2821 | |
2822 if (quit) | |
2823 break; | |
3021 | 2824 } |
3883 | 2825 else if (parser_end_of_input) |
3021 | 2826 break; |
3883 | 2827 } |
3021 | 2828 } |
2829 while (retval == 0); | |
2830 | |
2831 unwind_protect::run_frame ("parse_and_execute"); | |
2832 } | |
2833 | |
2834 static void | |
2835 safe_fclose (void *f) | |
2836 { | |
5775 | 2837 // FIXME -- comments at the end of an input file are |
3765 | 2838 // discarded (otherwise, they would be appended to the next |
2839 // statement, possibly from the command line or another file, which | |
2840 // can be quite confusing). | |
2841 | |
5308 | 2842 octave_comment_list *tc = octave_comment_buffer::get_comment (); |
2843 | |
2844 delete tc; | |
3765 | 2845 |
3021 | 2846 if (f) |
2847 fclose (static_cast<FILE *> (f)); | |
2848 } | |
2849 | |
2850 void | |
3523 | 2851 parse_and_execute (const std::string& s, bool verbose, const char *warn_for) |
3021 | 2852 { |
2853 unwind_protect::begin_frame ("parse_and_execute_2"); | |
2854 | |
2855 unwind_protect_bool (reading_script_file); | |
2856 unwind_protect_str (curr_fcn_file_full_name); | |
2857 | |
2858 reading_script_file = true; | |
2859 curr_fcn_file_full_name = s; | |
2860 | |
2861 FILE *f = get_input_from_file (s, 0); | |
2862 | |
2863 if (f) | |
2864 { | |
2865 unwind_protect::add (safe_fclose, f); | |
2866 | |
5744 | 2867 octave_user_script *script = new octave_user_script (s, s, ""); |
2868 octave_call_stack::push (script); | |
2869 unwind_protect::add (octave_call_stack::unwind_pop_script, 0); | |
2870 | |
3021 | 2871 unwind_protect_int (input_line_number); |
2872 unwind_protect_int (current_input_column); | |
2873 | |
2874 input_line_number = 0; | |
2875 current_input_column = 1; | |
2876 | |
2877 if (verbose) | |
2878 { | |
3531 | 2879 std::cout << "reading commands from " << s << " ... "; |
3021 | 2880 reading_startup_message_printed = true; |
3531 | 2881 std::cout.flush (); |
3021 | 2882 } |
2883 | |
2884 parse_and_execute (f); | |
2885 | |
2886 if (verbose) | |
3538 | 2887 std::cout << "done." << std::endl; |
3021 | 2888 } |
2889 else if (warn_for) | |
2890 error ("%s: unable to open file `%s'", warn_for, s.c_str ()); | |
2891 | |
2892 unwind_protect::run_frame ("parse_and_execute_2"); | |
2893 } | |
2894 | |
2895 static bool | |
5175 | 2896 looks_like_copyright (const std::string& s) |
3021 | 2897 { |
5175 | 2898 // Perhaps someday we will want to do more here, so leave this as a |
2899 // separate function. | |
2900 | |
5180 | 2901 return (s.substr (0, 9) == "Copyright"); |
3021 | 2902 } |
2903 | |
4540 | 2904 static int |
2905 text_getc (FILE *f) | |
2906 { | |
2907 int c = getc (f); | |
2908 | |
2909 // Convert CRLF into just LF. | |
2910 | |
2911 if (c == '\r') | |
2912 { | |
2913 c = getc (f); | |
2914 | |
2915 if (c != '\n') | |
2916 { | |
2917 ungetc (c, f); | |
2918 c = '\r'; | |
2919 } | |
2920 } | |
2921 | |
2922 return c; | |
2923 } | |
2924 | |
3021 | 2925 // Eat whitespace and comments from FFILE, returning the text of the |
2926 // comments read if it doesn't look like a copyright notice. If | |
2927 // IN_PARTS, consider each block of comments separately; otherwise, | |
2928 // grab them all at once. If UPDATE_POS is TRUE, line and column | |
3665 | 2929 // number information is updated. If SAVE_COPYRIGHT is TRUE, then |
2930 // comments that are recognized as a copyright notice are saved in the | |
5932 | 2931 // comment buffer. If SKIP_CODE is TRUE, then ignore code, otherwise |
2932 // stop at the first non-whitespace character that is not part of a | |
2933 // comment. | |
2934 | |
2935 // FIXME -- skipping code will fail for something like this: | |
2936 // | |
2937 // function foo (x) | |
2938 // fprintf ('%d\n', x); | |
2939 // | |
2940 // % This is the help for foo. | |
2941 // | |
2942 // because we recognize the '%' in the fprintf format as a comment | |
2943 // character. Fixing this will probably require actually parsing the | |
2944 // file properly. | |
3021 | 2945 |
5775 | 2946 // FIXME -- grab_help_text() in lex.l duplicates some of this |
3021 | 2947 // code! |
2948 | |
3536 | 2949 static std::string |
3665 | 2950 gobble_leading_white_space (FILE *ffile, bool in_parts, |
5932 | 2951 bool update_pos, bool save_copyright, |
2952 bool skip_code) | |
3021 | 2953 { |
3523 | 2954 std::string help_txt; |
3021 | 2955 |
3802 | 2956 // TRUE means we have already seen the first block of comments. |
3021 | 2957 bool first_comments_seen = false; |
3802 | 2958 |
2959 // TRUE means we are at the beginning of a comment block. | |
3021 | 2960 bool begin_comment = false; |
3802 | 2961 |
2962 // TRUE means we have already cached the help text. | |
3021 | 2963 bool have_help_text = false; |
3802 | 2964 |
2965 // TRUE means we are currently reading a comment block. | |
3021 | 2966 bool in_comment = false; |
3802 | 2967 |
2968 // TRUE means we should discard the first space from the input | |
2969 // (used to strip leading spaces from the help text). | |
3427 | 2970 bool discard_space = true; |
3802 | 2971 |
3021 | 2972 int c; |
2973 | |
4540 | 2974 while ((c = text_getc (ffile)) != EOF) |
3021 | 2975 { |
2976 if (update_pos) | |
2977 current_input_column++; | |
2978 | |
2979 if (begin_comment) | |
2980 { | |
2981 if (c == '%' || c == '#') | |
2982 continue; | |
3427 | 2983 else if (discard_space && c == ' ') |
2984 { | |
2985 discard_space = false; | |
2986 continue; | |
2987 } | |
3021 | 2988 else |
2989 begin_comment = false; | |
2990 } | |
2991 | |
2992 if (in_comment) | |
2993 { | |
2994 if (! have_help_text) | |
2995 { | |
2996 first_comments_seen = true; | |
5760 | 2997 help_txt += static_cast<char> (c); |
3021 | 2998 } |
2999 | |
3000 if (c == '\n') | |
3001 { | |
3002 if (update_pos) | |
3003 { | |
3004 input_line_number++; | |
3005 current_input_column = 0; | |
3006 } | |
3427 | 3007 |
3021 | 3008 in_comment = false; |
3427 | 3009 discard_space = true; |
3021 | 3010 |
3011 if (in_parts) | |
3012 { | |
4540 | 3013 if ((c = text_getc (ffile)) != EOF) |
3021 | 3014 { |
3015 if (update_pos) | |
3016 current_input_column--; | |
3017 ungetc (c, ffile); | |
3018 if (c == '\n') | |
3019 break; | |
3020 } | |
3021 else | |
3022 break; | |
3023 } | |
3024 } | |
3025 } | |
3026 else | |
3027 { | |
3028 switch (c) | |
3029 { | |
3030 case ' ': | |
3031 case '\t': | |
3032 if (first_comments_seen) | |
3033 have_help_text = true; | |
3034 break; | |
3035 | |
3802 | 3036 case '%': |
3037 case '#': | |
3038 begin_comment = true; | |
3039 in_comment = true; | |
3040 break; | |
3041 | |
3021 | 3042 case '\n': |
3043 if (first_comments_seen) | |
3044 have_help_text = true; | |
3045 if (update_pos) | |
3046 { | |
3047 input_line_number++; | |
3048 current_input_column = 0; | |
3049 } | |
3050 continue; | |
3051 | |
3052 default: | |
5932 | 3053 if (skip_code) |
3054 continue; | |
3055 else | |
3056 { | |
3057 if (update_pos) | |
3058 current_input_column--; | |
3059 ungetc (c, ffile); | |
3060 goto done; | |
3061 } | |
3021 | 3062 } |
3063 } | |
3064 } | |
3065 | |
3066 done: | |
3067 | |
3068 if (! help_txt.empty ()) | |
3069 { | |
5175 | 3070 if (looks_like_copyright (help_txt)) |
3665 | 3071 { |
3072 if (save_copyright) | |
3073 octave_comment_buffer::append (help_txt); | |
3074 | |
3075 help_txt.resize (0); | |
3076 } | |
3021 | 3077 |
3078 if (in_parts && help_txt.empty ()) | |
5932 | 3079 help_txt = gobble_leading_white_space (ffile, in_parts, update_pos, |
3080 false, skip_code); | |
3021 | 3081 } |
3082 | |
3083 return help_txt; | |
3084 } | |
3085 | |
3536 | 3086 std::string |
5484 | 3087 get_help_from_file (const std::string& nm, bool& symbol_found, |
5931 | 3088 std::string& file) |
3021 | 3089 { |
3523 | 3090 std::string retval; |
3021 | 3091 |
5931 | 3092 file = fcn_file_in_path (nm); |
5484 | 3093 |
3094 if (! file.empty ()) | |
3021 | 3095 { |
5484 | 3096 symbol_found = true; |
3097 | |
3098 FILE *fptr = fopen (file.c_str (), "r"); | |
3021 | 3099 |
3100 if (fptr) | |
3101 { | |
5760 | 3102 unwind_protect::add (safe_fclose, fptr); |
3021 | 3103 |
5932 | 3104 retval = gobble_leading_white_space (fptr, true, true, false, true); |
3021 | 3105 |
3106 unwind_protect::run (); | |
3107 } | |
3108 } | |
3109 | |
3110 return retval; | |
3111 } | |
3112 | |
5931 | 3113 std::string |
3114 get_help_from_file (const std::string& nm, bool& symbol_found) | |
3115 { | |
3116 std::string file; | |
3117 return get_help_from_file (nm, symbol_found, file); | |
3118 } | |
3119 | |
3021 | 3120 static int |
3121 is_function_file (FILE *ffile) | |
3122 { | |
3123 int status = 0; | |
3124 | |
3125 long pos = ftell (ffile); | |
3126 | |
5932 | 3127 gobble_leading_white_space (ffile, false, false, false, false); |
3021 | 3128 |
3129 char buf [10]; | |
3130 fgets (buf, 10, ffile); | |
3131 int len = strlen (buf); | |
3132 if (len > 8 && strncmp (buf, "function", 8) == 0 | |
3133 && ! (isalnum (buf[8]) || buf[8] == '_')) | |
3134 status = 1; | |
3135 | |
3136 fseek (ffile, pos, SEEK_SET); | |
3137 | |
3138 return status; | |
3139 } | |
3140 | |
7336 | 3141 static int |
3142 is_function_file (const std::string& fname) | |
3143 { | |
3144 int retval = 0; | |
3145 | |
3146 FILE *fid = fopen (fname.c_str (), "r"); | |
3147 | |
3148 if (fid) | |
3149 { | |
3150 retval = is_function_file (fid); | |
3151 | |
3152 fclose (fid); | |
3153 } | |
3154 | |
3155 return retval; | |
3156 } | |
3157 | |
3021 | 3158 static void |
3159 restore_command_history (void *) | |
3160 { | |
3161 command_history::ignore_entries (! Vsaving_history); | |
3162 } | |
3163 | |
3164 static void | |
3165 restore_input_stream (void *f) | |
3166 { | |
3167 command_editor::set_input_stream (static_cast<FILE *> (f)); | |
3168 } | |
3169 | |
7336 | 3170 typedef octave_function * octave_function_ptr; |
3171 | |
3172 static octave_function * | |
3173 parse_fcn_file (const std::string& ff, const std::string& dispatch_type, | |
3174 bool exec_script, bool force_script = false) | |
3021 | 3175 { |
3176 unwind_protect::begin_frame ("parse_fcn_file"); | |
3177 | |
7336 | 3178 octave_function *fcn_ptr = 0; |
3021 | 3179 |
3180 // Open function file and parse. | |
3181 | |
3182 bool old_reading_fcn_file_state = reading_fcn_file; | |
3183 | |
3184 FILE *in_stream = command_editor::get_input_stream (); | |
3185 | |
3186 unwind_protect::add (restore_input_stream, in_stream); | |
3187 | |
3188 unwind_protect_ptr (ff_instream); | |
3189 | |
3190 unwind_protect_int (input_line_number); | |
3191 unwind_protect_int (current_input_column); | |
4238 | 3192 unwind_protect_int (end_tokens_expected); |
3021 | 3193 unwind_protect_bool (reading_fcn_file); |
3194 unwind_protect_bool (line_editing); | |
4238 | 3195 unwind_protect_str (parent_function_name); |
7336 | 3196 unwind_protect_str (current_class_name); |
3021 | 3197 |
3198 input_line_number = 0; | |
3199 current_input_column = 1; | |
4238 | 3200 end_tokens_expected = 0; |
3021 | 3201 reading_fcn_file = true; |
3202 line_editing = false; | |
4238 | 3203 parent_function_name = ""; |
7336 | 3204 current_class_name = dispatch_type; |
3021 | 3205 |
3206 FILE *ffile = get_input_from_file (ff, 0); | |
3207 | |
3208 unwind_protect::add (safe_fclose, ffile); | |
3209 | |
3210 if (ffile) | |
3211 { | |
3212 // Check to see if this file defines a function or is just a | |
3213 // list of commands. | |
3214 | |
3165 | 3215 if (! force_script && is_function_file (ffile)) |
3021 | 3216 { |
5775 | 3217 // FIXME -- we shouldn't need both the |
3021 | 3218 // command_history object and the |
3219 // Vsaving_history variable... | |
3220 command_history::ignore_entries (); | |
3221 | |
3222 unwind_protect::add (restore_command_history, 0); | |
3223 | |
3224 unwind_protect_int (Vecho_executing_commands); | |
3225 unwind_protect_bool (Vsaving_history); | |
3226 unwind_protect_bool (reading_fcn_file); | |
3877 | 3227 unwind_protect_bool (get_input_from_eval_string); |
3883 | 3228 unwind_protect_bool (parser_end_of_input); |
3021 | 3229 |
3230 Vecho_executing_commands = ECHO_OFF; | |
3231 Vsaving_history = false; | |
3232 reading_fcn_file = true; | |
3877 | 3233 get_input_from_eval_string = false; |
3883 | 3234 parser_end_of_input = false; |
3021 | 3235 |
3236 YY_BUFFER_STATE old_buf = current_buffer (); | |
3237 YY_BUFFER_STATE new_buf = create_buffer (ffile); | |
3238 | |
5760 | 3239 unwind_protect::add (restore_input_buffer, old_buf); |
3240 unwind_protect::add (delete_input_buffer, new_buf); | |
3021 | 3241 |
3242 switch_to_buffer (new_buf); | |
3243 | |
7336 | 3244 unwind_protect_ptr (curr_fcn_ptr); |
3245 curr_fcn_ptr = 0; | |
3021 | 3246 |
3247 reset_parser (); | |
3248 | |
4426 | 3249 std::string txt |
5932 | 3250 = gobble_leading_white_space (ffile, true, true, true, false); |
4426 | 3251 |
3252 help_buf.push (txt); | |
3253 | |
3254 octave_comment_buffer::append (txt); | |
3021 | 3255 |
5775 | 3256 // FIXME -- this should not be necessary. |
5932 | 3257 gobble_leading_white_space (ffile, false, true, false, false); |
3021 | 3258 |
7336 | 3259 lexer_flags.parsing_class_method = ! dispatch_type.empty (); |
3260 | |
3021 | 3261 int status = yyparse (); |
3262 | |
7336 | 3263 fcn_ptr = curr_fcn_ptr; |
3264 | |
3021 | 3265 if (status != 0) |
7336 | 3266 error ("parse error while reading function file %s", ff.c_str ()); |
3021 | 3267 } |
3268 else if (exec_script) | |
3269 { | |
3270 // The value of `reading_fcn_file' will be restored to the | |
3271 // proper value when we unwind from this frame. | |
3272 reading_fcn_file = old_reading_fcn_file_state; | |
3273 | |
5775 | 3274 // FIXME -- we shouldn't need both the |
3021 | 3275 // command_history object and the |
3276 // Vsaving_history variable... | |
3277 command_history::ignore_entries (); | |
3278 | |
3279 unwind_protect::add (restore_command_history, 0); | |
3280 | |
3281 unwind_protect_bool (Vsaving_history); | |
3282 unwind_protect_bool (reading_script_file); | |
3283 | |
3284 Vsaving_history = false; | |
3285 reading_script_file = true; | |
3286 | |
5744 | 3287 octave_user_script *script = new octave_user_script (ff, ff, ""); |
3288 octave_call_stack::push (script); | |
3289 unwind_protect::add (octave_call_stack::unwind_pop_script, 0); | |
3290 | |
3021 | 3291 parse_and_execute (ffile); |
3292 } | |
3293 } | |
3880 | 3294 else |
3295 error ("no such file, `%s'", ff.c_str ()); | |
3021 | 3296 |
3297 unwind_protect::run_frame ("parse_fcn_file"); | |
3298 | |
7336 | 3299 return fcn_ptr; |
3021 | 3300 } |
3301 | |
5484 | 3302 std::string |
3303 lookup_autoload (const std::string& nm) | |
3304 { | |
5626 | 3305 std::string retval; |
3306 | |
3307 typedef std::map<std::string, std::string>::const_iterator am_iter; | |
3308 | |
3309 am_iter p = autoload_map.find (nm); | |
3310 | |
3311 if (p != autoload_map.end ()) | |
6323 | 3312 retval = load_path::find_file (p->second); |
5626 | 3313 |
3314 return retval; | |
5484 | 3315 } |
3316 | |
5592 | 3317 string_vector |
3318 autoloaded_functions (void) | |
3319 { | |
3320 string_vector names (autoload_map.size()); | |
3321 | |
3322 octave_idx_type i = 0; | |
5626 | 3323 typedef std::map<std::string, std::string>::const_iterator am_iter; |
3324 for (am_iter p = autoload_map.begin (); p != autoload_map.end (); p++) | |
5592 | 3325 names[i++] = p->first; |
3326 | |
3327 return names; | |
3328 } | |
3329 | |
3330 string_vector | |
3331 reverse_lookup_autoload (const std::string& nm) | |
3332 { | |
3333 string_vector names; | |
3334 | |
5626 | 3335 typedef std::map<std::string, std::string>::const_iterator am_iter; |
3336 for (am_iter p = autoload_map.begin (); p != autoload_map.end (); p++) | |
5592 | 3337 if (nm == p->second) |
3338 names.append (p->first); | |
3339 | |
3340 return names; | |
3341 } | |
3342 | |
7336 | 3343 octave_function * |
3344 load_fcn_from_file (const std::string& file_name, const std::string& dir_name, | |
3345 const std::string& dispatch_type, | |
3346 const std::string& fcn_name, bool autoload) | |
3021 | 3347 { |
7336 | 3348 octave_function *retval = 0; |
3349 | |
5484 | 3350 unwind_protect::begin_frame ("load_fcn_from_file"); |
3351 | |
7336 | 3352 std::string nm = file_name; |
6238 | 3353 |
3354 size_t nm_len = nm.length (); | |
5472 | 3355 |
3356 std::string file; | |
3357 | |
6323 | 3358 unwind_protect_bool (fcn_file_from_relative_lookup); |
3359 | |
3360 fcn_file_from_relative_lookup = false; | |
3361 | |
7336 | 3362 file = nm; |
3363 | |
3364 if ((nm_len > 4 && nm.substr (nm_len-4) == ".oct") | |
3365 || (nm_len > 4 && nm.substr (nm_len-4) == ".mex") | |
3366 || (nm_len > 2 && nm.substr (nm_len-2) == ".m")) | |
5472 | 3367 { |
6238 | 3368 nm = octave_env::base_pathname (file); |
3369 nm = nm.substr (0, nm.find_last_of ('.')); | |
5472 | 3370 } |
7336 | 3371 |
3372 if (autoload) | |
5472 | 3373 { |
7336 | 3374 unwind_protect_bool (autoloading); |
3375 autoloading = true; | |
3376 } | |
3377 | |
3378 if (! file.empty ()) | |
3379 { | |
6323 | 3380 fcn_file_from_relative_lookup = ! octave_env::absolute_pathname (file); |
3381 | |
3382 file = octave_env::make_absolute (file, octave_env::getcwd ()); | |
5472 | 3383 } |
4243 | 3384 |
3385 int len = file.length (); | |
3386 | |
4244 | 3387 if (len > 4 && file.substr (len-4, len-1) == ".oct") |
3021 | 3388 { |
7336 | 3389 if (autoload && ! fcn_name.empty ()) |
3390 nm = fcn_name; | |
3391 | |
3392 retval = octave_dynamic_loader::load_oct (nm, file, fcn_file_from_relative_lookup); | |
5864 | 3393 } |
3394 else if (len > 4 && file.substr (len-4, len-1) == ".mex") | |
7336 | 3395 retval = octave_dynamic_loader::load_mex (nm, file, fcn_file_from_relative_lookup); |
4244 | 3396 else if (len > 2) |
3021 | 3397 { |
7336 | 3398 if (is_function_file (file)) |
5484 | 3399 { |
7336 | 3400 // These are needed by yyparse. |
3401 | |
3402 unwind_protect_str (curr_fcn_file_name); | |
3403 unwind_protect_str (curr_fcn_file_full_name); | |
3404 | |
3405 curr_fcn_file_name = nm; | |
3406 curr_fcn_file_full_name = file; | |
3407 | |
3408 retval = parse_fcn_file (file, dispatch_type, false, autoloading); | |
5484 | 3409 } |
7336 | 3410 else |
3411 retval = new octave_user_script (file, fcn_name); | |
3021 | 3412 } |
3413 | |
7336 | 3414 if (retval) |
3415 retval->stash_dir_name (dir_name); | |
3416 | |
5484 | 3417 unwind_protect::run_frame ("load_fcn_from_file"); |
3418 | |
7336 | 3419 return retval; |
5312 | 3420 } |
3421 | |
5484 | 3422 DEFCMD (autoload, args, , |
3423 "-*- texinfo -*-\n\ | |
3424 @deftypefn {Built-in Function} {} autoload (@var{function}, @var{file})\n\ | |
3425 Define @var{function} to autoload from @var{file}.\n\ | |
5626 | 3426 \n\ |
6926 | 3427 The second argument, @var{file}, should be an absolute file name or\n\ |
3428 a file name in the same directory as the function or script from which\n\ | |
3429 the autoload command was run. @var{file} should not depend on the\n\ | |
3430 Octave load path.\n\ | |
6380 | 3431 \n\ |
3432 Normally, calls to @code{autoload} appear in PKG_ADD script files that\n\ | |
3433 are evaluated when a directory is added to the Octave's load path. To\n\ | |
6926 | 3434 avoid having to hardcode directory names in @var{file}, if @var{file}\n\ |
3435 is in the same directory as the PKG_ADD script then\n\ | |
6380 | 3436 \n\ |
3437 @example\n\ | |
6926 | 3438 autoload (\"foo\", \"bar.oct\");\n\ |
6380 | 3439 @end example\n\ |
3440 \n\ | |
6926 | 3441 will load the function @code{foo} from the file @code{bar.oct}. The above\n\ |
3442 when @code{bar.oct} is not in the same directory or uses like\n\ | |
6380 | 3443 \n\ |
3444 @example\n\ | |
6637 | 3445 autoload (\"foo\", file_in_loadpath (\"bar.oct\"))\n\ |
6380 | 3446 @end example\n\ |
3447 \n\ | |
3448 @noindent\n\ | |
6926 | 3449 are strongly discouraged, as their behavior might be unpredictable.\n\ |
6380 | 3450 \n\ |
6637 | 3451 With no arguments, return a structure containing the current autoload map.\n\ |
6380 | 3452 @seealso{PKG_ADD}\n\ |
5484 | 3453 @end deftypefn") |
3454 { | |
5626 | 3455 octave_value retval; |
5484 | 3456 |
3457 int nargin = args.length (); | |
3458 | |
5626 | 3459 if (nargin == 0) |
3460 { | |
3461 Cell func_names (dim_vector (autoload_map.size ()), 1); | |
3462 Cell file_names (dim_vector (autoload_map.size ()), 1); | |
3463 | |
3464 octave_idx_type i = 0; | |
3465 typedef std::map<std::string, std::string>::const_iterator am_iter; | |
3466 for (am_iter p = autoload_map.begin (); p != autoload_map.end (); p++) | |
3467 { | |
3468 func_names(i) = p->first; | |
3469 file_names(i) = p->second; | |
3470 | |
3471 i++; | |
3472 } | |
3473 | |
3474 Octave_map m; | |
3475 | |
3476 m.assign ("function", func_names); | |
3477 m.assign ("file", file_names); | |
3478 | |
3479 retval = m; | |
3480 } | |
3481 else if (nargin == 2) | |
5484 | 3482 { |
3483 string_vector argv = args.make_argv ("autoload"); | |
3484 | |
3485 if (! error_state) | |
6380 | 3486 { |
3487 std::string nm = argv[2]; | |
3488 | |
3489 if (! octave_env::absolute_pathname (nm)) | |
6926 | 3490 { |
3491 octave_function *fcn = | |
3492 octave_call_stack::caller_user_script_or_function (); | |
3493 bool found = false; | |
3494 if (fcn) | |
3495 { | |
3496 std::string fname = fcn->fcn_file_name (); | |
3497 if (! fname.empty ()) | |
3498 { | |
3499 fname = octave_env::make_absolute (fname, | |
3500 octave_env::getcwd ()); | |
3501 fname = fname.substr (0, | |
3502 fname.find_last_of (file_ops::dir_sep_str) + 1); | |
3503 file_stat fs (fname + nm); | |
3504 if (fs.exists ()) | |
3505 { | |
3506 nm = fname + nm; | |
3507 found = true; | |
3508 } | |
3509 } | |
3510 } | |
3511 if (! found) | |
3512 warning_with_id ("Octave:autoload-relative-file-name", | |
3513 "autoload: `%s' is not an absolute file name", | |
3514 nm.c_str ()); | |
3515 } | |
6380 | 3516 autoload_map[argv[1]] = nm; |
3517 } | |
5484 | 3518 } |
3519 else | |
5823 | 3520 print_usage (); |
5484 | 3521 |
3522 return retval; | |
3523 } | |
3524 | |
4486 | 3525 void |
5975 | 3526 source_file (const std::string& file_name, const std::string& context) |
4486 | 3527 { |
3528 std::string file_full_name = file_ops::tilde_expand (file_name); | |
3529 | |
3530 unwind_protect::begin_frame ("source_file"); | |
3531 | |
3532 unwind_protect_str (curr_fcn_file_name); | |
3533 unwind_protect_str (curr_fcn_file_full_name); | |
3534 | |
3535 curr_fcn_file_name = file_name; | |
3536 curr_fcn_file_full_name = file_full_name; | |
3537 | |
5975 | 3538 if (! context.empty ()) |
3539 { | |
3540 if (context == "caller") | |
7336 | 3541 symbol_table::push_scope (symbol_table::current_caller_scope ()); |
5975 | 3542 else if (context == "base") |
7336 | 3543 symbol_table::push_scope (symbol_table::top_scope ()); |
5975 | 3544 else |
3545 error ("source: context must be \"caller\" or \"base\""); | |
7336 | 3546 |
3547 if (! error_state) | |
3548 unwind_protect::add (symbol_table::pop_scope); | |
5975 | 3549 } |
3550 | |
3551 if (! error_state) | |
3552 { | |
7336 | 3553 parse_fcn_file (file_full_name, "", true, true); |
5975 | 3554 |
3555 if (error_state) | |
3556 error ("source: error sourcing file `%s'", | |
3557 file_full_name.c_str ()); | |
3558 } | |
4486 | 3559 |
3560 unwind_protect::run_frame ("source_file"); | |
3561 } | |
3562 | |
5739 | 3563 DEFUN (mfilename, args, , |
3564 "-*- texinfo -*-\n\ | |
3565 @deftypefn {Built-in Function} {} mfilename ()\n\ | |
3566 @deftypefnx {Built-in Function} {} mfilename (@code{\"fullpath\"})\n\ | |
3567 @deftypefnx {Built-in Function} {} mfilename (@code{\"fullpathext\"})\n\ | |
3568 Return the name of the currently executing file. At the top-level,\n\ | |
3569 return the empty string. Given the argument @code{\"fullpath\"},\n\ | |
3570 include the directory part of the file name, but not the extension.\n\ | |
3571 Given the argument @code{\"fullpathext\"}, include the directory part\n\ | |
3572 of the file name and the extension.\n\ | |
5774 | 3573 @end deftypefn") |
5739 | 3574 { |
3575 octave_value retval; | |
3576 | |
3577 int nargin = args.length (); | |
3578 | |
3579 if (nargin > 1) | |
3580 { | |
5823 | 3581 print_usage (); |
5739 | 3582 return retval; |
3583 } | |
3584 | |
3585 std::string arg; | |
3586 | |
3587 if (nargin == 1) | |
3588 { | |
3589 arg = args(0).string_value (); | |
3590 | |
3591 if (error_state) | |
3592 { | |
3593 error ("mfilename: expecting argument to be a character string"); | |
3594 return retval; | |
3595 } | |
3596 } | |
3597 | |
3598 std::string fname; | |
3599 | |
5744 | 3600 octave_function *fcn = octave_call_stack::caller_user_script_or_function (); |
5743 | 3601 |
3602 if (fcn) | |
3603 { | |
3604 fname = fcn->fcn_file_name (); | |
3605 | |
3606 if (fname.empty ()) | |
3607 fname = fcn->name (); | |
3608 } | |
5739 | 3609 |
3610 if (arg == "fullpathext") | |
3611 retval = fname; | |
3612 else | |
3613 { | |
5747 | 3614 size_t dpos = fname.rfind (file_ops::dir_sep_char); |
3615 size_t epos = fname.rfind ('.'); | |
3616 | |
3617 if (epos <= dpos) | |
3618 epos = NPOS; | |
3619 | |
3620 fname = (epos != NPOS) ? fname.substr (0, epos) : fname; | |
5739 | 3621 |
3622 if (arg == "fullpath") | |
3623 retval = fname; | |
3624 else | |
5747 | 3625 retval = (dpos != NPOS) ? fname.substr (dpos+1) : fname; |
5739 | 3626 } |
3627 | |
3628 return retval; | |
3629 } | |
3630 | |
3631 | |
3021 | 3632 DEFUN (source, args, , |
3371 | 3633 "-*- texinfo -*-\n\ |
3634 @deftypefn {Built-in Function} {} source (@var{file})\n\ | |
3635 Parse and execute the contents of @var{file}. This is equivalent to\n\ | |
3636 executing commands from a script file, but without requiring the file to\n\ | |
3637 be named @file{@var{file}.m}.\n\ | |
3638 @end deftypefn") | |
3021 | 3639 { |
3640 octave_value_list retval; | |
3641 | |
3642 int nargin = args.length (); | |
3643 | |
5975 | 3644 if (nargin == 1 || nargin == 2) |
3021 | 3645 { |
3523 | 3646 std::string file_name = args(0).string_value (); |
3021 | 3647 |
3648 if (! error_state) | |
5975 | 3649 { |
3650 std::string context; | |
3651 | |
3652 if (nargin == 2) | |
3653 context = args(1).string_value (); | |
3654 | |
3655 if (! error_state) | |
3656 source_file (file_name, context); | |
3657 else | |
3658 error ("source: expecting context to be character string"); | |
3659 } | |
3021 | 3660 else |
3661 error ("source: expecting file name as argument"); | |
3662 } | |
3663 else | |
5823 | 3664 print_usage (); |
3021 | 3665 |
3666 return retval; | |
3667 } | |
3668 | |
3726 | 3669 // Evaluate an Octave function (built-in or interpreted) and return |
3844 | 3670 // the list of result values. NAME is the name of the function to |
3671 // call. ARGS are the arguments to the function. NARGOUT is the | |
3672 // number of output arguments expected. | |
3726 | 3673 |
3021 | 3674 octave_value_list |
3523 | 3675 feval (const std::string& name, const octave_value_list& args, int nargout) |
3156 | 3676 { |
3677 octave_value_list retval; | |
3678 | |
7336 | 3679 octave_value fcn = symbol_table::find_function (name, args); |
3680 | |
3681 if (fcn.is_defined ()) | |
3682 retval = fcn.do_multi_index_op (nargout, args); | |
3683 else | |
3684 error ("feval: function `%s' not found", name.c_str ()); | |
3156 | 3685 |
3686 return retval; | |
3687 } | |
3688 | |
4342 | 3689 octave_value_list |
3690 feval (octave_function *fcn, const octave_value_list& args, int nargout) | |
3691 { | |
3692 octave_value_list retval; | |
3693 | |
3694 if (fcn) | |
3695 retval = fcn->do_multi_index_op (nargout, args); | |
3696 | |
3697 return retval; | |
3698 } | |
3699 | |
3700 static octave_value_list | |
3701 get_feval_args (const octave_value_list& args) | |
3702 { | |
3703 int tmp_nargin = args.length () - 1; | |
3704 | |
3705 octave_value_list retval (tmp_nargin, octave_value ()); | |
3706 | |
3707 for (int i = 0; i < tmp_nargin; i++) | |
3708 retval(i) = args(i+1); | |
3709 | |
3710 string_vector arg_names = args.name_tags (); | |
3711 | |
3712 if (! arg_names.empty ()) | |
3713 { | |
3714 // tmp_nargin and arg_names.length () - 1 may differ if | |
3715 // we are passed all_va_args. | |
3716 | |
3717 int n = arg_names.length () - 1; | |
3718 | |
3719 int len = n > tmp_nargin ? tmp_nargin : n; | |
3720 | |
3721 string_vector tmp_arg_names (len); | |
3722 | |
3723 for (int i = 0; i < len; i++) | |
3724 tmp_arg_names(i) = arg_names(i+1); | |
3725 | |
3726 retval.stash_name_tags (tmp_arg_names); | |
3727 } | |
3728 | |
3729 return retval; | |
3730 } | |
3731 | |
3732 | |
3726 | 3733 // Evaluate an Octave function (built-in or interpreted) and return |
3734 // the list of result values. The first element of ARGS should be a | |
3735 // string containing the name of the function to call, then the rest | |
3736 // are the actual arguments to the function. NARGOUT is the number of | |
3737 // output arguments expected. | |
3738 | |
3156 | 3739 octave_value_list |
3021 | 3740 feval (const octave_value_list& args, int nargout) |
3741 { | |
3742 octave_value_list retval; | |
3743 | |
4342 | 3744 int nargin = args.length (); |
3745 | |
3746 if (nargin > 0) | |
3021 | 3747 { |
4342 | 3748 octave_value f_arg = args(0); |
3749 | |
3750 if (f_arg.is_string ()) | |
3751 { | |
3752 std::string name = f_arg.string_value (); | |
3753 | |
3754 if (! error_state) | |
3156 | 3755 { |
4342 | 3756 octave_value_list tmp_args = get_feval_args (args); |
3757 | |
3758 retval = feval (name, tmp_args, nargout); | |
3156 | 3759 } |
4342 | 3760 } |
3761 else | |
3762 { | |
3763 octave_function *fcn = f_arg.function_value (); | |
3764 | |
3765 if (fcn) | |
3766 { | |
3767 octave_value_list tmp_args = get_feval_args (args); | |
3768 | |
3769 retval = feval (fcn, tmp_args, nargout); | |
3770 } | |
3021 | 3771 } |
3772 } | |
3773 | |
3774 return retval; | |
3775 } | |
3776 | |
3777 DEFUN (feval, args, nargout, | |
3371 | 3778 "-*- texinfo -*-\n\ |
3779 @deftypefn {Built-in Function} {} feval (@var{name}, @dots{})\n\ | |
3780 Evaluate the function named @var{name}. Any arguments after the first\n\ | |
3781 are passed on to the named function. For example,\n\ | |
3782 \n\ | |
3783 @example\n\ | |
3784 feval (\"acos\", -1)\n\ | |
3785 @result{} 3.1416\n\ | |
3786 @end example\n\ | |
3021 | 3787 \n\ |
3371 | 3788 @noindent\n\ |
3789 calls the function @code{acos} with the argument @samp{-1}.\n\ | |
3790 \n\ | |
3791 The function @code{feval} is necessary in order to be able to write\n\ | |
3792 functions that call user-supplied functions, because Octave does not\n\ | |
3793 have a way to declare a pointer to a function (like C) or to declare a\n\ | |
3794 special kind of variable that can be used to hold the name of a function\n\ | |
3795 (like @code{EXTERNAL} in Fortran). Instead, you must refer to functions\n\ | |
3796 by name, and use @code{feval} to call them.\n\ | |
3797 @end deftypefn") | |
3021 | 3798 { |
3799 octave_value_list retval; | |
3800 | |
3801 int nargin = args.length (); | |
3802 | |
3803 if (nargin > 0) | |
3804 retval = feval (args, nargout); | |
3805 else | |
5823 | 3806 print_usage (); |
3021 | 3807 |
3808 return retval; | |
3809 } | |
3810 | |
3099 | 3811 octave_value_list |
3523 | 3812 eval_string (const std::string& s, bool silent, int& parse_status, int nargout) |
3021 | 3813 { |
3877 | 3814 octave_value_list retval; |
3815 | |
3021 | 3816 unwind_protect::begin_frame ("eval_string"); |
3817 | |
3818 unwind_protect_bool (get_input_from_eval_string); | |
3877 | 3819 unwind_protect_bool (input_from_eval_string_pending); |
3883 | 3820 unwind_protect_bool (parser_end_of_input); |
5189 | 3821 unwind_protect_bool (line_editing); |
3021 | 3822 unwind_protect_str (current_eval_string); |
3823 | |
3824 get_input_from_eval_string = true; | |
3877 | 3825 input_from_eval_string_pending = true; |
3883 | 3826 parser_end_of_input = false; |
5189 | 3827 line_editing = false; |
3828 | |
3021 | 3829 current_eval_string = s; |
3830 | |
3877 | 3831 unwind_protect_ptr (global_command); |
3832 | |
3021 | 3833 YY_BUFFER_STATE old_buf = current_buffer (); |
3834 YY_BUFFER_STATE new_buf = create_buffer (0); | |
3835 | |
3836 unwind_protect::add (restore_input_buffer, old_buf); | |
3837 unwind_protect::add (delete_input_buffer, new_buf); | |
3838 | |
3839 switch_to_buffer (new_buf); | |
3840 | |
3877 | 3841 do |
3842 { | |
4318 | 3843 reset_parser (); |
3844 | |
3877 | 3845 parse_status = yyparse (); |
3846 | |
3847 tree_statement_list *command = global_command; | |
3848 | |
3883 | 3849 if (parse_status == 0) |
3877 | 3850 { |
3883 | 3851 if (command) |
3852 { | |
3853 retval = command->eval (silent, nargout); | |
3854 | |
3855 delete command; | |
3856 | |
3857 command = 0; | |
3858 | |
3859 if (error_state | |
4207 | 3860 || tree_return_command::returning |
3861 || tree_break_command::breaking | |
3862 || tree_continue_command::continuing) | |
3883 | 3863 break; |
3864 } | |
3865 else if (parser_end_of_input) | |
3877 | 3866 break; |
3883 | 3867 } |
3877 | 3868 } |
3869 while (parse_status == 0); | |
3021 | 3870 |
3871 unwind_protect::run_frame ("eval_string"); | |
3872 | |
3873 return retval; | |
3874 } | |
3875 | |
3876 octave_value | |
3523 | 3877 eval_string (const std::string& s, bool silent, int& parse_status) |
3021 | 3878 { |
3879 octave_value retval; | |
3880 | |
3881 octave_value_list tmp = eval_string (s, silent, parse_status, 1); | |
3882 | |
3883 if (! tmp.empty ()) | |
3884 retval = tmp(0); | |
3885 | |
3886 return retval; | |
3887 } | |
3888 | |
3889 static octave_value_list | |
3890 eval_string (const octave_value& arg, bool silent, int& parse_status, | |
3891 int nargout) | |
3892 { | |
3523 | 3893 std::string s = arg.string_value (); |
3021 | 3894 |
3895 if (error_state) | |
3896 { | |
3523 | 3897 error ("eval: expecting std::string argument"); |
4233 | 3898 return octave_value (-1); |
3021 | 3899 } |
3900 | |
3901 return eval_string (s, silent, parse_status, nargout); | |
3902 } | |
3903 | |
3904 DEFUN (eval, args, nargout, | |
3371 | 3905 "-*- texinfo -*-\n\ |
3906 @deftypefn {Built-in Function} {} eval (@var{try}, @var{catch})\n\ | |
3907 Parse the string @var{try} and evaluate it as if it were an Octave\n\ | |
6643 | 3908 program. If that fails, evaluate the optional string @var{catch}.\n\ |
4463 | 3909 The string @var{try} is evaluated in the current context,\n\ |
3910 so any results remain available after @code{eval} returns.\n\ | |
6643 | 3911 \n\ |
3912 The following example makes the variable @var{a} with the approximate\n\ | |
3913 value 3.1416 available.\n\ | |
3914 \n\ | |
3915 @example\n\ | |
3916 eval(\"a = acos(-1);\");\n\ | |
3917 @end example\n\ | |
3918 \n\ | |
3919 If an error occurs during the evaluation of @var{try} the @var{catch}\n\ | |
3920 string is evaluated, as the following example shows.\n\ | |
3921 \n\ | |
3922 @example\n\ | |
3923 eval ('error (\"This is a bad example\");',\n\ | |
7001 | 3924 'printf (\"This error occurred:\\n%s\", lasterr ());');\n\ |
3925 @print{} This error occurred:\n\ | |
6643 | 3926 error: This is a bad example\n\ |
3927 @end example\n\ | |
3371 | 3928 @end deftypefn") |
3021 | 3929 { |
3930 octave_value_list retval; | |
3931 | |
3932 int nargin = args.length (); | |
3933 | |
3934 if (nargin > 0) | |
3935 { | |
3936 unwind_protect::begin_frame ("Feval"); | |
3937 | |
3938 if (nargin > 1) | |
3939 { | |
4699 | 3940 unwind_protect_int (buffer_error_messages); |
3941 buffer_error_messages++; | |
3021 | 3942 } |
3943 | |
3944 int parse_status = 0; | |
3945 | |
4463 | 3946 octave_value_list tmp = eval_string (args(0), nargout > 0, |
3947 parse_status, nargout); | |
3948 | |
3949 if (nargout > 0) | |
3950 retval = tmp; | |
3021 | 3951 |
3952 if (nargin > 1 && (parse_status != 0 || error_state)) | |
3953 { | |
3954 error_state = 0; | |
3955 | |
3956 // Set up for letting the user print any messages from | |
3957 // errors that occurred in the first part of this eval(). | |
3958 | |
4699 | 3959 buffer_error_messages--; |
3021 | 3960 |
3961 eval_string (args(1), 0, parse_status, nargout); | |
3962 | |
3963 retval = octave_value_list (); | |
3964 } | |
3965 | |
3966 unwind_protect::run_frame ("Feval"); | |
3967 } | |
3968 else | |
5823 | 3969 print_usage (); |
3021 | 3970 |
3971 return retval; | |
3972 } | |
3973 | |
7562
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
3974 /* |
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-1.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 %% test/octave.test/eval/eval-2.m |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
3982 %!test |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
3983 %! x = 1; |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
3984 %! assert(eval ("x;")); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
3985 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
3986 %% test/octave.test/eval/eval-3.m |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
3987 %!test |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
3988 %! x = 1; |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
3989 %! assert(eval ("x;"),1); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
3990 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
3991 %% FIXME |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
3992 %% 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
|
3993 %% 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
|
3994 %% test/octave.test/eval/eval-4.m |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
3995 %!#test |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
3996 %! x = 1; |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
3997 %! assert(eval ("x");,1); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
3998 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
3999 %% test/octave.test/eval/eval-5.m |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4000 %!test |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4001 %! eval ("flipud = 2;"); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4002 %! assert(flipud,2); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4003 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4004 %% test/octave.test/eval/eval-6.m |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4005 %!function y = f () |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4006 %! eval ("flipud = 2;"); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4007 %! y = flipud; |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4008 %!test |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4009 %! assert(f,2); |
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-7.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-8.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-9.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 %! eval ("x = 1;"); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4024 %! assert(x,1); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4025 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4026 %% test/octave.test/eval/eval-10.m |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4027 %!#test |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4028 %! eval ("x = 1") |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4029 %! assert(x,1); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4030 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4031 %% test/octave.test/eval/eval-11.m |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4032 %!test |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4033 %! x = 1; |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4034 %! y = eval ("x"); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4035 %! assert(y,1); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4036 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4037 %% test/octave.test/eval/eval-12.m |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4038 %!test |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4039 %! x = 1; |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4040 %! y = eval ("x;"); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4041 %! assert(y,1); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4042 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4043 %% test/octave.test/eval/eval-13.m |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4044 %!test |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4045 %! x = 1; |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4046 %! y = eval ("x;"); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4047 %! assert(y,1); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4048 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4049 %% test/octave.test/eval/eval-14.m |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4050 %!test |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4051 %! x = 1; |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4052 %! y = eval ("x"); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4053 %! assert(y,1); |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4054 |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4055 */ |
c827f5673321
move tests to individual source files
John W. Eaton <jwe@octave.org>
parents:
7351
diff
changeset
|
4056 |
4661 | 4057 DEFUN (assignin, args, , |
4297 | 4058 "-*- texinfo -*-\n\ |
4059 @deftypefn {Built-in Function} {} assignin (@var{context}, @var{varname}, @var{value})\n\ | |
4060 Assign @var{value} to @var{varname} in context @var{context}, which\n\ | |
4061 may be either @code{\"base\"} or @code{\"caller\"}.\n\ | |
4062 @end deftypefn") | |
4063 { | |
4064 octave_value_list retval; | |
4065 | |
4066 int nargin = args.length (); | |
4067 | |
4068 if (nargin == 3) | |
4069 { | |
4070 std::string context = args(0).string_value (); | |
4071 | |
4072 if (! error_state) | |
4073 { | |
7336 | 4074 symbol_table::scope_id scope = -1; |
4297 | 4075 |
4076 if (context == "caller") | |
7336 | 4077 { |
4078 if (symbol_table::current_scope () == symbol_table::current_caller_scope ()) | |
4079 { | |
4080 error ("assignin: assignment in caller not implemented yet for direct recursion"); | |
4081 return retval; | |
4082 } | |
4083 else | |
4084 scope = symbol_table::current_caller_scope (); | |
4085 } | |
4297 | 4086 else if (context == "base") |
7336 | 4087 scope = symbol_table::top_scope (); |
4297 | 4088 else |
4089 error ("assignin: context must be \"caller\" or \"base\""); | |
4090 | |
4091 if (! error_state) | |
4092 { | |
4093 std::string nm = args(1).string_value (); | |
4094 | |
4095 if (! error_state) | |
4096 { | |
4097 if (valid_identifier (nm)) | |
7336 | 4098 symbol_table::varref (nm, scope) = args(2); |
4297 | 4099 else |
4100 error ("assignin: invalid variable name"); | |
4101 } | |
4102 else | |
4103 error ("assignin: expecting variable name as second argument"); | |
4104 } | |
4105 } | |
4106 else | |
4107 error ("assignin: expecting string as first argument"); | |
4108 } | |
4109 else | |
5823 | 4110 print_usage (); |
4297 | 4111 |
4112 return retval; | |
4113 } | |
4114 | |
4245 | 4115 DEFUN (evalin, args, nargout, |
4116 "-*- texinfo -*-\n\ | |
4117 @deftypefn {Built-in Function} {} evalin (@var{context}, @var{try}, @var{catch})\n\ | |
4118 Like @code{eval}, except that the expressions are evaluated in the\n\ | |
4119 context @var{context}, which may be either @code{\"caller\"} or\n\ | |
4246 | 4120 @code{\"base\"}.\n\ |
4245 | 4121 @end deftypefn") |
4122 { | |
4123 octave_value_list retval; | |
4124 | |
4125 int nargin = args.length (); | |
4126 | |
4127 if (nargin > 1) | |
4128 { | |
4129 std::string context = args(0).string_value (); | |
4130 | |
4131 if (! error_state) | |
4132 { | |
4133 unwind_protect::begin_frame ("Fevalin"); | |
4134 | |
4135 if (context == "caller") | |
7336 | 4136 { |
4137 if (symbol_table::current_scope () == symbol_table::current_caller_scope ()) | |
4138 { | |
4139 error ("evalin: evaluation in caller not implemented yet for direct recursion"); | |
4140 return retval; | |
4141 } | |
4142 else | |
4143 symbol_table::push_scope (symbol_table::current_caller_scope ()); | |
4144 } | |
4245 | 4145 else if (context == "base") |
7336 | 4146 symbol_table::push_scope (symbol_table::top_scope ()); |
4245 | 4147 else |
4148 error ("evalin: context must be \"caller\" or \"base\""); | |
4149 | |
4297 | 4150 if (! error_state) |
4245 | 4151 { |
7336 | 4152 unwind_protect::add (symbol_table::pop_scope); |
4153 | |
4297 | 4154 if (nargin > 2) |
4155 { | |
4699 | 4156 unwind_protect_int (buffer_error_messages); |
4157 buffer_error_messages++; | |
4297 | 4158 } |
4159 | |
4160 int parse_status = 0; | |
4161 | |
4463 | 4162 octave_value_list tmp = eval_string (args(1), nargout > 0, |
4163 parse_status, nargout); | |
4164 | |
4165 if (nargout > 0) | |
4166 retval = tmp; | |
4297 | 4167 |
4168 if (nargin > 2 && (parse_status != 0 || error_state)) | |
4169 { | |
4170 error_state = 0; | |
4171 | |
4172 // Set up for letting the user print any messages from | |
4173 // errors that occurred in the first part of this eval(). | |
4174 | |
4699 | 4175 buffer_error_messages--; |
4297 | 4176 |
4177 eval_string (args(2), 0, parse_status, nargout); | |
4178 | |
4179 retval = octave_value_list (); | |
4180 } | |
4245 | 4181 } |
4182 | |
4183 unwind_protect::run_frame ("Fevalin"); | |
4184 } | |
4185 else | |
4186 error ("evalin: expecting string as first argument"); | |
4187 } | |
4188 else | |
5823 | 4189 print_usage (); |
4245 | 4190 |
4191 return retval; | |
4192 } | |
4193 | |
1994 | 4194 /* |
4195 ;;; Local Variables: *** | |
4196 ;;; mode: text *** | |
4197 ;;; End: *** | |
4198 */ |