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