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