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