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