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