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