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