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