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