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