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