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