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