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