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