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. |
4426
|
115 std::stack<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 |
4426
|
2624 if (! help_buf.empty ()) |
|
2625 { |
|
2626 id->document (help_buf.top ()); |
|
2627 help_buf.pop (); |
|
2628 } |
3844
|
2629 |
4240
|
2630 if (lexer_flags.parsing_nested_function < 0) |
|
2631 lexer_flags.parsing_nested_function = 0; |
4426
|
2632 |
1623
|
2633 return fcn; |
|
2634 } |
|
2635 |
|
2636 // Finish defining a function. |
|
2637 |
2891
|
2638 static octave_user_function * |
3665
|
2639 finish_function (tree_identifier *id, octave_user_function *fcn, |
|
2640 octave_comment_list *lc) |
1623
|
2641 { |
2883
|
2642 tree_parameter_list *tpl = new tree_parameter_list (id); |
1623
|
2643 |
|
2644 tpl->mark_as_formal_parameters (); |
|
2645 |
3665
|
2646 fcn->stash_leading_comment (lc); |
|
2647 |
1623
|
2648 return fcn->define_ret_list (tpl); |
|
2649 } |
|
2650 |
|
2651 // Finish defining a function a different way. |
|
2652 |
2891
|
2653 static octave_user_function * |
3665
|
2654 finish_function (tree_parameter_list *ret_list, |
|
2655 octave_user_function *fcn, octave_comment_list *lc) |
1623
|
2656 { |
|
2657 ret_list->mark_as_formal_parameters (); |
|
2658 |
3665
|
2659 fcn->stash_leading_comment (lc); |
|
2660 |
1623
|
2661 return fcn->define_ret_list (ret_list); |
|
2662 } |
|
2663 |
2883
|
2664 static void |
|
2665 recover_from_parsing_function (void) |
|
2666 { |
4238
|
2667 if (symtab_context.empty ()) |
3903
|
2668 panic_impossible (); |
|
2669 |
4238
|
2670 curr_sym_tab = symtab_context.top (); |
|
2671 symtab_context.pop (); |
2883
|
2672 |
|
2673 lexer_flags.defining_func = false; |
|
2674 lexer_flags.beginning_of_function = false; |
|
2675 lexer_flags.parsed_function_name = false; |
|
2676 lexer_flags.looking_at_return_list = false; |
|
2677 lexer_flags.looking_at_parameter_list = false; |
|
2678 } |
|
2679 |
2846
|
2680 // Make an index expression. |
|
2681 |
751
|
2682 static tree_index_expression * |
3929
|
2683 make_index_expression (tree_expression *expr, tree_argument_list *args, |
3933
|
2684 char type) |
751
|
2685 { |
|
2686 tree_index_expression *retval = 0; |
|
2687 |
2970
|
2688 int l = expr->line (); |
|
2689 int c = expr->column (); |
|
2690 |
|
2691 expr->mark_postfix_indexed (); |
|
2692 |
3933
|
2693 if (expr->is_index_expression ()) |
|
2694 { |
|
2695 tree_index_expression *tmp = static_cast<tree_index_expression *> (expr); |
|
2696 |
|
2697 tmp->append (args, type); |
|
2698 |
|
2699 retval = tmp; |
|
2700 } |
|
2701 else |
|
2702 retval = new tree_index_expression (expr, args, l, c, type); |
2970
|
2703 |
|
2704 return retval; |
|
2705 } |
|
2706 |
|
2707 // Make an indirect reference expression. |
|
2708 |
3930
|
2709 static tree_index_expression * |
3523
|
2710 make_indirect_ref (tree_expression *expr, const std::string& elt) |
2970
|
2711 { |
3930
|
2712 tree_index_expression *retval = 0; |
2970
|
2713 |
|
2714 int l = expr->line (); |
|
2715 int c = expr->column (); |
|
2716 |
3933
|
2717 if (expr->is_index_expression ()) |
|
2718 { |
|
2719 tree_index_expression *tmp = static_cast<tree_index_expression *> (expr); |
|
2720 |
|
2721 tmp->append (elt); |
|
2722 |
|
2723 retval = tmp; |
|
2724 } |
|
2725 else |
|
2726 retval = new tree_index_expression (expr, elt, l, c); |
2970
|
2727 |
|
2728 lexer_flags.looking_at_indirect_ref = false; |
751
|
2729 |
|
2730 return retval; |
|
2731 } |
1511
|
2732 |
4131
|
2733 // Make an indirect reference expression with dynamic field name. |
|
2734 |
|
2735 static tree_index_expression * |
|
2736 make_indirect_ref (tree_expression *expr, tree_expression *elt) |
|
2737 { |
|
2738 tree_index_expression *retval = 0; |
|
2739 |
|
2740 int l = expr->line (); |
|
2741 int c = expr->column (); |
|
2742 |
|
2743 if (expr->is_index_expression ()) |
|
2744 { |
|
2745 tree_index_expression *tmp = static_cast<tree_index_expression *> (expr); |
|
2746 |
|
2747 tmp->append (elt); |
|
2748 |
|
2749 retval = tmp; |
|
2750 } |
|
2751 else |
|
2752 retval = new tree_index_expression (expr, elt, l, c); |
|
2753 |
|
2754 lexer_flags.looking_at_indirect_ref = false; |
|
2755 |
|
2756 return retval; |
|
2757 } |
|
2758 |
2846
|
2759 // Make a declaration command. |
|
2760 |
|
2761 static tree_decl_command * |
|
2762 make_decl_command (int tok, token *tok_val, tree_decl_init_list *lst) |
|
2763 { |
|
2764 tree_decl_command *retval = 0; |
|
2765 |
|
2766 int l = tok_val->line (); |
|
2767 int c = tok_val->column (); |
|
2768 |
|
2769 switch (tok) |
|
2770 { |
|
2771 case GLOBAL: |
|
2772 retval = new tree_global_command (lst, l, c); |
|
2773 break; |
|
2774 |
|
2775 case STATIC: |
|
2776 if (lexer_flags.defining_func) |
|
2777 retval = new tree_static_command (lst, l, c); |
|
2778 else |
|
2779 { |
|
2780 if (reading_script_file) |
|
2781 warning ("ignoring static declaration near line %d of file `%s'", |
|
2782 l, curr_fcn_file_full_name.c_str ()); |
|
2783 else |
|
2784 warning ("ignoring static declaration near line %d", l); |
|
2785 } |
|
2786 break; |
|
2787 |
|
2788 default: |
|
2789 panic_impossible (); |
|
2790 break; |
|
2791 } |
|
2792 |
|
2793 return retval; |
|
2794 } |
|
2795 |
1623
|
2796 // Finish building a matrix list. |
|
2797 |
|
2798 static tree_expression * |
1829
|
2799 finish_matrix (tree_matrix *m) |
1623
|
2800 { |
3110
|
2801 tree_expression *retval = m; |
|
2802 |
|
2803 unwind_protect::begin_frame ("finish_matrix"); |
|
2804 |
|
2805 unwind_protect_int (error_state); |
|
2806 |
3815
|
2807 unwind_protect_bool (discard_error_messages); |
|
2808 discard_error_messages = true; |
1623
|
2809 |
2533
|
2810 if (m->all_elements_are_constant ()) |
1829
|
2811 { |
2970
|
2812 octave_value tmp = m->rvalue (); |
1623
|
2813 |
3489
|
2814 if (! (error_state || warning_state)) |
2533
|
2815 { |
|
2816 tree_constant *tc_retval = new tree_constant (tmp); |
|
2817 |
4051
|
2818 OSSTREAM buf; |
2533
|
2819 |
|
2820 tree_print_code tpc (buf); |
|
2821 |
|
2822 m->accept (tpc); |
|
2823 |
4051
|
2824 buf << OSSTREAM_ENDS; |
|
2825 |
|
2826 tc_retval->stash_original_text (OSSTREAM_STR (buf)); |
|
2827 |
|
2828 OSSTREAM_FREEZE (buf); |
2533
|
2829 |
|
2830 delete m; |
|
2831 |
|
2832 retval = tc_retval; |
|
2833 } |
1623
|
2834 } |
3110
|
2835 |
|
2836 unwind_protect::run_frame ("finish_matrix"); |
1623
|
2837 |
|
2838 return retval; |
|
2839 } |
|
2840 |
3351
|
2841 // Finish building a cell list. |
|
2842 |
|
2843 static tree_expression * |
|
2844 finish_cell (tree_cell *c) |
|
2845 { |
|
2846 // For now, this doesn't do anything. |
|
2847 |
|
2848 return c; |
|
2849 } |
|
2850 |
1511
|
2851 static void |
|
2852 maybe_warn_missing_semi (tree_statement_list *t) |
|
2853 { |
2166
|
2854 if (lexer_flags.defining_func && Vwarn_missing_semicolon) |
1511
|
2855 { |
4219
|
2856 tree_statement *tmp = t->back(); |
1607
|
2857 |
1511
|
2858 if (tmp->is_expression ()) |
1607
|
2859 warning ("missing semicolon near line %d, column %d in file `%s'", |
1755
|
2860 tmp->line (), tmp->column (), |
|
2861 curr_fcn_file_full_name.c_str ()); |
1511
|
2862 } |
|
2863 } |
1994
|
2864 |
2525
|
2865 static void |
|
2866 set_stmt_print_flag (tree_statement_list *list, char sep, |
|
2867 bool warn_missing_semi) |
|
2868 { |
|
2869 switch (sep) |
|
2870 { |
|
2871 case ';': |
|
2872 { |
4219
|
2873 tree_statement *tmp = list->back (); |
2525
|
2874 tmp->set_print_flag (0); |
|
2875 } |
|
2876 break; |
|
2877 |
|
2878 case 0: |
|
2879 case ',': |
|
2880 case '\n': |
|
2881 if (warn_missing_semi) |
|
2882 maybe_warn_missing_semi (list); |
|
2883 break; |
|
2884 |
|
2885 default: |
|
2886 warning ("unrecognized separator type!"); |
|
2887 break; |
|
2888 } |
|
2889 } |
|
2890 |
3021
|
2891 void |
|
2892 parse_and_execute (FILE *f) |
|
2893 { |
|
2894 unwind_protect::begin_frame ("parse_and_execute"); |
3880
|
2895 |
3021
|
2896 YY_BUFFER_STATE old_buf = current_buffer (); |
|
2897 YY_BUFFER_STATE new_buf = create_buffer (f); |
|
2898 |
|
2899 unwind_protect::add (restore_input_buffer, old_buf); |
|
2900 unwind_protect::add (delete_input_buffer, new_buf); |
|
2901 |
|
2902 switch_to_buffer (new_buf); |
|
2903 |
|
2904 unwind_protect_bool (line_editing); |
|
2905 unwind_protect_bool (input_from_command_line_file); |
3880
|
2906 unwind_protect_bool (get_input_from_eval_string); |
3883
|
2907 unwind_protect_bool (parser_end_of_input); |
3021
|
2908 |
|
2909 line_editing = false; |
|
2910 input_from_command_line_file = false; |
3880
|
2911 get_input_from_eval_string = false; |
3883
|
2912 parser_end_of_input = false; |
3021
|
2913 |
|
2914 unwind_protect_ptr (curr_sym_tab); |
|
2915 |
|
2916 int retval; |
|
2917 do |
|
2918 { |
|
2919 reset_parser (); |
|
2920 |
|
2921 retval = yyparse (); |
|
2922 |
3883
|
2923 if (retval == 0) |
|
2924 { |
|
2925 if (global_command) |
3021
|
2926 { |
3883
|
2927 global_command->eval (); |
|
2928 |
|
2929 delete global_command; |
|
2930 |
|
2931 global_command = 0; |
|
2932 |
4171
|
2933 OCTAVE_QUIT; |
|
2934 |
4207
|
2935 bool quit = (tree_return_command::returning |
|
2936 || tree_break_command::breaking); |
|
2937 |
|
2938 if (tree_return_command::returning) |
|
2939 tree_return_command::returning = 0; |
|
2940 |
|
2941 if (tree_break_command::breaking) |
|
2942 tree_break_command::breaking--; |
3883
|
2943 |
|
2944 if (error_state) |
|
2945 { |
|
2946 error ("near line %d of file `%s'", input_line_number, |
|
2947 curr_fcn_file_full_name.c_str ()); |
|
2948 |
|
2949 break; |
|
2950 } |
|
2951 |
|
2952 if (quit) |
|
2953 break; |
3021
|
2954 } |
3883
|
2955 else if (parser_end_of_input) |
3021
|
2956 break; |
3883
|
2957 } |
3021
|
2958 } |
|
2959 while (retval == 0); |
|
2960 |
|
2961 unwind_protect::run_frame ("parse_and_execute"); |
|
2962 } |
|
2963 |
|
2964 static void |
|
2965 safe_fclose (void *f) |
|
2966 { |
3765
|
2967 // XXX FIXME XXX -- comments at the end of an input file are |
|
2968 // discarded (otherwise, they would be appended to the next |
|
2969 // statement, possibly from the command line or another file, which |
|
2970 // can be quite confusing). |
|
2971 |
|
2972 octave_comment_buffer::get_comment (); |
|
2973 |
3021
|
2974 if (f) |
|
2975 fclose (static_cast<FILE *> (f)); |
|
2976 } |
|
2977 |
|
2978 void |
3523
|
2979 parse_and_execute (const std::string& s, bool verbose, const char *warn_for) |
3021
|
2980 { |
|
2981 unwind_protect::begin_frame ("parse_and_execute_2"); |
|
2982 |
|
2983 unwind_protect_bool (reading_script_file); |
|
2984 unwind_protect_str (curr_fcn_file_full_name); |
|
2985 |
|
2986 reading_script_file = true; |
|
2987 curr_fcn_file_full_name = s; |
|
2988 |
|
2989 FILE *f = get_input_from_file (s, 0); |
|
2990 |
|
2991 if (f) |
|
2992 { |
|
2993 unwind_protect::add (safe_fclose, f); |
|
2994 |
|
2995 unwind_protect_int (input_line_number); |
|
2996 unwind_protect_int (current_input_column); |
|
2997 |
|
2998 input_line_number = 0; |
|
2999 current_input_column = 1; |
|
3000 |
|
3001 if (verbose) |
|
3002 { |
3531
|
3003 std::cout << "reading commands from " << s << " ... "; |
3021
|
3004 reading_startup_message_printed = true; |
3531
|
3005 std::cout.flush (); |
3021
|
3006 } |
|
3007 |
|
3008 parse_and_execute (f); |
|
3009 |
|
3010 if (verbose) |
3538
|
3011 std::cout << "done." << std::endl; |
3021
|
3012 } |
|
3013 else if (warn_for) |
|
3014 error ("%s: unable to open file `%s'", warn_for, s.c_str ()); |
|
3015 |
|
3016 unwind_protect::run_frame ("parse_and_execute_2"); |
|
3017 } |
|
3018 |
|
3019 static bool |
3523
|
3020 looks_like_octave_copyright (const std::string& s) |
3021
|
3021 { |
|
3022 bool retval = false; |
|
3023 |
3523
|
3024 std::string t = s.substr (0, 14); |
3427
|
3025 |
|
3026 if (t == "Copyright (C) ") |
3021
|
3027 { |
|
3028 size_t pos = s.find ('\n'); |
|
3029 |
|
3030 if (pos != NPOS) |
|
3031 { |
|
3032 pos = s.find ('\n', pos + 1); |
|
3033 |
|
3034 if (pos != NPOS) |
|
3035 { |
|
3036 pos++; |
|
3037 |
3427
|
3038 t = s.substr (pos, 28); |
|
3039 |
|
3040 if (t == "This file is part of Octave." |
|
3041 || t == "This program is free softwar") |
3021
|
3042 retval = true; |
|
3043 } |
|
3044 } |
|
3045 } |
|
3046 |
|
3047 return retval; |
|
3048 } |
|
3049 |
|
3050 // Eat whitespace and comments from FFILE, returning the text of the |
|
3051 // comments read if it doesn't look like a copyright notice. If |
|
3052 // IN_PARTS, consider each block of comments separately; otherwise, |
|
3053 // grab them all at once. If UPDATE_POS is TRUE, line and column |
3665
|
3054 // number information is updated. If SAVE_COPYRIGHT is TRUE, then |
|
3055 // comments that are recognized as a copyright notice are saved in the |
|
3056 // comment buffer. |
3021
|
3057 |
|
3058 // XXX FIXME XXX -- grab_help_text() in lex.l duplicates some of this |
|
3059 // code! |
|
3060 |
3536
|
3061 static std::string |
3665
|
3062 gobble_leading_white_space (FILE *ffile, bool in_parts, |
|
3063 bool update_pos, bool save_copyright) |
3021
|
3064 { |
3523
|
3065 std::string help_txt; |
3021
|
3066 |
3802
|
3067 // TRUE means we have already seen the first block of comments. |
3021
|
3068 bool first_comments_seen = false; |
3802
|
3069 |
|
3070 // TRUE means we are at the beginning of a comment block. |
3021
|
3071 bool begin_comment = false; |
3802
|
3072 |
|
3073 // TRUE means we have already cached the help text. |
3021
|
3074 bool have_help_text = false; |
3802
|
3075 |
|
3076 // TRUE means we are currently reading a comment block. |
3021
|
3077 bool in_comment = false; |
3802
|
3078 |
|
3079 // TRUE means we should discard the first space from the input |
|
3080 // (used to strip leading spaces from the help text). |
3427
|
3081 bool discard_space = true; |
3802
|
3082 |
3021
|
3083 int c; |
|
3084 |
|
3085 while ((c = getc (ffile)) != EOF) |
|
3086 { |
|
3087 if (update_pos) |
|
3088 current_input_column++; |
|
3089 |
|
3090 if (begin_comment) |
|
3091 { |
|
3092 if (c == '%' || c == '#') |
|
3093 continue; |
3427
|
3094 else if (discard_space && c == ' ') |
|
3095 { |
|
3096 discard_space = false; |
|
3097 continue; |
|
3098 } |
3021
|
3099 else |
|
3100 begin_comment = false; |
|
3101 } |
|
3102 |
|
3103 if (in_comment) |
|
3104 { |
|
3105 if (! have_help_text) |
|
3106 { |
|
3107 first_comments_seen = true; |
|
3108 help_txt += (char) c; |
|
3109 } |
|
3110 |
|
3111 if (c == '\n') |
|
3112 { |
|
3113 if (update_pos) |
|
3114 { |
|
3115 input_line_number++; |
|
3116 current_input_column = 0; |
|
3117 } |
3427
|
3118 |
3021
|
3119 in_comment = false; |
3427
|
3120 discard_space = true; |
3021
|
3121 |
|
3122 if (in_parts) |
|
3123 { |
|
3124 if ((c = getc (ffile)) != EOF) |
|
3125 { |
|
3126 if (update_pos) |
|
3127 current_input_column--; |
|
3128 ungetc (c, ffile); |
|
3129 if (c == '\n') |
|
3130 break; |
|
3131 } |
|
3132 else |
|
3133 break; |
|
3134 } |
|
3135 } |
|
3136 } |
|
3137 else |
|
3138 { |
|
3139 switch (c) |
|
3140 { |
|
3141 case ' ': |
|
3142 case '\t': |
|
3143 if (first_comments_seen) |
|
3144 have_help_text = true; |
|
3145 break; |
|
3146 |
3802
|
3147 case '%': |
|
3148 case '#': |
|
3149 begin_comment = true; |
|
3150 in_comment = true; |
|
3151 break; |
|
3152 |
3021
|
3153 case '\n': |
|
3154 if (first_comments_seen) |
|
3155 have_help_text = true; |
|
3156 if (update_pos) |
|
3157 { |
|
3158 input_line_number++; |
|
3159 current_input_column = 0; |
|
3160 } |
|
3161 continue; |
|
3162 |
3802
|
3163 case '\r': |
|
3164 c = getc (ffile); |
|
3165 if (update_pos) |
|
3166 current_input_column++; |
|
3167 if (c == EOF) |
|
3168 goto done; |
|
3169 else if (c == '\n') |
|
3170 { |
|
3171 if (first_comments_seen) |
|
3172 have_help_text = true; |
|
3173 if (update_pos) |
|
3174 { |
|
3175 input_line_number++; |
|
3176 current_input_column = 0; |
|
3177 } |
|
3178 continue; |
|
3179 } |
|
3180 |
|
3181 // Fall through... |
3021
|
3182 |
|
3183 default: |
|
3184 if (update_pos) |
|
3185 current_input_column--; |
|
3186 ungetc (c, ffile); |
|
3187 goto done; |
|
3188 } |
|
3189 } |
|
3190 } |
|
3191 |
|
3192 done: |
|
3193 |
|
3194 if (! help_txt.empty ()) |
|
3195 { |
3665
|
3196 if (looks_like_octave_copyright (help_txt)) |
|
3197 { |
|
3198 if (save_copyright) |
|
3199 octave_comment_buffer::append (help_txt); |
|
3200 |
|
3201 help_txt.resize (0); |
|
3202 } |
3021
|
3203 |
|
3204 if (in_parts && help_txt.empty ()) |
3665
|
3205 help_txt = gobble_leading_white_space (ffile, in_parts, |
|
3206 update_pos, false); |
3021
|
3207 } |
|
3208 |
|
3209 return help_txt; |
|
3210 } |
|
3211 |
3536
|
3212 std::string |
3523
|
3213 get_help_from_file (const std::string& path) |
3021
|
3214 { |
3523
|
3215 std::string retval; |
3021
|
3216 |
|
3217 if (! path.empty ()) |
|
3218 { |
|
3219 FILE *fptr = fopen (path.c_str (), "r"); |
|
3220 |
|
3221 if (fptr) |
|
3222 { |
|
3223 unwind_protect::add (safe_fclose, (void *) fptr); |
|
3224 |
3665
|
3225 retval = gobble_leading_white_space (fptr, true, true, false); |
3021
|
3226 |
|
3227 unwind_protect::run (); |
|
3228 } |
|
3229 } |
|
3230 |
|
3231 return retval; |
|
3232 } |
|
3233 |
|
3234 static int |
|
3235 is_function_file (FILE *ffile) |
|
3236 { |
|
3237 int status = 0; |
|
3238 |
|
3239 long pos = ftell (ffile); |
|
3240 |
3665
|
3241 gobble_leading_white_space (ffile, false, false, false); |
3021
|
3242 |
|
3243 char buf [10]; |
|
3244 fgets (buf, 10, ffile); |
|
3245 int len = strlen (buf); |
|
3246 if (len > 8 && strncmp (buf, "function", 8) == 0 |
|
3247 && ! (isalnum (buf[8]) || buf[8] == '_')) |
|
3248 status = 1; |
|
3249 |
|
3250 fseek (ffile, pos, SEEK_SET); |
|
3251 |
|
3252 return status; |
|
3253 } |
|
3254 |
|
3255 static void |
|
3256 restore_command_history (void *) |
|
3257 { |
|
3258 command_history::ignore_entries (! Vsaving_history); |
|
3259 } |
|
3260 |
|
3261 static void |
|
3262 restore_input_stream (void *f) |
|
3263 { |
|
3264 command_editor::set_input_stream (static_cast<FILE *> (f)); |
|
3265 } |
|
3266 |
3180
|
3267 static void |
|
3268 clear_current_script_file_name (void *) |
|
3269 { |
|
3270 bind_builtin_variable ("current_script_file_name", octave_value ()); |
|
3271 } |
|
3272 |
3021
|
3273 static bool |
3523
|
3274 parse_fcn_file (const std::string& ff, bool exec_script, bool force_script = false) |
3021
|
3275 { |
|
3276 unwind_protect::begin_frame ("parse_fcn_file"); |
|
3277 |
|
3278 int script_file_executed = false; |
|
3279 |
|
3280 // Open function file and parse. |
|
3281 |
|
3282 bool old_reading_fcn_file_state = reading_fcn_file; |
|
3283 |
|
3284 FILE *in_stream = command_editor::get_input_stream (); |
|
3285 |
|
3286 unwind_protect::add (restore_input_stream, in_stream); |
|
3287 |
|
3288 unwind_protect_ptr (ff_instream); |
|
3289 |
|
3290 unwind_protect_int (input_line_number); |
|
3291 unwind_protect_int (current_input_column); |
4238
|
3292 unwind_protect_int (end_tokens_expected); |
3021
|
3293 unwind_protect_bool (reading_fcn_file); |
|
3294 unwind_protect_bool (line_editing); |
4238
|
3295 unwind_protect_str (parent_function_name); |
3021
|
3296 |
|
3297 input_line_number = 0; |
|
3298 current_input_column = 1; |
4238
|
3299 end_tokens_expected = 0; |
3021
|
3300 reading_fcn_file = true; |
|
3301 line_editing = false; |
4238
|
3302 parent_function_name = ""; |
3021
|
3303 |
|
3304 FILE *ffile = get_input_from_file (ff, 0); |
|
3305 |
|
3306 unwind_protect::add (safe_fclose, ffile); |
|
3307 |
|
3308 if (ffile) |
|
3309 { |
|
3310 // Check to see if this file defines a function or is just a |
|
3311 // list of commands. |
|
3312 |
3165
|
3313 if (! force_script && is_function_file (ffile)) |
3021
|
3314 { |
|
3315 // XXX FIXME XXX -- we shouldn't need both the |
|
3316 // command_history object and the |
|
3317 // Vsaving_history variable... |
|
3318 command_history::ignore_entries (); |
|
3319 |
|
3320 unwind_protect::add (restore_command_history, 0); |
|
3321 |
|
3322 unwind_protect_int (Vecho_executing_commands); |
|
3323 unwind_protect_bool (Vsaving_history); |
|
3324 unwind_protect_bool (reading_fcn_file); |
|
3325 unwind_protect_bool (input_from_command_line_file); |
3877
|
3326 unwind_protect_bool (get_input_from_eval_string); |
3883
|
3327 unwind_protect_bool (parser_end_of_input); |
3021
|
3328 |
|
3329 Vecho_executing_commands = ECHO_OFF; |
|
3330 Vsaving_history = false; |
|
3331 reading_fcn_file = true; |
|
3332 input_from_command_line_file = false; |
3877
|
3333 get_input_from_eval_string = false; |
3883
|
3334 parser_end_of_input = false; |
3021
|
3335 |
|
3336 YY_BUFFER_STATE old_buf = current_buffer (); |
|
3337 YY_BUFFER_STATE new_buf = create_buffer (ffile); |
|
3338 |
|
3339 unwind_protect::add (restore_input_buffer, (void *) old_buf); |
|
3340 unwind_protect::add (delete_input_buffer, (void *) new_buf); |
|
3341 |
|
3342 switch_to_buffer (new_buf); |
|
3343 |
|
3344 unwind_protect_ptr (curr_sym_tab); |
|
3345 |
|
3346 reset_parser (); |
|
3347 |
4426
|
3348 std::string txt |
|
3349 = gobble_leading_white_space (ffile, true, true, true); |
|
3350 |
|
3351 help_buf.push (txt); |
|
3352 |
|
3353 octave_comment_buffer::append (txt); |
3021
|
3354 |
|
3355 // XXX FIXME XXX -- this should not be necessary. |
3665
|
3356 gobble_leading_white_space (ffile, false, true, false); |
3021
|
3357 |
|
3358 int status = yyparse (); |
|
3359 |
|
3360 if (status != 0) |
|
3361 { |
|
3362 error ("parse error while reading function file %s", |
|
3363 ff.c_str ()); |
4009
|
3364 fbi_sym_tab->clear (curr_fcn_file_name); |
3021
|
3365 } |
|
3366 } |
|
3367 else if (exec_script) |
|
3368 { |
|
3369 // The value of `reading_fcn_file' will be restored to the |
|
3370 // proper value when we unwind from this frame. |
|
3371 reading_fcn_file = old_reading_fcn_file_state; |
|
3372 |
|
3373 // XXX FIXME XXX -- we shouldn't need both the |
|
3374 // command_history object and the |
|
3375 // Vsaving_history variable... |
|
3376 command_history::ignore_entries (); |
|
3377 |
|
3378 unwind_protect::add (restore_command_history, 0); |
|
3379 |
|
3380 unwind_protect_bool (Vsaving_history); |
|
3381 unwind_protect_bool (reading_script_file); |
|
3382 |
|
3383 Vsaving_history = false; |
|
3384 reading_script_file = true; |
|
3385 |
3180
|
3386 unwind_protect::add (clear_current_script_file_name, 0); |
|
3387 |
|
3388 bind_builtin_variable ("current_script_file_name", ff); |
|
3389 |
3021
|
3390 parse_and_execute (ffile); |
|
3391 |
|
3392 script_file_executed = true; |
|
3393 } |
|
3394 } |
3880
|
3395 else |
|
3396 error ("no such file, `%s'", ff.c_str ()); |
3021
|
3397 |
|
3398 unwind_protect::run_frame ("parse_fcn_file"); |
|
3399 |
|
3400 return script_file_executed; |
|
3401 } |
|
3402 |
|
3403 bool |
|
3404 load_fcn_from_file (symbol_record *sym_rec, bool exec_script) |
|
3405 { |
|
3406 bool script_file_executed = false; |
|
3407 |
3523
|
3408 std::string nm = sym_rec->name (); |
3021
|
3409 |
4244
|
3410 string_vector names (2); |
4243
|
3411 |
|
3412 names[0] = nm + ".oct"; |
|
3413 names[1] = nm + ".m"; |
|
3414 |
|
3415 std::string file |
|
3416 = octave_env::make_absolute (Vload_path_dir_path.find_first_of (names), |
|
3417 octave_env::getcwd ()); |
|
3418 |
|
3419 int len = file.length (); |
|
3420 |
4244
|
3421 if (len > 4 && file.substr (len-4, len-1) == ".oct") |
3021
|
3422 { |
4243
|
3423 if (octave_dynamic_loader::load (nm, file)) |
|
3424 force_link_to_function (nm); |
3021
|
3425 } |
4244
|
3426 else if (len > 2) |
3021
|
3427 { |
|
3428 // These are needed by yyparse. |
|
3429 |
|
3430 unwind_protect::begin_frame ("load_fcn_from_file"); |
|
3431 |
|
3432 unwind_protect_str (curr_fcn_file_name); |
|
3433 unwind_protect_str (curr_fcn_file_full_name); |
|
3434 |
|
3435 curr_fcn_file_name = nm; |
4243
|
3436 curr_fcn_file_full_name = file; |
|
3437 |
4244
|
3438 script_file_executed = parse_fcn_file (file, exec_script); |
3021
|
3439 |
|
3440 if (! (error_state || script_file_executed)) |
|
3441 force_link_to_function (nm); |
|
3442 |
|
3443 unwind_protect::run_frame ("load_fcn_from_file"); |
|
3444 } |
|
3445 |
|
3446 return script_file_executed; |
|
3447 } |
|
3448 |
|
3449 DEFUN (source, args, , |
3371
|
3450 "-*- texinfo -*-\n\ |
|
3451 @deftypefn {Built-in Function} {} source (@var{file})\n\ |
|
3452 Parse and execute the contents of @var{file}. This is equivalent to\n\ |
|
3453 executing commands from a script file, but without requiring the file to\n\ |
|
3454 be named @file{@var{file}.m}.\n\ |
|
3455 @end deftypefn") |
3021
|
3456 { |
|
3457 octave_value_list retval; |
|
3458 |
|
3459 int nargin = args.length (); |
|
3460 |
|
3461 if (nargin == 1) |
|
3462 { |
3523
|
3463 std::string file_name = args(0).string_value (); |
3021
|
3464 |
|
3465 if (! error_state) |
|
3466 { |
3523
|
3467 std::string file_full_name = file_ops::tilde_expand (file_name); |
3388
|
3468 |
|
3469 unwind_protect::begin_frame ("Fsource"); |
|
3470 |
|
3471 unwind_protect_str (curr_fcn_file_name); |
|
3472 unwind_protect_str (curr_fcn_file_full_name); |
|
3473 |
|
3474 curr_fcn_file_name = file_name; |
|
3475 curr_fcn_file_full_name = file_full_name; |
|
3476 |
|
3477 parse_fcn_file (file_full_name, true, true); |
3021
|
3478 |
|
3479 if (error_state) |
3388
|
3480 error ("source: error sourcing file `%s'", |
|
3481 file_full_name.c_str ()); |
|
3482 |
|
3483 unwind_protect::run_frame ("Fsource"); |
3021
|
3484 } |
|
3485 else |
|
3486 error ("source: expecting file name as argument"); |
|
3487 } |
|
3488 else |
|
3489 print_usage ("source"); |
|
3490 |
|
3491 return retval; |
|
3492 } |
|
3493 |
3726
|
3494 // Evaluate an Octave function (built-in or interpreted) and return |
3844
|
3495 // the list of result values. NAME is the name of the function to |
|
3496 // call. ARGS are the arguments to the function. NARGOUT is the |
|
3497 // number of output arguments expected. |
3726
|
3498 |
3021
|
3499 octave_value_list |
3523
|
3500 feval (const std::string& name, const octave_value_list& args, int nargout) |
3156
|
3501 { |
|
3502 octave_value_list retval; |
|
3503 |
|
3504 octave_function *fcn = is_valid_function (name, "feval", 1); |
|
3505 |
|
3506 if (fcn) |
3544
|
3507 retval = fcn->do_multi_index_op (nargout, args); |
3156
|
3508 |
|
3509 return retval; |
|
3510 } |
|
3511 |
4342
|
3512 octave_value_list |
|
3513 feval (octave_function *fcn, const octave_value_list& args, int nargout) |
|
3514 { |
|
3515 octave_value_list retval; |
|
3516 |
|
3517 if (fcn) |
|
3518 retval = fcn->do_multi_index_op (nargout, args); |
|
3519 |
|
3520 return retval; |
|
3521 } |
|
3522 |
|
3523 static octave_value_list |
|
3524 get_feval_args (const octave_value_list& args) |
|
3525 { |
|
3526 int tmp_nargin = args.length () - 1; |
|
3527 |
|
3528 octave_value_list retval (tmp_nargin, octave_value ()); |
|
3529 |
|
3530 for (int i = 0; i < tmp_nargin; i++) |
|
3531 retval(i) = args(i+1); |
|
3532 |
|
3533 string_vector arg_names = args.name_tags (); |
|
3534 |
|
3535 if (! arg_names.empty ()) |
|
3536 { |
|
3537 // tmp_nargin and arg_names.length () - 1 may differ if |
|
3538 // we are passed all_va_args. |
|
3539 |
|
3540 int n = arg_names.length () - 1; |
|
3541 |
|
3542 int len = n > tmp_nargin ? tmp_nargin : n; |
|
3543 |
|
3544 string_vector tmp_arg_names (len); |
|
3545 |
|
3546 for (int i = 0; i < len; i++) |
|
3547 tmp_arg_names(i) = arg_names(i+1); |
|
3548 |
|
3549 retval.stash_name_tags (tmp_arg_names); |
|
3550 } |
|
3551 |
|
3552 return retval; |
|
3553 } |
|
3554 |
|
3555 |
3726
|
3556 // Evaluate an Octave function (built-in or interpreted) and return |
|
3557 // the list of result values. The first element of ARGS should be a |
|
3558 // string containing the name of the function to call, then the rest |
|
3559 // are the actual arguments to the function. NARGOUT is the number of |
|
3560 // output arguments expected. |
|
3561 |
3156
|
3562 octave_value_list |
3021
|
3563 feval (const octave_value_list& args, int nargout) |
|
3564 { |
|
3565 octave_value_list retval; |
|
3566 |
4342
|
3567 int nargin = args.length (); |
|
3568 |
|
3569 if (nargin > 0) |
3021
|
3570 { |
4342
|
3571 octave_value f_arg = args(0); |
|
3572 |
|
3573 if (f_arg.is_string ()) |
|
3574 { |
|
3575 std::string name = f_arg.string_value (); |
|
3576 |
|
3577 if (! error_state) |
3156
|
3578 { |
4342
|
3579 octave_value_list tmp_args = get_feval_args (args); |
|
3580 |
|
3581 retval = feval (name, tmp_args, nargout); |
3156
|
3582 } |
4342
|
3583 } |
|
3584 else |
|
3585 { |
|
3586 octave_function *fcn = f_arg.function_value (); |
|
3587 |
|
3588 if (fcn) |
|
3589 { |
|
3590 octave_value_list tmp_args = get_feval_args (args); |
|
3591 |
|
3592 retval = feval (fcn, tmp_args, nargout); |
|
3593 } |
3021
|
3594 } |
|
3595 } |
|
3596 |
|
3597 return retval; |
|
3598 } |
|
3599 |
|
3600 DEFUN (feval, args, nargout, |
3371
|
3601 "-*- texinfo -*-\n\ |
|
3602 @deftypefn {Built-in Function} {} feval (@var{name}, @dots{})\n\ |
|
3603 Evaluate the function named @var{name}. Any arguments after the first\n\ |
|
3604 are passed on to the named function. For example,\n\ |
|
3605 \n\ |
|
3606 @example\n\ |
|
3607 feval (\"acos\", -1)\n\ |
|
3608 @result{} 3.1416\n\ |
|
3609 @end example\n\ |
3021
|
3610 \n\ |
3371
|
3611 @noindent\n\ |
|
3612 calls the function @code{acos} with the argument @samp{-1}.\n\ |
|
3613 \n\ |
|
3614 The function @code{feval} is necessary in order to be able to write\n\ |
|
3615 functions that call user-supplied functions, because Octave does not\n\ |
|
3616 have a way to declare a pointer to a function (like C) or to declare a\n\ |
|
3617 special kind of variable that can be used to hold the name of a function\n\ |
|
3618 (like @code{EXTERNAL} in Fortran). Instead, you must refer to functions\n\ |
|
3619 by name, and use @code{feval} to call them.\n\ |
|
3620 @end deftypefn") |
3021
|
3621 { |
|
3622 octave_value_list retval; |
|
3623 |
|
3624 int nargin = args.length (); |
|
3625 |
|
3626 if (nargin > 0) |
|
3627 retval = feval (args, nargout); |
|
3628 else |
|
3629 print_usage ("feval"); |
|
3630 |
|
3631 return retval; |
|
3632 } |
|
3633 |
3099
|
3634 octave_value_list |
3523
|
3635 eval_string (const std::string& s, bool silent, int& parse_status, int nargout) |
3021
|
3636 { |
3877
|
3637 octave_value_list retval; |
|
3638 |
3021
|
3639 unwind_protect::begin_frame ("eval_string"); |
|
3640 |
|
3641 unwind_protect_bool (get_input_from_eval_string); |
3877
|
3642 unwind_protect_bool (input_from_eval_string_pending); |
3021
|
3643 unwind_protect_bool (input_from_command_line_file); |
3883
|
3644 unwind_protect_bool (parser_end_of_input); |
3021
|
3645 unwind_protect_str (current_eval_string); |
|
3646 |
|
3647 get_input_from_eval_string = true; |
3877
|
3648 input_from_eval_string_pending = true; |
3021
|
3649 input_from_command_line_file = false; |
3883
|
3650 parser_end_of_input = false; |
3021
|
3651 current_eval_string = s; |
|
3652 |
3877
|
3653 unwind_protect_ptr (global_command); |
|
3654 |
3021
|
3655 YY_BUFFER_STATE old_buf = current_buffer (); |
|
3656 YY_BUFFER_STATE new_buf = create_buffer (0); |
|
3657 |
|
3658 unwind_protect::add (restore_input_buffer, old_buf); |
|
3659 unwind_protect::add (delete_input_buffer, new_buf); |
|
3660 |
|
3661 switch_to_buffer (new_buf); |
|
3662 |
|
3663 unwind_protect_ptr (curr_sym_tab); |
|
3664 |
3877
|
3665 do |
|
3666 { |
4318
|
3667 reset_parser (); |
|
3668 |
3877
|
3669 parse_status = yyparse (); |
|
3670 |
|
3671 tree_statement_list *command = global_command; |
|
3672 |
3883
|
3673 if (parse_status == 0) |
3877
|
3674 { |
3883
|
3675 if (command) |
|
3676 { |
|
3677 retval = command->eval (silent, nargout); |
|
3678 |
|
3679 delete command; |
|
3680 |
|
3681 command = 0; |
|
3682 |
|
3683 if (error_state |
4207
|
3684 || tree_return_command::returning |
|
3685 || tree_break_command::breaking |
|
3686 || tree_continue_command::continuing) |
3883
|
3687 break; |
|
3688 } |
|
3689 else if (parser_end_of_input) |
3877
|
3690 break; |
3883
|
3691 } |
3877
|
3692 } |
|
3693 while (parse_status == 0); |
3021
|
3694 |
|
3695 unwind_protect::run_frame ("eval_string"); |
|
3696 |
|
3697 return retval; |
|
3698 } |
|
3699 |
|
3700 octave_value |
3523
|
3701 eval_string (const std::string& s, bool silent, int& parse_status) |
3021
|
3702 { |
|
3703 octave_value retval; |
|
3704 |
|
3705 octave_value_list tmp = eval_string (s, silent, parse_status, 1); |
|
3706 |
|
3707 if (! tmp.empty ()) |
|
3708 retval = tmp(0); |
|
3709 |
|
3710 return retval; |
|
3711 } |
|
3712 |
|
3713 static octave_value_list |
|
3714 eval_string (const octave_value& arg, bool silent, int& parse_status, |
|
3715 int nargout) |
|
3716 { |
3523
|
3717 std::string s = arg.string_value (); |
3021
|
3718 |
|
3719 if (error_state) |
|
3720 { |
3523
|
3721 error ("eval: expecting std::string argument"); |
4233
|
3722 return octave_value (-1); |
3021
|
3723 } |
|
3724 |
|
3725 return eval_string (s, silent, parse_status, nargout); |
|
3726 } |
|
3727 |
|
3728 DEFUN (eval, args, nargout, |
3371
|
3729 "-*- texinfo -*-\n\ |
|
3730 @deftypefn {Built-in Function} {} eval (@var{try}, @var{catch})\n\ |
|
3731 Parse the string @var{try} and evaluate it as if it were an Octave\n\ |
|
3732 program, returning the last value computed. If that fails, evaluate\n\ |
|
3733 the string @var{catch}. The string @var{try} is evaluated in the\n\ |
|
3734 current context, so any results remain available after @code{eval}\n\ |
|
3735 returns. For example,\n\ |
|
3736 \n\ |
|
3737 @example\n\ |
|
3738 @group\n\ |
|
3739 eval (\"a = 13\")\n\ |
|
3740 @print{} a = 13\n\ |
|
3741 @result{} 13\n\ |
|
3742 @end group\n\ |
|
3743 @end example\n\ |
3021
|
3744 \n\ |
3371
|
3745 In this case, the value of the evaluated expression is printed and it is\n\ |
|
3746 also returned returned from @code{eval}. Just as with any other\n\ |
|
3747 expression, you can turn printing off by ending the expression in a\n\ |
|
3748 semicolon. For example,\n\ |
|
3749 \n\ |
|
3750 @example\n\ |
|
3751 eval (\"a = 13;\")\n\ |
|
3752 @result{} 13\n\ |
|
3753 @end example\n\ |
|
3754 \n\ |
|
3755 In this example, the variable @code{a} has been given the value 13, but\n\ |
|
3756 the value of the expression is not printed. You can also turn off\n\ |
|
3757 automatic printing for all expressions executed by @code{eval} using the\n\ |
|
3758 variable @code{default_eval_print_flag}.\n\ |
|
3759 @end deftypefn") |
3021
|
3760 { |
|
3761 octave_value_list retval; |
|
3762 |
|
3763 int nargin = args.length (); |
|
3764 |
|
3765 if (nargin > 0) |
|
3766 { |
|
3767 unwind_protect::begin_frame ("Feval"); |
|
3768 |
|
3769 if (nargin > 1) |
|
3770 { |
|
3771 unwind_protect_bool (buffer_error_messages); |
|
3772 buffer_error_messages = true; |
|
3773 } |
|
3774 |
|
3775 int parse_status = 0; |
|
3776 |
|
3777 retval = eval_string (args(0), ! Vdefault_eval_print_flag, |
|
3778 parse_status, nargout); |
|
3779 |
|
3780 if (nargin > 1 && (parse_status != 0 || error_state)) |
|
3781 { |
|
3782 error_state = 0; |
|
3783 |
|
3784 // Set up for letting the user print any messages from |
|
3785 // errors that occurred in the first part of this eval(). |
|
3786 |
|
3787 buffer_error_messages = false; |
3815
|
3788 |
3021
|
3789 bind_global_error_variable (); |
3815
|
3790 |
3021
|
3791 unwind_protect::add (clear_global_error_variable, 0); |
|
3792 |
|
3793 eval_string (args(1), 0, parse_status, nargout); |
|
3794 |
|
3795 retval = octave_value_list (); |
|
3796 } |
|
3797 |
|
3798 unwind_protect::run_frame ("Feval"); |
|
3799 } |
|
3800 else |
|
3801 print_usage ("eval"); |
|
3802 |
|
3803 return retval; |
|
3804 } |
|
3805 |
4297
|
3806 DEFUN (assignin, args, nargout, |
|
3807 "-*- texinfo -*-\n\ |
|
3808 @deftypefn {Built-in Function} {} assignin (@var{context}, @var{varname}, @var{value})\n\ |
|
3809 Assign @var{value} to @var{varname} in context @var{context}, which\n\ |
|
3810 may be either @code{\"base\"} or @code{\"caller\"}.\n\ |
|
3811 @end deftypefn") |
|
3812 { |
|
3813 octave_value_list retval; |
|
3814 |
|
3815 int nargin = args.length (); |
|
3816 |
|
3817 if (nargin == 3) |
|
3818 { |
|
3819 std::string context = args(0).string_value (); |
|
3820 |
|
3821 if (! error_state) |
|
3822 { |
|
3823 unwind_protect::begin_frame ("Fassignin"); |
|
3824 |
|
3825 unwind_protect_ptr (curr_sym_tab); |
|
3826 |
|
3827 if (context == "caller") |
|
3828 curr_sym_tab = curr_caller_sym_tab; |
|
3829 else if (context == "base") |
|
3830 curr_sym_tab = top_level_sym_tab; |
|
3831 else |
|
3832 error ("assignin: context must be \"caller\" or \"base\""); |
|
3833 |
|
3834 if (! error_state) |
|
3835 { |
|
3836 std::string nm = args(1).string_value (); |
|
3837 |
|
3838 if (! error_state) |
|
3839 { |
|
3840 if (valid_identifier (nm)) |
|
3841 { |
|
3842 symbol_record *sr = curr_sym_tab->lookup (nm, true); |
|
3843 |
|
3844 if (sr) |
|
3845 { |
|
3846 tree_identifier *id = new tree_identifier (sr); |
|
3847 tree_constant *rhs = new tree_constant (args(2)); |
|
3848 |
|
3849 tree_simple_assignment tsa (id, rhs); |
|
3850 |
|
3851 tsa.rvalue (); |
|
3852 } |
|
3853 } |
|
3854 else |
|
3855 error ("assignin: invalid variable name"); |
|
3856 } |
|
3857 else |
|
3858 error ("assignin: expecting variable name as second argument"); |
|
3859 } |
|
3860 |
|
3861 unwind_protect::run_frame ("Fassignin"); |
|
3862 } |
|
3863 else |
|
3864 error ("assignin: expecting string as first argument"); |
|
3865 } |
|
3866 else |
|
3867 print_usage ("assignin"); |
|
3868 |
|
3869 return retval; |
|
3870 } |
|
3871 |
4245
|
3872 DEFUN (evalin, args, nargout, |
|
3873 "-*- texinfo -*-\n\ |
|
3874 @deftypefn {Built-in Function} {} evalin (@var{context}, @var{try}, @var{catch})\n\ |
|
3875 Like @code{eval}, except that the expressions are evaluated in the\n\ |
|
3876 context @var{context}, which may be either @code{\"caller\"} or\n\ |
4246
|
3877 @code{\"base\"}.\n\ |
4245
|
3878 @end deftypefn") |
|
3879 { |
|
3880 octave_value_list retval; |
|
3881 |
|
3882 int nargin = args.length (); |
|
3883 |
|
3884 if (nargin > 1) |
|
3885 { |
|
3886 std::string context = args(0).string_value (); |
|
3887 |
|
3888 if (! error_state) |
|
3889 { |
|
3890 unwind_protect::begin_frame ("Fevalin"); |
|
3891 |
|
3892 unwind_protect_ptr (curr_sym_tab); |
|
3893 |
|
3894 if (context == "caller") |
|
3895 curr_sym_tab = curr_caller_sym_tab; |
|
3896 else if (context == "base") |
|
3897 curr_sym_tab = top_level_sym_tab; |
|
3898 else |
|
3899 error ("evalin: context must be \"caller\" or \"base\""); |
|
3900 |
4297
|
3901 if (! error_state) |
4245
|
3902 { |
4297
|
3903 if (nargin > 2) |
|
3904 { |
|
3905 unwind_protect_bool (buffer_error_messages); |
|
3906 buffer_error_messages = true; |
|
3907 } |
|
3908 |
|
3909 int parse_status = 0; |
|
3910 |
|
3911 retval = eval_string (args(1), ! Vdefault_eval_print_flag, |
|
3912 parse_status, nargout); |
|
3913 |
|
3914 if (nargin > 2 && (parse_status != 0 || error_state)) |
|
3915 { |
|
3916 error_state = 0; |
|
3917 |
|
3918 // Set up for letting the user print any messages from |
|
3919 // errors that occurred in the first part of this eval(). |
|
3920 |
|
3921 buffer_error_messages = false; |
|
3922 |
|
3923 bind_global_error_variable (); |
|
3924 |
|
3925 unwind_protect::add (clear_global_error_variable, 0); |
|
3926 |
|
3927 eval_string (args(2), 0, parse_status, nargout); |
|
3928 |
|
3929 retval = octave_value_list (); |
|
3930 } |
4245
|
3931 } |
|
3932 |
|
3933 unwind_protect::run_frame ("Fevalin"); |
|
3934 } |
|
3935 else |
|
3936 error ("evalin: expecting string as first argument"); |
|
3937 } |
|
3938 else |
|
3939 print_usage ("evalin"); |
|
3940 |
|
3941 return retval; |
|
3942 } |
|
3943 |
3021
|
3944 static int |
|
3945 default_eval_print_flag (void) |
|
3946 { |
|
3947 Vdefault_eval_print_flag = check_preference ("default_eval_print_flag"); |
|
3948 |
|
3949 return 0; |
|
3950 } |
|
3951 |
2166
|
3952 static int |
|
3953 warn_assign_as_truth_value (void) |
|
3954 { |
|
3955 Vwarn_assign_as_truth_value |
|
3956 = check_preference ("warn_assign_as_truth_value"); |
|
3957 |
|
3958 return 0; |
|
3959 } |
|
3960 |
|
3961 static int |
|
3962 warn_function_name_clash (void) |
|
3963 { |
|
3964 Vwarn_function_name_clash = check_preference ("warn_function_name_clash"); |
|
3965 |
|
3966 return 0; |
|
3967 } |
|
3968 |
|
3969 static int |
3162
|
3970 warn_future_time_stamp (void) |
|
3971 { |
|
3972 Vwarn_future_time_stamp = check_preference ("warn_future_time_stamp"); |
|
3973 |
|
3974 return 0; |
|
3975 } |
|
3976 |
|
3977 static int |
2166
|
3978 warn_missing_semicolon (void) |
|
3979 { |
|
3980 Vwarn_missing_semicolon = check_preference ("warn_missing_semicolon"); |
|
3981 |
|
3982 return 0; |
|
3983 } |
|
3984 |
2764
|
3985 static int |
4023
|
3986 warn_precedence_change (void) |
|
3987 { |
|
3988 Vwarn_precedence_change = check_preference ("warn_precedence_change"); |
|
3989 |
|
3990 return 0; |
|
3991 } |
|
3992 |
|
3993 static int |
2764
|
3994 warn_variable_switch_label (void) |
|
3995 { |
|
3996 Vwarn_variable_switch_label |
|
3997 = check_preference ("warn_variable_switch_label"); |
|
3998 |
|
3999 return 0; |
|
4000 } |
|
4001 |
2166
|
4002 void |
|
4003 symbols_of_parse (void) |
|
4004 { |
4233
|
4005 DEFVAR (default_eval_print_flag, true, default_eval_print_flag, |
3371
|
4006 "-*- texinfo -*-\n\ |
|
4007 @defvr {Built-in Variable} default_eval_print_flag\n\ |
|
4008 If the value of this variable is nonzero, Octave prints the results of\n\ |
|
4009 commands executed by @code{eval} that do not end with semicolons. If it\n\ |
|
4010 is zero, automatic printing is suppressed. The default value is 1.\n\ |
|
4011 @end defvr"); |
3021
|
4012 |
4233
|
4013 DEFVAR (warn_assign_as_truth_value, true, warn_assign_as_truth_value, |
3366
|
4014 "-*- texinfo -*-\n\ |
|
4015 @defvr {Built-in Variable} warn_assign_as_truth_value\n\ |
|
4016 If the value of @code{warn_assign_as_truth_value} is nonzero, a\n\ |
|
4017 warning is issued for statements like\n\ |
|
4018 \n\ |
|
4019 @example\n\ |
|
4020 if (s = t)\n\ |
|
4021 ...\n\ |
|
4022 @end example\n\ |
|
4023 \n\ |
|
4024 @noindent\n\ |
|
4025 since such statements are not common, and it is likely that the intent\n\ |
|
4026 was to write\n\ |
|
4027 \n\ |
|
4028 @example\n\ |
|
4029 if (s == t)\n\ |
|
4030 ...\n\ |
|
4031 @end example\n\ |
|
4032 \n\ |
|
4033 @noindent\n\ |
|
4034 instead.\n\ |
|
4035 \n\ |
|
4036 There are times when it is useful to write code that contains\n\ |
|
4037 assignments within the condition of a @code{while} or @code{if}\n\ |
|
4038 statement. For example, statements like\n\ |
|
4039 \n\ |
|
4040 @example\n\ |
|
4041 while (c = getc())\n\ |
|
4042 ...\n\ |
|
4043 @end example\n\ |
|
4044 \n\ |
|
4045 @noindent\n\ |
|
4046 are common in C programming.\n\ |
|
4047 \n\ |
|
4048 It is possible to avoid all warnings about such statements by setting\n\ |
|
4049 @code{warn_assign_as_truth_value} to 0, but that may also\n\ |
|
4050 let real errors like\n\ |
|
4051 \n\ |
|
4052 @example\n\ |
|
4053 if (x = 1) # intended to test (x == 1)!\n\ |
|
4054 ...\n\ |
|
4055 @end example\n\ |
|
4056 \n\ |
|
4057 @noindent\n\ |
|
4058 slip by.\n\ |
|
4059 \n\ |
|
4060 In such cases, it is possible suppress errors for specific statements by\n\ |
|
4061 writing them with an extra set of parentheses. For example, writing the\n\ |
|
4062 previous example as\n\ |
|
4063 \n\ |
|
4064 @example\n\ |
|
4065 while ((c = getc()))\n\ |
|
4066 ...\n\ |
|
4067 @end example\n\ |
|
4068 \n\ |
|
4069 @noindent\n\ |
|
4070 will prevent the warning from being printed for this statement, while\n\ |
|
4071 allowing Octave to warn about other assignments used in conditional\n\ |
|
4072 contexts.\n\ |
|
4073 \n\ |
|
4074 The default value of @code{warn_assign_as_truth_value} is 1.\n\ |
|
4075 @end defvr"); |
2166
|
4076 |
4233
|
4077 DEFVAR (warn_function_name_clash, true, warn_function_name_clash, |
3371
|
4078 "-*- texinfo -*-\n\ |
|
4079 @defvr {Built-in Variable} warn_function_name_clash\n\ |
|
4080 If the value of @code{warn_function_name_clash} is nonzero, a warning is\n\ |
|
4081 issued when Octave finds that the name of a function defined in a\n\ |
|
4082 function file differs from the name of the file. (If the names\n\ |
|
4083 disagree, the name declared inside the file is ignored.) If the value\n\ |
|
4084 is 0, the warning is omitted. The default value is 1.\n\ |
|
4085 @end defvr"); |
2166
|
4086 |
4233
|
4087 DEFVAR (warn_future_time_stamp, true, warn_future_time_stamp, |
3446
|
4088 "-*- texinfo -*-\n\ |
|
4089 @defvr {Built-in Variable} warn_future_time_stamp\n\ |
|
4090 If the value of this variable is nonzero, Octave will print a warning\n\ |
|
4091 if it finds a function file with a time stamp that is in the future.\n\ |
|
4092 @end defvr"); |
3162
|
4093 |
4233
|
4094 DEFVAR (warn_missing_semicolon, false, warn_missing_semicolon, |
3371
|
4095 "-*- texinfo -*-\n\ |
|
4096 @defvr {Built-in Variable} warn_missing_semicolon\n\ |
|
4097 If the value of this variable is nonzero, Octave will warn when\n\ |
|
4098 statements in function definitions don't end in semicolons. The default\n\ |
|
4099 value is 0.\n\ |
|
4100 @end defvr"); |
2764
|
4101 |
4233
|
4102 DEFVAR (warn_precedence_change, true, warn_precedence_change, |
4023
|
4103 "-*- texinfo -*-\n\ |
|
4104 @defvr {Built-in Variable} warn_precedence_change\n\ |
|
4105 If the value of this variable is nonzero, Octave will warn about\n\ |
|
4106 possible changes in the meaning of some code due to changes in\n\ |
|
4107 precedence for some operators. Precedence changes have typically\n\ |
|
4108 been made for Matlab compatibility. The default value is 1.\n\ |
|
4109 @end defvr"); |
|
4110 |
4233
|
4111 DEFVAR (warn_variable_switch_label, false, warn_variable_switch_label, |
3366
|
4112 "-*- texinfo -*-\n\ |
|
4113 @defvr {Built-in Variable} warn_variable_switch_label\n\ |
|
4114 If the value of this variable is nonzero, Octave will print a warning if\n\ |
|
4115 a switch label is not a constant or constant expression\n\ |
|
4116 @end defvr"); |
3347
|
4117 |
2166
|
4118 } |
|
4119 |
1994
|
4120 /* |
|
4121 ;;; Local Variables: *** |
|
4122 ;;; mode: text *** |
|
4123 ;;; End: *** |
|
4124 */ |