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