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