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