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