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