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