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